From 882111a52aa35a65554e3f09f506ca5884e3fc15 Mon Sep 17 00:00:00 2001 From: Ron Pedde Date: Sat, 15 Apr 2006 23:03:31 +0000 Subject: [PATCH] reopen log and re-read config on sighup -- fixes ticket #73 --- src/conf.c | 8 ++++++++ src/conf.h | 1 + src/err.c | 35 ++++++++++++++++++++++++++++++++++- src/err.h | 1 + src/os-unix.c | 5 +++++ 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/conf.c b/src/conf.c index 279ce3d9..4dddf179 100644 --- a/src/conf.c +++ b/src/conf.c @@ -361,6 +361,14 @@ int _conf_verify(LL_HANDLE pll) { return is_valid; } +/** + * reload the existing config file. + * + * @returns CONF_E_SUCCESS on success + */ +int conf_reload(void) { + return conf_read(conf_main_file); +} /** * read a configfile into a tree diff --git a/src/conf.h b/src/conf.h index 2ab25af9..336e3429 100644 --- a/src/conf.h +++ b/src/conf.h @@ -33,6 +33,7 @@ #define CONF_E_NOTWRITABLE 8 extern int conf_read(char *file); +extern int conf_reload(void); extern int conf_close(void); extern int conf_get_int(char *section, char *key, int dflt); extern int conf_get_string(char *section, char *key, char *dflt, diff --git a/src/err.c b/src/err.c index fb90986d..f8e1df9f 100644 --- a/src/err.c +++ b/src/err.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -45,8 +46,13 @@ # include "os.h" #endif +#ifndef PACKAGE +# define PACKAGE "unknown daemon" +#endif + static int err_debuglevel=0; /**< current debuglevel, set from command line with -d */ static int err_logdestination=LOGDEST_STDERR; /**< current log destination */ +static char err_filename[PATH_MAX + 1]; static FILE *err_file=NULL; /**< if logging to file, the handle of that file */ static pthread_mutex_t err_mutex=PTHREAD_MUTEX_INITIALIZER; /**< for serializing log messages */ static unsigned int err_debugmask=0xFFFFFFFF; /**< modules to debug, see \ref log_categories */ @@ -64,6 +70,32 @@ static char *err_categorylist[] = { static int _err_lock(void); static int _err_unlock(void); + +/** + * if we are logging to a file, then re-open the file. This + * would help for log rotation + */ +void err_reopen(void) { + int err; + + if(err_logdestination != LOGDEST_LOGFILE) + return; + + fclose(err_file); + err_file = fopen(err_filename,"a"); + if(!err_file) { + /* what to do when you lose your logging mechanism? Keep + * going? + */ + err = errno; + err_setdest(PACKAGE,LOGDEST_SYSLOG); + DPRINTF(E_LOG,L_MISC,"Could not rotate log file: %s\n", + strerror(err)); + } else { + DPRINTF(E_LOG,L_MISC,"Rotated logs\n"); + } +} + /** * Write a printf-style formatted message to the log destination. * This can be stderr, syslog/eventviewer, or a logfile, as determined by @@ -160,7 +192,8 @@ void err_setdest(char *cvalue, int destination) { switch(destination) { case LOGDEST_LOGFILE: - err_file=fopen(cvalue,"a"); + strncpy(err_filename,cvalue,PATH_MAX); + err_file=fopen(err_filename,"a"); if(err_file==NULL) { fprintf(stderr,"Error opening %s: %s\n",cvalue,strerror(errno)); exit(EXIT_FAILURE); diff --git a/src/err.h b/src/err.h index 3ff29129..1704b385 100644 --- a/src/err.h +++ b/src/err.h @@ -64,6 +64,7 @@ #endif extern void err_log(int level, unsigned int cat, char *fmt, ...); +extern void err_reopen(void); /** rotate logfile */ extern void err_setdest(char *cvalue, int destination); extern void err_setlevel(int level); extern int err_getlevel(void); diff --git a/src/os-unix.c b/src/os-unix.c index 87d92f93..2a03229f 100644 --- a/src/os-unix.c +++ b/src/os-unix.c @@ -363,6 +363,11 @@ void *_os_signal_handler(void *arg) { break; case SIGHUP: DPRINTF(E_LOG,L_MAIN,"Got HUP signal. Notifying daap server.\n"); + /* if we can't reload, it keeps the old config file, + * so no real damage */ + conf_reload(); + err_reopen(); + config.reload=1; break; default: