author | Michael Krelin <hacker@klever.net> | 2006-11-08 14:18:04 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2006-11-08 14:18:04 (UTC) |
commit | cc0b2a8b1cea327bc0616e0b67dcda0b1fbe83e4 (patch) (unidiff) | |
tree | 3d77e76a304eaf1c5b4b54d979d9a41ebd184ed3 /src | |
parent | 4148b7ad68f2b5bf83d637bdc5ebbb9f0d2aa869 (diff) | |
download | dudki-cc0b2a8b1cea327bc0616e0b67dcda0b1fbe83e4.zip dudki-cc0b2a8b1cea327bc0616e0b67dcda0b1fbe83e4.tar.gz dudki-cc0b2a8b1cea327bc0616e0b67dcda0b1fbe83e4.tar.bz2 |
bumped year
-rw-r--r-- | src/dudki.cc | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/src/dudki.cc b/src/dudki.cc index 3af5372..91a3342 100644 --- a/src/dudki.cc +++ b/src/dudki.cc | |||
@@ -1,214 +1,214 @@ | |||
1 | #include <unistd.h> | 1 | #include <unistd.h> |
2 | #include <signal.h> | 2 | #include <signal.h> |
3 | #include <syslog.h> | 3 | #include <syslog.h> |
4 | #include <errno.h> | 4 | #include <errno.h> |
5 | #include <iostream> | 5 | #include <iostream> |
6 | #include <fstream> | 6 | #include <fstream> |
7 | #include <stdexcept> | 7 | #include <stdexcept> |
8 | using namespace std; | 8 | using namespace std; |
9 | #include "configuration.h" | 9 | #include "configuration.h" |
10 | #include "util.h" | 10 | #include "util.h" |
11 | 11 | ||
12 | #include "config.h" | 12 | #include "config.h" |
13 | #ifdef HAVE_GETOPT_H | 13 | #ifdef HAVE_GETOPT_H |
14 | # include <getopt.h> | 14 | # include <getopt.h> |
15 | #endif | 15 | #endif |
16 | 16 | ||
17 | #ifndef DEFAULT_CONF_FILE | 17 | #ifndef DEFAULT_CONF_FILE |
18 | # define DEFAULT_CONF_FILE "/etc/dudki.conf" | 18 | # define DEFAULT_CONF_FILE "/etc/dudki.conf" |
19 | #endif | 19 | #endif |
20 | 20 | ||
21 | #define PHEADER PACKAGE " Version " VERSION | 21 | #define PHEADER PACKAGE " Version " VERSION |
22 | #define PCOPY "Copyright (c) 2004 Klever Group" | 22 | #define PCOPY "Copyright (c) 2004-2006 Klever Group" |
23 | 23 | ||
24 | bool finishing = false; | 24 | bool finishing = false; |
25 | bool restarting = false; | 25 | bool restarting = false; |
26 | static char **_argv = NULL; | 26 | static char **_argv = NULL; |
27 | 27 | ||
28 | static void lethal_signal_handler(int signum) { | 28 | static void lethal_signal_handler(int signum) { |
29 | syslog(LOG_NOTICE,"Lethal signal received. Terminating."); | 29 | syslog(LOG_NOTICE,"Lethal signal received. Terminating."); |
30 | finishing = true; | 30 | finishing = true; |
31 | } | 31 | } |
32 | static void sighup_handler(int signum) { | 32 | static void sighup_handler(int signum) { |
33 | syslog(LOG_NOTICE,"SUGHUP received, reloading."); | 33 | syslog(LOG_NOTICE,"SUGHUP received, reloading."); |
34 | restarting = finishing = true; | 34 | restarting = finishing = true; |
35 | } | 35 | } |
36 | 36 | ||
37 | void check_herd(configuration& config) { | 37 | void check_herd(configuration& config) { |
38 | process::prepare_herd(); | 38 | process::prepare_herd(); |
39 | for(processes_t::iterator i=config.processes.begin();i!=config.processes.end();++i) | 39 | for(processes_t::iterator i=config.processes.begin();i!=config.processes.end();++i) |
40 | i->second.check(i->first,config); | 40 | i->second.check(i->first,config); |
41 | process::unprepare_herd(); | 41 | process::unprepare_herd(); |
42 | } | 42 | } |
43 | 43 | ||
44 | void signal_self(const configuration& config,int signum) { | 44 | void signal_self(const configuration& config,int signum) { |
45 | ifstream pids(config.pidfile.c_str(),ios::in); | 45 | ifstream pids(config.pidfile.c_str(),ios::in); |
46 | if(!pids) | 46 | if(!pids) |
47 | throw runtime_error("Can't detect running instance"); | 47 | throw runtime_error("Can't detect running instance"); |
48 | pid_t pid = 0; | 48 | pid_t pid = 0; |
49 | pids >> pid; | 49 | pids >> pid; |
50 | if(!pid) | 50 | if(!pid) |
51 | throw runtime_error("Can't detect running instance"); | 51 | throw runtime_error("Can't detect running instance"); |
52 | if(pid==getpid()) | 52 | if(pid==getpid()) |
53 | throw 0; | 53 | throw 0; |
54 | if(kill(pid,signum)) | 54 | if(kill(pid,signum)) |
55 | throw runtime_error("Failed to signal running instance"); | 55 | throw runtime_error("Failed to signal running instance"); |
56 | } | 56 | } |
57 | 57 | ||
58 | int main(int argc,char **argv) { | 58 | int main(int argc,char **argv) { |
59 | try { | 59 | try { |
60 | _argv = new char*[argc+1]; | 60 | _argv = new char*[argc+1]; |
61 | if(!_argv) | 61 | if(!_argv) |
62 | throw runtime_error("memory allocation problem at the very start"); | 62 | throw runtime_error("memory allocation problem at the very start"); |
63 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); | 63 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); |
64 | string config_file = DEFAULT_CONF_FILE; | 64 | string config_file = DEFAULT_CONF_FILE; |
65 | enum { | 65 | enum { |
66 | op_default, | 66 | op_default, |
67 | op_work, | 67 | op_work, |
68 | op_signal, | 68 | op_signal, |
69 | op_ensure, | 69 | op_ensure, |
70 | op_test | 70 | op_test |
71 | } op = op_default; | 71 | } op = op_default; |
72 | int op_signum = 0; | 72 | int op_signum = 0; |
73 | while(true) { | 73 | while(true) { |
74 | #defineSHORTOPTSTRING "f:hVLrkcets:" | 74 | #defineSHORTOPTSTRING "f:hVLrkcets:" |
75 | #ifdef HAVE_GETOPT_LONG | 75 | #ifdef HAVE_GETOPT_LONG |
76 | static struct option opts[] = { | 76 | static struct option opts[] = { |
77 | { "help", no_argument, 0, 'h' }, | 77 | { "help", no_argument, 0, 'h' }, |
78 | { "usage", no_argument, 0, 'h' }, | 78 | { "usage", no_argument, 0, 'h' }, |
79 | { "version", no_argument, 0, 'V' }, | 79 | { "version", no_argument, 0, 'V' }, |
80 | { "license", no_argument, 0, 'L' }, | 80 | { "license", no_argument, 0, 'L' }, |
81 | { "config", required_argument, 0, 'f' }, | 81 | { "config", required_argument, 0, 'f' }, |
82 | { "kill", no_argument, 0, 'k' }, | 82 | { "kill", no_argument, 0, 'k' }, |
83 | { "reload", no_argument, 0, 'r' }, | 83 | { "reload", no_argument, 0, 'r' }, |
84 | { "signal", required_argument, 0, 's' }, | 84 | { "signal", required_argument, 0, 's' }, |
85 | { "check", no_argument, 0, 'c' }, | 85 | { "check", no_argument, 0, 'c' }, |
86 | { "ensure", no_argument, 0, 'e' }, | 86 | { "ensure", no_argument, 0, 'e' }, |
87 | { "test", no_argument, 0, 't' }, | 87 | { "test", no_argument, 0, 't' }, |
88 | { NULL, 0, 0, 0 } | 88 | { NULL, 0, 0, 0 } |
89 | }; | 89 | }; |
90 | int c = getopt_long(argc,argv,SHORTOPTSTRING,opts,NULL); | 90 | int c = getopt_long(argc,argv,SHORTOPTSTRING,opts,NULL); |
91 | #else /* !HAVE_GETOPT_LONG */ | 91 | #else /* !HAVE_GETOPT_LONG */ |
92 | int c = getopt(argc,argv,SHORTOPTSTRING); | 92 | int c = getopt(argc,argv,SHORTOPTSTRING); |
93 | #endif /* /HAVE_GETOPT_LONG */ | 93 | #endif /* /HAVE_GETOPT_LONG */ |
94 | if(c==-1) | 94 | if(c==-1) |
95 | break; | 95 | break; |
96 | switch(c) { | 96 | switch(c) { |
97 | case 'h': | 97 | case 'h': |
98 | cerr << PHEADER << endl | 98 | cerr << PHEADER << endl |
99 | << PCOPY << endl << endl | 99 | << PCOPY << endl << endl |
100 | << " " << argv[0] << " [options] [processes]" << endl << endl << | 100 | << " " << argv[0] << " [options] [processes]" << endl << endl << |
101 | #ifdef HAVE_GETOPT_LONG | 101 | #ifdef HAVE_GETOPT_LONG |
102 | " -h, --help\n" | 102 | " -h, --help\n" |
103 | " --usage display this text\n" | 103 | " --usage display this text\n" |
104 | " -V, --version display version number\n" | 104 | " -V, --version display version number\n" |
105 | " -L, --license show license\n" | 105 | " -L, --license show license\n" |
106 | " -f filename, --config=filename\n" | 106 | " -f filename, --config=filename\n" |
107 | " specify the configuration file to use\n" | 107 | " specify the configuration file to use\n" |
108 | "\n" | 108 | "\n" |
109 | " -k, --kill stop running instance (send SIGTERM)\n" | 109 | " -k, --kill stop running instance (send SIGTERM)\n" |
110 | " -r, --reload reload running instance (send SIGHUP)\n" | 110 | " -r, --reload reload running instance (send SIGHUP)\n" |
111 | " -s signum, --signal=signum\n" | 111 | " -s signum, --signal=signum\n" |
112 | " send the specified signal to the running process\n" | 112 | " send the specified signal to the running process\n" |
113 | " -c, --check check if the process is running\n" | 113 | " -c, --check check if the process is running\n" |
114 | " (the above commands operate on dudki itself if no\n" | 114 | " (the above commands operate on dudki itself if no\n" |
115 | " process name has been specified)\n" | 115 | " process name has been specified)\n" |
116 | " -e, --ensure ensure that dudki is running\n" | 116 | " -e, --ensure ensure that dudki is running\n" |
117 | " -t, --test test configuration file and exit" | 117 | " -t, --test test configuration file and exit" |
118 | #else /* !HAVE_GETOPT_LONG */ | 118 | #else /* !HAVE_GETOPT_LONG */ |
119 | " -h display this text\n" | 119 | " -h display this text\n" |
120 | " -V display version number\n" | 120 | " -V display version number\n" |
121 | " -L show license\n" | 121 | " -L show license\n" |
122 | " -f filename specify the configuration file to use\n" | 122 | " -f filename specify the configuration file to use\n" |
123 | "\n" | 123 | "\n" |
124 | " -k stop running instance (send SIGTERM)\n" | 124 | " -k stop running instance (send SIGTERM)\n" |
125 | " -r reload running instance (send SIGHUP)\n" | 125 | " -r reload running instance (send SIGHUP)\n" |
126 | " -s signum send the specified signal to the running process\n" | 126 | " -s signum send the specified signal to the running process\n" |
127 | " -c check if the process is running\n" | 127 | " -c check if the process is running\n" |
128 | " (the above commands operate on dudki itself if no\n" | 128 | " (the above commands operate on dudki itself if no\n" |
129 | " process name has been specified)\n" | 129 | " process name has been specified)\n" |
130 | " -e ensure that dudki is running\n" | 130 | " -e ensure that dudki is running\n" |
131 | " -t test configuration file and exit" | 131 | " -t test configuration file and exit" |
132 | #endif /* /HAVE_GETOPT_LONG */ | 132 | #endif /* /HAVE_GETOPT_LONG */ |
133 | << endl; | 133 | << endl; |
134 | exit(0); | 134 | exit(0); |
135 | break; | 135 | break; |
136 | case 'V': | 136 | case 'V': |
137 | cerr << VERSION << endl; | 137 | cerr << VERSION << endl; |
138 | exit(0); | 138 | exit(0); |
139 | break; | 139 | break; |
140 | case 'L': | 140 | case 'L': |
141 | extern const char *COPYING; | 141 | extern const char *COPYING; |
142 | cerr << COPYING << endl; | 142 | cerr << COPYING << endl; |
143 | exit(0); | 143 | exit(0); |
144 | break; | 144 | break; |
145 | case 'f': | 145 | case 'f': |
146 | config_file = optarg; | 146 | config_file = optarg; |
147 | break; | 147 | break; |
148 | case 'k': | 148 | case 'k': |
149 | if(op!=op_default) { | 149 | if(op!=op_default) { |
150 | cerr << "Can't obey two or more orders at once" << endl; | 150 | cerr << "Can't obey two or more orders at once" << endl; |
151 | exit(1); | 151 | exit(1); |
152 | } | 152 | } |
153 | op = op_signal; op_signum = SIGTERM; | 153 | op = op_signal; op_signum = SIGTERM; |
154 | break; | 154 | break; |
155 | case 'r': | 155 | case 'r': |
156 | if(op!=op_default) { | 156 | if(op!=op_default) { |
157 | cerr << "Can't obey two or more orders at once" << endl; | 157 | cerr << "Can't obey two or more orders at once" << endl; |
158 | exit(1); | 158 | exit(1); |
159 | } | 159 | } |
160 | op = op_signal; op_signum = SIGHUP; | 160 | op = op_signal; op_signum = SIGHUP; |
161 | break; | 161 | break; |
162 | case 'c': | 162 | case 'c': |
163 | if(op!=op_default) { | 163 | if(op!=op_default) { |
164 | cerr << "Can't obey two or more orders at once" << endl; | 164 | cerr << "Can't obey two or more orders at once" << endl; |
165 | exit(1); | 165 | exit(1); |
166 | } | 166 | } |
167 | op = op_signal; op_signum = 0; | 167 | op = op_signal; op_signum = 0; |
168 | break; | 168 | break; |
169 | case 'e': | 169 | case 'e': |
170 | if(op!=op_default) { | 170 | if(op!=op_default) { |
171 | cerr << "Can't obey two or more orders at once" << endl; | 171 | cerr << "Can't obey two or more orders at once" << endl; |
172 | exit(1); | 172 | exit(1); |
173 | } | 173 | } |
174 | op = op_ensure; | 174 | op = op_ensure; |
175 | break; | 175 | break; |
176 | case 't': | 176 | case 't': |
177 | if(op!=op_default) { | 177 | if(op!=op_default) { |
178 | cerr << "Can't obey two or more orders at once" << endl; | 178 | cerr << "Can't obey two or more orders at once" << endl; |
179 | exit(1); | 179 | exit(1); |
180 | } | 180 | } |
181 | op = op_test; | 181 | op = op_test; |
182 | break; | 182 | break; |
183 | case 's': | 183 | case 's': |
184 | if(op!=op_default) { | 184 | if(op!=op_default) { |
185 | cerr << "Can't obey two or more orders at once" << endl; | 185 | cerr << "Can't obey two or more orders at once" << endl; |
186 | exit(1); | 186 | exit(1); |
187 | } | 187 | } |
188 | op = op_signal; | 188 | op = op_signal; |
189 | errno = 0; | 189 | errno = 0; |
190 | op_signum = strtol(optarg,NULL,0); | 190 | op_signum = strtol(optarg,NULL,0); |
191 | if(errno) { | 191 | if(errno) { |
192 | cerr << "Can't obtain the signal value" << endl; | 192 | cerr << "Can't obtain the signal value" << endl; |
193 | exit(1); | 193 | exit(1); |
194 | } | 194 | } |
195 | break; | 195 | break; |
196 | default: | 196 | default: |
197 | cerr << "Huh??" << endl; | 197 | cerr << "Huh??" << endl; |
198 | exit(1); | 198 | exit(1); |
199 | break; | 199 | break; |
200 | } | 200 | } |
201 | } | 201 | } |
202 | const char *sid = *argv; | 202 | const char *sid = *argv; |
203 | const char *t; | 203 | const char *t; |
204 | while(t = index(sid,'/')) { | 204 | while(t = index(sid,'/')) { |
205 | sid = t; sid++; | 205 | sid = t; sid++; |
206 | } | 206 | } |
207 | openlog(sid,LOG_CONS|LOG_PERROR|LOG_PID,LOG_DAEMON); | 207 | openlog(sid,LOG_CONS|LOG_PERROR|LOG_PID,LOG_DAEMON); |
208 | configuration config; | 208 | configuration config; |
209 | config.parse(config_file); | 209 | config.parse(config_file); |
210 | switch(op) { | 210 | switch(op) { |
211 | case op_test: | 211 | case op_test: |
212 | cerr << "Configuration OK" << endl; | 212 | cerr << "Configuration OK" << endl; |
213 | break; | 213 | break; |
214 | case op_signal: | 214 | case op_signal: |