Update version for release
[tftp-hpa.git] / lib / daemon.c
blobc3106b5096a909a6207939312a59581931ad908e
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 ||
21 dup2(nullfd, 2) < 0)
22 return -1;
23 close(nullfd);
26 f = fork();
27 if (f < 0)
28 return -1;
29 else if (f > 0)
30 _exit(0);
32 #ifdef HAVE_SETSID
33 return setsid();
34 #else
35 return 0;
36 #endif