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>
41 typedef boost::shared_ptr<Updater> UpdaterPtr;
47 * Checks if a bpdyndnsd process is already running.
48 * @param updater Shared Pointer to updater, needed for logging.
49 * @return 0 if process not running already or pid of already running process.
51 int check_for_running_process()
53 ifstream pidfile(PIDFILE);
54 if ( pidfile.is_open() )
59 updater->get_logger()->print_pid_found(pid);
63 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
64 if ( kill(pid,0) == 0)
66 updater->get_logger()->print_process_already_running(pid);
77 * Writes the pid into the pidfile.
78 * @param pid The process's pid.
80 void write_pidfile(int pid)
82 ofstream pidfile(PIDFILE);
83 if ( pidfile.is_open() )
85 pidfile << pid << endl;
92 * Signal SIGTERM caught, releasing resources and exit.
93 * @param param Parameter from the signal interface.
95 void terminate(int param)
97 updater->get_logger()->print_caught_sigterm();
104 * Signal SIGUSR1 caught, switching to offline mode.
105 * @param param Parameter from the signal interface.
107 void switch_to_offline(int param)
109 updater->get_logger()->print_caught_siguser1();
115 * Signal SIGHUP caught, reloading config and switching to online mode.
116 * @param param Parameter from the signal interface.
118 void reload_config(int param)
120 updater->get_logger()->print_caught_sighup();
121 updater->reload_config();
127 * Initialize the signals we handle.
131 sighandler_t ret_val;
132 ret_val = signal(SIGTERM,terminate);
133 if ( ret_val == SIG_ERR )
135 ret_val = signal(SIGUSR1,switch_to_offline);
136 if ( ret_val == SIG_ERR )
138 ret_val = signal(SIGHUP,reload_config);
139 if ( ret_val == SIG_ERR )
147 * @brief The main part.
148 * @param argc Number of arguments
149 * @param argv Command line arguments
150 * @return 0 if all is fine.
152 int main(int argc, char *argv[])
154 // initialize Updater
155 UpdaterPtr _updater(new Updater);
159 // load the cmd options
160 if ( updater->init_config_from_cmd(argc,argv) != 0 )
163 // load the config and service files
164 if ( updater->init_config_from_files() != 0 )
167 // open pidfile and check for running process
168 if ( check_for_running_process() != 0)
171 // init signal handling
172 if ( init_signals() != 0)
174 updater->get_logger()->print_error_setting_signal();
178 // initialize daemon mode if configured
179 updater->get_logger()->print_daemon_mode(updater->get_config()->get_daemon_mode());
180 if ( updater->get_config()->get_daemon_mode() == 1 )
186 updater->get_logger()->print_error_fork();
193 updater->get_logger()->print_runnig_as_daemon(pid);
200 // service processing starts here
203 if ( online_mode == 1 )
205 // update all configured services
206 updater->update_services();
210 updater->get_logger()->print_offline_mode();
213 if ( updater->get_config()->get_daemon_mode() == 1 )
214 sleep(10); // TODO: in a final release, correct the sleep value to something suitable
216 }while ( updater->get_config()->get_daemon_mode() == 1 );