chiark / gitweb /
[PATCH] klibc: update to version 0.196
[elogind.git] / klibc / klibc / daemon.c
1 /*
2  * daemon.c - "daemonize" a process
3  */
4
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8
9 int daemon(int nochdir, int noclose)
10 {
11   int nullfd;
12   pid_t f;
13
14   if ( !nochdir ) {
15     if ( chdir("/") )
16       return -1;
17   }
18
19   if ( !noclose ) {
20     if ( (nullfd = open("/dev/null", O_RDWR)) < 0 ||
21          dup2(nullfd, 0) < 0 ||
22          dup2(nullfd, 1) < 0 ||
23          dup2(nullfd, 2) < 0 )
24       return -1;
25     close(nullfd);
26   }
27   
28   f = fork();
29   if ( f < 0 )
30     return -1;
31   else if ( f > 0 )
32     _exit(0);
33
34
35   return setsid();
36 }
37
38