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>
33 #include "service.cpp"
34 #include "serviceholder.cpp"
35 #include "updater.cpp"
36 #include "iphelper.cpp"
37 #include "httphelper.cpp"
38 #include "serializeservicecontainer.cpp"
44 #include "easydns.cpp"
46 #include "zoneedit.cpp"
51 bool is_online = true;
54 * Checks if a bpdyndnsd process is already running.
55 * @param updater Shared Pointer to updater, needed for logging.
56 * @return 0 if process not running already or pid of already running process.
58 int check_for_running_process()
60 ifstream pidfile(PIDFILE);
61 if ( pidfile.is_open() )
66 updater->get_logger()->print_pid_found(pid);
70 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
71 if ( kill(pid,0) == 0)
73 updater->get_logger()->print_process_already_running(pid);
84 * Writes the pid into the pidfile.
85 * @param pid The process's pid.
87 int write_pidfile(int pid)
89 ofstream pidfile(PIDFILE);
90 if ( pidfile.is_open() )
92 pidfile << pid << endl;
96 updater->get_logger()->print_error_opening_rw(PIDFILE);
105 * Parent shutdown function
106 * @return 0 if all is fine, -1 otherwise
108 int shutdown_parent(bool remove_pid, int ret_val)
110 // starting shutdown_parent
111 updater->get_logger()->print_starting_shutdown_parent();
113 // remove pidfile if requested
117 // shutdown parent complete
118 updater->get_logger()->print_shutdown_parent_succeeded();
120 // release shared pointer
129 * @return 0 if all is fine, -1 otherwise
136 updater->get_logger()->print_starting_shutdown();
138 // serialize actual service objects
139 if ( updater->get_service_holder()->serialize_services() != 0 )
146 updater->get_logger()->print_shutdown_succeeded();
148 // release shared pointer
156 * Signal SIGTERM caught, releasing resources and exit.
157 * @param param Parameter from the signal interface.
159 void terminate(int param)
161 updater->get_logger()->print_caught_sigterm();
168 * Signal SIGUSR1 caught, switching to offline mode.
169 * @param param Parameter from the signal interface.
171 void switch_to_offline(int param)
173 updater->get_logger()->print_caught_siguser1();
179 * Signal SIGHUP caught, reloading config and switching to online mode.
180 * @param param Parameter from the signal interface.
182 void reload_config(int param)
184 updater->get_logger()->print_caught_sighup();
185 if ( updater->reload_config() != 0 )
192 * Initialize the signals we handle.
193 * @return 0 if all is fine, -1 on error.
197 sighandler_t ret_val;
198 ret_val = signal(SIGTERM,terminate);
199 if ( ret_val == SIG_ERR )
201 updater->get_logger()->print_error_setting_signal("SIGTERM");
204 ret_val = signal(SIGUSR1,switch_to_offline);
205 if ( ret_val == SIG_ERR )
207 updater->get_logger()->print_error_setting_signal("SIGUSR1");
210 ret_val = signal(SIGHUP,reload_config);
211 if ( ret_val == SIG_ERR )
213 updater->get_logger()->print_error_setting_signal("SIGHUP");
222 * Try to run in daemon mode if enabled in config.
223 * @param daemon_mode True if process should detach to init (run as daemon), false if not detach to init.
224 * @return 0 if all is fine, -1 on error.
226 int init_daemon_mode(bool daemon_mode)
228 updater->get_logger()->print_daemon_mode(daemon_mode);
229 if ( daemon_mode == true )
235 updater->get_logger()->print_error_fork();
240 // parent continues here
241 if ( write_pidfile(pid) != 0 )
243 if ( kill(pid,SIGTERM) != 0 )
245 updater->get_logger()->print_error_kill_child(pid);
246 shutdown_parent(false,-1); // keep pidfile
250 updater->get_logger()->print_child_killed(pid);
251 shutdown_parent(true,-1); // remove pidfile
254 updater->get_logger()->print_runnig_as_daemon(pid);
255 shutdown_parent(false,0); // keep pidfile
261 if ( write_pidfile(getpid()) != 0 )
271 * @brief The main part.
272 * @param argc Number of arguments
273 * @param argv Command line arguments
274 * @return 0 if all is fine.
276 int main(int argc, char *argv[])
278 // initialize Updater
279 Updater::Ptr _updater(new Updater);
283 // load config and initialize helper classes
284 if ( updater->load_config(argc,argv) != 0 )
287 // open pidfile and check for running process
288 if ( check_for_running_process() != 0)
291 // init signal handling
292 if ( init_signals() != 0)
295 // init daemon_mode if enabled
296 if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
299 // service processing starts here
302 if ( is_online == true )
304 // update all configured services
305 updater->update_services();
309 // We are in offline mode, do nothing, expect printing "offline mode".
310 updater->get_logger()->print_offline_mode();
313 // Snore, snore... don't hug the cpu if we are in daemon_mode.
314 if ( updater->get_config()->get_daemon_mode() == 1 )
317 }while ( updater->get_config()->get_daemon_mode() == 1 );
319 // Serialize services to save their actual state.
320 if ( shutdown() != 0 )