From d3de8241ea1ecfcc397aac557ad28fc7cbc34035 Mon Sep 17 00:00:00 2001 From: s3lph <5564491+s3lph@users.noreply.github.com> Date: Fri, 9 Feb 2024 00:52:11 +0100 Subject: [PATCH 1/3] feat: implement support for logrotate --- debian/daliserver.logrotate | 13 +++++++++++++ lib/dispatch.c | 2 +- lib/log.c | 23 ++++++++++++++++++----- lib/log.h | 6 +++++- src/daliserver.c | 6 ++++++ 5 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 debian/daliserver.logrotate 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..7d79848 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,7 +156,11 @@ 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) { @@ -167,13 +172,21 @@ int log_set_logfile(const char *logfile) { } fp_logfile = fp; } + } + return 0; +} + +int log_set_logfile(const char *logfile_path) { + if (logfile_path == NULL) { + logfile = NULL; } else { - if (fp_logfile) { - fclose(fp_logfile); - fp_logfile = NULL; + logfile = strdup(logfile_path); + if (!logfile) { + log_error("Error setting log file %s: %s", logfile_path, strerror(errno)); + return -1; } } - return 0; + return log_reopen_file(); } void log_set_logfile_level(unsigned int level) { 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; From acaea55df2384848e65da228f8b2597897dc1a11 Mon Sep 17 00:00:00 2001 From: s3lph <5564491+s3lph@users.noreply.github.com> Date: Sun, 11 Feb 2024 16:08:26 +0100 Subject: [PATCH 2/3] fix: free logfile name before it is changed --- lib/log.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/log.c b/lib/log.c index 7d79848..11bfa05 100644 --- a/lib/log.c +++ b/lib/log.c @@ -177,9 +177,9 @@ int log_reopen_file() { } int log_set_logfile(const char *logfile_path) { - if (logfile_path == NULL) { - logfile = NULL; - } else { + 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)); From 8abfd566c0556b302db823782dfa33c09d4377cf Mon Sep 17 00:00:00 2001 From: s3lph <5564491+s3lph@users.noreply.github.com> Date: Sun, 11 Feb 2024 23:32:07 +0100 Subject: [PATCH 3/3] fix: remove redundant local log file handle --- lib/log.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/log.c b/lib/log.c index 11bfa05..2fdf379 100644 --- a/lib/log.c +++ b/lib/log.c @@ -162,15 +162,10 @@ int log_reopen_file() { 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; } } return 0;