Implemented daemon ability.
[bpdyndnsd] / src / main.cpp
1 /** @file
2  * @brief The main function.
3  *
4  *
5  *
6  * @copyright Intra2net AG
7  * @license GPLv2
8 */
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
18 #define PIDFILE "/var/run/bpdyndnsd.pid"
19
20 #include <iostream>
21 #include <list>
22 #include <string>
23
24 #include <boost/foreach.hpp>
25
26 #include "updater.cpp"
27 #include "config.cpp"
28 #include "logger.cpp"
29
30 #include "service.cpp"
31
32 #include "dhs.cpp"
33 #include "ods.cpp"
34
35 #include <sys/types.h>
36 #include <signal.h>
37
38
39 using namespace std;
40
41 typedef boost::shared_ptr<Updater> UpdaterPtr;
42
43 /**
44  * Checks if a bpdyndnsd process is already running.
45  * @param updater Shared Pointer to updater, needed for logging.
46  * @return 0 if process not running already or pid of already running process.
47  */
48 int check_for_running_process(UpdaterPtr updater)
49 {
50     ifstream pidfile(PIDFILE);
51     if ( pidfile.is_open() )
52     {
53         int pid;
54         pidfile >> pid;
55         if ( pid > 0 )
56             updater->get_logger()->print_pid_found(pid);
57         else
58             return 0;
59
60         // check if process still running ret_val==-1 -> not runnig, ret_val==0 -> running
61         if ( kill(pid,0) == 0)
62         {
63             updater->get_logger()->print_process_already_running(pid);
64             pidfile.close();
65             return pid;
66         }
67     }
68     pidfile.close();
69     return 0;
70 }
71
72 void write_pidfile(int pid)
73 {
74     ofstream pidfile(PIDFILE);
75     if ( pidfile.is_open() )
76     {
77         pidfile << pid << endl;
78     }
79     pidfile.close();
80 }
81
82 /**
83  * @brief The main part.
84  * @param argc Number of arguments
85  * @param argv Command line arguments
86  * @return 0 if all is fine.
87  */
88 int main(int argc, char *argv[])
89 {
90     // initialize Updater
91     UpdaterPtr updater(new Updater);
92
93     // load the cmd options
94     if ( updater->init_config_from_cmd(argc,argv) != 0 )
95         return 0;
96
97     // load the config and service files
98     if ( updater->init_config_from_files() != 0 )
99         return 0;
100
101     // open pidfile and check for running process
102     if ( check_for_running_process(updater) != 0)
103         return 0;
104
105     // set the configured loggin facility, default stdout
106
107     // initialize daemon mode if configured
108     bool daemon_mode = updater->get_config()->get_daemon_mode();
109     updater->get_logger()->print_daemon_mode(daemon_mode);
110
111     // fork if daemon mode is enabled
112     if ( daemon_mode == 1 )
113     {
114         int pid = fork();
115         if ( pid < 0 )
116         {
117             // error fork
118             updater->get_logger()->print_error_fork();
119             return 0;
120         }
121         else if ( pid > 0 )
122         {
123             // parent
124             write_pidfile(pid);
125             updater->get_logger()->print_runnig_as_daemon(pid);
126             return 0;
127         }
128         // child starts here
129     }
130
131     // service processing starts here
132     do
133     {
134         // update all configured services
135         updater->update_services();
136         sleep(3);
137
138     }while ( daemon_mode == 1 );
139
140     return 0;
141 }