Implemented signal handling.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 31 Jul 2009 16:29:59 +0000 (18:29 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 31 Jul 2009 16:29:59 +0000 (18:29 +0200)
src/config.cpp
src/config.h
src/logger.cpp
src/logger.h
src/main.cpp
src/updater.cpp
src/updater.h

index 8ccaaef..a155dbe 100644 (file)
@@ -357,6 +357,7 @@ po::options_description* Config::get_opt_desc_conf_service()
     return Opt_desc_conf_service;
 }
 
+
 /**
  * Getter for member Loglevel.
  * @return Member Loglevel.
@@ -366,6 +367,7 @@ int Config::get_loglevel()
     return Loglevel;
 }
 
+
 /**
  * Getter for member Daemon_mode.
  * @return TRUE if enabled, FALSE if disabled.
@@ -374,3 +376,16 @@ bool Config::get_daemon_mode()
 {
     return Daemon_mode;
 }
+
+
+/**
+ * Resets all shared Service pointers and clears the Services list.
+ */
+void Config::delete_services()
+{
+    BOOST_FOREACH( ServicePtr service, Services )
+    {
+        service.reset();
+    }
+    Services.clear();
+}
index 26b74e9..dbda160 100644 (file)
@@ -79,6 +79,8 @@ public:
 
     bool get_daemon_mode();
 
+    void delete_services();
+
 };
 
 #endif
index 9906e54..1686e11 100644 (file)
@@ -199,11 +199,20 @@ void Logger::print_missing_cmd_service_option()
 }
 
 
+/**
+ * Process running as daemon.
+ * @param pid The pid of the daemon.
+ */
 void Logger::print_runnig_as_daemon(const int pid)
 {
     cout << "Runnig as daemon: " << pid << endl;
 }
 
+
+/**
+ * Prints out the daemon mode.
+ * @param daemon_mode The daemon mode.
+ */
 void Logger::print_daemon_mode(const bool daemon_mode)
 {
     string mode = "disabled";
@@ -213,19 +222,57 @@ void Logger::print_daemon_mode(const bool daemon_mode)
 }
 
 
+/**
+ * There was an error while trying to fork.
+ */
 void Logger::print_error_fork()
 {
     cerr << "Error while trying to fork." << endl;
 }
 
 
+/**
+ * A pid in the pidfile was found.
+ * @param pid The pid found in the pidfile.
+ */
 void Logger::print_pid_found(const int pid)
 {
     cout << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
 }
 
 
+/**
+ * Another process is already running.
+ * @param pid The pid of the other process.
+ */
 void Logger::print_process_already_running(const int pid)
 {
     cerr << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
 }
+
+
+/**
+ * SIGTERM caught.
+ */
+void Logger::print_caught_sigterm()
+{
+    cout << "Caught SIGTERM. Exiting..." << endl;
+}
+
+
+/**
+ * SIGUSR1 caught.
+ */
+void Logger::print_caught_siguser1()
+{
+    cout << "Caught SIGUSR1. Switching to offline mode..." << endl;
+}
+
+
+/**
+ * SIGHUP caught.
+ */
+void Logger::print_caught_sighup()
+{
+    cout << "Caught SIGHUP. Reloading config and switching to online mode..." << endl;
+}
index 5d73358..5ed40ae 100644 (file)
@@ -66,6 +66,12 @@ public:
     void print_pid_found(const int);
 
     void print_process_already_running(const int);
+
+    void print_caught_sigterm();
+
+    void print_caught_siguser1();
+
+    void print_caught_sighup();
 };
 
 #endif
index 46def11..5beb295 100644 (file)
@@ -40,12 +40,15 @@ using namespace std;
 
 typedef boost::shared_ptr<Updater> UpdaterPtr;
 
+UpdaterPtr updater;
+bool online_mode = 1;
+
 /**
  * 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)
+int check_for_running_process()
 {
     ifstream pidfile(PIDFILE);
     if ( pidfile.is_open() )
@@ -69,6 +72,11 @@ int check_for_running_process(UpdaterPtr updater)
     return 0;
 }
 
+
+/**
+ * Writes the pid into the pidfile.
+ * @param pid The process's pid.
+ */
 void write_pidfile(int pid)
 {
     ofstream pidfile(PIDFILE);
@@ -79,6 +87,51 @@ void write_pidfile(int pid)
     pidfile.close();
 }
 
+
+/**
+ * Signal SIGTERM caught, releasing resources and exit.
+ * @param param Parameter from the signal interface.
+ */
+void terminate(int param)
+{
+    updater->get_logger()->print_caught_sigterm();
+    updater.reset();
+    exit(1);
+}
+
+
+/**
+ * Signal SIGUSR1 caught, switching to offline mode.
+ * @param param Parameter from the signal interface.
+ */
+void switch_to_offline(int param)
+{
+    online_mode = 0;
+}
+
+
+/**
+ * Signal SIGHUP caught, reloading config and switching to online mode.
+ * @param param 
+ */
+void reload_config(int param)
+{
+    updater->reload_config();
+    online_mode = 1;
+}
+
+
+/**
+ * Initialize the signals we handle.
+ */
+void init_signals()
+{
+    signal(SIGTERM,terminate);
+    signal(SIGUSR1,switch_to_offline);
+    signal(SIGHUP,reload_config);
+}
+
+
 /**
  * @brief The main part.
  * @param argc Number of arguments
@@ -88,7 +141,9 @@ void write_pidfile(int pid)
 int main(int argc, char *argv[])
 {
     // initialize Updater
-    UpdaterPtr updater(new Updater);
+    UpdaterPtr _updater(new Updater);
+    updater = _updater;
+    _updater.reset();
 
     // load the cmd options
     if ( updater->init_config_from_cmd(argc,argv) != 0 )
@@ -99,9 +154,12 @@ int main(int argc, char *argv[])
         return 0;
 
     // open pidfile and check for running process
-    if ( check_for_running_process(updater) != 0)
+    if ( check_for_running_process() != 0)
         return 0;
 
+    // init signal handling
+    init_signals();
+
     // set the configured loggin facility, default stdout
 
     // initialize daemon mode if configured
@@ -131,9 +189,12 @@ int main(int argc, char *argv[])
     // service processing starts here
     do
     {
-        // update all configured services
-        updater->update_services();
-        sleep(3);
+        if ( online_mode == 1 )
+        {
+            // update all configured services
+            updater->update_services();
+        }
+        sleep(2);
 
     }while ( daemon_mode == 1 );
 
index 6f97121..48a90c7 100644 (file)
@@ -92,6 +92,16 @@ LoggerPtr Updater::get_logger()
 
 
 /**
+ * Reloading the config. Delete all Service objects and then init new Service objects from config files.
+ */
+void Updater::reload_config()
+{
+    Conf->delete_services();
+    init_config_from_files();
+}
+
+
+/**
  * Update all configured services.
  */
 void Updater::update_services()
index 89ab806..89a8c63 100644 (file)
@@ -36,6 +36,8 @@ public:
     ConfigPtr get_config();
 
     LoggerPtr get_logger();
+
+    void reload_config();
 };
 
 #endif