Implemented daemon ability.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 31 Jul 2009 15:02:26 +0000 (17:02 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 31 Jul 2009 15:02:26 +0000 (17:02 +0200)
bpdyndnsd.conf
src/config.cpp
src/config.h
src/logger.cpp
src/logger.h
src/main.cpp

index 974c890..10413ed 100644 (file)
@@ -1,4 +1,4 @@
-daemon_mode=0
+daemon_mode=1
 logfile=/var/log/bpdyndnsd.log
-loglevel=1
+loglevel=9
 syslog=1
index d8bd029..8ccaaef 100644 (file)
@@ -365,3 +365,12 @@ int Config::get_loglevel()
 {
     return Loglevel;
 }
+
+/**
+ * Getter for member Daemon_mode.
+ * @return TRUE if enabled, FALSE if disabled.
+ */
+bool Config::get_daemon_mode()
+{
+    return Daemon_mode;
+}
index 657a2e3..26b74e9 100644 (file)
@@ -77,6 +77,8 @@ public:
 
     int get_loglevel();
 
+    bool get_daemon_mode();
+
 };
 
 #endif
index d9ad2b8..9906e54 100644 (file)
@@ -197,3 +197,35 @@ void Logger::print_missing_cmd_service_option()
 {
     cerr << "Missing option to initialize service. Protocol, host, login and password must be specified." << endl;
 }
+
+
+void Logger::print_runnig_as_daemon(const int pid)
+{
+    cout << "Runnig as daemon: " << pid << endl;
+}
+
+void Logger::print_daemon_mode(const bool daemon_mode)
+{
+    string mode = "disabled";
+    if (daemon_mode == true)
+        mode = "enabled";
+    cout << "Daemon mode is " << mode << "." << endl;
+}
+
+
+void Logger::print_error_fork()
+{
+    cerr << "Error while trying to fork." << endl;
+}
+
+
+void Logger::print_pid_found(const int pid)
+{
+    cout << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
+}
+
+
+void Logger::print_process_already_running(const int pid)
+{
+    cerr << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
+}
index cb99f84..5d73358 100644 (file)
@@ -56,6 +56,16 @@ public:
     void print_conf_not_loaded(const string&);
 
     void print_missing_cmd_service_option();
+
+    void print_runnig_as_daemon(const int);
+
+    void print_daemon_mode(const bool);
+
+    void print_error_fork();
+
+    void print_pid_found(const int);
+
+    void print_process_already_running(const int);
 };
 
 #endif
index ca779c5..46def11 100644 (file)
@@ -15,6 +15,8 @@
 #define REVISION    1
 #define RELEASE     0
 
+#define PIDFILE "/var/run/bpdyndnsd.pid"
+
 #include <iostream>
 #include <list>
 #include <string>
 #include "dhs.cpp"
 #include "ods.cpp"
 
+#include <sys/types.h>
+#include <signal.h>
+
+
 using namespace std;
 
 typedef boost::shared_ptr<Updater> UpdaterPtr;
 
 /**
+ * Checks if a bpdyndnsd process is already running.
+ * @param updater Shared Pointer to updater, needed for logging.
+ * @return 0 if process not running already or pid of already running process.
+ */
+int check_for_running_process(UpdaterPtr updater)
+{
+    ifstream pidfile(PIDFILE);
+    if ( pidfile.is_open() )
+    {
+        int pid;
+        pidfile >> pid;
+        if ( pid > 0 )
+            updater->get_logger()->print_pid_found(pid);
+        else
+            return 0;
+
+        // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
+        if ( kill(pid,0) == 0)
+        {
+            updater->get_logger()->print_process_already_running(pid);
+            pidfile.close();
+            return pid;
+        }
+    }
+    pidfile.close();
+    return 0;
+}
+
+void write_pidfile(int pid)
+{
+    ofstream pidfile(PIDFILE);
+    if ( pidfile.is_open() )
+    {
+        pidfile << pid << endl;
+    }
+    pidfile.close();
+}
+
+/**
  * @brief The main part.
  * @param argc Number of arguments
  * @param argv Command line arguments
@@ -53,14 +98,44 @@ int main(int argc, char *argv[])
     if ( updater->init_config_from_files() != 0 )
         return 0;
 
-    cout << "Loglevel: " << updater->get_config()->get_loglevel() << endl;
+    // open pidfile and check for running process
+    if ( check_for_running_process(updater) != 0)
+        return 0;
 
     // set the configured loggin facility, default stdout
 
     // initialize daemon mode if configured
-
-    // update all configured services
-    updater->update_services();
+    bool daemon_mode = updater->get_config()->get_daemon_mode();
+    updater->get_logger()->print_daemon_mode(daemon_mode);
+
+    // fork if daemon mode is enabled
+    if ( daemon_mode == 1 )
+    {
+        int pid = fork();
+        if ( pid < 0 )
+        {
+            // error fork
+            updater->get_logger()->print_error_fork();
+            return 0;
+        }
+        else if ( pid > 0 )
+        {
+            // parent
+            write_pidfile(pid);
+            updater->get_logger()->print_runnig_as_daemon(pid);
+            return 0;
+        }
+        // child starts here
+    }
+
+    // service processing starts here
+    do
+    {
+        // update all configured services
+        updater->update_services();
+        sleep(3);
+
+    }while ( daemon_mode == 1 );
 
     return 0;
 }