From c5675c01c139cf0c0b96badbeafdadb576a877f9 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Fri, 31 Jul 2009 18:29:59 +0200 Subject: [PATCH] Implemented signal handling. --- src/config.cpp | 15 +++++++++++ src/config.h | 2 + src/logger.cpp | 47 +++++++++++++++++++++++++++++++++++ src/logger.h | 6 ++++ src/main.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++---- src/updater.cpp | 10 +++++++ src/updater.h | 2 + 7 files changed, 149 insertions(+), 6 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 8ccaaef..a155dbe 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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(); +} diff --git a/src/config.h b/src/config.h index 26b74e9..dbda160 100644 --- a/src/config.h +++ b/src/config.h @@ -79,6 +79,8 @@ public: bool get_daemon_mode(); + void delete_services(); + }; #endif diff --git a/src/logger.cpp b/src/logger.cpp index 9906e54..1686e11 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -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; +} diff --git a/src/logger.h b/src/logger.h index 5d73358..5ed40ae 100644 --- a/src/logger.h +++ b/src/logger.h @@ -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 diff --git a/src/main.cpp b/src/main.cpp index 46def11..5beb295 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,12 +40,15 @@ using namespace std; typedef boost::shared_ptr 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 ); diff --git a/src/updater.cpp b/src/updater.cpp index 6f97121..48a90c7 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -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() diff --git a/src/updater.h b/src/updater.h index 89ab806..89a8c63 100644 --- a/src/updater.h +++ b/src/updater.h @@ -36,6 +36,8 @@ public: ConfigPtr get_config(); LoggerPtr get_logger(); + + void reload_config(); }; #endif -- 1.7.1