]> git.ozlabs.org Git - ccan/blobdiff - ccan/daemonize/daemonize.c
tdb2: copy tdb1's changed expansion logic.
[ccan] / ccan / daemonize / daemonize.c
index ca4aafc8fa99c403b8396dd0d871d3840e536e46..3c0a53a8bfa11a25a6e0685dc4c7f7ed72a4ab10 100644 (file)
@@ -1,8 +1,10 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
 #include <ccan/daemonize/daemonize.h>
 #include <unistd.h>
 #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 +23,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. */