[PATCH] core: Avoid lock file removal race condition

Anton Löfgren alofgren at op5.com
Tue Apr 9 16:27:51 CEST 2013


This patch avoids a  race condition in removal of lock files. The race
condition happens when:

1. we receive a SIGHUP, breaking us out of the event execution loop, and
2. receive a SIGTERM - or any other "shutdown" signal - AFTER checking for
the sigshutdown state but BEFORE the do-while loop terminates. I.e, during
execution of cleanup() or close_debug_log() towards the end of the loop.

Signed-off-by: Anton Lofgren <alofgren at op5.com>
---
 base/nagios.c | 15 +++++++++++----
 base/utils.c  |  1 -
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/base/nagios.c b/base/nagios.c
index 6b834b2..806d355 100644
--- a/base/nagios.c
+++ b/base/nagios.c
@@ -539,6 +539,11 @@ int main(int argc, char **argv) {

         /* keep monitoring things until we get a shutdown command */
         do {
+            if (daemon_mode == TRUE && sigrestart == TRUE) {
+                /* if we've restarted in daemon mode, we don't want to
leak the
+                 * lock file */
+                my_free(lock_file);
+            }

             /* reset program variables */
             reset_variables();
@@ -827,10 +832,6 @@ int main(int argc, char **argv) {
                 iobroker_destroy(nagios_iobs, IOBROKER_CLOSE_SOCKETS);
                 nagios_iobs = NULL;

-                /* make sure lock file has been removed - it may not have
been if we received a shutdown command */
-                if(daemon_mode == TRUE)
-                    unlink(lock_file);
-
                 /* log a shutdown message */
                 logit(NSLOG_PROCESS_INFO, TRUE, "Successfully shutdown...
(PID=%d)\n", (int)getpid());
                 }
@@ -844,6 +845,12 @@ int main(int argc, char **argv) {
             }
         while(sigrestart == TRUE && sigshutdown == FALSE);

+        /* make sure lock file has been removed - it may not have been if
we received a shutdown command */
+        if(daemon_mode == TRUE) {
+            unlink(lock_file);
+            my_free(lock_file);
+        }
+
         /* free misc memory */
         my_free(config_file);
         }
diff --git a/base/utils.c b/base/utils.c
index af95663..1ff1e7b 100644
--- a/base/utils.c
+++ b/base/utils.c
@@ -3157,7 +3157,6 @@ void free_memory(nagios_macros *mac) {
     my_free(temp_path);
     my_free(check_result_path);
     my_free(command_file);
-    my_free(lock_file);
     my_free(log_archive_path);

     return;
-- 
1.8.2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-lists.org/archive/developers/attachments/20130409/a3d2d13e/attachment.html>
-------------- next part --------------
------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
-------------- next part --------------
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list