Separated init of daemon mode into extra function.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 10 Aug 2009 15:39:07 +0000 (17:39 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 10 Aug 2009 15:39:07 +0000 (17:39 +0200)
src/logger.cpp
src/logger.h
src/main.cpp

index f8d936e..37c642f 100644 (file)
@@ -669,6 +669,17 @@ void Logger::print_error_kill_child(const int pid)
 }
 
 
+void Logger::print_child_killed(const int pid)
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Killed child process with PID: " << pid << endl;
+        log_notice(msg.str());
+    }
+}
+
+
 /**
  * There is no object file.
  * @param object_file The object file.
index eb1fa29..5f008b7 100644 (file)
@@ -121,6 +121,8 @@ public:
 
     void print_error_kill_child(const int);
 
+    void print_child_killed(const int);
+
     void print_no_object_file(const std::string&);
 };
 
index bd354b2..c287f6f 100644 (file)
@@ -168,6 +168,42 @@ int init_signals()
 
 
 /**
+ * Try to run in daemon mode if enabled in config.
+ * @param daemon_mode True if process should detach to init (run as daemon), false if not detach to init.
+ * @return 0 if all is fine, -1 on error.
+ */
+int init_daemon_mode(bool daemon_mode)
+{
+    updater->get_logger()->print_daemon_mode(daemon_mode);
+    if ( daemon_mode == true )
+    {
+        int pid = fork();
+        if ( pid < 0 )
+        {
+            // error fork
+            updater->get_logger()->print_error_fork();
+            return -1;
+        }
+        else if ( pid > 0 )
+        {
+            // parent continues here
+            if ( write_pidfile(pid) != 0 )
+            {
+                if ( kill(pid,SIGTERM) != 0 )
+                    updater->get_logger()->print_error_kill_child(pid);
+                else
+                    updater->get_logger()->print_child_killed(pid);
+                exit(-1);
+            }
+            updater->get_logger()->print_runnig_as_daemon(pid);
+            exit(0);
+        }
+        // child starts here
+    }
+    return 0;
+}
+
+/**
  * @brief The main part.
  * @param argc Number of arguments
  * @param argv Command line arguments
@@ -196,31 +232,9 @@ int main(int argc, char *argv[])
     if ( init_signals() != 0)
         return -1;
 
-    // initialize daemon mode if configured TODO: into separate function
-    updater->get_logger()->print_daemon_mode(updater->get_config()->get_daemon_mode());
-    if ( updater->get_config()->get_daemon_mode() == 1 )
-    {
-        int pid = fork();
-        if ( pid < 0 )
-        {
-            // error fork
-            updater->get_logger()->print_error_fork();
-            return -1;
-        }
-        else if ( pid > 0 )
-        {
-            // parent
-            if ( write_pidfile(pid) != 0 )
-            {
-                if ( kill(pid,SIGTERM) != 0 )
-                    updater->get_logger()->print_error_kill_child(pid);
-                return -1;
-            }
-            updater->get_logger()->print_runnig_as_daemon(pid);
-            return 0;
-        }
-        // child starts here
-    }
+    // init daemon_mode if enabled
+    if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
+        return -1;
 
     // service processing starts here
     do
@@ -236,7 +250,7 @@ int main(int argc, char *argv[])
         }
 
         if ( updater->get_config()->get_daemon_mode() == 1 )
-            sleep(10);  // TODO: in a final release, correct the sleep value to something suitable
+            sleep(5);
 
     }while ( updater->get_config()->get_daemon_mode() == 1 );