2 * @brief The main function.
6 * @copyright Intra2net AG
18 #define PIDFILE "/var/run/bpdyndnsd.pid"
24 #include <boost/foreach.hpp>
26 #include "updater.cpp"
30 #include "service.cpp"
35 #include <sys/types.h>
41 typedef boost::shared_ptr<Updater> UpdaterPtr;
44 * Checks if a bpdyndnsd process is already running.
45 * @param updater Shared Pointer to updater, needed for logging.
46 * @return 0 if process not running already or pid of already running process.
48 int check_for_running_process(UpdaterPtr updater)
50 ifstream pidfile(PIDFILE);
51 if ( pidfile.is_open() )
56 updater->get_logger()->print_pid_found(pid);
60 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
61 if ( kill(pid,0) == 0)
63 updater->get_logger()->print_process_already_running(pid);
72 void write_pidfile(int pid)
74 ofstream pidfile(PIDFILE);
75 if ( pidfile.is_open() )
77 pidfile << pid << endl;
83 * @brief The main part.
84 * @param argc Number of arguments
85 * @param argv Command line arguments
86 * @return 0 if all is fine.
88 int main(int argc, char *argv[])
91 UpdaterPtr updater(new Updater);
93 // load the cmd options
94 if ( updater->init_config_from_cmd(argc,argv) != 0 )
97 // load the config and service files
98 if ( updater->init_config_from_files() != 0 )
101 // open pidfile and check for running process
102 if ( check_for_running_process(updater) != 0)
105 // set the configured loggin facility, default stdout
107 // initialize daemon mode if configured
108 bool daemon_mode = updater->get_config()->get_daemon_mode();
109 updater->get_logger()->print_daemon_mode(daemon_mode);
111 // fork if daemon mode is enabled
112 if ( daemon_mode == 1 )
118 updater->get_logger()->print_error_fork();
125 updater->get_logger()->print_runnig_as_daemon(pid);
131 // service processing starts here
134 // update all configured services
135 updater->update_services();
138 }while ( daemon_mode == 1 );