diff options
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/xfuncs.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 207537929..6a6bdced3 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
| @@ -509,6 +509,30 @@ void xdaemon(int nochdir, int noclose) | |||
| 509 | } | 509 | } |
| 510 | #endif | 510 | #endif |
| 511 | 511 | ||
| 512 | void bb_sanitize_stdio(int daemonize) | ||
| 513 | { | ||
| 514 | int fd; | ||
| 515 | /* Mega-paranoid */ | ||
| 516 | fd = xopen(bb_dev_null, O_RDWR); | ||
| 517 | while (fd < 2) | ||
| 518 | fd = dup(fd); /* have 0,1,2 open at least to /dev/null */ | ||
| 519 | if (daemonize) { | ||
| 520 | pid_t pid = fork(); | ||
| 521 | if (pid < 0) /* wtf? */ | ||
| 522 | bb_perror_msg_and_die("fork"); | ||
| 523 | if (pid) /* parent */ | ||
| 524 | exit(0); | ||
| 525 | /* child */ | ||
| 526 | setsid(); | ||
| 527 | /* if daemonizing, make sure we detach from stdio */ | ||
| 528 | dup2(fd, 0); | ||
| 529 | dup2(fd, 1); | ||
| 530 | dup2(fd, 2); | ||
| 531 | } | ||
| 532 | while (fd > 2) | ||
| 533 | close(fd--); /* close everything after fd#2 */ | ||
| 534 | } | ||
| 535 | |||
| 512 | // Die with an error message if we can't open a new socket. | 536 | // Die with an error message if we can't open a new socket. |
| 513 | int xsocket(int domain, int type, int protocol) | 537 | int xsocket(int domain, int type, int protocol) |
| 514 | { | 538 | { |
