summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2004-03-15 15:36:38 +0000
committerLennart Poettering <lennart@poettering.net>2004-03-15 15:36:38 +0000
commit4c212cb85705c994399cca7b1c91212ec63384ab (patch)
treea9dea048c14db525c39917128e6ed358ded99a58
parenta1f50593b8de260a508c7860fd1bc06093c58f89 (diff)
add some mor forking magic
git-svn-id: file:///home/lennart/svn/public/libdaemon/trunk@62 153bfa13-eec0-0310-be40-b0cb6a0e1b4b
-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;