summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2006-01-16 16:19:28 +0000
committerMarcel Holtmann <marcel@holtmann.org>2006-01-16 16:19:28 +0000
commitfc073b9af90ff44efcbb318633d026e754f86907 (patch)
treec0405794f4a429e2bf8165cd5c55abf156638f72
parente9799abfdd2520e3adbd5c3a1d27a9e466361598 (diff)
Redirect stdin, stdout, stderr to /dev/null for daemon
-rw-r--r--sdpd/main.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/sdpd/main.c b/sdpd/main.c
index dd725c81..685ffa8b 100644
--- a/sdpd/main.c
+++ b/sdpd/main.c
@@ -30,6 +30,7 @@
#include <stdio.h>
#include <errno.h>
+#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
@@ -301,8 +302,16 @@ static int become_daemon(void)
return 0;
setsid();
}
- for (fd = 0; fd < 3; fd++)
- close(fd);
+
+ fd = open("/dev/null", O_RDWR);
+ if (fd != -1) {
+ dup2(fd, STDIN_FILENO);
+ dup2(fd, STDOUT_FILENO);
+ dup2(fd, STDERR_FILENO);
+
+ if (fd > STDERR_FILENO)
+ close(fd);
+ }
chdir("/");
return 1;