From: Gunnar Beutner Date: Fri, 7 Nov 2014 08:53:23 +0000 (+0100) Subject: Properly report errors when CreateProcess() fails X-Git-Tag: v2.2.0~100 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3cf08ad050bd4f9422b7e3b1be8beaba1c82b64e;p=icinga2 Properly report errors when CreateProcess() fails fixes #7599 --- diff --git a/lib/base/exception.hpp b/lib/base/exception.hpp index ab9ea1f70..431422bd5 100644 --- a/lib/base/exception.hpp +++ b/lib/base/exception.hpp @@ -24,6 +24,7 @@ #include "base/string.hpp" #include "base/stacktrace.hpp" #include "base/context.hpp" +#include "base/utility.hpp" #include #include #include @@ -97,26 +98,7 @@ typedef boost::error_info errinfo_win32_error; inline std::string to_string(const errinfo_win32_error& e) { - std::ostringstream tmp; - int code = e.value(); - - char *message; - String result = "Unknown error."; - - DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message, - 0, NULL); - - if (rc != 0) { - result = String(message); - LocalFree(message); - - /* remove trailing new-line characters */ - boost::algorithm::trim_right(result); - } - - tmp << code << ", \"" << result << "\""; - return tmp.str(); + return Utility::FormatErrorNumber(e.value()); } #endif /* _WIN32 */ diff --git a/lib/base/process.cpp b/lib/base/process.cpp index 5d368f4a7..68fdfa468 100644 --- a/lib/base/process.cpp +++ b/lib/base/process.cpp @@ -413,21 +413,30 @@ void Process::Run(const boost::function& callback) if (!CreateProcess(NULL, args, NULL, NULL, TRUE, EXTENDED_STARTUPINFO_PRESENT, envp, NULL, &si.StartupInfo, &pi)) { + DWORD error = GetLastError(); CloseHandle(outWritePipe); CloseHandle(outWritePipeDup); - delete args; free(envp); DeleteProcThreadAttributeList(lpAttributeList); delete [] reinterpret_cast(lpAttributeList); - BOOST_THROW_EXCEPTION(win32_error() - << boost::errinfo_api_function("CreateProcess") - << errinfo_win32_error(GetLastError())); + + m_Result.PID = 0; + m_Result.ExecutionEnd = Utility::GetTime(); + m_Result.ExitStatus = 127; + m_Result.Output = "Command " + String(args) + " failed to execute: " + Utility::FormatErrorNumber(error); + + delete [] args; + + if (callback) + Utility::QueueAsyncCallback(boost::bind(callback, m_Result)); + + return; } - delete args; + delete [] args; free(envp); DeleteProcThreadAttributeList(lpAttributeList); - delete[] reinterpret_cast(lpAttributeList); + delete [] reinterpret_cast(lpAttributeList); CloseHandle(outWritePipe); CloseHandle(outWritePipeDup);