util: Add error handling to become_daemon()
authorMartin Schwenke <martin@meltin.net>
Tue, 15 Aug 2017 02:41:03 +0000 (12:41 +1000)
committerAndreas Schneider <asn@cryptomilk.org>
Thu, 17 Aug 2017 09:48:32 +0000 (11:48 +0200)
Log failure and exit if fork() or setsid() fails.

Leave the logic in the non-setsid() code as it is.  This is probably
meant to fall through on failure of either opening /dev/tty or
ioctl().  Documentation for the ioctl() failure case is far from
clear.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Andreas Schneider <asn@samba.org>
Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org>
Autobuild-Date(master): Thu Aug 17 11:48:32 CEST 2017 on sn-devel-144

lib/util/become_daemon.c

index 9574641..22c1778 100644 (file)
@@ -69,6 +69,9 @@ void become_daemon(bool do_fork, bool no_session, bool log_stdout)
        pid_t newpid;
        if (do_fork) {
                newpid = fork();
+               if (newpid == -1) {
+                       exit_daemon("Fork failed", errno);
+               }
                if (newpid) {
 #if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD)
                        sd_notifyf(0,
@@ -82,7 +85,12 @@ void become_daemon(bool do_fork, bool no_session, bool log_stdout)
 
        /* detach from the terminal */
 #ifdef HAVE_SETSID
-       if (!no_session) setsid();
+       if (!no_session) {
+               int ret = setsid();
+               if (ret == -1) {
+                       exit_daemon("Failed to create session", errno);
+               }
+       }
 #elif defined(TIOCNOTTY)
        if (!no_session) {
                int i = open("/dev/tty", O_RDWR, 0);