util: fix file descriptor leak in mrafs-style logging 22/11722/17
authorMichael Meffie <mmeffie@sinenomine.net>
Thu, 5 Feb 2015 20:42:16 +0000 (15:42 -0500)
committerBenjamin Kaduk <kaduk@mit.edu>
Mon, 25 Apr 2016 04:05:35 +0000 (00:05 -0400)
When MR-AFS style logging is in effect, the SIGHUP signal handler will rename
then create a new, empty server log file to support log rotation.

Unfortunately, the old log file descriptor is not closed, so each SIGHUP
signal will leak one file descriptor.

Be sure to close the current log file descriptor before opening the log again.
The OpenLog() routine will move the current log file to a new file, with a
timestamp string appended to the log file, then open the server log file with
truncate flag to start a new log file.

Change-Id: Ic3f29607fa50ed868b9245865e375dedde438471
Reviewed-on: https://gerrit.openafs.org/11722
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
Tested-by: BuildBot <buildbot@rampaginggeek.com>

src/util/serverLog.c

index 90dd452..4ce8460 100644 (file)
@@ -76,6 +76,8 @@ static int threadIdLogs = 0;
 static int resetSignals = 0;
 static char *ourName = NULL;
 
+static void RotateLogFile(void);
+
 void
 SetLogThreadNumProgram(int (*func) (void) )
 {
@@ -268,11 +270,7 @@ ResetDebug_Signal(int signo)
         threadIdLogs = 0;
 #endif
     if (mrafsStyleLogs) {
-       LOCK_SERVERLOG();
-       if (ourName != NULL) {
-           OpenLog(ourName);
-       }
-       UNLOCK_SERVERLOG();
+       RotateLogFile();
     }
 }                              /*ResetDebug_Signal */
 
@@ -462,6 +460,23 @@ ReOpenLog(const char *fileName)
 }
 
 /*!
+ * Rotate the log file by renaming then truncating.
+ */
+static void
+RotateLogFile(void)
+{
+    LOCK_SERVERLOG();
+    if (ourName != NULL) {
+       if (serverLogFD >= 0) {
+           close(serverLogFD);
+           serverLogFD = -1;
+       }
+       OpenLog(ourName);
+    }
+    UNLOCK_SERVERLOG();
+}
+
+/*!
  * Close the server log file.
  *
  * \note Must be preceeded by OpenLog().