<div dir="ltr">This patch avoids a race condition in removal of lock files. The race<br>condition happens when:<br><br>1. we receive a SIGHUP, breaking us out of the event execution loop, and<br>2. receive a SIGTERM - or any other "shutdown" signal - AFTER checking for<br>
the sigshutdown state but BEFORE the do-while loop terminates. I.e, during<br>execution of cleanup() or close_debug_log() towards the end of the loop.<br><br>Signed-off-by: Anton Lofgren <<a href="mailto:alofgren@op5.com">alofgren@op5.com</a>><br>
---<br> base/nagios.c | 15 +++++++++++----<br> base/utils.c | 1 -<br> 2 files changed, 11 insertions(+), 5 deletions(-)<br><br>diff --git a/base/nagios.c b/base/nagios.c<br>index 6b834b2..806d355 100644<br>--- a/base/nagios.c<br>
+++ b/base/nagios.c<br>@@ -539,6 +539,11 @@ int main(int argc, char **argv) {<br> <br> /* keep monitoring things until we get a shutdown command */<br> do {<br>+ if (daemon_mode == TRUE && sigrestart == TRUE) {<br>
+ /* if we've restarted in daemon mode, we don't want to leak the<br>+ * lock file */<br>+ my_free(lock_file);<br>+ }<br> <br> /* reset program variables */<br>
reset_variables();<br>@@ -827,10 +832,6 @@ int main(int argc, char **argv) {<br> iobroker_destroy(nagios_iobs, IOBROKER_CLOSE_SOCKETS);<br> nagios_iobs = NULL;<br> <br>- /* make sure lock file has been removed - it may not have been if we received a shutdown command */<br>
- if(daemon_mode == TRUE)<br>- unlink(lock_file);<br>-<br> /* log a shutdown message */<br> logit(NSLOG_PROCESS_INFO, TRUE, "Successfully shutdown... (PID=%d)\n", (int)getpid());<br>
}<br>@@ -844,6 +845,12 @@ int main(int argc, char **argv) {<br> }<br> while(sigrestart == TRUE && sigshutdown == FALSE);<br> <br>+ /* make sure lock file has been removed - it may not have been if we received a shutdown command */<br>
+ if(daemon_mode == TRUE) {<br>+ unlink(lock_file);<br>+ my_free(lock_file);<br>+ }<br>+<br> /* free misc memory */<br> my_free(config_file);<br> }<br>diff --git a/base/utils.c b/base/utils.c<br>
index af95663..1ff1e7b 100644<br>--- a/base/utils.c<br>+++ b/base/utils.c<br>@@ -3157,7 +3157,6 @@ void free_memory(nagios_macros *mac) {<br> my_free(temp_path);<br> my_free(check_result_path);<br> my_free(command_file);<br>
- my_free(lock_file);<br> my_free(log_archive_path);<br> <br> return;<br>-- <br>1.8.2<br><br></div>