X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fdaemonize%2Fdaemonize.c;h=ff12bfce3f7d15dfcab1053f7a673cae23963125;hb=74b0109ebd2a30d57d19ae9a56f16c0f3b3637eb;hp=861b0fc79cb961d5754899c23b31f0532f8edd32;hpb=75a2f5a4b855ba9efd9e6de954eb35fda633909e;p=ccan diff --git a/ccan/daemonize/daemonize.c b/ccan/daemonize/daemonize.c index 861b0fc7..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,10 +22,20 @@ 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. */ - chdir("/"); + if (chdir("/") != 0) + return false; + /* Discard our parent's old-fashioned umask prejudices. */ umask(0); return true;