2 * @brief The main function.
6 * @copyright Intra2net AG
18 #define PIDFILE "/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 "serializeservicecontainer.cpp"
45 bool is_online = true;
48 * Checks if a bpdyndnsd process is already running.
49 * @param updater Shared Pointer to updater, needed for logging.
50 * @return 0 if process not running already or pid of already running process.
52 int check_for_running_process()
54 ifstream pidfile(PIDFILE);
55 if ( pidfile.is_open() )
60 updater->get_logger()->print_pid_found(pid);
64 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
65 if ( kill(pid,0) == 0)
67 updater->get_logger()->print_process_already_running(pid);
78 * Writes the pid into the pidfile.
79 * @param pid The process's pid.
81 int write_pidfile(int pid)
83 ofstream pidfile(PIDFILE);
84 if ( pidfile.is_open() )
86 pidfile << pid << endl;
90 updater->get_logger()->print_error_opening_rw(PIDFILE);
99 * Signal SIGTERM caught, releasing resources and exit.
100 * @param param Parameter from the signal interface.
102 void terminate(int param)
104 updater->get_logger()->print_caught_sigterm();
106 // unfortunately we can't call serialize_services in any destructor
107 // cause some singleton are already destroyed !?!
108 if ( updater->get_service_holder()->serialize_services() != 0 )
120 * Signal SIGUSR1 caught, switching to offline mode.
121 * @param param Parameter from the signal interface.
123 void switch_to_offline(int param)
125 updater->get_logger()->print_caught_siguser1();
131 * Signal SIGHUP caught, reloading config and switching to online mode.
132 * @param param Parameter from the signal interface.
134 void reload_config(int param)
136 updater->get_logger()->print_caught_sighup();
137 if ( updater->reload_config() != 0 )
144 * Initialize the signals we handle.
145 * @return 0 if all is fine, -1 on error.
149 sighandler_t ret_val;
150 ret_val = signal(SIGTERM,terminate);
151 if ( ret_val == SIG_ERR )
153 updater->get_logger()->print_error_setting_signal("SIGTERM");
156 ret_val = signal(SIGUSR1,switch_to_offline);
157 if ( ret_val == SIG_ERR )
159 updater->get_logger()->print_error_setting_signal("SIGUSR1");
162 ret_val = signal(SIGHUP,reload_config);
163 if ( ret_val == SIG_ERR )
165 updater->get_logger()->print_error_setting_signal("SIGHUP");
174 * Try to run in daemon mode if enabled in config.
175 * @param daemon_mode True if process should detach to init (run as daemon), false if not detach to init.
176 * @return 0 if all is fine, -1 on error.
178 int init_daemon_mode(bool daemon_mode)
180 updater->get_logger()->print_daemon_mode(daemon_mode);
181 if ( daemon_mode == true )
187 updater->get_logger()->print_error_fork();
192 // parent continues here
193 if ( write_pidfile(pid) != 0 )
195 if ( kill(pid,SIGTERM) != 0 )
196 updater->get_logger()->print_error_kill_child(pid);
198 updater->get_logger()->print_child_killed(pid);
201 updater->get_logger()->print_runnig_as_daemon(pid);
210 * @brief The main part.
211 * @param argc Number of arguments
212 * @param argv Command line arguments
213 * @return 0 if all is fine.
215 int main(int argc, char *argv[])
217 // initialize Updater
218 Updater::Ptr _updater(new Updater);
222 // load the cmd options
223 if ( updater->init_config_from_cmd(argc,argv) != 0 )
226 // load the config and service files
227 if ( updater->init_config_from_files() != 0 )
230 // open pidfile and check for running process
231 if ( check_for_running_process() != 0)
234 // init signal handling
235 if ( init_signals() != 0)
238 // init the ip_helper
239 if ( updater->init_ip_helper() != 0 )
242 // init daemon_mode if enabled
243 if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
246 // service processing starts here
249 if ( is_online == true )
251 // update all configured services
252 updater->update_services();
256 // We are in offline mode, do nothing, expect printing "offline mode".
257 updater->get_logger()->print_offline_mode();
260 // Snore, snore... don't hug the cpu if we are in daemon_mode.
261 if ( updater->get_config()->get_daemon_mode() == 1 )
264 }while ( updater->get_config()->get_daemon_mode() == 1 );
267 // Serialize services to save their actual state.
268 if ( updater->get_service_holder()->serialize_services() != 0 )