]> git.ozlabs.org Git - ccan/blobdiff - ccan/daemonize/daemonize.c
htable: increase density from 75% to 87.5%
[ccan] / ccan / daemonize / daemonize.c
index ff12bfce3f7d15dfcab1053f7a673cae23963125..ff5807240ae67a4417403c6118db11ca190782b6 100644 (file)
@@ -1,3 +1,4 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
 #include <ccan/daemonize/daemonize.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -5,7 +6,7 @@
 #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. */
 bool daemonize(void)
 {
@@ -14,8 +15,9 @@ bool daemonize(void)
        /* Separate from our parent via fork, so init inherits us. */
        if ((pid = fork()) < 0)
                return false;
+       /* use _exit() to avoid triggering atexit() processing */
        if (pid != 0)
-               exit(0);
+               _exit(0);
 
        /* Don't hold files open. */
        close(STDIN_FILENO);
@@ -31,7 +33,8 @@ bool daemonize(void)
        close(0);
 
        /* Session leader so ^C doesn't whack us. */
-       setsid();
+       if (setsid() == (pid_t)-1)
+               return false;
        /* Move off any mount points we might be in. */
        if (chdir("/") != 0)
                return false;