From: Rusty Russell Date: Thu, 6 Jan 2011 01:54:05 +0000 (+1030) Subject: daemonize: set stderr to /dev/null. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=ca4485a1cd6ed331aa9a52662a00de81a9209fd6 daemonize: set stderr to /dev/null. --- 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 #include #include +#include /* 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(); }