2 * @brief The main function.
6 * @copyright Intra2net AG
14 #define PIDFILE "/var/run/bpdyndnsd/bpdyndnsd.pid"
20 #include <boost/foreach.hpp>
21 #include <sys/types.h>
24 #include "updater.hpp"
29 volatile bool caught_sig_hup = false;
30 volatile bool caught_sig_usr1 = false;
31 volatile bool caught_sig_usr2 = false;
32 volatile bool caught_sig_rtmin = false;
33 volatile bool caught_sig_rtmax = false;
34 volatile bool caught_sig_term = false;
37 * Checks if a bpdyndnsd process is already running.
38 * @param updater Shared Pointer to updater, needed for logging.
39 * @return 0 if process not running already or pid of already running process.
41 int check_for_running_process()
43 ifstream pidfile(PIDFILE);
44 if ( pidfile.is_open() )
49 updater->get_logger()->print_pid_found(pid);
53 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
54 if ( kill(pid,0) == 0)
56 updater->get_logger()->print_process_already_running(pid);
67 * Writes the pid into the pidfile.
68 * @param pid The process's pid.
70 int write_pidfile(int pid)
72 ofstream pidfile(PIDFILE);
73 if ( pidfile.is_open() )
75 pidfile << pid << endl;
79 updater->get_logger()->print_error_opening_rw(PIDFILE);
88 * Parent shutdown function
89 * @return 0 if all is fine, -1 otherwise
91 int shutdown_parent(bool remove_pid, int ret_val)
93 // starting shutdown_parent
94 updater->get_logger()->print_starting_shutdown_parent();
96 // remove pidfile if requested
100 // shutdown parent complete
101 updater->get_logger()->print_shutdown_parent_succeeded();
103 // release shared pointer
112 * @return 0 if all is fine, -1 otherwise
119 updater->get_logger()->print_starting_shutdown();
121 // serialize actual service objects
122 if ( updater->get_service_holder()->serialize_services() != 0 )
129 updater->get_logger()->print_shutdown_succeeded();
131 // release shared pointer
139 * Signal SIGTERM caught (exit program)
140 * @param param Parameter from the signal interface.
142 void sigterm_func(int param)
144 caught_sig_term = true;
149 * Signal SIGUSR1 caught (switch to offline mode)
150 * @param param Parameter from the signal interface.
152 void sigusr1_func(int param)
154 caught_sig_usr1 = true;
159 * Signal SIGHUP caught (reload config and reset log level)
160 * @param param Parameter from the signal interface.
162 void sighup_func(int param)
164 caught_sig_hup = true;
169 * Signal SIGUSR2 caught (switch to online mode)
170 * @param param Parameter from the signal interface.
172 void sigusr2_func(int param)
174 caught_sig_usr2 = true;
179 * Signal SIGRTMIN caught (switch to online mode with webcheck enabled)
180 * @param param Parameter from the signal interface.
182 void sigrtmin_func(int param)
184 caught_sig_rtmin = true;
189 * Signal SIGRTMAX caught (increase log level)
190 * @param param Parameter from the signal interface.
192 void sigrtmax_func(int param)
194 caught_sig_rtmax = true;
199 * Initialize the signals we handle.
200 * @return 0 if all is fine, -1 on error.
204 sighandler_t ret_val;
205 ret_val = signal(SIGTERM, sigterm_func);
206 if ( ret_val == SIG_ERR )
208 updater->get_logger()->print_error_setting_signal("SIGTERM");
211 ret_val = signal(SIGUSR1, sigusr1_func);
212 if ( ret_val == SIG_ERR )
214 updater->get_logger()->print_error_setting_signal("SIGUSR1");
217 ret_val = signal(SIGHUP, sighup_func);
218 if ( ret_val == SIG_ERR )
220 updater->get_logger()->print_error_setting_signal("SIGHUP");
223 ret_val = signal(SIGUSR2, sigusr2_func);
224 if ( ret_val == SIG_ERR )
226 updater->get_logger()->print_error_setting_signal("SIGUSR2");
229 ret_val = signal(SIGRTMIN, sigrtmin_func);
230 if ( ret_val == SIG_ERR )
232 updater->get_logger()->print_error_setting_signal("SIGRTMIN");
235 ret_val = signal(SIGRTMAX, sigrtmax_func);
236 if ( ret_val == SIG_ERR )
238 updater->get_logger()->print_error_setting_signal("SIGRTMAX");
247 * Try to run in daemon mode if enabled in config.
248 * @param daemon_mode True if process should detach to init (run as daemon), false if not detach to init.
249 * @return 0 if all is fine, -1 on error.
251 int init_daemon_mode(bool daemon_mode)
253 updater->get_logger()->print_daemon_mode(daemon_mode);
254 if ( daemon_mode == true )
260 updater->get_logger()->print_error_fork();
265 // parent continues here
266 if ( write_pidfile(pid) != 0 )
268 if ( kill(pid,SIGTERM) != 0 )
270 updater->get_logger()->print_error_kill_child(pid);
271 shutdown_parent(false,-1); /*lint !e534 */
275 updater->get_logger()->print_child_killed(pid);
276 shutdown_parent(true,-1); /*lint !e534 */
279 updater->get_logger()->print_runnig_as_daemon(pid);
280 shutdown_parent(false,0); /*lint !e534 */
286 if ( write_pidfile(getpid()) != 0 )
288 shutdown(); /*lint !e534 */
296 * @brief The main part.
297 * @param argc Number of arguments
298 * @param argv Command line arguments
299 * @return 0 if all is fine.
301 int main(int argc, char *argv[])
303 // initialize Updater
304 updater = Updater::Ptr(new Updater);
306 // load config and initialize helper classes
307 if ( updater->load_config(argc,argv) != 0 )
310 // open pidfile and check for running process
311 if ( check_for_running_process() != 0)
314 // init signal handling
315 if ( init_signals() != 0)
318 // init daemon_mode if enabled
319 if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
322 // Should we start in offline mode?
323 bool old_online_state = false;
324 bool is_online = !updater->get_config()->get_start_offline();
325 bool webcheck_enabled = updater->get_config()->get_webcheck_enabled();
326 bool exit_now = false;
328 // One shot run if daemon mode is disabled
329 if (updater->get_config()->get_daemon_mode() != 1)
332 // Tell the world we are running
333 updater->get_logger()->print_started();
335 int original_log_level = updater->get_logger()->get_loglevel();
337 // service processing starts here
343 caught_sig_usr1 = false;
344 updater->get_logger()->print_caught_siguser1();
348 } else if (caught_sig_usr2)
350 caught_sig_usr2 = false;
351 updater->get_logger()->print_caught_siguser2();
355 webcheck_enabled = false;
356 } else if (caught_sig_rtmin)
358 caught_sig_rtmin = false;
359 updater->get_logger()->print_caught_sigrtmin();
361 // Go online - with webcheck
363 webcheck_enabled = true;
364 } else if (caught_sig_rtmax)
366 caught_sig_rtmax = false;
368 // Increase log level
369 int new_loglevel = updater->get_logger()->get_loglevel() + 1;
370 updater->get_logger()->print_caught_sigrtmax(new_loglevel);
371 updater->get_logger()->set_loglevel(new_loglevel);
372 } else if (caught_sig_term)
374 caught_sig_term = false;
375 updater->get_logger()->print_caught_sigterm();
378 } else if (caught_sig_hup)
380 caught_sig_hup = false;
381 updater->get_logger()->print_caught_sighup();
383 if ( updater->reload_config() != 0 )
385 updater->get_logger()->print_conf_reload_failed_exit();
389 // Reset log level to startup log level in case it got elevated by SIGRTMAX
390 updater->get_logger()->set_loglevel(original_log_level);
394 if ( is_online == true )
396 // Check if webcheck_enabled differs due to caught singnal then set it in config correspondingly
397 if ( updater->get_config()->get_webcheck_enabled() != webcheck_enabled ) /*lint !e731 */
398 updater->get_config()->set_webcheck_enabled(webcheck_enabled);
400 // update all configured services
401 updater->update_services(is_online != old_online_state);
405 // We are in offline mode, do nothing, expect printing "offline mode".
406 updater->get_logger()->print_offline_mode();
409 // Keep old online state so we can determine
410 // if we switched from offline to online
411 old_online_state = is_online;
413 // Snore, snore... don't hog the cpu if we are in daemon_mode.
415 sleep(10); /*lint !e534 */
416 } while ( !exit_now );
418 // Serialize services to save their actual state.
419 if ( shutdown() != 0 )