From 9468d0f41e37d505502f3faee736aaf3b44a17ff Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Tue, 15 Aug 2017 12:41:03 +1000 Subject: [PATCH] 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 --- lib/util/become_daemon.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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); -- 2.34.1