From: Bjoern Sikora Date: Fri, 31 Jul 2009 15:02:26 +0000 (+0200) Subject: Implemented daemon ability. X-Git-Tag: v1.1~262 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=388f4ab0c0f91c8dd02477fb82d574bb65914227;p=bpdyndnsd Implemented daemon ability. --- diff --git a/bpdyndnsd.conf b/bpdyndnsd.conf index 974c890..10413ed 100644 --- a/bpdyndnsd.conf +++ b/bpdyndnsd.conf @@ -1,4 +1,4 @@ -daemon_mode=0 +daemon_mode=1 logfile=/var/log/bpdyndnsd.log -loglevel=1 +loglevel=9 syslog=1 diff --git a/src/config.cpp b/src/config.cpp index d8bd029..8ccaaef 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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; +} diff --git a/src/config.h b/src/config.h index 657a2e3..26b74e9 100644 --- a/src/config.h +++ b/src/config.h @@ -77,6 +77,8 @@ public: int get_loglevel(); + bool get_daemon_mode(); + }; #endif diff --git a/src/logger.cpp b/src/logger.cpp index d9ad2b8..9906e54 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -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; +} diff --git a/src/logger.h b/src/logger.h index cb99f84..5d73358 100644 --- a/src/logger.h +++ b/src/logger.h @@ -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 diff --git a/src/main.cpp b/src/main.cpp index ca779c5..46def11 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,8 @@ #define REVISION 1 #define RELEASE 0 +#define PIDFILE "/var/run/bpdyndnsd.pid" + #include #include #include @@ -30,11 +32,54 @@ #include "dhs.cpp" #include "ods.cpp" +#include +#include + + using namespace std; typedef boost::shared_ptr 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; }