util: reopen server logs on SIGUSR1 for external log rotation 27/11727/14
authorMichael Meffie <mmeffie@sinenomine.net>
Fri, 6 Feb 2015 15:56:43 +0000 (10:56 -0500)
committerBenjamin Kaduk <kaduk@mit.edu>
Tue, 26 Apr 2016 00:09:12 +0000 (20:09 -0400)
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 <buildbot@rampaginggeek.com>
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>

src/util/serverLog.c

index c77c60e..41a07f5 100644 (file)
@@ -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