diff --git a/debian/daliserver.logrotate b/debian/daliserver.logrotate new file mode 100644 index 0000000..16b9b87 --- /dev/null +++ b/debian/daliserver.logrotate @@ -0,0 +1,13 @@ +/var/log/daliserver.log +{ + rotate 7 + daily + missingok + notifempty + compress + delaycompress + sharedscripts + postrotate + /usr/bin/killall -HUP daliserver + endscript +} diff --git a/lib/dispatch.c b/lib/dispatch.c index 16f3334..acef847 100644 --- a/lib/dispatch.c +++ b/lib/dispatch.c @@ -73,7 +73,7 @@ DispatchStatus dispatch_run(DispatchPtr table, int timeout) { if (errno != EINTR) { log_error("Error waiting for I/O events"); } - return 0; + return 1; } else if (ready == 0) { // timeout log_debug("poll() timeout"); diff --git a/lib/log.c b/lib/log.c index 29498d2..2fdf379 100644 --- a/lib/log.c +++ b/lib/log.c @@ -45,6 +45,7 @@ static unsigned int loglevel = LOG_LEVEL_DEFAULT; static unsigned int loglevel_file = LOG_LEVEL_DEFAULT; static unsigned int loglevel_syslog = LOG_LEVEL_ERROR; +static char *logfile = NULL; static FILE *fp_logfile = NULL; static int enabled_syslog = 0; @@ -155,27 +156,34 @@ unsigned int log_get_level() { return loglevel; } -int log_set_logfile(const char *logfile) { +int log_reopen_file() { + if (fp_logfile) { + fclose(fp_logfile); + fp_logfile = NULL; + } if (logfile) { - FILE *fp = fopen(logfile, "a"); - if (!fp) { + fp_logfile = fopen(logfile, "a"); + if (!fp_logfile) { log_error("Error opening log file %s: %s", logfile, strerror(errno)); return -1; - } else { - if (fp_logfile) { - fclose(fp_logfile); - } - fp_logfile = fp; - } - } else { - if (fp_logfile) { - fclose(fp_logfile); - fp_logfile = NULL; } } return 0; } +int log_set_logfile(const char *logfile_path) { + free(logfile); + logfile = NULL; + if (logfile_path != NULL) { + logfile = strdup(logfile_path); + if (!logfile) { + log_error("Error setting log file %s: %s", logfile_path, strerror(errno)); + return -1; + } + } + return log_reopen_file(); +} + void log_set_logfile_level(unsigned int level) { if (level > LOG_LEVEL_MAX) { loglevel_file = LOG_LEVEL_MAX; diff --git a/lib/log.h b/lib/log.h index f98bfcf..4db5259 100644 --- a/lib/log.h +++ b/lib/log.h @@ -51,11 +51,15 @@ void log_printf(unsigned int level, const char *format, ...); void log_set_level(unsigned int level); // Gets the current log level unsigned int log_get_level(); +// Reopen the configured log file (if any), e.g. after logrotate +// Returns 0 upon success, -1 if the file could not be opened +// (also generates a warning to the other logging channels in this case) +int log_reopen_file(); // Enables/disables logging to a log file // Pass NULL for logfile to disable // Returns 0 upon success, -1 if the file could not be opened // (also generates a warning to the other logging channels in this case) -int log_set_logfile(const char *logfile); +int log_set_logfile(const char *logfile_path); // Set the log level for the logfile // The default is the same as for console logging void log_set_logfile_level(unsigned int level); diff --git a/src/daliserver.c b/src/daliserver.c index 73a4414..009b4c9 100644 --- a/src/daliserver.c +++ b/src/daliserver.c @@ -177,6 +177,7 @@ int main(int argc, char *const argv[]) { running = 1; signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); + signal(SIGHUP, signal_handler); while (running && dispatch_run(dispatch, usbdali_get_timeout(usb))); log_info("Shutting daliserver down"); @@ -201,6 +202,11 @@ int main(int argc, char *const argv[]) { } static void signal_handler(int sig) { + if (sig == SIGHUP) { + log_info("Signal received, reopening log file"); + log_reopen_file(); + return; + } if (running) { log_info("Signal received, shutting down"); running = 0;