From 7b98443a13dcd6dbb169c756e86c2b71c3ccf2d9 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 21 Feb 2018 12:40:53 +0100 Subject: [PATCH] logging: don't write fatal messages to invalid descriptor If opening the log file specified with the -l option failed (after closing all descriptors), the error message is written to an invalid descriptor as no log file or syslog is opened yet. Fix the code to track when the output is usable. --- logging.c | 30 +++++++++++++++--------------- logging.h | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/logging.c b/logging.c index b39c4d5..7d9dbb8 100644 --- a/logging.c +++ b/logging.c @@ -79,11 +79,11 @@ LOG_Initialise(void) void LOG_Finalise(void) { - if (system_log) { + if (system_log) closelog(); - } else { + + if (file_log) fclose(file_log); - } LOG_CycleLogFiles(); @@ -116,7 +116,7 @@ static void log_message(int fatal, LOG_Severity severity, const char *message) assert(0); } syslog(priority, fatal ? "Fatal error : %s" : "%s", message); - } else { + } else if (file_log) { fprintf(file_log, fatal ? "Fatal error : %s\n" : "%s\n", message); } } @@ -134,7 +134,7 @@ void LOG_Message(LOG_Severity severity, time_t t; struct tm stm; - if (!system_log) { + if (!system_log && file_log) { /* Don't clutter up syslog with timestamps and internal debugging info */ time(&t); stm = *gmtime(&t); @@ -160,16 +160,14 @@ void LOG_Message(LOG_Severity severity, case LOGS_FATAL: log_message(1, severity, buf); - /* With syslog, send the message also to the grandparent - process or write it to stderr if not detached */ - if (system_log) { - if (parent_fd > 0) { - if (write(parent_fd, buf, strlen(buf) + 1) < 0) - ; /* Not much we can do here */ - } else if (parent_fd == 0) { - system_log = 0; - log_message(1, severity, buf); - } + /* Send the message also to the foreground process if it is + still running, or stderr if it is still open */ + if (parent_fd > 0) { + if (write(parent_fd, buf, strlen(buf) + 1) < 0) + ; /* Not much we can do here */ + } else if (system_log && parent_fd == 0) { + system_log = 0; + log_message(1, severity, buf); } break; default: @@ -220,6 +218,8 @@ void LOG_SetParentFd(int fd) { parent_fd = fd; + if (file_log == stderr) + file_log = NULL; } /* ================================================== */ diff --git a/logging.h b/logging.h index c50bcf5..5bb46f5 100644 --- a/logging.h +++ b/logging.h @@ -105,7 +105,7 @@ extern void LOG_OpenFileLog(const char *log_file); /* Log messages to syslog instead of stderr */ extern void LOG_OpenSystemLog(void); -/* Send fatal message also to the foreground process */ +/* Stop using stderr and send fatal message to the foreground process */ extern void LOG_SetParentFd(int fd); /* Close the pipe to the foreground process so it can exit */