10 #include <sys/types.h>
13 #if defined BSD || defined __APPLE__
15 #include <sys/ioctl.h>
23 PRINT_DEBUG(
"Daemonizing...");
44 PRINT_DEBUG(
"Started by init, skipping some daemon setup");
45 goto started_with_init;
50 signal(SIGTTOU, SIG_IGN);
54 signal(SIGTTIN, SIG_IGN);
58 signal(SIGTSTP, SIG_IGN);
63 printf(
"forking...\n");
71 #if defined BSD || defined __APPLE__
72 PRINT_DEBUG(
"Using BSD daemon proccess");
75 if((fd = open(
"/dev/tty", O_RDWR)) >= 0) {
78 ioctl(fd, TIOCNOTTY, 0);
84 PRINT_ERROR(
"Could not dissassociate from controlling terminal: openning /dev/tty failed.");
88 PRINT_DEBUG(
"Using UNIX daemon proccess");
94 signal(SIGHUP,SIG_IGN);
118 PRINT_DEBUG(
"Closing all open descriptors");
119 max_open_fds = sysconf(_SC_OPEN_MAX);
120 for(fd = 0; fd < max_open_fds; fd++) {
127 if(-1 == chdir(
"/")) {
128 PRINT_ERROR(
"Failed to change to a safe directory");
135 PRINT_DEBUG(
"Now running in daemon mode");