diff --git a/base/utility.cpp b/base/utility.cpp index 167f70310..f1bd7e970 100644 --- a/base/utility.cpp +++ b/base/utility.cpp @@ -10,39 +10,33 @@ using namespace icinga; void Utility::Daemonize(void) { #ifndef _WIN32 pid_t pid; - pid_t sid; int fd; pid = fork(); - if (pid == -1) { - return false; - } + if (pid < 0) + throw PosixException("fork failed", errno); if (pid) exit(0); fd = open("/dev/null", O_RDWR); - if (fd) { - if (fd != 0) { - dup2(fd, 0); - } - if (fd != 1) { - dup2(fd, 1); - } + if (fd < 0) + throw PosixException("open failed", errno); - if (fd != 2) { - dup2(fd, 2); - } + if (fd != 0) + dup2(fd, 0); - if (fd > 2) { - close(fd); - } - } + if (fd != 1) + dup2(fd, 1); - sid = setsid(); - if (sid == -1) { - return false; - } + if (fd != 2) + dup2(fd, 2); + + if (fd > 2) + close(fd); + + if (setsid() < 0) + throw PosixException("setsid failed", errno); #endif }