-rw-r--r-- | src/dudki.cc | 2 | ||||
-rw-r--r-- | src/process.cc | 3 |
2 files changed, 4 insertions, 1 deletions
diff --git a/src/dudki.cc b/src/dudki.cc index 91a3342..1f95be4 100644 --- a/src/dudki.cc +++ b/src/dudki.cc | |||
@@ -1,39 +1,41 @@ | |||
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) |
diff --git a/src/process.cc b/src/process.cc index 4807b98..3e9cc2b 100644 --- a/src/process.cc +++ b/src/process.cc | |||
@@ -1,41 +1,42 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <sys/types.h> | 2 | #include <sys/types.h> |
3 | #include <unistd.h> | 3 | #include <unistd.h> |
4 | #include <signal.h> | 4 | #include <signal.h> |
5 | #include <pwd.h> | 5 | #include <pwd.h> |
6 | #include <grp.h> | 6 | #include <grp.h> |
7 | #include <dirent.h> | 7 | #include <dirent.h> |
8 | #include <sys/wait.h> | 8 | #include <sys/wait.h> |
9 | #include <syslog.h> | 9 | #include <syslog.h> |
10 | #include <stdlib.h> | ||
10 | #include <errno.h> | 11 | #include <errno.h> |
11 | #include <iostream> | 12 | #include <iostream> |
12 | #include <fstream> | 13 | #include <fstream> |
13 | #include <sstream> | 14 | #include <sstream> |
14 | #include <stdexcept> | 15 | #include <stdexcept> |
15 | #include <string> | 16 | #include <string> |
16 | #include <vector> | 17 | #include <vector> |
17 | using namespace std; | 18 | using namespace std; |
18 | #include "process.h" | 19 | #include "process.h" |
19 | #include "configuration.h" | 20 | #include "configuration.h" |
20 | 21 | ||
21 | static multimap<string,pid_t> procpids; | 22 | static multimap<string,pid_t> procpids; |
22 | 23 | ||
23 | void process::check() const { | 24 | void process::check() const { |
24 | if(!pidfile.empty()) { | 25 | if(!pidfile.empty()) { |
25 | signal(0); | 26 | signal(0); |
26 | }else if(!process_name.empty()) { | 27 | }else if(!process_name.empty()) { |
27 | if(procpids.empty()) | 28 | if(procpids.empty()) |
28 | gather_proc_info(); | 29 | gather_proc_info(); |
29 | if(procpids.find(process_name)==procpids.end()) | 30 | if(procpids.find(process_name)==procpids.end()) |
30 | throw runtime_error("no such process"); | 31 | throw runtime_error("no such process"); |
31 | } // XXX: or else? | 32 | } // XXX: or else? |
32 | } | 33 | } |
33 | void process::check(const string& id,configuration& config) { | 34 | void process::check(const string& id,configuration& config) { |
34 | try { | 35 | try { |
35 | check(); | 36 | check(); |
36 | patience = 0; | 37 | patience = 0; |
37 | }catch(exception& e) { | 38 | }catch(exception& e) { |
38 | if(patience>60) { // TODO: configurable | 39 | if(patience>60) { // TODO: configurable |
39 | patience = 0; | 40 | patience = 0; |
40 | }else{ | 41 | }else{ |
41 | if(patience<10) { // TODO: configurable | 42 | if(patience<10) { // TODO: configurable |
@@ -83,65 +84,65 @@ void process::launch(const string& id,configuration& config) { | |||
83 | gid = gtmp->gr_gid; | 84 | gid = gtmp->gr_gid; |
84 | }else{ | 85 | }else{ |
85 | errno = 0; | 86 | errno = 0; |
86 | gid = strtol(group.c_str(),NULL,0); | 87 | gid = strtol(group.c_str(),NULL,0); |
87 | if(errno) | 88 | if(errno) |
88 | throw runtime_error("Failed to reslove Group value to gid"); | 89 | throw runtime_error("Failed to reslove Group value to gid"); |
89 | } | 90 | } |
90 | } | 91 | } |
91 | pid_t p = fork(); | 92 | pid_t p = fork(); |
92 | if(p<0) | 93 | if(p<0) |
93 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to fork()"); | 94 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to fork()"); |
94 | if(!p) { | 95 | if(!p) { |
95 | // child | 96 | // child |
96 | try { | 97 | try { |
97 | setsid(); | 98 | setsid(); |
98 | if(!group.empty()) { | 99 | if(!group.empty()) { |
99 | if(user.empty()) { | 100 | if(user.empty()) { |
100 | if((getgid()!=gid) && setgid(gid)) | 101 | if((getgid()!=gid) && setgid(gid)) |
101 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to setgid()"); | 102 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to setgid()"); |
102 | }else{ | 103 | }else{ |
103 | if(initgroups(user.c_str(),gid)) | 104 | if(initgroups(user.c_str(),gid)) |
104 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to initgroups()"); | 105 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to initgroups()"); |
105 | } | 106 | } |
106 | } | 107 | } |
107 | if(!chroot.empty()) { | 108 | if(!chroot.empty()) { |
108 | if(::chroot(chroot.c_str())) | 109 | if(::chroot(chroot.c_str())) |
109 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to chroot()"); | 110 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to chroot()"); |
110 | } | 111 | } |
111 | if(!user.empty()) { | 112 | if(!user.empty()) { |
112 | if((getuid()!=uid) && setuid(uid)) | 113 | if((getuid()!=uid) && setuid(uid)) |
113 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to setuid()"); | 114 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to setuid()"); |
114 | } | 115 | } |
115 | char *argv[] = { "/bin/sh", "-c", (char*)restart_cmd.c_str(), NULL }; | 116 | char *argv[] = { const_cast<char*>("/bin/sh"), const_cast<char*>("-c"), (char*)restart_cmd.c_str(), NULL }; |
116 | close(0); close(1); close(2); | 117 | close(0); close(1); close(2); |
117 | execv("/bin/sh",argv); | 118 | execv("/bin/sh",argv); |
118 | }catch(exception& e) { | 119 | }catch(exception& e) { |
119 | syslog(LOG_ERR,"Error trying to launch process '%s': %s",id.c_str(),e.what()); | 120 | syslog(LOG_ERR,"Error trying to launch process '%s': %s",id.c_str(),e.what()); |
120 | } | 121 | } |
121 | _exit(-1); | 122 | _exit(-1); |
122 | } | 123 | } |
123 | // parent | 124 | // parent |
124 | int rv; | 125 | int rv; |
125 | if(waitpid(p,&rv,0)<0) | 126 | if(waitpid(p,&rv,0)<0) |
126 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to waitpid()"); | 127 | throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to waitpid()"); |
127 | } | 128 | } |
128 | 129 | ||
129 | void process::do_notify(const string& id,const string& event,const string& description,configuration& config) { | 130 | void process::do_notify(const string& id,const string& event,const string& description,configuration& config) { |
130 | string the_notify; | 131 | string the_notify; |
131 | if(!notify.empty()) | 132 | if(!notify.empty()) |
132 | the_notify=notify; | 133 | the_notify=notify; |
133 | else if(!config.notify.empty()) | 134 | else if(!config.notify.empty()) |
134 | the_notify=config.notify; | 135 | the_notify=config.notify; |
135 | else | 136 | else |
136 | return; | 137 | return; |
137 | try { | 138 | try { |
138 | string::size_type colon = the_notify.find(':'); | 139 | string::size_type colon = the_notify.find(':'); |
139 | if(colon==string::npos) | 140 | if(colon==string::npos) |
140 | throw runtime_error("invalid notify action specification"); | 141 | throw runtime_error("invalid notify action specification"); |
141 | string nschema = the_notify.substr(0,colon); | 142 | string nschema = the_notify.substr(0,colon); |
142 | string ntarget = the_notify.substr(colon+1); | 143 | string ntarget = the_notify.substr(colon+1); |
143 | if(nschema=="mailto") { | 144 | if(nschema=="mailto") { |
144 | notify_mailto(ntarget,id,event,description,config); | 145 | notify_mailto(ntarget,id,event,description,config); |
145 | }else | 146 | }else |
146 | throw runtime_error("unrecognized notification schema"); | 147 | throw runtime_error("unrecognized notification schema"); |
147 | }catch(exception& e) { | 148 | }catch(exception& e) { |