Hi, The following patch allows for re-opening the slpd log file on a USR1 signal, making it possible to let 'logrotate' clean-up the log files.
Unfortunately, user 'daemon' is normally not allowed to open/create a log file, so suid() to this user after start-up will cause problems. In this patch the suid switch has been removed, which makes it probably unsuitable for applying it. But perhaps anyone on the list finds it useful anyway. The patch is against trunk/r1589. Kind regards, Roel van de Kraats --- begin patch --- Allow re-opening log file with SIGUSR1; useful for logrotate. Unfortunately, it is therefore no longer possible to run slpd as user 'daemon', since that user is not allowed to create a new log file. Roel van de Kraats diff -uar openslp-2.0.beta1.dalog/slpd/slpd_log.c openslp-2.0.beta1/slpd/slpd_log.c --- openslp-2.0.beta1.dalog/slpd/slpd_log.c 2008-03-27 00:27:31.000000000 +0100 +++ openslp-2.0.beta1/slpd/slpd_log.c 2008-06-26 17:06:36.000000000 +0200 @@ -59,8 +59,7 @@ */ int SLPDLogFileOpen(const char * path, int append) { - if (G_SlpdLogFile) - fclose(G_SlpdLogFile); /* logfile was already open close it */ + SLPDLogFileClose(); /* logfile was already open close it */ if (*path == 0) G_SlpdLogFile = stdout; /* Log to console. */ @@ -77,22 +76,29 @@ G_SlpdLogFile = fopen(path,"w"); if (G_SlpdLogFile == 0) + { + G_SlpdLogFile = stdout; + SLPDLog("Could not open log file: %s\n", strerror(errno)); return -1; /* could not open the log file */ + } } return 0; } -#ifdef DEBUG /** Releases resources associated with the log file. * * @return Zero - always. */ int SLPDLogFileClose(void) { - fclose(G_SlpdLogFile); + if (G_SlpdLogFile) + { + if (G_SlpdLogFile != stdout) + fclose(G_SlpdLogFile); + G_SlpdLogFile = 0; + } return 0; } -#endif /** Logs a message. * diff -uar openslp-2.0.beta1.dalog/slpd/slpd_log.h openslp-2.0.beta1/slpd/slpd_log.h --- openslp-2.0.beta1.dalog/slpd/slpd_log.h 2008-03-27 00:27:31.000000000 +0100 +++ openslp-2.0.beta1/slpd/slpd_log.h 2008-06-26 17:06:45.000000000 +0200 @@ -59,11 +59,7 @@ #define SLPDLOG_TRACEMSG_OUT (SLPDLOG_TRACEMSG | 0x00000002) int SLPDLogFileOpen(const char * path, int append); - -#ifdef DEBUG int SLPDLogFileClose(void); -#endif - void SLPDLogBuffer(const char * prefix, size_t bufsize, const char * buf); void SLPDLog(const char * msg, ...); void SLPDFatal(const char * msg, ...); diff -uar openslp-2.0.beta1.dalog/slpd/slpd_main.c openslp-2.0.beta1/slpd/slpd_main.c --- openslp-2.0.beta1.dalog/slpd/slpd_main.c 2008-03-27 00:27:31.000000000 +0100 +++ openslp-2.0.beta1/slpd/slpd_main.c 2008-06-26 17:13:35.000000000 +0200 @@ -60,7 +60,8 @@ int G_SIGALRM; int G_SIGTERM; -int G_SIGHUP; +int G_SIGHUP; +int G_SIGUSR1; #ifdef DEBUG int G_SIGINT; /* Signal being used for dumping registrations */ #endif @@ -219,6 +220,24 @@ SLPDLog("****************************************\n\n"); } +/** Handles a SIGUSR1 signal from the system. + * + * @internal + */ +void HandleSigUsr1() +{ + SLPDLogFileClose(); + + /* Re-initialize the log file */ + if(SLPDLogFileOpen(G_SlpdCommandLine.logfile, 1)) + SLPDLog("Could not open logfile %s\n",G_SlpdCommandLine.logfile); + + SLPDLog("****************************************\n"); + SLPDLogTime(); + SLPDLog("Re-opened log file on SIGUSR1\n"); + SLPDLog("****************************************\n\n"); +} + /** Handles a SIG_ALRM signal from the system. */ void HandleSigAlrm(void) @@ -352,11 +371,16 @@ break; } - close(0); - close(1); - close(2); + if (G_SlpdCommandLine.detach) + { + close(0); + close(1); + close(2); + } setsid(); /* will only fail if we are already the process group leader */ + /* User 'daemon' is not allowed to (re)open the log file */ +#if 0 /* suid to daemon */ /* TODO: why do the following lines mess up my signal handlers? */ pwent = getpwnam("daemon"); @@ -368,6 +392,7 @@ /* TODO: should we log here and return fail */ } } +#endif /* Set cwd to / (root)*/ chdir("/"); @@ -397,6 +422,10 @@ G_SIGHUP = 1; break; + case SIGUSR1: + G_SIGUSR1 = 1; + break; + #ifdef DEBUG case SIGINT: G_SIGINT = 1; @@ -440,6 +469,7 @@ #endif signal(SIGHUP, SignalHandler); + signal(SIGUSR1, SignalHandler); /* result |= sigaction(SIGHUP,&sa,0); */ return result; @@ -526,7 +556,8 @@ SLPDLog("Startup complete entering main run loop ...\n\n"); G_SIGALRM = 0; G_SIGTERM = 0; - G_SIGHUP = 0; + G_SIGHUP = 0; + G_SIGUSR1 = 0; #ifdef DEBUG G_SIGINT = 0; #endif @@ -541,7 +572,7 @@ LoadFdSets(&G_OutgoingSocketList, &highfd, &readfds, &writefds); /* before select(), check to see if we got a signal */ - if (G_SIGALRM || G_SIGHUP) + if (G_SIGALRM || G_SIGHUP || G_SIGUSR1) goto HANDLE_SIGNAL; /* main select -- we time out every second so the outgoing retries can occur*/ @@ -566,6 +597,11 @@ HandleSigHup(); G_SIGHUP = 0; } + if (G_SIGUSR1) + { + HandleSigUsr1(); + G_SIGUSR1 = 0; + } if (G_SIGALRM) { HandleSigAlrm(); ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ Openslp-devel mailing list Openslp-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openslp-devel