]> git.ozlabs.org Git - ccan/blob - ccan/daemonize/daemonize.c
Rename _info.c to _info: this means we can simple compile *.c.
[ccan] / ccan / daemonize / daemonize.c
1 #include <ccan/daemonize/daemonize.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6
7 /* This code is based on Stevens Advanced Programming in the UNIX
8  * Environment. */
9 bool daemonize(void)
10 {
11         pid_t pid;
12
13         /* Separate from our parent via fork, so init inherits us. */
14         if ((pid = fork()) < 0)
15                 return false;
16         if (pid != 0)
17                 exit(0);
18
19         /* Don't hold files open. */
20         close(STDIN_FILENO);
21         close(STDOUT_FILENO);
22         close(STDERR_FILENO);
23
24         /* Session leader so ^C doesn't whack us. */
25         setsid();
26         /* Move off any mount points we might be in. */
27         chdir("/");
28         /* Discard our parent's old-fashioned umask prejudices. */
29         umask(0);
30         return true;
31 }