tftpd: make it possible to adjust the remap deadman
[tftp-hpa.git] / lib / daemon.c
blob0eb39c91336a8443de6168c3ee0030d1c7783259
1 /*
2 * daemon.c - "daemonize" a process
3 */
5 #include "config.h"
7 int daemon(int nochdir, int noclose)
9 int nullfd;
10 pid_t f;
12 if (!nochdir) {
13 if (chdir("/"))
14 return -1;
17 if (!noclose) {
18 if ((nullfd = open("/dev/null", O_RDWR)) < 0 ||
19 dup2(nullfd, 0) < 0 ||
20 dup2(nullfd, 1) < 0 || dup2(nullfd, 2) < 0)
21 return -1;
22 close(nullfd);
25 f = fork();
26 if (f < 0)
27 return -1;
28 else if (f > 0)
29 _exit(0);
31 #ifdef HAVE_SETSID
32 return setsid();
33 #else
34 return 0;
35 #endif