1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #include <ccan/daemonize/daemonize.h>
9 /* This code is based on Stevens Advanced Programming in the UNIX
15 /* Separate from our parent via fork, so init inherits us. */
16 if ((pid = fork()) < 0)
18 /* use _exit() to avoid triggering atexit() processing */
22 /* Don't hold files open. */
27 /* Many routines write to stderr; that can cause chaos if used
28 * for something else, so set it here. */
29 if (open("/dev/null", O_WRONLY) != 0)
31 if (dup2(0, STDERR_FILENO) != STDERR_FILENO)
35 /* Session leader so ^C doesn't whack us. */
36 if (setsid() == (pid_t)-1)
38 /* Move off any mount points we might be in. */
42 /* Discard our parent's old-fashioned umask prejudices. */