Improved shut down behaviour.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 25 Aug 2009 08:55:37 +0000 (10:55 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 25 Aug 2009 08:55:37 +0000 (10:55 +0200)
src/logger.cpp
src/logger.h
src/main.cpp

index d49d24b..0f68ff5 100644 (file)
@@ -919,3 +919,59 @@ void Logger::print_update_service_failure(const std::string& service) const
         log_warning(msg.str());
     }
 }
+
+
+/**
+ * Starting shutdown
+ */
+void Logger::print_starting_shutdown() const
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Shutting down ..." << endl;
+        log_notice(msg.str());
+    }
+}
+
+
+/**
+ * Shutdown complete
+ */
+void Logger::print_shutdown_succeeded() const
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Shutting down complete ..." << endl;
+        log_notice(msg.str());
+    }
+}
+
+
+/**
+ * Shutdown parent succeeded
+ */
+void Logger::print_shutdown_parent_succeeded() const
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Shutting down parent process completed ..." << endl;
+        log_notice(msg.str());
+    }
+}
+
+
+/**
+ * Starting shutdown parent
+ */
+void Logger::print_starting_shutdown_parent() const
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Shutting down parent process ..." << endl;
+        log_notice(msg.str());
+    }
+}
index b1cae14..d9bf65b 100644 (file)
@@ -152,6 +152,14 @@ public:
     void print_update_not_allowed(const int current_time, const int old_time, const int MaxUpdatesWithinInterval, const std::string& service) const;
 
     void print_update_service_failure(const std::string& service) const;
+
+    void print_starting_shutdown() const;
+
+    void print_shutdown_succeeded() const;
+
+    void print_shutdown_parent_succeeded() const;
+
+    void print_starting_shutdown_parent() const;
 };
 
 #endif
index 3f8d16a..992bd22 100644 (file)
@@ -15,7 +15,7 @@
 #define REVISION    1
 #define RELEASE     0
 
-#define PIDFILE "/var/run/bpdyndnsd.pid"
+#define PIDFILE "/home/bjoern/intranator/bpdyndnsd/var/run/bpdyndnsd.pid"
 #define OBJECT_FILE "/home/bjoern/intranator/bpdyndnsd/objects.ser"
 
 #include <iostream>
@@ -96,6 +96,57 @@ int write_pidfile(int pid)
 
 
 /**
+ * Parent shutdown function
+ * @return 0 if all is fine, -1 otherwise
+ */
+int shutdown_parent(bool remove_pid, int ret_val)
+{
+    // starting shutdown_parent
+    updater->get_logger()->print_starting_shutdown_parent();
+
+    // remove pidfile if requested
+    if(remove_pid)
+        unlink(PIDFILE);
+
+    // shutdown parent complete
+    updater->get_logger()->print_shutdown_parent_succeeded();
+
+    // release shared pointer
+    updater.reset();
+
+    exit(ret_val);
+}
+
+
+/**
+ * Shutdown function
+ * @return 0 if all is fine, -1 otherwise
+ */
+int shutdown()
+{
+    int ret_val = 0;
+
+    // starting shutdown
+    updater->get_logger()->print_starting_shutdown();
+
+    // serialize actual service objects
+    if ( updater->get_service_holder()->serialize_services() != 0 )
+        ret_val = -1;
+
+    // unlink pidfile
+    unlink(PIDFILE);
+
+    // shutdown complete
+    updater->get_logger()->print_shutdown_succeeded();
+
+    // release shared pointer
+    updater.reset();
+
+    return ret_val;
+}
+
+
+/**
  * Signal SIGTERM caught, releasing resources and exit.
  * @param param Parameter from the signal interface.
  */
@@ -103,19 +154,10 @@ void terminate(int param)
 {
     updater->get_logger()->print_caught_sigterm();
 
-    // unfortunately we can't call serialize_services in any destructor
-    // cause some singleton are already destroyed !?!
-    if ( updater->get_service_holder()->serialize_services() != 0 )
-    {
-        updater.reset();
-        exit(-1);
-    }
-    updater.reset();
-    exit(0);
+    exit(shutdown());
 }
 
 
-
 /**
  * Signal SIGUSR1 caught, switching to offline mode.
  * @param param Parameter from the signal interface.
@@ -193,16 +235,29 @@ int init_daemon_mode(bool daemon_mode)
             if ( write_pidfile(pid) != 0 )
             {
                 if ( kill(pid,SIGTERM) != 0 )
+                {
                     updater->get_logger()->print_error_kill_child(pid);
+                    shutdown_parent(false,-1); // keep pidfile
+                }
                 else
+                {
                     updater->get_logger()->print_child_killed(pid);
-                exit(-1);
+                    shutdown_parent(true,-1); // remove pidfile
+                }
             }
             updater->get_logger()->print_runnig_as_daemon(pid);
-            exit(0);
+            shutdown_parent(false,0); // keep pidfile
         }
         // child starts here
     }
+    else
+    {
+        if ( write_pidfile(getpid()) != 0 )
+        {
+            shutdown();
+            exit(-1);
+        }
+    }
     return 0;
 }
 
@@ -265,7 +320,7 @@ int main(int argc, char *argv[])
 
 
     // Serialize services to save their actual state.
-    if ( updater->get_service_holder()->serialize_services() != 0 )
+    if ( shutdown() != 0 )
         return -1;
 
     return 0;