summaryrefslogtreecommitdiffabout
path: root/src
authorMichael Krelin <hacker@klever.net>2004-07-10 19:59:06 (UTC)
committer Michael Krelin <hacker@klever.net>2004-07-10 19:59:06 (UTC)
commit4f8a6f291a231410a03c438bc9d63a7beb861e7b (patch) (unidiff)
tree790352f42d045e23e9bbb6ae3a210d4faeae8244 /src
parent9148dac885c0325636c2d33715ba248371706d0d (diff)
downloaddudki-4f8a6f291a231410a03c438bc9d63a7beb861e7b.zip
dudki-4f8a6f291a231410a03c438bc9d63a7beb861e7b.tar.gz
dudki-4f8a6f291a231410a03c438bc9d63a7beb861e7b.tar.bz2
fixed signal handling, so that dudki can be HUP-ped more than once.0.0
Diffstat (limited to 'src') (more/less context) (ignore whitespace changes)
-rw-r--r--src/dudki.cc5
1 files changed, 5 insertions, 0 deletions
diff --git a/src/dudki.cc b/src/dudki.cc
index 3c50e56..b769109 100644
--- a/src/dudki.cc
+++ b/src/dudki.cc
@@ -135,110 +135,115 @@ int main(int argc,char **argv) {
135 break; 135 break;
136 case 'k': 136 case 'k':
137 if(op!=op_default) { 137 if(op!=op_default) {
138 cerr << "Can't obey two or more orders at once" << endl; 138 cerr << "Can't obey two or more orders at once" << endl;
139 exit(1); 139 exit(1);
140 } 140 }
141 op = op_term; 141 op = op_term;
142 break; 142 break;
143 case 'r': 143 case 'r':
144 if(op!=op_default) { 144 if(op!=op_default) {
145 cerr << "Can't obey two or more orders at once" << endl; 145 cerr << "Can't obey two or more orders at once" << endl;
146 exit(1); 146 exit(1);
147 } 147 }
148 op = op_hup; 148 op = op_hup;
149 break; 149 break;
150 case 'c': 150 case 'c':
151 if(op!=op_default) { 151 if(op!=op_default) {
152 cerr << "Can't obey two or more orders at once" << endl; 152 cerr << "Can't obey two or more orders at once" << endl;
153 exit(1); 153 exit(1);
154 } 154 }
155 op = op_check; 155 op = op_check;
156 break; 156 break;
157 case 'e': 157 case 'e':
158 if(op!=op_default) { 158 if(op!=op_default) {
159 cerr << "Can't obey two or more orders at once" << endl; 159 cerr << "Can't obey two or more orders at once" << endl;
160 exit(1); 160 exit(1);
161 } 161 }
162 op = op_ensure; 162 op = op_ensure;
163 break; 163 break;
164 case 't': 164 case 't':
165 if(op!=op_default) { 165 if(op!=op_default) {
166 cerr << "Can't obey two or more orders at once" << endl; 166 cerr << "Can't obey two or more orders at once" << endl;
167 exit(1); 167 exit(1);
168 } 168 }
169 op = op_test; 169 op = op_test;
170 break; 170 break;
171 default: 171 default:
172 cerr << "Huh??" << endl; 172 cerr << "Huh??" << endl;
173 exit(1); 173 exit(1);
174 break; 174 break;
175 } 175 }
176 } 176 }
177 const char *sid = *argv; 177 const char *sid = *argv;
178 const char *t; 178 const char *t;
179 while(t = index(sid,'/')) { 179 while(t = index(sid,'/')) {
180 sid = t; sid++; 180 sid = t; sid++;
181 } 181 }
182 openlog(sid,LOG_CONS|LOG_PERROR|LOG_PID,LOG_DAEMON); 182 openlog(sid,LOG_CONS|LOG_PERROR|LOG_PID,LOG_DAEMON);
183 configuration config; 183 configuration config;
184 config.parse(config_file); 184 config.parse(config_file);
185 switch(op) { 185 switch(op) {
186 case op_test: 186 case op_test:
187 cerr << "Configuration OK" << endl; 187 cerr << "Configuration OK" << endl;
188 break; 188 break;
189 case op_hup: 189 case op_hup:
190 signal_self(config,SIGHUP); 190 signal_self(config,SIGHUP);
191 break; 191 break;
192 case op_term: 192 case op_term:
193 signal_self(config,SIGTERM); 193 signal_self(config,SIGTERM);
194 break; 194 break;
195 case op_check: 195 case op_check:
196 try{ 196 try{
197 signal_self(config,0); 197 signal_self(config,0);
198 exit(0); 198 exit(0);
199 }catch(exception& e) { 199 }catch(exception& e) {
200 exit(1); 200 exit(1);
201 } 201 }
202 case op_ensure: 202 case op_ensure:
203 try { 203 try {
204 signal_self(config,0); 204 signal_self(config,0);
205 break; 205 break;
206 }catch(exception& e) { 206 }catch(exception& e) {
207 syslog(LOG_NOTICE,"The dudki process is down, taking its place"); 207 syslog(LOG_NOTICE,"The dudki process is down, taking its place");
208 config.daemonize = true; 208 config.daemonize = true;
209 }catch(int zero) { 209 }catch(int zero) {
210 // we throw zero in case we're ensuring that this very process is running. 210 // 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. 211 // we don't have to daemonize if we're daemonic.
212 config.daemonize = false; 212 config.daemonize = false;
213 } 213 }
214 case op_default: 214 case op_default:
215 case op_work: 215 case op_work:
216 { 216 {
217 if(config.daemonize) { 217 if(config.daemonize) {
218 pid_t pf = fork(); 218 pid_t pf = fork();
219 if(pf<0) 219 if(pf<0)
220 throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to fork()"); 220 throw runtime_error(string(__PRETTY_FUNCTION__)+": failed to fork()");
221 if(pf) { 221 if(pf) {
222 _exit(0); 222 _exit(0);
223 } 223 }
224 } 224 }
225 pid_file pidfile; 225 pid_file pidfile;
226 pidfile.set(config.pidfile); 226 pidfile.set(config.pidfile);
227 signal(SIGINT,lethal_signal_handler); 227 signal(SIGINT,lethal_signal_handler);
228 signal(SIGABRT,lethal_signal_handler); 228 signal(SIGABRT,lethal_signal_handler);
229 signal(SIGTERM,lethal_signal_handler); 229 signal(SIGTERM,lethal_signal_handler);
230 signal(SIGHUP,sighup_handler); 230 signal(SIGHUP,sighup_handler);
231 sigset_t sset;
232 sigemptyset(&sset);
233 sigaddset(&sset,SIGINT); sigaddset(&sset,SIGABRT);
234 sigaddset(&sset,SIGTERM); sigaddset(&sset,SIGHUP);
235 sigprocmask(SIG_UNBLOCK,&sset,NULL);
231 while(!finishing) { 236 while(!finishing) {
232 check_herd(config); 237 check_herd(config);
233 sleep(config.check_interval); 238 sleep(config.check_interval);
234 } 239 }
235 } 240 }
236 break; 241 break;
237 default: 242 default:
238 throw runtime_error(string(__PRETTY_FUNCTION__)+": internal error"); 243 throw runtime_error(string(__PRETTY_FUNCTION__)+": internal error");
239 } 244 }
240 }catch(exception& e) { 245 }catch(exception& e) {
241 cerr << "Oops: " << e.what() << endl; 246 cerr << "Oops: " << e.what() << endl;
242 return 1; 247 return 1;
243 } 248 }
244} 249}