From 951df6d7d47fbd628518974104cf91c7e468b562 Mon Sep 17 00:00:00 2001 From: Shawn Landden Date: Tue, 13 Mar 2018 14:08:50 -0700 Subject: fix race condition in signal handler Closes: #269 --- src/daemon.c | 2 ++ src/dparent.c | 4 +++- src/dsignal.c | 8 ++++++-- src/trace.c | 4 +++- src/trace.h | 2 ++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index cbec849..9df4f7f 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -108,6 +108,7 @@ static int dcc_setup_startup_log(void) rs_add_logger(rs_logger_file, RS_LOG_DEBUG, 0, STDERR_FILENO); } else { openlog("distccd", LOG_PID, LOG_DAEMON); + rs_trace_syslog = TRUE; rs_add_logger(rs_logger_syslog, RS_LOG_DEBUG, NULL, 0); } @@ -304,6 +305,7 @@ static void dcc_setup_real_log(void) rs_remove_all_loggers(); openlog("distccd", LOG_PID, LOG_DAEMON); + rs_trace_syslog = TRUE; rs_add_logger(rs_logger_syslog, opt_log_level_num, NULL, 0); } diff --git a/src/dparent.c b/src/dparent.c index f8dc534..485f9c4 100644 --- a/src/dparent.c +++ b/src/dparent.c @@ -324,7 +324,9 @@ void dcc_remove_pid(void) if (!arg_pid_file) return; - if (unlink(arg_pid_file)) { + /* this may be called from a signal handler, + and syslog is not safe from signal handler */ + if (unlink(arg_pid_file) && !rs_trace_syslog) { rs_log_warning("failed to remove pid file %s: %s", arg_pid_file, strerror(errno)); } diff --git a/src/dsignal.c b/src/dsignal.c index de033b7..018ff04 100644 --- a/src/dsignal.c +++ b/src/dsignal.c @@ -120,7 +120,8 @@ static RETSIGTYPE dcc_daemon_terminate(int whichsig) am_parent = getpid() == dcc_master_pid; - if (am_parent) { + /* syslog is not safe from a signal handler */ + if (am_parent && !rs_trace_syslog) { #ifdef HAVE_STRSIGNAL rs_log_info("%s", strsignal(whichsig)); #else @@ -139,6 +140,9 @@ static RETSIGTYPE dcc_daemon_terminate(int whichsig) raise(whichsig); +/* malloc() stuff not safe from a signal handler, but keep this here in case + this memory non-leak is to be fixed in the future. */ +/* #ifdef HAVE_GSSAPI if (dcc_auth_enabled) { dcc_gssapi_release_credentials(); @@ -147,5 +151,5 @@ static RETSIGTYPE dcc_daemon_terminate(int whichsig) dcc_gssapi_free_list(); } } -#endif +#endif*/ } diff --git a/src/trace.c b/src/trace.c index c5f148b..8b64e29 100644 --- a/src/trace.c +++ b/src/trace.c @@ -39,6 +39,7 @@ #include #include +#include "distcc.h" #include "trace.h" #include "snprintf.h" #include "va_copy.h" @@ -53,7 +54,8 @@ struct rs_logger_list { static struct rs_logger_list *logger_list = NULL; - +/* really bool */ +int rs_trace_syslog = FALSE; int rs_trace_level = RS_LOG_NOTICE; #ifdef UNUSED diff --git a/src/trace.h b/src/trace.h index f01b1f2..0db4c7f 100644 --- a/src/trace.h +++ b/src/trace.h @@ -210,6 +210,8 @@ void rs_log0_nofn(int level, char const *fmt, ...); * messages. */ +/* really bool */ +extern int rs_trace_syslog; extern int rs_trace_level; #ifdef DO_RS_TRACE -- cgit v1.2.1