summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-01-06 12:24:05 +1030
committerRusty Russell <rusty@rustcorp.com.au>2011-01-06 12:24:05 +1030
commitca4485a1cd6ed331aa9a52662a00de81a9209fd6 (patch)
tree092eb5743c0cc4f6e4849838892fad315c2ff127
parent6835b78d7de1644607ad28b39286dc3090b51fe3 (diff)
daemonize: set stderr to /dev/null.
-rw-r--r--ccan/daemonize/daemonize.c9
-rw-r--r--ccan/daemonize/daemonize.h5
-rw-r--r--ccan/daemonize/test/run.c10
3 files changed, 19 insertions, 5 deletions
diff --git a/ccan/daemonize/daemonize.c b/ccan/daemonize/daemonize.c
index ca4aafc8..ff12bfce 100644
--- a/ccan/daemonize/daemonize.c
+++ b/ccan/daemonize/daemonize.c
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <fcntl.h>
/* This code is based on Stevens Advanced Programming in the UNIX
* Environment. */
@@ -21,6 +22,14 @@ bool daemonize(void)
close(STDOUT_FILENO);
close(STDERR_FILENO);
+ /* Many routines write to stderr; that can cause chaos if used
+ * for something else, so set it here. */
+ if (open("/dev/null", O_WRONLY) != 0)
+ return false;
+ if (dup2(0, STDERR_FILENO) != STDERR_FILENO)
+ return false;
+ close(0);
+
/* Session leader so ^C doesn't whack us. */
setsid();
/* Move off any mount points we might be in. */
diff --git a/ccan/daemonize/daemonize.h b/ccan/daemonize/daemonize.h
index cc425a49..abff7cca 100644
--- a/ccan/daemonize/daemonize.h
+++ b/ccan/daemonize/daemonize.h
@@ -6,11 +6,12 @@
* daemonize - turn this process into a daemon.
*
* This routine forks us off to become a daemon. It returns false on failure
- * (ie. fork() failed) and sets errno.
+ * (eg. fork(), chdir or open failed) and sets errno.
*
* Side effects for programmers to be aware of:
* - PID changes (our parent exits, we become child of init)
- * - stdin, stdout and stderr file descriptors are closed
+ * - stdin and stdout file descriptors are closed
+ * - stderr is reopened to /dev/null so you don't reuse it
* - Current working directory changes to /
* - Umask is set to 0.
*/
diff --git a/ccan/daemonize/test/run.c b/ccan/daemonize/test/run.c
index c86f214b..9bb966da 100644
--- a/ccan/daemonize/test/run.c
+++ b/ccan/daemonize/test/run.c
@@ -43,8 +43,12 @@ int main(int argc, char *argv[])
= read(STDIN_FILENO, buffer, 1) == -1 ? errno : 0;
daemonized.write_to_stdout
= write(STDOUT_FILENO, buffer, 1) == -1 ? errno : 0;
- daemonized.write_to_stderr
- = write(STDERR_FILENO, buffer, 1) == -1 ? errno : 0;
+ if (write(STDERR_FILENO, buffer, 1) != 1) {
+ daemonized.write_to_stderr = errno;
+ if (daemonized.write_to_stderr == 0)
+ daemonized.write_to_stderr = -1;
+ } else
+ daemonized.write_to_stderr = 0;
/* Make sure parent exits. */
while (getppid() == pid)
@@ -64,7 +68,7 @@ int main(int argc, char *argv[])
ok1(daemonized.in_root_dir);
ok1(daemonized.read_from_stdin == EBADF);
ok1(daemonized.write_to_stdout == EBADF);
- ok1(daemonized.write_to_stderr == EBADF);
+ ok1(daemonized.write_to_stderr == 0);
return exit_status();
}