Do the SIGTERM cleanup outside the signal handler
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 6 Oct 2010 13:54:28 +0000 (15:54 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 6 Oct 2010 13:54:28 +0000 (15:54 +0200)
src/main.cpp

index 54022da..4decd4a 100644 (file)
@@ -29,6 +29,7 @@ Updater::Ptr updater;
 volatile bool is_online = false;
 volatile bool webcheck_enabled = false;
 volatile bool need_config_reload = false;
+volatile bool exit_now = false;
 
 /**
  * Checks if a bpdyndnsd process is already running.
@@ -139,8 +140,7 @@ int shutdown()
 void terminate(int param)
 {
     updater->get_logger()->print_caught_sigterm();
-
-    exit(shutdown());
+    exit_now = true;
 } /*lint !e715 */
 
 
@@ -161,7 +161,6 @@ void switch_to_offline(int param)
  */
 void reload_config(int param)
 {
-    updater->get_logger()->print_caught_sighup();
     need_config_reload = true;
 } /*lint !e715 */
 
@@ -312,6 +311,10 @@ int main(int argc, char *argv[])
     is_online = !updater->get_config()->get_start_offline();
     webcheck_enabled = updater->get_config()->get_webcheck_enabled();
 
+    // One shot run if daemon mode is disabled
+    if (updater->get_config()->get_daemon_mode() != 1)
+        exit_now = true;
+
     // service processing starts here
     do
     {
@@ -332,6 +335,7 @@ int main(int argc, char *argv[])
 
         if (need_config_reload)
         {
+            updater->get_logger()->print_caught_sighup();
             need_config_reload = false;
 
             if ( updater->reload_config() != 0 )
@@ -342,10 +346,9 @@ int main(int argc, char *argv[])
         }
 
         // Snore, snore... don't hog the cpu if we are in daemon_mode.
-        if ( updater->get_config()->get_daemon_mode() == 1 )
+        if ( !exit_now )
             sleep(10); /*lint !e534 */
-
-    }while ( updater->get_config()->get_daemon_mode() == 1 );
+    } while ( !exit_now );
 
     // Serialize services to save their actual state.
     if ( shutdown() != 0 )