2 * @brief The main function.
6 * @copyright Intra2net AG
18 #define PIDFILE "/home/bjoern/intranator/bpdyndnsd/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"
38 #include "iphelper.cpp"
39 #include "httphelper.cpp"
40 #include "serializeservicecontainer.cpp"
46 bool is_online = true;
49 * Checks if a bpdyndnsd process is already running.
50 * @param updater Shared Pointer to updater, needed for logging.
51 * @return 0 if process not running already or pid of already running process.
53 int check_for_running_process()
55 ifstream pidfile(PIDFILE);
56 if ( pidfile.is_open() )
61 updater->get_logger()->print_pid_found(pid);
65 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
66 if ( kill(pid,0) == 0)
68 updater->get_logger()->print_process_already_running(pid);
79 * Writes the pid into the pidfile.
80 * @param pid The process's pid.
82 int write_pidfile(int pid)
84 ofstream pidfile(PIDFILE);
85 if ( pidfile.is_open() )
87 pidfile << pid << endl;
91 updater->get_logger()->print_error_opening_rw(PIDFILE);
100 * Parent shutdown function
101 * @return 0 if all is fine, -1 otherwise
103 int shutdown_parent(bool remove_pid, int ret_val)
105 // starting shutdown_parent
106 updater->get_logger()->print_starting_shutdown_parent();
108 // remove pidfile if requested
112 // shutdown parent complete
113 updater->get_logger()->print_shutdown_parent_succeeded();
115 // release shared pointer
124 * @return 0 if all is fine, -1 otherwise
131 updater->get_logger()->print_starting_shutdown();
133 // serialize actual service objects
134 if ( updater->get_service_holder()->serialize_services() != 0 )
141 updater->get_logger()->print_shutdown_succeeded();
143 // release shared pointer
151 * Signal SIGTERM caught, releasing resources and exit.
152 * @param param Parameter from the signal interface.
154 void terminate(int param)
156 updater->get_logger()->print_caught_sigterm();
163 * Signal SIGUSR1 caught, switching to offline mode.
164 * @param param Parameter from the signal interface.
166 void switch_to_offline(int param)
168 updater->get_logger()->print_caught_siguser1();
174 * Signal SIGHUP caught, reloading config and switching to online mode.
175 * @param param Parameter from the signal interface.
177 void reload_config(int param)
179 updater->get_logger()->print_caught_sighup();
180 if ( updater->reload_config() != 0 )
187 * Initialize the signals we handle.
188 * @return 0 if all is fine, -1 on error.
192 sighandler_t ret_val;
193 ret_val = signal(SIGTERM,terminate);
194 if ( ret_val == SIG_ERR )
196 updater->get_logger()->print_error_setting_signal("SIGTERM");
199 ret_val = signal(SIGUSR1,switch_to_offline);
200 if ( ret_val == SIG_ERR )
202 updater->get_logger()->print_error_setting_signal("SIGUSR1");
205 ret_val = signal(SIGHUP,reload_config);
206 if ( ret_val == SIG_ERR )
208 updater->get_logger()->print_error_setting_signal("SIGHUP");
217 * Try to run in daemon mode if enabled in config.
218 * @param daemon_mode True if process should detach to init (run as daemon), false if not detach to init.
219 * @return 0 if all is fine, -1 on error.
221 int init_daemon_mode(bool daemon_mode)
223 updater->get_logger()->print_daemon_mode(daemon_mode);
224 if ( daemon_mode == true )
230 updater->get_logger()->print_error_fork();
235 // parent continues here
236 if ( write_pidfile(pid) != 0 )
238 if ( kill(pid,SIGTERM) != 0 )
240 updater->get_logger()->print_error_kill_child(pid);
241 shutdown_parent(false,-1); // keep pidfile
245 updater->get_logger()->print_child_killed(pid);
246 shutdown_parent(true,-1); // remove pidfile
249 updater->get_logger()->print_runnig_as_daemon(pid);
250 shutdown_parent(false,0); // keep pidfile
256 if ( write_pidfile(getpid()) != 0 )
266 * @brief The main part.
267 * @param argc Number of arguments
268 * @param argv Command line arguments
269 * @return 0 if all is fine.
271 int main(int argc, char *argv[])
273 // initialize Updater
274 Updater::Ptr _updater(new Updater);
278 // load config and initialize helper classes
279 if ( updater->load_config(argc,argv) != 0 )
282 // open pidfile and check for running process
283 if ( check_for_running_process() != 0)
286 // init signal handling
287 if ( init_signals() != 0)
290 // init daemon_mode if enabled
291 if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
294 // service processing starts here
297 if ( is_online == true )
299 // update all configured services
300 updater->update_services();
304 // We are in offline mode, do nothing, expect printing "offline mode".
305 updater->get_logger()->print_offline_mode();
308 // Snore, snore... don't hug the cpu if we are in daemon_mode.
309 if ( updater->get_config()->get_daemon_mode() == 1 )
312 }while ( updater->get_config()->get_daemon_mode() == 1 );
314 // Serialize services to save their actual state.
315 if ( shutdown() != 0 )