Commit | Line | Data |
---|---|---|
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 |
39 | using namespace std; |
40 | ||
85a0abf9 BS |
41 | typedef boost::shared_ptr<Updater> UpdaterPtr; |
42 | ||
4545a371 | 43 | /** |
388f4ab0 BS |
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 | /** | |
b1be615b BS |
83 | * @brief The main part. |
84 | * @param argc Number of arguments | |
85 | * @param argv Command line arguments | |
86 | * @return 0 if all is fine. | |
4545a371 BS |
87 | */ |
88 | int main(int argc, char *argv[]) | |
89 | { | |
38060291 | 90 | // initialize Updater |
254bbf53 | 91 | UpdaterPtr updater(new Updater); |
38060291 | 92 | |
254bbf53 | 93 | // load the cmd options |
38060291 | 94 | if ( updater->init_config_from_cmd(argc,argv) != 0 ) |
1cf4e2b5 | 95 | return 0; |
38060291 | 96 | |
254bbf53 | 97 | // load the config and service files |
38060291 | 98 | if ( updater->init_config_from_files() != 0 ) |
98bd3874 | 99 | return 0; |
4545a371 | 100 | |
388f4ab0 BS |
101 | // open pidfile and check for running process |
102 | if ( check_for_running_process(updater) != 0) | |
103 | return 0; | |
3434b35f | 104 | |
254bbf53 BS |
105 | // set the configured loggin facility, default stdout |
106 | ||
107 | // initialize daemon mode if configured | |
388f4ab0 BS |
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 ); | |
4545a371 | 139 | |
4545a371 BS |
140 | return 0; |
141 | } |