]> git.ozlabs.org Git - ccan/commitdiff
daemonize: set stderr to /dev/null.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 6 Jan 2011 01:54:05 +0000 (12:24 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 6 Jan 2011 01:54:05 +0000 (12:24 +1030)
ccan/daemonize/daemonize.c
ccan/daemonize/daemonize.h
ccan/daemonize/test/run.c

index ca4aafc8fa99c403b8396dd0d871d3840e536e46..ff12bfce3f7d15dfcab1053f7a673cae23963125 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.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. */
 
 /* 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);
 
        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. */
        /* Session leader so ^C doesn't whack us. */
        setsid();
        /* Move off any mount points we might be in. */
index cc425a49288ea7c65f5ac91a2e874a5bff1c52be..abff7cca3a8787673f54c6ff22d34d2da27edbbd 100644 (file)
@@ -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
  * 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)
  *
  * 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.
  */
  *  - Current working directory changes to /
  *  - Umask is set to 0.
  */
index c86f214bcae6e3fcfb2901e436971471e21e2479..9bb966da240b722e33dd29f9ea4cfc399f159b5d 100644 (file)
@@ -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;
                        = 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)
 
                /* 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.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();
 }
 
        return exit_status();
 }