summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--src/dudki.cc2
-rw-r--r--src/process.cc3
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,55 +1,57 @@
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>
8using namespace std; 10using 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
24bool finishing = false; 26bool finishing = false;
25bool restarting = false; 27bool restarting = false;
26static char **_argv = NULL; 28static char **_argv = NULL;
27 29
28static void lethal_signal_handler(int signum) { 30static 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}
32static void sighup_handler(int signum) { 34static 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
37void check_herd(configuration& config) { 39void 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
44void signal_self(const configuration& config,int signum) { 46void 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");
diff --git a/src/process.cc b/src/process.cc
index 4807b98..3e9cc2b 100644
--- a/src/process.cc
+++ b/src/process.cc
@@ -1,57 +1,58 @@
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>
17using namespace std; 18using namespace std;
18#include "process.h" 19#include "process.h"
19#include "configuration.h" 20#include "configuration.h"
20 21
21static multimap<string,pid_t> procpids; 22static multimap<string,pid_t> procpids;
22 23
23void process::check() const { 24void 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}
33void process::check(const string& id,configuration& config) { 34void 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
42 syslog(LOG_NOTICE,"The process '%s' is down, trying to launch.",id.c_str()); 43 syslog(LOG_NOTICE,"The process '%s' is down, trying to launch.",id.c_str());
43 do_notify(id,"Starting up", 44 do_notify(id,"Starting up",
44 "The named process seems to be down. Dudki will try\n" 45 "The named process seems to be down. Dudki will try\n"
45 "to revive it by running the specified command.\n", 46 "to revive it by running the specified command.\n",
46 config); 47 config);
47 try { 48 try {
48 launch(id,config); 49 launch(id,config);
49 }catch(exception& e) { 50 }catch(exception& e) {
50 syslog(LOG_ERR,"Error trying to launch process '%s': %s",id.c_str(),e.what()); 51 syslog(LOG_ERR,"Error trying to launch process '%s': %s",id.c_str(),e.what());
51 } 52 }
52 }else if(patience==10){ // TODO: configurable like the above 53 }else if(patience==10){ // TODO: configurable like the above
53 syslog(LOG_NOTICE,"Giving up on process '%s' for a while",id.c_str()); 54 syslog(LOG_NOTICE,"Giving up on process '%s' for a while",id.c_str());
54 do_notify(id,"Giving up", 55 do_notify(id,"Giving up",
55 "After a number of attempts to relaunch the named process\n" 56 "After a number of attempts to relaunch the named process\n"
56 "It still seems to be down. Dudki is giving up attempts\n" 57 "It still seems to be down. Dudki is giving up attempts\n"
57 "to revive the process for a while.\n", 58 "to revive the process for a while.\n",
@@ -67,97 +68,97 @@ void process::launch(const string& id,configuration& config) {
67 gid_t gid = (gid_t)-1; 68 gid_t gid = (gid_t)-1;
68 if(!user.empty()) { 69 if(!user.empty()) {
69 struct passwd *ptmp = getpwnam(user.c_str()); 70 struct passwd *ptmp = getpwnam(user.c_str());
70 if(ptmp) { 71 if(ptmp) {
71 uid = ptmp->pw_uid; 72 uid = ptmp->pw_uid;
72 gid = ptmp->pw_gid; 73 gid = ptmp->pw_gid;
73 }else{ 74 }else{
74 errno=0; 75 errno=0;
75 uid = strtol(user.c_str(),NULL,0); 76 uid = strtol(user.c_str(),NULL,0);
76 if(errno) 77 if(errno)
77 throw runtime_error("Failed to resolve User value to uid"); 78 throw runtime_error("Failed to resolve User value to uid");
78 } 79 }
79 } 80 }
80 if(!group.empty()) { 81 if(!group.empty()) {
81 struct group *gtmp = getgrnam(group.c_str()); 82 struct group *gtmp = getgrnam(group.c_str());
82 if(gtmp) { 83 if(gtmp) {
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
129void process::do_notify(const string& id,const string& event,const string& description,configuration& config) { 130void 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) {
148 syslog(LOG_ERR,"Notification error: %s",e.what()); 149 syslog(LOG_ERR,"Notification error: %s",e.what());
149 } 150 }
150} 151}
151 152
152void process::notify_mailto(const string& email,const string& id,const string& event,const string& description,configuration& config) { 153void process::notify_mailto(const string& email,const string& id,const string& event,const string& description,configuration& config) {
153 int files[2]; 154 int files[2];
154 if(pipe(files)) 155 if(pipe(files))
155 throw runtime_error("Failed to pipe()"); 156 throw runtime_error("Failed to pipe()");
156 pid_t pid = vfork(); 157 pid_t pid = vfork();
157 if(pid==-1) { 158 if(pid==-1) {
158 close(files[0]); 159 close(files[0]);
159 close(files[1]); 160 close(files[1]);
160 throw runtime_error("Failed to vfork()"); 161 throw runtime_error("Failed to vfork()");
161 } 162 }
162 if(!pid) { 163 if(!pid) {
163 // child 164 // child