From: Martin Schwenke Date: Tue, 15 Aug 2017 02:41:03 +0000 (+1000) Subject: util: Add error handling to become_daemon() X-Git-Tag: tdb-1.3.15~66 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9468d0f41e37d505502f3faee736aaf3b44a17ff;p=thirdparty%2Fsamba.git util: Add error handling to become_daemon() 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 Reviewed-by: Andreas Schneider Autobuild-User(master): Andreas Schneider Autobuild-Date(master): Thu Aug 17 11:48:32 CEST 2017 on sn-devel-144 --- diff --git a/lib/util/become_daemon.c b/lib/util/become_daemon.c index 957464148ff..22c1778d4a4 100644 --- a/lib/util/become_daemon.c +++ b/lib/util/become_daemon.c @@ -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);