git-svn: trunk@3526
Tomasz Kojm authored on 2008/01/23 02:47:15... | ... |
@@ -1,3 +1,7 @@ |
1 |
+Tue Jan 22 18:24:12 CET 2008 (tk) |
|
2 |
+--------------------------------- |
|
3 |
+ * shared/misc.c: add error reporting to daemonize() (bb#729) |
|
4 |
+ |
|
1 | 5 |
Tue Jan 22 18:47:54 EET 2008 (edwin) |
2 | 6 |
----------------------------------- |
3 | 7 |
* libclamav/entconv.c: handle E2BIG correctly, avoid infinite loops on iconv |
... | ... |
@@ -280,7 +280,12 @@ int main(int argc, char **argv) |
280 | 280 |
|
281 | 281 |
/* fork into background */ |
282 | 282 |
if(!cfgopt(copt, "Foreground")->enabled) { |
283 |
- daemonize(); |
|
283 |
+ if(daemonize() == -1) { |
|
284 |
+ logg("!daemonize() failed\n"); |
|
285 |
+ logg_close(); |
|
286 |
+ freecfg(copt); |
|
287 |
+ return 1; |
|
288 |
+ } |
|
284 | 289 |
if(!debug_mode) |
285 | 290 |
if(chdir("/") == -1) |
286 | 291 |
logg("^Can't change current working directory to root\n"); |
... | ... |
@@ -54,7 +54,6 @@ struct thrwarg { |
54 | 54 |
int acceptloop_th(int *socketds, int nsockets, struct cl_engine *engine, unsigned int dboptions, const struct cfgstruct *copt); |
55 | 55 |
void sighandler(int sig); |
56 | 56 |
void sighandler_th(int sig); |
57 |
-void daemonize(void); |
|
58 | 57 |
void sigsegv(int sig); |
59 | 58 |
|
60 | 59 |
#endif |
... | ... |
@@ -481,8 +481,13 @@ int main(int argc, char **argv) |
481 | 481 |
bigsleep = 24 * 3600 / checks; |
482 | 482 |
|
483 | 483 |
if(!cfgopt(copt, "Foreground")->enabled) { |
484 |
+ if(daemonize() == -1) { |
|
485 |
+ logg("!daemonize() failed\n"); |
|
486 |
+ opt_free(opt); |
|
487 |
+ freecfg(copt); |
|
488 |
+ return 70; /* FIXME */ |
|
489 |
+ } |
|
484 | 490 |
foreground = 0; |
485 |
- daemonize(); |
|
486 | 491 |
mprintf_disabled = 1; |
487 | 492 |
} |
488 | 493 |
|
... | ... |
@@ -283,30 +283,50 @@ int cvd_unpack(const char *cvd, const char *destdir) |
283 | 283 |
} |
284 | 284 |
#endif |
285 | 285 |
|
286 |
-void daemonize(void) |
|
286 |
+int daemonize(void) |
|
287 | 287 |
{ |
288 |
-#if defined(C_OS2) || defined(C_WINDOWS) |
|
289 |
- fputs("Background mode is not supported on your operating system\n", stderr); |
|
288 |
+#if defined(C_OS2) || defined(C_WINDOWS) |
|
289 |
+ fputs("Background mode is not supported on your operating system\n", stderr); |
|
290 | 290 |
return; |
291 | 291 |
#else |
292 |
- int i; |
|
292 |
+ int fds[3], i; |
|
293 |
+ pid_t pid; |
|
293 | 294 |
|
294 | 295 |
|
295 |
- if((i = open("/dev/null", O_RDWR)) == -1) { |
|
296 |
+ fds[0] = open("/dev/null", O_RDONLY); |
|
297 |
+ fds[1] = open("/dev/null", O_WRONLY); |
|
298 |
+ fds[2] = open("/dev/null", O_WRONLY); |
|
299 |
+ if(fds[0] == -1 || fds[1] == -1 || fds[2] == -1) { |
|
300 |
+ fputs("Can't open /dev/null\n", stderr); |
|
296 | 301 |
for(i = 0; i <= 2; i++) |
297 |
- close(i); |
|
302 |
+ if(fds[i] != -1) |
|
303 |
+ close(fds[i]); |
|
304 |
+ return -1; |
|
305 |
+ } |
|
298 | 306 |
|
299 |
- } else { |
|
300 |
- dup2(i, 0); |
|
301 |
- dup2(i, 1); |
|
302 |
- dup2(i, 2); |
|
303 |
- if(i > 2) |
|
304 |
- close(i); |
|
307 |
+ for(i = 0; i <= 2; i++) { |
|
308 |
+ if(dup2(fds[i], i) == -1) { |
|
309 |
+ fprintf(stderr, "dup2(%d, %d) failed\n", fds[i], i); /* may not be printed */ |
|
310 |
+ for(i = 0; i <= 2; i++) |
|
311 |
+ if(fds[i] != -1) |
|
312 |
+ close(fds[i]); |
|
313 |
+ return -1; |
|
314 |
+ } |
|
305 | 315 |
} |
306 | 316 |
|
307 |
- if(fork()) |
|
317 |
+ for(i = 0; i <= 2; i++) |
|
318 |
+ if(fds[i] > 2) |
|
319 |
+ close(fds[i]); |
|
320 |
+ |
|
321 |
+ pid = fork(); |
|
322 |
+ |
|
323 |
+ if(pid == -1) |
|
324 |
+ return -1; |
|
325 |
+ |
|
326 |
+ if(pid) |
|
308 | 327 |
exit(0); |
309 | 328 |
|
310 | 329 |
setsid(); |
330 |
+ return 0; |
|
311 | 331 |
#endif |
312 | 332 |
} |