summaryrefslogtreecommitdiffabout
path: root/src
authorMichael Krelin <hacker@klever.net>2004-07-23 20:40:46 (UTC)
committer Michael Krelin <hacker@klever.net>2004-07-23 20:40:46 (UTC)
commit546858a1e4d13d179a6af27b474e1396cfdf0c29 (patch) (unidiff)
treeac19b0ff5e4b3164ad5375bda112a9d6d2f88c2b /src
parent76921288a0aa39acb53102863523c388b5d0f9ee (diff)
downloaddudki-546858a1e4d13d179a6af27b474e1396cfdf0c29.zip
dudki-546858a1e4d13d179a6af27b474e1396cfdf0c29.tar.gz
dudki-546858a1e4d13d179a6af27b474e1396cfdf0c29.tar.bz2
the ability to check/kill/reload any of the processes being monitored added.
Diffstat (limited to 'src') (more/less context) (show whitespace changes)
-rw-r--r--src/dudki.cc55
-rw-r--r--src/process.cc30
-rw-r--r--src/process.h2
3 files changed, 54 insertions, 33 deletions
diff --git a/src/dudki.cc b/src/dudki.cc
index b4e95a7..e91ad5e 100644
--- a/src/dudki.cc
+++ b/src/dudki.cc
@@ -63,10 +63,9 @@ int main(int argc,char **argv) {
63 op_default, 63 op_default,
64 op_work, 64 op_work,
65 op_hup, 65 op_signal,
66 op_term,
67 op_check,
68 op_ensure, 66 op_ensure,
69 op_test 67 op_test
70 } op = op_default; 68 } op = op_default;
69 int op_signum = 0;
71 while(true) { 70 while(true) {
72 #defineSHORTOPTSTRING "f:hVLrkcet" 71 #defineSHORTOPTSTRING "f:hVLrkcet"
@@ -94,5 +93,6 @@ int main(int argc,char **argv) {
94 case 'h': 93 case 'h':
95 cerr << PHEADER << endl 94 cerr << PHEADER << endl
96 << PCOPY << endl << endl << 95 << PCOPY << endl << endl
96 << " " << argv[0] << " [options] [processes]" << endl << endl <<
97#ifdef HAVE_GETOPT_LONG 97#ifdef HAVE_GETOPT_LONG
98 " -h, --help\n" 98 " -h, --help\n"
@@ -103,7 +103,9 @@ int main(int argc,char **argv) {
103 " specify the configuration file to use\n" 103 " specify the configuration file to use\n"
104 "\n" 104 "\n"
105 " -k, --kill stop running instance\n" 105 " -k, --kill stop running instance (send SIGTERM)\n"
106 " -r, --reload reload running instance (send SIGHUP)\n" 106 " -r, --reload reload running instance (send SIGHUP)\n"
107 " -c, --check check if dudki is running\n" 107 " -c, --check check if the process is running\n"
108 " (the above commands operate on dudki itself if no\n"
109 " process name has been specified)\n"
108 " -e, --ensure ensure that dudki is running\n" 110 " -e, --ensure ensure that dudki is running\n"
109 " -t, --test test configuration file and exit" 111 " -t, --test test configuration file and exit"
@@ -114,7 +116,9 @@ int main(int argc,char **argv) {
114 " -f filename specify the configuration file to use\n" 116 " -f filename specify the configuration file to use\n"
115 "\n" 117 "\n"
116 " -k stop running instance\n" 118 " -k stop running instance (send SIGTERM)\n"
117 " -r reload running instance (send SIGHUP)\n" 119 " -r reload running instance (send SIGHUP)\n"
118 " -c check if dudki is running\n" 120 " -c check if the process is running\n"
121 " (the above commands operate on dudki itself if no\n"
122 " process name has been specified)\n"
119 " -e ensure that dudki is running\n" 123 " -e ensure that dudki is running\n"
120 " -t test configuration file and exit" 124 " -t test configuration file and exit"
@@ -140,5 +144,5 @@ int main(int argc,char **argv) {
140 exit(1); 144 exit(1);
141 } 145 }
142 op = op_term; 146 op = op_signal; op_signum = SIGTERM;
143 break; 147 break;
144 case 'r': 148 case 'r':
@@ -147,5 +151,5 @@ int main(int argc,char **argv) {
147 exit(1); 151 exit(1);
148 } 152 }
149 op = op_hup; 153 op = op_signal; op_signum = SIGHUP;
150 break; 154 break;
151 case 'c': 155 case 'c':
@@ -154,5 +158,5 @@ int main(int argc,char **argv) {
154 exit(1); 158 exit(1);
155 } 159 }
156 op = op_check; 160 op = op_signal; op_signum = 0;
157 break; 161 break;
158 case 'e': 162 case 'e':
@@ -188,15 +192,28 @@ int main(int argc,char **argv) {
188 cerr << "Configuration OK" << endl; 192 cerr << "Configuration OK" << endl;
189 break; 193 break;
190 case op_hup: 194 case op_signal:
191 signal_self(config,SIGHUP);
192 break;
193 case op_term:
194 signal_self(config,SIGTERM);
195 break;
196 case op_check:
197 try{ 195 try{
198 signal_self(config,0); 196 if(optind>=argc) {
197 signal_self(config,op_signum);
198 }else{
199 int failures = 0;
200 for(int narg=optind;narg<argc;narg++) {
201 try {
202 processes_t::const_iterator i = config.processes.find(argv[narg]);
203 if(i==config.processes.end())
204 throw runtime_error("no such process configured");
205 i->second.signal(op_signum);
206 }catch(exception& e) {
207 cerr << "dudki(" << argv[narg] << "): " << e.what() << endl;
208 failures++;
209 }
210 }
211 if(failures)
212 throw runtime_error("not all processes have been successfully signaled");
213 }
214 if(!op_signum)
199 exit(0); 215 exit(0);
200 }catch(exception& e) { 216 }catch(exception& e) {
217 if(!op_signum)
201 exit(1); 218 exit(1);
202 } 219 }
diff --git a/src/process.cc b/src/process.cc
index bfab311..1ffac9f 100644
--- a/src/process.cc
+++ b/src/process.cc
@@ -16,19 +16,8 @@ using namespace std;
16 16
17void process::check(const string& id,configuration& config) { 17void process::check(const string& id,configuration& config) {
18 bool running = false; 18 try {
19 ifstream pids(pidfile.c_str(),ios::in); 19 signal(0);
20 if(pids) {
21 pid_t pid = 0;
22 pids >> pid;
23 pids.close();
24 if(pid) {
25 if(!kill(pid,0)) {
26 running = true;
27 }
28 }
29 }
30 if(running){
31 patience = 0; 20 patience = 0;
32 }else{ 21 }catch(exception& e) {
33 if(patience>60) { // TODO: configurable 22 if(patience>60) { // TODO: configurable
34 patience = 0; 23 patience = 0;
@@ -186,2 +175,15 @@ void process::notify_mailto(const string& email,const string& id,const string& e
186 // TODO: check the return code 175 // TODO: check the return code
187} 176}
177
178void process::signal(int signum) const {
179 ifstream pids(pidfile.c_str(),ios::in);
180 if(!pids)
181 throw runtime_error("no pidfile found");
182 pid_t pid = 0;
183 pids >> pid;
184 pids.close();
185 if(!pid)
186 throw runtime_error("no pid in pidfile");
187 if(kill(pid,signum))
188 throw runtime_error("failed to signal process");
189}
diff --git a/src/process.h b/src/process.h
index b6d7091..27ee049 100644
--- a/src/process.h
+++ b/src/process.h
@@ -30,4 +30,6 @@ class process {
30 void notify_mailto(const string& email,const string& id,const string& event, 30 void notify_mailto(const string& email,const string& id,const string& event,
31 const string& description,configuration& config); 31 const string& description,configuration& config);
32
33 void signal(int signum) const;
32}; 34};
33 35