Commit ca4485a1 authored by Rusty Russell's avatar Rusty Russell

daemonize: set stderr to /dev/null.

parent 6835b78d
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
/* This code is based on Stevens Advanced Programming in the UNIX /* This code is based on Stevens Advanced Programming in the UNIX
* Environment. */ * Environment. */
...@@ -21,6 +22,14 @@ bool daemonize(void) ...@@ -21,6 +22,14 @@ bool daemonize(void)
close(STDOUT_FILENO); close(STDOUT_FILENO);
close(STDERR_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. */ /* Session leader so ^C doesn't whack us. */
setsid(); setsid();
/* Move off any mount points we might be in. */ /* Move off any mount points we might be in. */
......
...@@ -6,11 +6,12 @@ ...@@ -6,11 +6,12 @@
* daemonize - turn this process into a daemon. * daemonize - turn this process into a daemon.
* *
* This routine forks us off to become a daemon. It returns false on failure * 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: * Side effects for programmers to be aware of:
* - PID changes (our parent exits, we become child of init) * - 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 / * - Current working directory changes to /
* - Umask is set to 0. * - Umask is set to 0.
*/ */
......
...@@ -43,8 +43,12 @@ int main(int argc, char *argv[]) ...@@ -43,8 +43,12 @@ int main(int argc, char *argv[])
= read(STDIN_FILENO, buffer, 1) == -1 ? errno : 0; = read(STDIN_FILENO, buffer, 1) == -1 ? errno : 0;
daemonized.write_to_stdout daemonized.write_to_stdout
= write(STDOUT_FILENO, buffer, 1) == -1 ? errno : 0; = write(STDOUT_FILENO, buffer, 1) == -1 ? errno : 0;
daemonized.write_to_stderr if (write(STDERR_FILENO, buffer, 1) != 1) {
= write(STDERR_FILENO, buffer, 1) == -1 ? errno : 0; 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. */ /* Make sure parent exits. */
while (getppid() == pid) while (getppid() == pid)
...@@ -64,7 +68,7 @@ int main(int argc, char *argv[]) ...@@ -64,7 +68,7 @@ int main(int argc, char *argv[])
ok1(daemonized.in_root_dir); ok1(daemonized.in_root_dir);
ok1(daemonized.read_from_stdin == EBADF); ok1(daemonized.read_from_stdin == EBADF);
ok1(daemonized.write_to_stdout == EBADF); ok1(daemonized.write_to_stdout == EBADF);
ok1(daemonized.write_to_stderr == EBADF); ok1(daemonized.write_to_stderr == 0);
return exit_status(); return exit_status();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment