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"
41 #include "service_dyndns.cpp"
42 #include "service_dyns.cpp"
43 #include "service_ods.cpp"
44 #include "service_dhs.cpp"
45 #include "service_easydns.cpp"
46 #include "service_tzo.cpp"
47 #include "service_zoneedit.cpp"
48 #include "service_gnudip.cpp"
53 bool is_online = true;
56 * Checks if a bpdyndnsd process is already running.
57 * @param updater Shared Pointer to updater, needed for logging.
58 * @return 0 if process not running already or pid of already running process.
60 int check_for_running_process()
62 ifstream pidfile(PIDFILE);
63 if ( pidfile.is_open() )
68 updater->get_logger()->print_pid_found(pid);
72 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
73 if ( kill(pid,0) == 0)
75 updater->get_logger()->print_process_already_running(pid);
86 * Writes the pid into the pidfile.
87 * @param pid The process's pid.
89 int write_pidfile(int pid)
91 ofstream pidfile(PIDFILE);
92 if ( pidfile.is_open() )
94 pidfile << pid << endl;
98 updater->get_logger()->print_error_opening_rw(PIDFILE);
107 * Parent shutdown function
108 * @return 0 if all is fine, -1 otherwise
110 int shutdown_parent(bool remove_pid, int ret_val)
112 // starting shutdown_parent
113 updater->get_logger()->print_starting_shutdown_parent();
115 // remove pidfile if requested
119 // shutdown parent complete
120 updater->get_logger()->print_shutdown_parent_succeeded();
122 // release shared pointer
131 * @return 0 if all is fine, -1 otherwise
138 updater->get_logger()->print_starting_shutdown();
140 // serialize actual service objects
141 if ( updater->get_service_holder()->serialize_services() != 0 )
148 updater->get_logger()->print_shutdown_succeeded();
150 // release shared pointer
158 * Signal SIGTERM caught, releasing resources and exit.
159 * @param param Parameter from the signal interface.
161 void terminate(int param)
163 updater->get_logger()->print_caught_sigterm();
170 * Signal SIGUSR1 caught, switching to offline mode.
171 * @param param Parameter from the signal interface.
173 void switch_to_offline(int param)
175 updater->get_logger()->print_caught_siguser1();
181 * Signal SIGHUP caught, reloading config and switching to online mode.
182 * @param param Parameter from the signal interface.
184 void reload_config(int param)
186 updater->get_logger()->print_caught_sighup();
187 if ( updater->reload_config() != 0 )
194 * Initialize the signals we handle.
195 * @return 0 if all is fine, -1 on error.
199 sighandler_t ret_val;
200 ret_val = signal(SIGTERM,terminate);
201 if ( ret_val == SIG_ERR )
203 updater->get_logger()->print_error_setting_signal("SIGTERM");
206 ret_val = signal(SIGUSR1,switch_to_offline);
207 if ( ret_val == SIG_ERR )
209 updater->get_logger()->print_error_setting_signal("SIGUSR1");
212 ret_val = signal(SIGHUP,reload_config);
213 if ( ret_val == SIG_ERR )
215 updater->get_logger()->print_error_setting_signal("SIGHUP");
224 * Try to run in daemon mode if enabled in config.
225 * @param daemon_mode True if process should detach to init (run as daemon), false if not detach to init.
226 * @return 0 if all is fine, -1 on error.
228 int init_daemon_mode(bool daemon_mode)
230 updater->get_logger()->print_daemon_mode(daemon_mode);
231 if ( daemon_mode == true )
237 updater->get_logger()->print_error_fork();
242 // parent continues here
243 if ( write_pidfile(pid) != 0 )
245 if ( kill(pid,SIGTERM) != 0 )
247 updater->get_logger()->print_error_kill_child(pid);
248 shutdown_parent(false,-1); // keep pidfile
252 updater->get_logger()->print_child_killed(pid);
253 shutdown_parent(true,-1); // remove pidfile
256 updater->get_logger()->print_runnig_as_daemon(pid);
257 shutdown_parent(false,0); // keep pidfile
263 if ( write_pidfile(getpid()) != 0 )
273 * @brief The main part.
274 * @param argc Number of arguments
275 * @param argv Command line arguments
276 * @return 0 if all is fine.
278 int main(int argc, char *argv[])
280 // initialize Updater
281 Updater::Ptr _updater(new Updater);
285 // load config and initialize helper classes
286 if ( updater->load_config(argc,argv) != 0 )
289 // open pidfile and check for running process
290 if ( check_for_running_process() != 0)
293 // init signal handling
294 if ( init_signals() != 0)
297 // init daemon_mode if enabled
298 if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
301 // service processing starts here
304 if ( is_online == true )
306 // update all configured services
307 updater->update_services();
311 // We are in offline mode, do nothing, expect printing "offline mode".
312 updater->get_logger()->print_offline_mode();
315 // Snore, snore... don't hug the cpu if we are in daemon_mode.
316 if ( updater->get_config()->get_daemon_mode() == 1 )
319 }while ( updater->get_config()->get_daemon_mode() == 1 );
321 // Serialize services to save their actual state.
322 if ( shutdown() != 0 )