From 79c8b36e11073c40fde2918ae9ee80cc5c3b8efe Mon Sep 17 00:00:00 2001 From: Michael Meffie Date: Fri, 6 Feb 2015 10:56:43 -0500 Subject: [PATCH] util: reopen server logs on SIGUSR1 for external log rotation Claim the SIGUSR1 signal for reopening server log files. A server process will reopen the log file when the SIGUSR1 signal is received. If the log file does not exist, the server process will create a new, empty log file. This allows external log rotation programs to rotate log files by renaming an existing log file then sending a SIGUSR1 signal to the corresponding server process. Any messages written to the log after the log file was renamed but before the SIGUSR1 signal is received will continue to be written to the renamed log file. The server process will write messages to the new log file after handling the SIGUSR1 signal. The SIGUSR1 signal is used to reopen the log file instead of the more commonly used SIGHUP signal, since SIGHUP is already used for resetting the logging level. The retirement of Linux 2.4 support, in particular the desupport of LinuxThreads, in commit ccf353ede6ef5cce7c562993d1bea0d20844bdb7 allows for the use of SIGUSR1 in OpenAFS. Change-Id: Ie3ff52ae4986eae30c7420b5f05ff1eacdfe7596 Reviewed-on: https://gerrit.openafs.org/11727 Tested-by: BuildBot Reviewed-by: Benjamin Kaduk --- src/util/serverLog.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/util/serverLog.c b/src/util/serverLog.c index c77c60ec4..41a07f5de 100644 --- a/src/util/serverLog.c +++ b/src/util/serverLog.c @@ -443,6 +443,29 @@ ResetDebug_Signal(int signo) } } /*ResetDebug_Signal */ +/*! + * Handle requests to reopen the log. + * + * This signal handler will reopen the log file. A new, empty log file + * will be created if the log file does not already exist. + * + * External log rotation programs may rotate a server log file by + * renaming the existing server log file and then immediately sending a + * signal to the corresponding server process. Server log messages will + * continue to be appended to the renamed server log file until the + * server log is reopened. After this signal handler completes, server + * log messages will be written to the new log file. This allows + * external log rotation programs to rotate log files without + * messages being dropped. + */ +void +ReOpenLog_Signal(int signo) +{ + ReOpenLog(); + if (resetSignals) { + (void)signal(signo, ReOpenLog_Signal); + } +} #ifdef AFS_PTHREAD_ENV /*! @@ -455,6 +478,7 @@ SetupLogSoftSignals(void) { opr_softsig_Register(SIGHUP, ResetDebug_Signal); opr_softsig_Register(SIGTSTP, SetDebug_Signal); + opr_softsig_Register(SIGUSR1, ReOpenLog_Signal); #ifndef AFS_NT40_ENV (void)signal(SIGPIPE, SIG_IGN); #endif @@ -474,8 +498,8 @@ SetupLogSignals(void) { resetSignals = 1; (void)signal(SIGHUP, ResetDebug_Signal); - /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */ (void)signal(SIGTSTP, SetDebug_Signal); + (void)signal(SIGUSR1, ReOpenLog_Signal); #ifndef AFS_NT40_ENV (void)signal(SIGPIPE, SIG_IGN); #endif -- 2.39.5