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"
48 bool is_online = true;
51 * Checks if a bpdyndnsd process is already running.
52 * @param updater Shared Pointer to updater, needed for logging.
53 * @return 0 if process not running already or pid of already running process.
55 int check_for_running_process()
57 ifstream pidfile(PIDFILE);
58 if ( pidfile.is_open() )
63 updater->get_logger()->print_pid_found(pid);
67 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
68 if ( kill(pid,0) == 0)
70 updater->get_logger()->print_process_already_running(pid);
81 * Writes the pid into the pidfile.
82 * @param pid The process's pid.
84 int write_pidfile(int pid)
86 ofstream pidfile(PIDFILE);
87 if ( pidfile.is_open() )
89 pidfile << pid << endl;
93 updater->get_logger()->print_error_opening_rw(PIDFILE);
102 * Parent shutdown function
103 * @return 0 if all is fine, -1 otherwise
105 int shutdown_parent(bool remove_pid, int ret_val)
107 // starting shutdown_parent
108 updater->get_logger()->print_starting_shutdown_parent();
110 // remove pidfile if requested
114 // shutdown parent complete
115 updater->get_logger()->print_shutdown_parent_succeeded();
117 // release shared pointer
126 * @return 0 if all is fine, -1 otherwise
133 updater->get_logger()->print_starting_shutdown();
135 // serialize actual service objects
136 if ( updater->get_service_holder()->serialize_services() != 0 )
143 updater->get_logger()->print_shutdown_succeeded();
145 // release shared pointer
153 * Signal SIGTERM caught, releasing resources and exit.
154 * @param param Parameter from the signal interface.
156 void terminate(int param)
158 updater->get_logger()->print_caught_sigterm();
165 * Signal SIGUSR1 caught, switching to offline mode.
166 * @param param Parameter from the signal interface.
168 void switch_to_offline(int param)
170 updater->get_logger()->print_caught_siguser1();
176 * Signal SIGHUP caught, reloading config and switching to online mode.
177 * @param param Parameter from the signal interface.
179 void reload_config(int param)
181 updater->get_logger()->print_caught_sighup();
182 if ( updater->reload_config() != 0 )
189 * Initialize the signals we handle.
190 * @return 0 if all is fine, -1 on error.
194 sighandler_t ret_val;
195 ret_val = signal(SIGTERM,terminate);
196 if ( ret_val == SIG_ERR )
198 updater->get_logger()->print_error_setting_signal("SIGTERM");
201 ret_val = signal(SIGUSR1,switch_to_offline);
202 if ( ret_val == SIG_ERR )
204 updater->get_logger()->print_error_setting_signal("SIGUSR1");
207 ret_val = signal(SIGHUP,reload_config);
208 if ( ret_val == SIG_ERR )
210 updater->get_logger()->print_error_setting_signal("SIGHUP");
219 * Try to run in daemon mode if enabled in config.
220 * @param daemon_mode True if process should detach to init (run as daemon), false if not detach to init.
221 * @return 0 if all is fine, -1 on error.
223 int init_daemon_mode(bool daemon_mode)
225 updater->get_logger()->print_daemon_mode(daemon_mode);
226 if ( daemon_mode == true )
232 updater->get_logger()->print_error_fork();
237 // parent continues here
238 if ( write_pidfile(pid) != 0 )
240 if ( kill(pid,SIGTERM) != 0 )
242 updater->get_logger()->print_error_kill_child(pid);
243 shutdown_parent(false,-1); // keep pidfile
247 updater->get_logger()->print_child_killed(pid);
248 shutdown_parent(true,-1); // remove pidfile
251 updater->get_logger()->print_runnig_as_daemon(pid);
252 shutdown_parent(false,0); // keep pidfile
258 if ( write_pidfile(getpid()) != 0 )
268 * @brief The main part.
269 * @param argc Number of arguments
270 * @param argv Command line arguments
271 * @return 0 if all is fine.
273 int main(int argc, char *argv[])
275 // initialize Updater
276 Updater::Ptr _updater(new Updater);
280 // load config and initialize helper classes
281 if ( updater->load_config(argc,argv) != 0 )
284 // open pidfile and check for running process
285 if ( check_for_running_process() != 0)
288 // init signal handling
289 if ( init_signals() != 0)
292 // init daemon_mode if enabled
293 if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
296 // service processing starts here
299 if ( is_online == true )
301 // update all configured services
302 updater->update_services();
306 // We are in offline mode, do nothing, expect printing "offline mode".
307 updater->get_logger()->print_offline_mode();
310 // Snore, snore... don't hug the cpu if we are in daemon_mode.
311 if ( updater->get_config()->get_daemon_mode() == 1 )
314 }while ( updater->get_config()->get_daemon_mode() == 1 );
316 // Serialize services to save their actual state.
317 if ( shutdown() != 0 )