2 * @brief The main function.
6 * @copyright Intra2net AG
18 #define PIDFILE "/var/run/bpdyndnsd.pid"
19 #define OBJECT_FILE "/home/bjoern/intranator/bpdyndnsd/objects.ser"
25 #include <boost/foreach.hpp>
26 #include <sys/types.h>
35 #include "service.cpp"
36 #include "serviceholder.cpp"
37 #include "updater.cpp"
43 bool is_online = true;
46 * Checks if a bpdyndnsd process is already running.
47 * @param updater Shared Pointer to updater, needed for logging.
48 * @return 0 if process not running already or pid of already running process.
50 int check_for_running_process()
52 ifstream pidfile(PIDFILE);
53 if ( pidfile.is_open() )
58 updater->get_logger()->print_pid_found(pid);
62 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
63 if ( kill(pid,0) == 0)
65 updater->get_logger()->print_process_already_running(pid);
76 * Writes the pid into the pidfile.
77 * @param pid The process's pid.
79 int write_pidfile(int pid)
81 ofstream pidfile(PIDFILE);
82 if ( pidfile.is_open() )
84 pidfile << pid << endl;
88 updater->get_logger()->print_error_opening_rw(PIDFILE);
97 * Signal SIGTERM caught, releasing resources and exit.
98 * @param param Parameter from the signal interface.
100 void terminate(int param)
102 updater->get_logger()->print_caught_sigterm();
104 // unfortunately we can't call serialize_services in any destructor
105 // cause some singleton are already destroyed !?!
106 if ( updater->get_config()->serialize_services() != 0 )
118 * Signal SIGUSR1 caught, switching to offline mode.
119 * @param param Parameter from the signal interface.
121 void switch_to_offline(int param)
123 updater->get_logger()->print_caught_siguser1();
129 * Signal SIGHUP caught, reloading config and switching to online mode.
130 * @param param Parameter from the signal interface.
132 void reload_config(int param)
134 updater->get_logger()->print_caught_sighup();
135 if ( updater->reload_config() != 0 )
142 * Initialize the signals we handle.
146 sighandler_t ret_val;
147 ret_val = signal(SIGTERM,terminate);
148 if ( ret_val == SIG_ERR )
150 updater->get_logger()->print_error_setting_signal("SIGTERM");
153 ret_val = signal(SIGUSR1,switch_to_offline);
154 if ( ret_val == SIG_ERR )
156 updater->get_logger()->print_error_setting_signal("SIGUSR1");
159 ret_val = signal(SIGHUP,reload_config);
160 if ( ret_val == SIG_ERR )
162 updater->get_logger()->print_error_setting_signal("SIGHUP");
171 * @brief The main part.
172 * @param argc Number of arguments
173 * @param argv Command line arguments
174 * @return 0 if all is fine.
176 int main(int argc, char *argv[])
178 // initialize Updater
179 Updater::Ptr _updater(new Updater);
183 // load the cmd options
184 if ( updater->init_config_from_cmd(argc,argv) != 0 )
187 // load the config and service files
188 if ( updater->init_config_from_files() != 0 )
191 // open pidfile and check for running process
192 if ( check_for_running_process() != 0)
195 // init signal handling
196 if ( init_signals() != 0)
199 // initialize daemon mode if configured TODO: into separate function
200 updater->get_logger()->print_daemon_mode(updater->get_config()->get_daemon_mode());
201 if ( updater->get_config()->get_daemon_mode() == 1 )
207 updater->get_logger()->print_error_fork();
213 if ( write_pidfile(pid) != 0 )
215 if ( kill(pid,SIGTERM) != 0 )
216 updater->get_logger()->print_error_kill_child(pid);
219 updater->get_logger()->print_runnig_as_daemon(pid);
225 // service processing starts here
228 if ( is_online == true )
230 // update all configured services
231 updater->update_services();
235 updater->get_logger()->print_offline_mode();
238 if ( updater->get_config()->get_daemon_mode() == 1 )
239 sleep(10); // TODO: in a final release, correct the sleep value to something suitable
241 }while ( updater->get_config()->get_daemon_mode() == 1 );
244 // serialize services to save their actual status
245 if ( updater->get_config()->serialize_services() != 0 )