From 03efec27123efb43c3350ceaf5675fd68d8b3f50 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Wed, 6 Sep 2017 11:47:53 +0200 Subject: deamonize: restore detection of errors Keep forked environment for daemon more strick and check even for nearly impossible to happen errors. --- libdaemon/server/daemon-server.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'libdaemon') diff --git a/libdaemon/server/daemon-server.c b/libdaemon/server/daemon-server.c index 0950c2e16..2ffdf402d 100644 --- a/libdaemon/server/daemon-server.c +++ b/libdaemon/server/daemon-server.c @@ -377,15 +377,22 @@ static void _daemonise(daemon_state s) exit(WEXITSTATUS(child_status)); } - if (chdir("/")) + if (chdir("/")) { + perror("Cannot chdir to /"); exit(1); + } - (void) dup2(fd, STDIN_FILENO); - (void) dup2(fd, STDOUT_FILENO); - (void) dup2(fd, STDERR_FILENO); + if ((dup2(fd, STDIN_FILENO) == -1) || + (dup2(fd, STDOUT_FILENO) == -1) || + (dup2(fd, STDERR_FILENO) == -1)) { + perror("Error setting terminal FDs to /dev/null"); + exit(2); + } - if (fd > STDERR_FILENO) - (void) close(fd); + if ((fd > STDERR_FILENO) && close(fd)) { + perror("Failed to close /dev/null descriptor"); + exit(3); + } /* Switch to sysconf(_SC_OPEN_MAX) ?? */ if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) -- cgit v1.2.1