From: Noah Hilverling Date: Mon, 23 Oct 2017 09:01:42 +0000 (+0200) Subject: Process: Fix fork error handling X-Git-Tag: v2.8.0~30^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3e8f78e2cc64640fd0d5741a19d99e3a1934cc88;p=icinga2 Process: Fix fork error handling refs #5617 --- diff --git a/lib/base/process.cpp b/lib/base/process.cpp index 9f587bc12..44af34a02 100644 --- a/lib/base/process.cpp +++ b/lib/base/process.cpp @@ -138,11 +138,10 @@ static Value ProcessSpawnImpl(struct msghdr *msgh, const Dictionary::Ptr& reques pid_t pid = fork(); - if (pid < 0) { - BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("fork") - << boost::errinfo_errno(errno)); - } + int errorCode = 0; + + if (pid < 0) + errorCode = errno; if (pid == 0) { // child process @@ -203,6 +202,8 @@ static Value ProcessSpawnImpl(struct msghdr *msgh, const Dictionary::Ptr& reques Dictionary::Ptr response = new Dictionary(); response->Set("rc", pid); + if (errorCode) + response->Set("errno", errorCode); return response; } @@ -424,6 +425,10 @@ send_message: String jresponse = String(buf, buf + rc); Dictionary::Ptr response = JsonDecode(jresponse); + + if (response->Get("rc") == -1) + errno = response->Get("errno"); + return response->Get("rc"); } @@ -978,6 +983,11 @@ void Process::Run(const boost::function& callback) m_Process = ProcessSpawn(m_Arguments, m_ExtraEnvironment, m_AdjustPriority, fds); m_PID = m_Process; + if (m_PID == -1) { + m_OutputStream << "Fork failed with error code " << errno << " (" << Utility::FormatErrorNumber(errno) << ")"; + Log(LogCritical, "Process", m_OutputStream.str()); + } + Log(LogNotice, "Process") << "Running command " << PrettyPrintArguments(m_Arguments) << ": PID " << m_PID; @@ -1079,7 +1089,7 @@ bool Process::DoEvents(void) << "PID " << m_PID << " (" << PrettyPrintArguments(m_Arguments) << ") terminated with exit code " << exitcode; #else /* _WIN32 */ int status, exitcode; - if (could_not_kill) { + if (could_not_kill || m_PID == -1) { exitcode = 128; } else if (ProcessWaitPID(m_Process, &status) != m_Process) { exitcode = 128;