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 "serviceholder.cpp"
34 #include "updater.cpp"
35 #include "ip_addr_helper.cpp"
36 #include "net_helper.cpp"
37 #include "tcp_service.cpp"
38 #include "httphelper.cpp"
39 #include "serializeservicecontainer.cpp"
42 #include "service.cpp"
43 #include "service_dyndns.cpp"
44 #include "service_dyns.cpp"
45 #include "service_ods.cpp"
46 #include "service_dhs.cpp"
47 #include "service_easydns.cpp"
48 #include "service_tzo.cpp"
49 #include "service_zoneedit.cpp"
50 #include "service_gnudip.cpp"
56 bool is_online = true;
59 * Checks if a bpdyndnsd process is already running.
60 * @param updater Shared Pointer to updater, needed for logging.
61 * @return 0 if process not running already or pid of already running process.
63 int check_for_running_process()
65 ifstream pidfile(PIDFILE);
66 if ( pidfile.is_open() )
71 updater->get_logger()->print_pid_found(pid);
75 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
76 if ( kill(pid,0) == 0)
78 updater->get_logger()->print_process_already_running(pid);
89 * Writes the pid into the pidfile.
90 * @param pid The process's pid.
92 int write_pidfile(int pid)
94 ofstream pidfile(PIDFILE);
95 if ( pidfile.is_open() )
97 pidfile << pid << endl;
101 updater->get_logger()->print_error_opening_rw(PIDFILE);
110 * Parent shutdown function
111 * @return 0 if all is fine, -1 otherwise
113 int shutdown_parent(bool remove_pid, int ret_val)
115 // starting shutdown_parent
116 updater->get_logger()->print_starting_shutdown_parent();
118 // remove pidfile if requested
122 // shutdown parent complete
123 updater->get_logger()->print_shutdown_parent_succeeded();
125 // release shared pointer
134 * @return 0 if all is fine, -1 otherwise
141 updater->get_logger()->print_starting_shutdown();
143 // serialize actual service objects
144 if ( updater->get_service_holder()->serialize_services() != 0 )
151 updater->get_logger()->print_shutdown_succeeded();
153 // release shared pointer
161 * Signal SIGTERM caught, releasing resources and exit.
162 * @param param Parameter from the signal interface.
164 void terminate(int param)
166 updater->get_logger()->print_caught_sigterm();
173 * Signal SIGUSR1 caught, switching to offline mode.
174 * @param param Parameter from the signal interface.
176 void switch_to_offline(int param)
178 updater->get_logger()->print_caught_siguser1();
184 * Signal SIGHUP caught, reloading config and switching to online mode.
185 * @param param Parameter from the signal interface.
187 void reload_config(int param)
189 updater->get_logger()->print_caught_sighup();
190 if ( updater->reload_config() != 0 )
197 * Initialize the signals we handle.
198 * @return 0 if all is fine, -1 on error.
202 sighandler_t ret_val;
203 ret_val = signal(SIGTERM,terminate);
204 if ( ret_val == SIG_ERR )
206 updater->get_logger()->print_error_setting_signal("SIGTERM");
209 ret_val = signal(SIGUSR1,switch_to_offline);
210 if ( ret_val == SIG_ERR )
212 updater->get_logger()->print_error_setting_signal("SIGUSR1");
215 ret_val = signal(SIGHUP,reload_config);
216 if ( ret_val == SIG_ERR )
218 updater->get_logger()->print_error_setting_signal("SIGHUP");
227 * Try to run in daemon mode if enabled in config.
228 * @param daemon_mode True if process should detach to init (run as daemon), false if not detach to init.
229 * @return 0 if all is fine, -1 on error.
231 int init_daemon_mode(bool daemon_mode)
233 updater->get_logger()->print_daemon_mode(daemon_mode);
234 if ( daemon_mode == true )
240 updater->get_logger()->print_error_fork();
245 // parent continues here
246 if ( write_pidfile(pid) != 0 )
248 if ( kill(pid,SIGTERM) != 0 )
250 updater->get_logger()->print_error_kill_child(pid);
251 shutdown_parent(false,-1); // keep pidfile
255 updater->get_logger()->print_child_killed(pid);
256 shutdown_parent(true,-1); // remove pidfile
259 updater->get_logger()->print_runnig_as_daemon(pid);
260 shutdown_parent(false,0); // keep pidfile
266 if ( write_pidfile(getpid()) != 0 )
276 * @brief The main part.
277 * @param argc Number of arguments
278 * @param argv Command line arguments
279 * @return 0 if all is fine.
281 int main(int argc, char *argv[])
283 // initialize Updater
284 Updater::Ptr updater(new Updater);
286 // load config and initialize helper classes
287 if ( updater->load_config(argc,argv) != 0 )
290 // open pidfile and check for running process
291 if ( check_for_running_process() != 0)
294 // init signal handling
295 if ( init_signals() != 0)
298 // init daemon_mode if enabled
299 if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
302 // service processing starts here
305 if ( is_online == true )
307 // update all configured services
308 updater->update_services();
312 // We are in offline mode, do nothing, expect printing "offline mode".
313 updater->get_logger()->print_offline_mode();
316 // Snore, snore... don't hug the cpu if we are in daemon_mode.
317 if ( updater->get_config()->get_daemon_mode() == 1 )
320 }while ( updater->get_config()->get_daemon_mode() == 1 );
322 // Serialize services to save their actual state.
323 if ( shutdown() != 0 )