author | Michael Krelin <hacker@klever.net> | 2004-07-24 00:24:07 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2004-07-24 00:24:07 (UTC) |
commit | fbc32792b8d8266ff90aa60403f5da78739236f4 (patch) (unidiff) | |
tree | 77bc3f196a3733c9c86290f8a73d60bb609bbdd5 /src/dudki.cc | |
parent | 125671c860a82643d36bc3da279d0b831fae4b34 (diff) | |
download | dudki-fbc32792b8d8266ff90aa60403f5da78739236f4.zip dudki-fbc32792b8d8266ff90aa60403f5da78739236f4.tar.gz dudki-fbc32792b8d8266ff90aa60403f5da78739236f4.tar.bz2 |
processes specified by process names (pidof-like).
-rw-r--r-- | src/dudki.cc | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/dudki.cc b/src/dudki.cc index 9562079..c966695 100644 --- a/src/dudki.cc +++ b/src/dudki.cc | |||
@@ -14,50 +14,52 @@ using namespace std; | |||
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 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 | 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) |
39 | i->second.check(i->first,config); | 40 | i->second.check(i->first,config); |
41 | process::unprepare_herd(); | ||
40 | } | 42 | } |
41 | 43 | ||
42 | void signal_self(const configuration& config,int signum) { | 44 | void signal_self(const configuration& config,int signum) { |
43 | ifstream pids(config.pidfile.c_str(),ios::in); | 45 | ifstream pids(config.pidfile.c_str(),ios::in); |
44 | if(!pids) | 46 | if(!pids) |
45 | throw runtime_error("Can't detect running instance"); | 47 | throw runtime_error("Can't detect running instance"); |
46 | pid_t pid = 0; | 48 | pid_t pid = 0; |
47 | pids >> pid; | 49 | pids >> pid; |
48 | if(!pid) | 50 | if(!pid) |
49 | throw runtime_error("Can't detect running instance"); | 51 | throw runtime_error("Can't detect running instance"); |
50 | if(pid==getpid()) | 52 | if(pid==getpid()) |
51 | throw 0; | 53 | throw 0; |
52 | if(kill(pid,signum)) | 54 | if(kill(pid,signum)) |
53 | throw runtime_error("Failed to signal running instance"); | 55 | throw runtime_error("Failed to signal running instance"); |
54 | } | 56 | } |
55 | 57 | ||
56 | int main(int argc,char **argv) { | 58 | int main(int argc,char **argv) { |
57 | try { | 59 | try { |
58 | _argv = new char*[argc+1]; | 60 | _argv = new char*[argc+1]; |
59 | if(!_argv) | 61 | if(!_argv) |
60 | throw runtime_error("memory allocation problem at the very start"); | 62 | throw runtime_error("memory allocation problem at the very start"); |
61 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); | 63 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); |
62 | string config_file = DEFAULT_CONF_FILE; | 64 | string config_file = DEFAULT_CONF_FILE; |
63 | enum { | 65 | enum { |
@@ -199,49 +201,52 @@ int main(int argc,char **argv) { | |||
199 | } | 201 | } |
200 | const char *sid = *argv; | 202 | const char *sid = *argv; |
201 | const char *t; | 203 | const char *t; |
202 | while(t = index(sid,'/')) { | 204 | while(t = index(sid,'/')) { |
203 | sid = t; sid++; | 205 | sid = t; sid++; |
204 | } | 206 | } |
205 | openlog(sid,LOG_CONS|LOG_PERROR|LOG_PID,LOG_DAEMON); | 207 | openlog(sid,LOG_CONS|LOG_PERROR|LOG_PID,LOG_DAEMON); |
206 | configuration config; | 208 | configuration config; |
207 | config.parse(config_file); | 209 | config.parse(config_file); |
208 | switch(op) { | 210 | switch(op) { |
209 | case op_test: | 211 | case op_test: |
210 | cerr << "Configuration OK" << endl; | 212 | cerr << "Configuration OK" << endl; |
211 | break; | 213 | break; |
212 | case op_signal: | 214 | case op_signal: |
213 | try { | 215 | try { |
214 | if(optind>=argc) { | 216 | if(optind>=argc) { |
215 | signal_self(config,op_signum); | 217 | signal_self(config,op_signum); |
216 | }else{ | 218 | }else{ |
217 | int failures = 0; | 219 | int failures = 0; |
218 | for(int narg=optind;narg<argc;narg++) { | 220 | for(int narg=optind;narg<argc;narg++) { |
219 | try { | 221 | try { |
220 | processes_t::const_iterator i = config.processes.find(argv[narg]); | 222 | processes_t::const_iterator i = config.processes.find(argv[narg]); |
221 | if(i==config.processes.end()) | 223 | if(i==config.processes.end()) |
222 | throw runtime_error("no such process configured"); | 224 | throw runtime_error("no such process configured"); |
223 | i->second.signal(op_signum); | 225 | if(op_signum) |
226 | i->second.signal(op_signum); | ||
227 | else | ||
228 | i->second.check(); | ||
224 | }catch(exception& e) { | 229 | }catch(exception& e) { |
225 | cerr << "dudki(" << argv[narg] << "): " << e.what() << endl; | 230 | cerr << "dudki(" << argv[narg] << "): " << e.what() << endl; |
226 | failures++; | 231 | failures++; |
227 | } | 232 | } |
228 | } | 233 | } |
229 | if(failures) | 234 | if(failures) |
230 | throw runtime_error("not all processes have been successfully signaled"); | 235 | throw runtime_error("not all processes have been successfully signaled"); |
231 | } | 236 | } |
232 | if(!op_signum) | 237 | if(!op_signum) |
233 | exit(0); | 238 | exit(0); |
234 | }catch(exception& e) { | 239 | }catch(exception& e) { |
235 | if(!op_signum) | 240 | if(!op_signum) |
236 | exit(1); | 241 | exit(1); |
237 | } | 242 | } |
238 | case op_ensure: | 243 | case op_ensure: |
239 | try { | 244 | try { |
240 | signal_self(config,0); | 245 | signal_self(config,0); |
241 | break; | 246 | break; |
242 | }catch(exception& e) { | 247 | }catch(exception& e) { |
243 | syslog(LOG_NOTICE,"The dudki process is down, taking its place"); | 248 | syslog(LOG_NOTICE,"The dudki process is down, taking its place"); |
244 | config.daemonize = true; | 249 | config.daemonize = true; |
245 | }catch(int zero) { | 250 | }catch(int zero) { |
246 | // we throw zero in case we're ensuring that this very process is running. | 251 | // we throw zero in case we're ensuring that this very process is running. |
247 | // we don't have to daemonize if we're daemonic. | 252 | // we don't have to daemonize if we're daemonic. |