2 * @brief The main function.
6 * @copyright Intra2net AG
18 #define PIDFILE "/var/run/bpdyndnsd.pid"
24 #include <boost/foreach.hpp>
26 #include "updater.cpp"
30 #include "service.cpp"
35 #include <sys/types.h>
40 typedef boost::shared_ptr<Updater> UpdaterPtr;
46 * Checks if a bpdyndnsd process is already running.
47 * @param updater Shared Pointer to updater, needed for logging.
48 * @return 0 if process not running already or pid of already running process.
50 int check_for_running_process()
52 ifstream pidfile(PIDFILE);
53 if ( pidfile.is_open() )
58 updater->get_logger()->print_pid_found(pid);
62 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
63 if ( kill(pid,0) == 0)
65 updater->get_logger()->print_process_already_running(pid);
76 * Writes the pid into the pidfile.
77 * @param pid The process's pid.
79 void write_pidfile(int pid)
81 ofstream pidfile(PIDFILE);
82 if ( pidfile.is_open() )
84 pidfile << pid << endl;
91 * Signal SIGTERM caught, releasing resources and exit.
92 * @param param Parameter from the signal interface.
94 void terminate(int param)
96 updater->get_logger()->print_caught_sigterm();
98 // unfortunately we can't call serialize_services in any destructor
99 // cause some singleton are already destroyed !?!
100 updater->get_config()->serialize_services();
108 * Signal SIGUSR1 caught, switching to offline mode.
109 * @param param Parameter from the signal interface.
111 void switch_to_offline(int param)
113 updater->get_logger()->print_caught_siguser1();
119 * Signal SIGHUP caught, reloading config and switching to online mode.
120 * @param param Parameter from the signal interface.
122 void reload_config(int param)
124 updater->get_logger()->print_caught_sighup();
125 updater->reload_config();
131 * Initialize the signals we handle.
135 sighandler_t ret_val;
136 ret_val = signal(SIGTERM,terminate);
137 if ( ret_val == SIG_ERR )
139 ret_val = signal(SIGUSR1,switch_to_offline);
140 if ( ret_val == SIG_ERR )
142 ret_val = signal(SIGHUP,reload_config);
143 if ( ret_val == SIG_ERR )
151 * @brief The main part.
152 * @param argc Number of arguments
153 * @param argv Command line arguments
154 * @return 0 if all is fine.
156 int main(int argc, char *argv[])
158 // initialize Updater
159 UpdaterPtr _updater(new Updater);
163 // load the cmd options
164 if ( updater->init_config_from_cmd(argc,argv) != 0 )
167 // load the config and service files
168 if ( updater->init_config_from_files() != 0 )
171 // open pidfile and check for running process
172 if ( check_for_running_process() != 0)
175 // init signal handling
176 if ( init_signals() != 0)
178 updater->get_logger()->print_error_setting_signal();
182 // initialize daemon mode if configured
183 updater->get_logger()->print_daemon_mode(updater->get_config()->get_daemon_mode());
184 if ( updater->get_config()->get_daemon_mode() == 1 )
190 updater->get_logger()->print_error_fork();
197 updater->get_logger()->print_runnig_as_daemon(pid);
203 // service processing starts here
206 if ( online_mode == 1 )
208 // update all configured services
209 updater->update_services();
213 updater->get_logger()->print_offline_mode();
216 if ( updater->get_config()->get_daemon_mode() == 1 )
217 sleep(10); // TODO: in a final release, correct the sleep value to something suitable
219 }while ( updater->get_config()->get_daemon_mode() == 1 );
222 // serialize services
223 updater->get_config()->serialize_services();