Reload log file on SIGHUP

There's a report about a nightly "crash" when users download, compile
from source, and replace their distro's mindlnad binary.  This is because
the Debian package includes a patch that uses SIGUSR2 to reopen the log
file and sends SIGUSR2 from logrotate instead of just using the
"copytruncate" logrotate option.  Then logrotate sends SIGUSR2 at 6:25AM,
which causes us to abort due to the unhandled signal.

I don't want to sacrifice SIGUSR2 just for log rotation, especially when
we already do some reload operations on SIGHUP.  So to avoid this
Debian/Ubuntu issue, we'll explicitly ignore SIGUSR2, and add log file
reopening to the SIGHUP handler.  Then hopefully a future Debian package
version will remove the SIGUSR2 patch and use SIGHUP instead (or
copytruncate).

Fixes: SF Bug #313 (log rotation kills minidlna service)
This commit is contained in:
Justin Maggard
2018-01-23 22:01:10 -08:00
parent 7a8ef80af0
commit 138d03db19
5 changed files with 67 additions and 50 deletions

47
log.c
View File

@ -63,28 +63,46 @@ log_close(void)
fclose(log_fp);
}
int find_matching_name(const char* str, const char* names[]) {
if (str == NULL) return -1;
void
log_reopen(void)
{
if (log_path[0] && log_fp)
{
char logfile[1048];
snprintf(logfile, sizeof(logfile), "%s/" LOGFILE_NAME, log_path);
fclose(log_fp);
log_fp = fopen(logfile, "a");
DPRINTF(E_INFO, L_GENERAL, "Reopened log file\n");
}
}
const char* start = strpbrk(str, ",=");
int level, c = (start != NULL) ? start - str : strlen(str);
int find_matching_name(const char* str, const char* names[])
{
const char *start;
int level, c;
if (!str)
return -1;
start = strpbrk(str, ",=");
c = start ? start - str : strlen(str);
for (level = 0; names[level] != 0; level++) {
if (!(strncasecmp(names[level], str, c)))
if (!strncasecmp(names[level], str, c))
return level;
}
return -1;
}
int
log_init(const char *fname, const char *debug)
log_init(const char *debug)
{
int i;
FILE *fp;
FILE *fp = NULL;
int level = find_matching_name(debug, level_name);
int default_log_level = (level == -1) ? _default_log_level : level;
for (i=0; i<L_MAX; i++)
for (i = 0; i < L_MAX; i++)
log_level[i] = default_log_level;
if (debug)
@ -117,12 +135,15 @@ log_init(const char *fname, const char *debug)
}
}
if (!fname) // use default i.e. stdout
return 0;
if (!(fp = fopen(fname, "a")))
return 1;
if (log_path[0])
{
char logfile[1048];
snprintf(logfile, sizeof(logfile), "%s/" LOGFILE_NAME, log_path);
if (!(fp = fopen(logfile, "a")))
return -1;
}
log_fp = fp;
return 0;
}