Fix test for valid pointer after conversion to boost::smart_ptr (The class defines...
[bpdyndnsd] / src / main.cpp
CommitLineData
b1be615b
BS
1/** @file
2 * @brief The main function.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
4545a371
BS
9
10#ifdef HAVE_CONFIG_H
11#include <config.h>
12#endif
13
14#define VERSION 0
15#define REVISION 1
16#define RELEASE 0
17
388f4ab0
BS
18#define PIDFILE "/var/run/bpdyndnsd.pid"
19
4545a371
BS
20#include <iostream>
21#include <list>
22#include <string>
23
24#include <boost/foreach.hpp>
25
26#include "updater.cpp"
27#include "config.cpp"
254bbf53 28#include "logger.cpp"
4545a371
BS
29
30#include "service.cpp"
31
32#include "dhs.cpp"
33#include "ods.cpp"
34
388f4ab0
BS
35#include <sys/types.h>
36#include <signal.h>
37
38
4545a371
BS
39using namespace std;
40
85a0abf9
BS
41typedef boost::shared_ptr<Updater> UpdaterPtr;
42
c5675c01
BS
43UpdaterPtr updater;
44bool online_mode = 1;
45
4545a371 46/**
388f4ab0
BS
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.
50 */
c5675c01 51int check_for_running_process()
388f4ab0
BS
52{
53 ifstream pidfile(PIDFILE);
54 if ( pidfile.is_open() )
55 {
56 int pid;
57 pidfile >> pid;
58 if ( pid > 0 )
59 updater->get_logger()->print_pid_found(pid);
60 else
61 return 0;
62
63 // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
64 if ( kill(pid,0) == 0)
65 {
66 updater->get_logger()->print_process_already_running(pid);
67 pidfile.close();
68 return pid;
69 }
70 }
71 pidfile.close();
72 return 0;
73}
74
c5675c01
BS
75
76/**
77 * Writes the pid into the pidfile.
78 * @param pid The process's pid.
79 */
388f4ab0
BS
80void write_pidfile(int pid)
81{
82 ofstream pidfile(PIDFILE);
83 if ( pidfile.is_open() )
84 {
85 pidfile << pid << endl;
86 }
87 pidfile.close();
88}
89
c5675c01
BS
90
91/**
92 * Signal SIGTERM caught, releasing resources and exit.
93 * @param param Parameter from the signal interface.
94 */
95void terminate(int param)
96{
97 updater->get_logger()->print_caught_sigterm();
98 updater.reset();
99 exit(1);
100}
101
102
103/**
104 * Signal SIGUSR1 caught, switching to offline mode.
105 * @param param Parameter from the signal interface.
106 */
107void switch_to_offline(int param)
108{
109 online_mode = 0;
110}
111
112
113/**
114 * Signal SIGHUP caught, reloading config and switching to online mode.
115 * @param param
116 */
117void reload_config(int param)
118{
119 updater->reload_config();
120 online_mode = 1;
121}
122
123
124/**
125 * Initialize the signals we handle.
126 */
127void init_signals()
128{
129 signal(SIGTERM,terminate);
130 signal(SIGUSR1,switch_to_offline);
131 signal(SIGHUP,reload_config);
132}
133
134
388f4ab0 135/**
b1be615b
BS
136 * @brief The main part.
137 * @param argc Number of arguments
138 * @param argv Command line arguments
139 * @return 0 if all is fine.
4545a371
BS
140 */
141int main(int argc, char *argv[])
142{
38060291 143 // initialize Updater
c5675c01
BS
144 UpdaterPtr _updater(new Updater);
145 updater = _updater;
146 _updater.reset();
38060291 147
254bbf53 148 // load the cmd options
38060291 149 if ( updater->init_config_from_cmd(argc,argv) != 0 )
1cf4e2b5 150 return 0;
38060291 151
254bbf53 152 // load the config and service files
38060291 153 if ( updater->init_config_from_files() != 0 )
98bd3874 154 return 0;
4545a371 155
388f4ab0 156 // open pidfile and check for running process
c5675c01 157 if ( check_for_running_process() != 0)
388f4ab0 158 return 0;
3434b35f 159
c5675c01
BS
160 // init signal handling
161 init_signals();
162
254bbf53
BS
163 // set the configured loggin facility, default stdout
164
165 // initialize daemon mode if configured
388f4ab0
BS
166 bool daemon_mode = updater->get_config()->get_daemon_mode();
167 updater->get_logger()->print_daemon_mode(daemon_mode);
168
169 // fork if daemon mode is enabled
170 if ( daemon_mode == 1 )
171 {
172 int pid = fork();
173 if ( pid < 0 )
174 {
175 // error fork
176 updater->get_logger()->print_error_fork();
177 return 0;
178 }
179 else if ( pid > 0 )
180 {
181 // parent
182 write_pidfile(pid);
183 updater->get_logger()->print_runnig_as_daemon(pid);
184 return 0;
185 }
186 // child starts here
187 }
188
189 // service processing starts here
190 do
191 {
c5675c01
BS
192 if ( online_mode == 1 )
193 {
194 // update all configured services
195 updater->update_services();
196 }
197 sleep(2);
388f4ab0
BS
198
199 }while ( daemon_mode == 1 );
4545a371 200
4545a371
BS
201 return 0;
202}