]> granicus.if.org Git - icinga2/commitdiff
Properly report errors when CreateProcess() fails
authorGunnar Beutner <gunnar@beutner.name>
Fri, 7 Nov 2014 08:53:23 +0000 (09:53 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Fri, 7 Nov 2014 09:01:44 +0000 (10:01 +0100)
fixes #7599

lib/base/exception.hpp
lib/base/process.cpp

index ab9ea1f707c7b86c16ecc95f05b011da2ef894aa..431422bd593d0ae956be4c81c4f2262dc42c55b9 100644 (file)
@@ -24,6 +24,7 @@
 #include "base/string.hpp"
 #include "base/stacktrace.hpp"
 #include "base/context.hpp"
+#include "base/utility.hpp"
 #include <sstream>
 #include <boost/exception/errinfo_api_function.hpp>
 #include <boost/exception/errinfo_errno.hpp>
@@ -97,26 +98,7 @@ typedef boost::error_info<struct errinfo_win32_error_, int> 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 */
 
index 5d368f4a7c706d05abe8d607a38f733e377f909b..68fdfa468b5902d020768ad88c1854d3a24aae95 100644 (file)
@@ -413,21 +413,30 @@ void Process::Run(const boost::function<void(const ProcessResult&)>& 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<char *>(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<char *>(lpAttributeList);
+       delete [] reinterpret_cast<char *>(lpAttributeList);
 
        CloseHandle(outWritePipe);
        CloseHandle(outWritePipeDup);