From 4b698db3198d30aa1cf1028d3cb7856211792f18 Mon Sep 17 00:00:00 2001 From: Michael Meffie Date: Thu, 5 Feb 2015 15:42:16 -0500 Subject: [PATCH] util: fix file descriptor leak in mrafs-style logging 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 Tested-by: BuildBot --- src/util/serverLog.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/util/serverLog.c b/src/util/serverLog.c index 90dd45238..4ce846068 100644 --- a/src/util/serverLog.c +++ b/src/util/serverLog.c @@ -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 */ @@ -461,6 +459,23 @@ ReOpenLog(const char *fileName) return serverLogFD < 0 ? -1 : 0; } +/*! + * 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. * -- 2.39.5