summaryrefslogtreecommitdiffstats
path: root/src/dfork.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dfork.c')
-rw-r--r--src/dfork.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/dfork.c b/src/dfork.c
index 486a9fc..b9794d1 100644
--- a/src/dfork.c
+++ b/src/dfork.c
@@ -28,6 +28,7 @@
#include <stdlib.h>
#include <sys/wait.h>
#include <assert.h>
+#include <sys/ioctl.h>
#include "dfork.h"
#include "dnonblock.h"
@@ -186,27 +187,34 @@ pid_t daemon_fork(void) {
umask(0777);
chdir("/");
- if ((tty_fd = open("/dev/tty", O_RDWR|O_NOCTTY)) >= 0) {
- ioctl(tty_fd, TIOCNOTTY);
- close(tty_fd);
- }
-
if ((pid = fork()) < 0) { // Second fork
daemon_log(LOG_ERR, "Second fork() failed: %s", strerror(errno));
goto fail;
} else if (pid == 0) {
+ int tty_fd;
/* Second child */
- setsid();
+ if (daemon_log_use & DAEMON_LOG_AUTO)
+ daemon_log_use = DAEMON_LOG_SYSLOG;
+
+ signal(SIGTTOU, SIG_IGN);
+ signal(SIGTTIN, SIG_IGN);
+ signal(SIGTSTP, SIG_IGN);
+
+ setsid();
+ setpgrp();
+ if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) {
+ ioctl(tty_fd, TIOCNOTTY, (char*) 0);
+ close(tty_fd);
+ }
+
dpid = getpid();
if (atomic_write(pipe_fds[1], &dpid, sizeof(dpid)) != sizeof(dpid))
goto fail;
close(pipe_fds[1]);
- if (daemon_log_use & DAEMON_LOG_AUTO)
- daemon_log_use = DAEMON_LOG_SYSLOG;
return 0;