2 * @brief The main function.
6 * @copyright Intra2net AG
14 #define PIDFILE "/var/run/bpdyndnsd.pid"
20 #include <boost/foreach.hpp>
21 #include <sys/types.h>
29 bool is_online = true;
32 * Checks if a bpdyndnsd process is already running.
33 * @param updater Shared Pointer to updater, needed for logging.
34 * @return 0 if process not running already or pid of already running process.
36 int check_for_running_process()
38 ifstream pidfile(PIDFILE);
39 if ( pidfile.is_open() )
44 updater->get_logger()->print_pid_found(pid);
48 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
49 if ( kill(pid,0) == 0)
51 updater->get_logger()->print_process_already_running(pid);
62 * Writes the pid into the pidfile.
63 * @param pid The process's pid.
65 int write_pidfile(int pid)
67 ofstream pidfile(PIDFILE);
68 if ( pidfile.is_open() )
70 pidfile << pid << endl;
74 updater->get_logger()->print_error_opening_rw(PIDFILE);
83 * Parent shutdown function
84 * @return 0 if all is fine, -1 otherwise
86 int shutdown_parent(bool remove_pid, int ret_val)
88 // starting shutdown_parent
89 updater->get_logger()->print_starting_shutdown_parent();
91 // remove pidfile if requested
95 // shutdown parent complete
96 updater->get_logger()->print_shutdown_parent_succeeded();
98 // release shared pointer
107 * @return 0 if all is fine, -1 otherwise
114 updater->get_logger()->print_starting_shutdown();
116 // serialize actual service objects
117 if ( updater->get_service_holder()->serialize_services() != 0 )
124 updater->get_logger()->print_shutdown_succeeded();
126 // release shared pointer
134 * Signal SIGTERM caught, releasing resources and exit.
135 * @param param Parameter from the signal interface.
137 void terminate(int param)
139 updater->get_logger()->print_caught_sigterm();
146 * Signal SIGUSR1 caught, switching to offline mode.
147 * @param param Parameter from the signal interface.
149 void switch_to_offline(int param)
151 updater->get_logger()->print_caught_siguser1();
157 * Signal SIGHUP caught, reloading config and switching to online mode.
158 * @param param Parameter from the signal interface.
160 void reload_config(int param)
162 updater->get_logger()->print_caught_sighup();
163 if ( updater->reload_config() != 0 )
170 * Initialize the signals we handle.
171 * @return 0 if all is fine, -1 on error.
175 sighandler_t ret_val;
176 ret_val = signal(SIGTERM,terminate);
177 if ( ret_val == SIG_ERR )
179 updater->get_logger()->print_error_setting_signal("SIGTERM");
182 ret_val = signal(SIGUSR1,switch_to_offline);
183 if ( ret_val == SIG_ERR )
185 updater->get_logger()->print_error_setting_signal("SIGUSR1");
188 ret_val = signal(SIGHUP,reload_config);
189 if ( ret_val == SIG_ERR )
191 updater->get_logger()->print_error_setting_signal("SIGHUP");
200 * Try to run in daemon mode if enabled in config.
201 * @param daemon_mode True if process should detach to init (run as daemon), false if not detach to init.
202 * @return 0 if all is fine, -1 on error.
204 int init_daemon_mode(bool daemon_mode)
206 updater->get_logger()->print_daemon_mode(daemon_mode);
207 if ( daemon_mode == true )
213 updater->get_logger()->print_error_fork();
218 // parent continues here
219 if ( write_pidfile(pid) != 0 )
221 if ( kill(pid,SIGTERM) != 0 )
223 updater->get_logger()->print_error_kill_child(pid);
224 shutdown_parent(false,-1); /*lint !e534 */
228 updater->get_logger()->print_child_killed(pid);
229 shutdown_parent(true,-1); /*lint !e534 */
232 updater->get_logger()->print_runnig_as_daemon(pid);
233 shutdown_parent(false,0); /*lint !e534 */
239 if ( write_pidfile(getpid()) != 0 )
241 shutdown(); /*lint !e534 */
249 * @brief The main part.
250 * @param argc Number of arguments
251 * @param argv Command line arguments
252 * @return 0 if all is fine.
254 int main(int argc, char *argv[])
256 // initialize Updater
257 updater = Updater::Ptr(new Updater);
259 // load config and initialize helper classes
260 if ( updater->load_config(argc,argv) != 0 )
263 // open pidfile and check for running process
264 if ( check_for_running_process() != 0)
267 // init signal handling
268 if ( init_signals() != 0)
271 // init daemon_mode if enabled
272 if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
275 // service processing starts here
278 if ( is_online == true )
280 // update all configured services
281 updater->update_services();
285 // We are in offline mode, do nothing, expect printing "offline mode".
286 updater->get_logger()->print_offline_mode();
289 // Snore, snore... don't hug the cpu if we are in daemon_mode.
290 if ( updater->get_config()->get_daemon_mode() == 1 )
291 sleep(10); /*lint !e534 */
293 }while ( updater->get_config()->get_daemon_mode() == 1 );
295 // Serialize services to save their actual state.
296 if ( shutdown() != 0 )