-rw-r--r-- | src/dudki.cc | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/src/dudki.cc b/src/dudki.cc index 91a3342..1f95be4 100644 --- a/src/dudki.cc +++ b/src/dudki.cc | |||
@@ -1,135 +1,137 @@ | |||
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 | #include <cstring> | ||
9 | #include <stdlib.h> | ||
8 | using namespace std; | 10 | using namespace std; |
9 | #include "configuration.h" | 11 | #include "configuration.h" |
10 | #include "util.h" | 12 | #include "util.h" |
11 | 13 | ||
12 | #include "config.h" | 14 | #include "config.h" |
13 | #ifdef HAVE_GETOPT_H | 15 | #ifdef HAVE_GETOPT_H |
14 | # include <getopt.h> | 16 | # include <getopt.h> |
15 | #endif | 17 | #endif |
16 | 18 | ||
17 | #ifndef DEFAULT_CONF_FILE | 19 | #ifndef DEFAULT_CONF_FILE |
18 | # define DEFAULT_CONF_FILE "/etc/dudki.conf" | 20 | # define DEFAULT_CONF_FILE "/etc/dudki.conf" |
19 | #endif | 21 | #endif |
20 | 22 | ||
21 | #define PHEADER PACKAGE " Version " VERSION | 23 | #define PHEADER PACKAGE " Version " VERSION |
22 | #define PCOPY "Copyright (c) 2004-2006 Klever Group" | 24 | #define PCOPY "Copyright (c) 2004-2006 Klever Group" |
23 | 25 | ||
24 | bool finishing = false; | 26 | bool finishing = false; |
25 | bool restarting = false; | 27 | bool restarting = false; |
26 | static char **_argv = NULL; | 28 | static char **_argv = NULL; |
27 | 29 | ||
28 | static void lethal_signal_handler(int signum) { | 30 | static void lethal_signal_handler(int signum) { |
29 | syslog(LOG_NOTICE,"Lethal signal received. Terminating."); | 31 | syslog(LOG_NOTICE,"Lethal signal received. Terminating."); |
30 | finishing = true; | 32 | finishing = true; |
31 | } | 33 | } |
32 | static void sighup_handler(int signum) { | 34 | static void sighup_handler(int signum) { |
33 | syslog(LOG_NOTICE,"SUGHUP received, reloading."); | 35 | syslog(LOG_NOTICE,"SUGHUP received, reloading."); |
34 | restarting = finishing = true; | 36 | restarting = finishing = true; |
35 | } | 37 | } |
36 | 38 | ||
37 | void check_herd(configuration& config) { | 39 | void check_herd(configuration& config) { |
38 | process::prepare_herd(); | 40 | process::prepare_herd(); |
39 | for(processes_t::iterator i=config.processes.begin();i!=config.processes.end();++i) | 41 | for(processes_t::iterator i=config.processes.begin();i!=config.processes.end();++i) |
40 | i->second.check(i->first,config); | 42 | i->second.check(i->first,config); |
41 | process::unprepare_herd(); | 43 | process::unprepare_herd(); |
42 | } | 44 | } |
43 | 45 | ||
44 | void signal_self(const configuration& config,int signum) { | 46 | void signal_self(const configuration& config,int signum) { |
45 | ifstream pids(config.pidfile.c_str(),ios::in); | 47 | ifstream pids(config.pidfile.c_str(),ios::in); |
46 | if(!pids) | 48 | if(!pids) |
47 | throw runtime_error("Can't detect running instance"); | 49 | throw runtime_error("Can't detect running instance"); |
48 | pid_t pid = 0; | 50 | pid_t pid = 0; |
49 | pids >> pid; | 51 | pids >> pid; |
50 | if(!pid) | 52 | if(!pid) |
51 | throw runtime_error("Can't detect running instance"); | 53 | throw runtime_error("Can't detect running instance"); |
52 | if(pid==getpid()) | 54 | if(pid==getpid()) |
53 | throw 0; | 55 | throw 0; |
54 | if(kill(pid,signum)) | 56 | if(kill(pid,signum)) |
55 | throw runtime_error("Failed to signal running instance"); | 57 | throw runtime_error("Failed to signal running instance"); |
56 | } | 58 | } |
57 | 59 | ||
58 | int main(int argc,char **argv) { | 60 | int main(int argc,char **argv) { |
59 | try { | 61 | try { |
60 | _argv = new char*[argc+1]; | 62 | _argv = new char*[argc+1]; |
61 | if(!_argv) | 63 | if(!_argv) |
62 | throw runtime_error("memory allocation problem at the very start"); | 64 | throw runtime_error("memory allocation problem at the very start"); |
63 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); | 65 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); |
64 | string config_file = DEFAULT_CONF_FILE; | 66 | string config_file = DEFAULT_CONF_FILE; |
65 | enum { | 67 | enum { |
66 | op_default, | 68 | op_default, |
67 | op_work, | 69 | op_work, |
68 | op_signal, | 70 | op_signal, |
69 | op_ensure, | 71 | op_ensure, |
70 | op_test | 72 | op_test |
71 | } op = op_default; | 73 | } op = op_default; |
72 | int op_signum = 0; | 74 | int op_signum = 0; |
73 | while(true) { | 75 | while(true) { |
74 | #defineSHORTOPTSTRING "f:hVLrkcets:" | 76 | #defineSHORTOPTSTRING "f:hVLrkcets:" |
75 | #ifdef HAVE_GETOPT_LONG | 77 | #ifdef HAVE_GETOPT_LONG |
76 | static struct option opts[] = { | 78 | static struct option opts[] = { |
77 | { "help", no_argument, 0, 'h' }, | 79 | { "help", no_argument, 0, 'h' }, |
78 | { "usage", no_argument, 0, 'h' }, | 80 | { "usage", no_argument, 0, 'h' }, |
79 | { "version", no_argument, 0, 'V' }, | 81 | { "version", no_argument, 0, 'V' }, |
80 | { "license", no_argument, 0, 'L' }, | 82 | { "license", no_argument, 0, 'L' }, |
81 | { "config", required_argument, 0, 'f' }, | 83 | { "config", required_argument, 0, 'f' }, |
82 | { "kill", no_argument, 0, 'k' }, | 84 | { "kill", no_argument, 0, 'k' }, |
83 | { "reload", no_argument, 0, 'r' }, | 85 | { "reload", no_argument, 0, 'r' }, |
84 | { "signal", required_argument, 0, 's' }, | 86 | { "signal", required_argument, 0, 's' }, |
85 | { "check", no_argument, 0, 'c' }, | 87 | { "check", no_argument, 0, 'c' }, |
86 | { "ensure", no_argument, 0, 'e' }, | 88 | { "ensure", no_argument, 0, 'e' }, |
87 | { "test", no_argument, 0, 't' }, | 89 | { "test", no_argument, 0, 't' }, |
88 | { NULL, 0, 0, 0 } | 90 | { NULL, 0, 0, 0 } |
89 | }; | 91 | }; |
90 | int c = getopt_long(argc,argv,SHORTOPTSTRING,opts,NULL); | 92 | int c = getopt_long(argc,argv,SHORTOPTSTRING,opts,NULL); |
91 | #else /* !HAVE_GETOPT_LONG */ | 93 | #else /* !HAVE_GETOPT_LONG */ |
92 | int c = getopt(argc,argv,SHORTOPTSTRING); | 94 | int c = getopt(argc,argv,SHORTOPTSTRING); |
93 | #endif /* /HAVE_GETOPT_LONG */ | 95 | #endif /* /HAVE_GETOPT_LONG */ |
94 | if(c==-1) | 96 | if(c==-1) |
95 | break; | 97 | break; |
96 | switch(c) { | 98 | switch(c) { |
97 | case 'h': | 99 | case 'h': |
98 | cerr << PHEADER << endl | 100 | cerr << PHEADER << endl |
99 | << PCOPY << endl << endl | 101 | << PCOPY << endl << endl |
100 | << " " << argv[0] << " [options] [processes]" << endl << endl << | 102 | << " " << argv[0] << " [options] [processes]" << endl << endl << |
101 | #ifdef HAVE_GETOPT_LONG | 103 | #ifdef HAVE_GETOPT_LONG |
102 | " -h, --help\n" | 104 | " -h, --help\n" |
103 | " --usage display this text\n" | 105 | " --usage display this text\n" |
104 | " -V, --version display version number\n" | 106 | " -V, --version display version number\n" |
105 | " -L, --license show license\n" | 107 | " -L, --license show license\n" |
106 | " -f filename, --config=filename\n" | 108 | " -f filename, --config=filename\n" |
107 | " specify the configuration file to use\n" | 109 | " specify the configuration file to use\n" |
108 | "\n" | 110 | "\n" |
109 | " -k, --kill stop running instance (send SIGTERM)\n" | 111 | " -k, --kill stop running instance (send SIGTERM)\n" |
110 | " -r, --reload reload running instance (send SIGHUP)\n" | 112 | " -r, --reload reload running instance (send SIGHUP)\n" |
111 | " -s signum, --signal=signum\n" | 113 | " -s signum, --signal=signum\n" |
112 | " send the specified signal to the running process\n" | 114 | " send the specified signal to the running process\n" |
113 | " -c, --check check if the process is running\n" | 115 | " -c, --check check if the process is running\n" |
114 | " (the above commands operate on dudki itself if no\n" | 116 | " (the above commands operate on dudki itself if no\n" |
115 | " process name has been specified)\n" | 117 | " process name has been specified)\n" |
116 | " -e, --ensure ensure that dudki is running\n" | 118 | " -e, --ensure ensure that dudki is running\n" |
117 | " -t, --test test configuration file and exit" | 119 | " -t, --test test configuration file and exit" |
118 | #else /* !HAVE_GETOPT_LONG */ | 120 | #else /* !HAVE_GETOPT_LONG */ |
119 | " -h display this text\n" | 121 | " -h display this text\n" |
120 | " -V display version number\n" | 122 | " -V display version number\n" |
121 | " -L show license\n" | 123 | " -L show license\n" |
122 | " -f filename specify the configuration file to use\n" | 124 | " -f filename specify the configuration file to use\n" |
123 | "\n" | 125 | "\n" |
124 | " -k stop running instance (send SIGTERM)\n" | 126 | " -k stop running instance (send SIGTERM)\n" |
125 | " -r reload running instance (send SIGHUP)\n" | 127 | " -r reload running instance (send SIGHUP)\n" |
126 | " -s signum send the specified signal to the running process\n" | 128 | " -s signum send the specified signal to the running process\n" |
127 | " -c check if the process is running\n" | 129 | " -c check if the process is running\n" |
128 | " (the above commands operate on dudki itself if no\n" | 130 | " (the above commands operate on dudki itself if no\n" |
129 | " process name has been specified)\n" | 131 | " process name has been specified)\n" |
130 | " -e ensure that dudki is running\n" | 132 | " -e ensure that dudki is running\n" |
131 | " -t test configuration file and exit" | 133 | " -t test configuration file and exit" |
132 | #endif /* /HAVE_GETOPT_LONG */ | 134 | #endif /* /HAVE_GETOPT_LONG */ |
133 | << endl; | 135 | << endl; |
134 | exit(0); | 136 | exit(0); |
135 | break; | 137 | break; |