-rw-r--r-- | NEWS | 3 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/dudki.cc | 5 |
3 files changed, 8 insertions, 2 deletions
@@ -1,2 +1,5 @@ | |||
1 | 0.1 | ||
2 | - initgroups() before executing RestartCommand. | ||
3 | - more civilized restart. | ||
1 | 0.0 | 4 | 0.0 |
2 | - Initial release. | 5 | - Initial release. |
diff --git a/configure.ac b/configure.ac index 8a2a10b..8521a34 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -1,38 +1,38 @@ | |||
1 | AC_INIT([dudki], [0.0], [dudki-bugs@klever.net]) | 1 | AC_INIT([dudki], [0.1], [dudki-bugs@klever.net]) |
2 | AC_CONFIG_SRCDIR([src/dudki.cc]) | 2 | AC_CONFIG_SRCDIR([src/dudki.cc]) |
3 | AC_CONFIG_HEADER([config.h]) | 3 | AC_CONFIG_HEADER([config.h]) |
4 | AM_INIT_AUTOMAKE([dist-bzip2]) | 4 | AM_INIT_AUTOMAKE([dist-bzip2]) |
5 | 5 | ||
6 | AC_PROG_CXX | 6 | AC_PROG_CXX |
7 | AC_PROG_CC | 7 | AC_PROG_CC |
8 | 8 | ||
9 | AC_HEADER_SYS_WAIT | 9 | AC_HEADER_SYS_WAIT |
10 | AC_CHECK_HEADERS([syslog.h unistd.h getopt.h]) | 10 | AC_CHECK_HEADERS([syslog.h unistd.h getopt.h]) |
11 | 11 | ||
12 | AC_HEADER_STDBOOL | 12 | AC_HEADER_STDBOOL |
13 | AC_C_CONST | 13 | AC_C_CONST |
14 | AC_TYPE_UID_T | 14 | AC_TYPE_UID_T |
15 | AC_TYPE_PID_T | 15 | AC_TYPE_PID_T |
16 | 16 | ||
17 | AC_FUNC_FORK | 17 | AC_FUNC_FORK |
18 | AC_HEADER_STDC | 18 | AC_HEADER_STDC |
19 | AC_TYPE_SIGNAL | 19 | AC_TYPE_SIGNAL |
20 | AC_CHECK_FUNCS([dup2 strtol]) | 20 | AC_CHECK_FUNCS([dup2 strtol]) |
21 | AC_CHECK_FUNC([getopt_long],[ | 21 | AC_CHECK_FUNC([getopt_long],[ |
22 | AC_DEFINE([HAVE_GETOPT_LONG],[1],[Define to make use of getopt_long]) | 22 | AC_DEFINE([HAVE_GETOPT_LONG],[1],[Define to make use of getopt_long]) |
23 | AC_SUBST(HAVE_GETOPT_LONG,1) | 23 | AC_SUBST(HAVE_GETOPT_LONG,1) |
24 | ],[ | 24 | ],[ |
25 | AC_SUBST(HAVE_GETOPT_LONG,0) | 25 | AC_SUBST(HAVE_GETOPT_LONG,0) |
26 | ]) | 26 | ]) |
27 | 27 | ||
28 | PKG_CHECK_MODULES(DOTCONF,[dotconf],,[ | 28 | PKG_CHECK_MODULES(DOTCONF,[dotconf],,[ |
29 | AC_MSG_ERROR([no dotconf library found]) | 29 | AC_MSG_ERROR([no dotconf library found]) |
30 | ]) | 30 | ]) |
31 | 31 | ||
32 | AC_CONFIG_FILES([ | 32 | AC_CONFIG_FILES([ |
33 | Makefile | 33 | Makefile |
34 | src/Makefile | 34 | src/Makefile |
35 | man/Makefile | 35 | man/Makefile |
36 | man/dudki.8 man/dudki.conf.5 | 36 | man/dudki.8 man/dudki.conf.5 |
37 | ]) | 37 | ]) |
38 | AC_OUTPUT | 38 | AC_OUTPUT |
diff --git a/src/dudki.cc b/src/dudki.cc index b769109..b4e95a7 100644 --- a/src/dudki.cc +++ b/src/dudki.cc | |||
@@ -1,80 +1,81 @@ | |||
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 <iostream> | 4 | #include <iostream> |
5 | #include <fstream> | 5 | #include <fstream> |
6 | #include <stdexcept> | 6 | #include <stdexcept> |
7 | using namespace std; | 7 | using namespace std; |
8 | #include "configuration.h" | 8 | #include "configuration.h" |
9 | #include "util.h" | 9 | #include "util.h" |
10 | 10 | ||
11 | #include "config.h" | 11 | #include "config.h" |
12 | #ifdef HAVE_GETOPT_H | 12 | #ifdef HAVE_GETOPT_H |
13 | # include <getopt.h> | 13 | # include <getopt.h> |
14 | #endif | 14 | #endif |
15 | 15 | ||
16 | #ifndef DEFAULT_CONF_FILE | 16 | #ifndef DEFAULT_CONF_FILE |
17 | # define DEFAULT_CONF_FILE "/etc/dudki.conf" | 17 | # define DEFAULT_CONF_FILE "/etc/dudki.conf" |
18 | #endif | 18 | #endif |
19 | 19 | ||
20 | #define PHEADER PACKAGE " Version " VERSION | 20 | #define PHEADER PACKAGE " Version " VERSION |
21 | #define PCOPY "Copyright (c) 2004 Klever Group" | 21 | #define PCOPY "Copyright (c) 2004 Klever Group" |
22 | 22 | ||
23 | bool finishing = false; | 23 | bool finishing = false; |
24 | bool restarting = false; | ||
24 | static char **_argv = NULL; | 25 | static char **_argv = NULL; |
25 | 26 | ||
26 | static void lethal_signal_handler(int signum) { | 27 | static void lethal_signal_handler(int signum) { |
27 | syslog(LOG_NOTICE,"Lethal signal received. Terminating."); | 28 | syslog(LOG_NOTICE,"Lethal signal received. Terminating."); |
28 | finishing = true; | 29 | finishing = true; |
29 | } | 30 | } |
30 | static void sighup_handler(int signum) { | 31 | static void sighup_handler(int signum) { |
31 | syslog(LOG_NOTICE,"SUGHUP received, reloading."); | 32 | syslog(LOG_NOTICE,"SUGHUP received, reloading."); |
32 | execvp(_argv[0],_argv); | 33 | restarting = finishing = true; |
33 | } | 34 | } |
34 | 35 | ||
35 | void check_herd(configuration& config) { | 36 | void check_herd(configuration& config) { |
36 | for(processes_t::iterator i=config.processes.begin();i!=config.processes.end();++i) | 37 | for(processes_t::iterator i=config.processes.begin();i!=config.processes.end();++i) |
37 | i->second.check(i->first,config); | 38 | i->second.check(i->first,config); |
38 | } | 39 | } |
39 | 40 | ||
40 | void signal_self(const configuration& config,int signum) { | 41 | void signal_self(const configuration& config,int signum) { |
41 | ifstream pids(config.pidfile.c_str(),ios::in); | 42 | ifstream pids(config.pidfile.c_str(),ios::in); |
42 | if(!pids) | 43 | if(!pids) |
43 | throw runtime_error("Can't detect running instance"); | 44 | throw runtime_error("Can't detect running instance"); |
44 | pid_t pid = 0; | 45 | pid_t pid = 0; |
45 | pids >> pid; | 46 | pids >> pid; |
46 | if(!pid) | 47 | if(!pid) |
47 | throw runtime_error("Can't detect running instance"); | 48 | throw runtime_error("Can't detect running instance"); |
48 | if(pid==getpid()) | 49 | if(pid==getpid()) |
49 | throw 0; | 50 | throw 0; |
50 | if(kill(pid,signum)) | 51 | if(kill(pid,signum)) |
51 | throw runtime_error("Failed to signal running instance"); | 52 | throw runtime_error("Failed to signal running instance"); |
52 | } | 53 | } |
53 | 54 | ||
54 | int main(int argc,char **argv) { | 55 | int main(int argc,char **argv) { |
55 | try { | 56 | try { |
56 | _argv = new char*[argc+1]; | 57 | _argv = new char*[argc+1]; |
57 | if(!_argv) | 58 | if(!_argv) |
58 | throw runtime_error("memory allocation problem at the very start"); | 59 | throw runtime_error("memory allocation problem at the very start"); |
59 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); | 60 | memmove(_argv,argv,sizeof(*_argv)*(argc+1)); |
60 | string config_file = DEFAULT_CONF_FILE; | 61 | string config_file = DEFAULT_CONF_FILE; |
61 | enum { | 62 | enum { |
62 | op_default, | 63 | op_default, |
63 | op_work, | 64 | op_work, |
64 | op_hup, | 65 | op_hup, |
65 | op_term, | 66 | op_term, |
66 | op_check, | 67 | op_check, |
67 | op_ensure, | 68 | op_ensure, |
68 | op_test | 69 | op_test |
69 | } op = op_default; | 70 | } op = op_default; |
70 | while(true) { | 71 | while(true) { |
71 | #defineSHORTOPTSTRING "f:hVLrkcet" | 72 | #defineSHORTOPTSTRING "f:hVLrkcet" |
72 | #ifdef HAVE_GETOPT_LONG | 73 | #ifdef HAVE_GETOPT_LONG |
73 | static struct option opts[] = { | 74 | static struct option opts[] = { |
74 | { "help", no_argument, 0, 'h' }, | 75 | { "help", no_argument, 0, 'h' }, |
75 | { "usage", no_argument, 0, 'h' }, | 76 | { "usage", no_argument, 0, 'h' }, |
76 | { "version", no_argument, 0, 'V' }, | 77 | { "version", no_argument, 0, 'V' }, |
77 | { "license", no_argument, 0, 'L' }, | 78 | { "license", no_argument, 0, 'L' }, |
78 | { "config", required_argument, 0, 'f' }, | 79 | { "config", required_argument, 0, 'f' }, |
79 | { "kill", no_argument, 0, 'k' }, | 80 | { "kill", no_argument, 0, 'k' }, |
80 | { "reload", no_argument, 0, 'r' }, | 81 | { "reload", no_argument, 0, 'r' }, |
@@ -192,58 +193,60 @@ int main(int argc,char **argv) { | |||
192 | case op_term: | 193 | case op_term: |
193 | signal_self(config,SIGTERM); | 194 | signal_self(config,SIGTERM); |
194 | break; | 195 | break; |
195 | case op_check: | 196 | case op_check: |
196 | try{ | 197 | try{ |
197 | signal_self(config,0); | 198 | signal_self(config,0); |
198 | exit(0); | 199 | exit(0); |
199 | }catch(exception& e) { | 200 | }catch(exception& e) { |
200 | exit(1); | 201 | exit(1); |
201 | } | 202 | } |
202 | case op_ensure: | 203 | case op_ensure: |
203 | try { | 204 | try { |
204 | signal_self(config,0); | 205 | signal_self(config,0); |
205 | break; | 206 | break; |
206 | }catch(exception& e) { | 207 | }catch(exception& e) { |
207 | syslog(LOG_NOTICE,"The dudki process is down, taking its place"); | 208 | syslog(LOG_NOTICE,"The dudki process is down, taking its place"); |
208 | config.daemonize = true; | 209 | config.daemonize = true; |
209 | }catch(int zero) { | 210 | }catch(int zero) { |
210 | // we throw zero in case we're ensuring that this very process is running. | 211 | // we throw zero in case we're ensuring that this very process is running. |
211 | // we don't have to daemonize if we're daemonic. | 212 | // we don't have to daemonize if we're daemonic. |
212 | config.daemonize = false; | 213 | config.daemonize = false; |
213 | } | 214 | } |
214 | case op_default: | 215 | case op_default: |
215 | case op_work: | 216 | case op_work: |
216 | { | 217 | { |
217 | if(config.daemonize) { | 218 | if(config.daemonize) { |
218 | pid_t pf = fork(); | 219 | pid_t pf = fork(); |
219 | if(pf<0) | 220 | if(pf<0) |
220 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to fork()"); | 221 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to fork()"); |
221 | if(pf) { | 222 | if(pf) { |
222 | _exit(0); | 223 | _exit(0); |
223 | } | 224 | } |
224 | } | 225 | } |
225 | pid_file pidfile; | 226 | pid_file pidfile; |
226 | pidfile.set(config.pidfile); | 227 | pidfile.set(config.pidfile); |
227 | signal(SIGINT,lethal_signal_handler); | 228 | signal(SIGINT,lethal_signal_handler); |
228 | signal(SIGABRT,lethal_signal_handler); | 229 | signal(SIGABRT,lethal_signal_handler); |
229 | signal(SIGTERM,lethal_signal_handler); | 230 | signal(SIGTERM,lethal_signal_handler); |
230 | signal(SIGHUP,sighup_handler); | 231 | signal(SIGHUP,sighup_handler); |
231 | sigset_t sset; | 232 | sigset_t sset; |
232 | sigemptyset(&sset); | 233 | sigemptyset(&sset); |
233 | sigaddset(&sset,SIGINT); sigaddset(&sset,SIGABRT); | 234 | sigaddset(&sset,SIGINT); sigaddset(&sset,SIGABRT); |
234 | sigaddset(&sset,SIGTERM); sigaddset(&sset,SIGHUP); | 235 | sigaddset(&sset,SIGTERM); sigaddset(&sset,SIGHUP); |
235 | sigprocmask(SIG_UNBLOCK,&sset,NULL); | 236 | sigprocmask(SIG_UNBLOCK,&sset,NULL); |
236 | while(!finishing) { | 237 | while(!finishing) { |
237 | check_herd(config); | 238 | check_herd(config); |
238 | sleep(config.check_interval); | 239 | sleep(config.check_interval); |
239 | } | 240 | } |
241 | if(restarting) | ||
242 | execvp(_argv[0],_argv); | ||
240 | } | 243 | } |
241 | break; | 244 | break; |
242 | default: | 245 | default: |
243 | throw runtime_error(string(__PRETTY_FUNCTION__)+": internal error"); | 246 | throw runtime_error(string(__PRETTY_FUNCTION__)+": internal error"); |
244 | } | 247 | } |
245 | }catch(exception& e) { | 248 | }catch(exception& e) { |
246 | cerr << "Oops: " << e.what() << endl; | 249 | cerr << "Oops: " << e.what() << endl; |
247 | return 1; | 250 | return 1; |
248 | } | 251 | } |
249 | } | 252 | } |