1 /* $Id: daemonize.c 6395 2003-07-12 19:13:49Z rra $
3 ** Become a long-running daemon.
9 ** Performs all of the various system-specific stuff required to become a
10 ** long-running daemon. Also chdir to the provided path (which is where
11 ** core dumps will go on most systems).
17 #include <sys/ioctl.h>
20 #include "inn/messages.h"
24 daemonize(const char *path)
29 /* Fork and exit in the parent to disassociate from the current process
30 group and become the leader of a new process group. */
37 /* setsid() should take care of disassociating from the controlling
38 terminal, and FreeBSD at least doesn't like TIOCNOTTY if you don't
39 already have a controlling terminal. So only use the older TIOCNOTTY
40 method if setsid() isn't available. */
43 syswarn("cant become session leader");
44 #elif defined(TIOCNOTTY)
45 fd = open("/dev/tty", O_RDWR);
47 if (ioctl(fd, TIOCNOTTY, NULL) < 0)
48 syswarn("cant disassociate from the terminal");
51 #endif /* defined(TIOCNOTTY) */
54 syswarn("cant chdir to %s", path);
56 fd = open("/dev/null", O_RDWR, 0);
58 dup2(fd, STDIN_FILENO);
59 dup2(fd, STDOUT_FILENO);
60 dup2(fd, STDERR_FILENO);