-rw-r--r-- | src/dudki.cc | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/dudki.cc b/src/dudki.cc index c966695..3af5372 100644 --- a/src/dudki.cc +++ b/src/dudki.cc | |||
@@ -141,152 +141,152 @@ int main(int argc,char **argv) { | |||
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: |
215 | try { | 215 | try { |
216 | if(optind>=argc) { | 216 | if(optind>=argc) { |
217 | signal_self(config,op_signum); | 217 | signal_self(config,op_signum); |
218 | }else{ | 218 | }else{ |
219 | int failures = 0; | 219 | int failures = 0; |
220 | for(int narg=optind;narg<argc;narg++) { | 220 | for(int narg=optind;narg<argc;narg++) { |
221 | try { | 221 | try { |
222 | processes_t::const_iterator i = config.processes.find(argv[narg]); | 222 | processes_t::const_iterator i = config.processes.find(argv[narg]); |
223 | if(i==config.processes.end()) | 223 | if(i==config.processes.end()) |
224 | throw runtime_error("no such process configured"); | 224 | throw runtime_error("no such process configured"); |
225 | if(op_signum) | 225 | if(op_signum) |
226 | i->second.signal(op_signum); | 226 | i->second.signal(op_signum); |
227 | else | 227 | else |
228 | i->second.check(); | 228 | i->second.check(); |
229 | }catch(exception& e) { | 229 | }catch(exception& e) { |
230 | cerr << "dudki(" << argv[narg] << "): " << e.what() << endl; | 230 | cerr << "dudki(" << argv[narg] << "): " << e.what() << endl; |
231 | failures++; | 231 | failures++; |
232 | } | 232 | } |
233 | } | 233 | } |
234 | if(failures) | 234 | if(failures) |
235 | throw runtime_error("not all processes have been successfully signaled"); | 235 | throw runtime_error("not all processes have been successfully signaled"); |
236 | } | 236 | } |
237 | if(!op_signum) | 237 | exit(0); |
238 | exit(0); | ||
239 | }catch(exception& e) { | 238 | }catch(exception& e) { |
240 | if(!op_signum) | 239 | exit(1); |
241 | exit(1); | ||
242 | } | 240 | } |
241 | break; | ||
243 | case op_ensure: | 242 | case op_ensure: |
244 | try { | 243 | try { |
245 | signal_self(config,0); | 244 | signal_self(config,0); |
246 | break; | 245 | break; |
247 | }catch(exception& e) { | 246 | }catch(exception& e) { |
248 | syslog(LOG_NOTICE,"The dudki process is down, taking its place"); | 247 | syslog(LOG_NOTICE,"The dudki process is down, taking its place"); |
249 | config.daemonize = true; | 248 | config.daemonize = true; |
250 | }catch(int zero) { | 249 | }catch(int zero) { |
251 | // we throw zero in case we're ensuring that this very process is running. | 250 | // we throw zero in case we're ensuring that this very process is running. |
252 | // we don't have to daemonize if we're daemonic. | 251 | // we don't have to daemonize if we're daemonic. |
253 | config.daemonize = false; | 252 | config.daemonize = false; |
254 | } | 253 | } |
255 | case op_default: | 254 | case op_default: |
256 | case op_work: | 255 | case op_work: |
257 | { | 256 | { |
258 | if(config.daemonize) { | 257 | if(config.daemonize) { |
259 | pid_t pf = fork(); | 258 | pid_t pf = fork(); |
260 | if(pf<0) | 259 | if(pf<0) |
261 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to fork()"); | 260 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to fork()"); |
262 | if(pf) { | 261 | if(pf) { |
263 | _exit(0); | 262 | _exit(0); |
264 | } | 263 | } |
265 | } | 264 | } |
266 | pid_file pidfile; | 265 | pid_file pidfile; |
267 | pidfile.set(config.pidfile); | 266 | pidfile.set(config.pidfile); |
268 | signal(SIGINT,lethal_signal_handler); | 267 | signal(SIGINT,lethal_signal_handler); |
269 | signal(SIGABRT,lethal_signal_handler); | 268 | signal(SIGABRT,lethal_signal_handler); |
270 | signal(SIGTERM,lethal_signal_handler); | 269 | signal(SIGTERM,lethal_signal_handler); |
271 | signal(SIGHUP,sighup_handler); | 270 | signal(SIGHUP,sighup_handler); |
272 | sigset_t sset; | 271 | sigset_t sset; |
273 | sigemptyset(&sset); | 272 | sigemptyset(&sset); |
274 | sigaddset(&sset,SIGINT); sigaddset(&sset,SIGABRT); | 273 | sigaddset(&sset,SIGINT); sigaddset(&sset,SIGABRT); |
275 | sigaddset(&sset,SIGTERM); sigaddset(&sset,SIGHUP); | 274 | sigaddset(&sset,SIGTERM); sigaddset(&sset,SIGHUP); |
276 | sigprocmask(SIG_UNBLOCK,&sset,NULL); | 275 | sigprocmask(SIG_UNBLOCK,&sset,NULL); |
277 | while(!finishing) { | 276 | while(!finishing) { |
278 | check_herd(config); | 277 | check_herd(config); |
279 | sleep(config.check_interval); | 278 | sleep(config.check_interval); |
280 | } | 279 | } |
281 | if(restarting) | 280 | if(restarting) |
282 | execvp(_argv[0],_argv); | 281 | execvp(_argv[0],_argv); |
283 | } | 282 | } |
284 | break; | 283 | break; |
285 | default: | 284 | default: |
286 | throw runtime_error(string(__PRETTY_FUNCTION__)+": internal error"); | 285 | throw runtime_error(string(__PRETTY_FUNCTION__)+": internal error"); |
287 | } | 286 | } |
288 | }catch(exception& e) { | 287 | }catch(exception& e) { |
289 | cerr << "Oops: " << e.what() << endl; | 288 | cerr << "Oops: " << e.what() << endl; |
290 | return 1; | 289 | return 1; |
291 | } | 290 | } |
291 | exit(0); | ||
292 | } | 292 | } |