From 9c22e1df93b36beb4b880c0276873d5d102ddfce Mon Sep 17 00:00:00 2001 From: Shauren Date: Mon, 23 Sep 2024 14:17:33 +0200 Subject: Core/Common: Catch and log child process creation errors (cherry picked from commit fff12eafdf971e88ff9b3abe2c6e0a62e050b450) --- src/common/Utilities/StartProcess.cpp | 42 ++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) (limited to 'src/common/Utilities/StartProcess.cpp') diff --git a/src/common/Utilities/StartProcess.cpp b/src/common/Utilities/StartProcess.cpp index 26d5f876abc..b3d79cde516 100644 --- a/src/common/Utilities/StartProcess.cpp +++ b/src/common/Utilities/StartProcess.cpp @@ -19,13 +19,18 @@ #include "Errors.h" #include "Log.h" #include "Optional.h" +#ifndef BOOST_ALLOW_DEPRECATED_HEADERS +#define BOOST_ALLOW_DEPRECATED_HEADERS #include #include #include +#include #include #include #include #include +#undef BOOST_ALLOW_DEPRECATED_HEADERS +#endif #include namespace bp = boost::process; @@ -61,7 +66,7 @@ public: ~AsyncProcessResultImplementation() = default; - int StartProcess() + int32 StartProcess() { ASSERT(!my_child, "Process started already!"); @@ -83,15 +88,22 @@ public: #pragma warning(pop) #endif - if (!is_secure) + if (is_secure) + { + TC_LOG_TRACE(logger, R"(Starting process "{}".)", + executable); + } + else { - TC_LOG_TRACE(logger, "Starting process \"{}\" with arguments: \"{}\".", + TC_LOG_TRACE(logger, R"(Starting process "{}" with arguments: "{}".)", executable, fmt::join(args, " ")); } // prepare file with only read permission (boost process opens with read_write) auto inputFile = std::shared_ptr(!input_file.empty() ? fopen(input_file.c_str(), "rb") : nullptr, [](FILE* f) { if (f) fclose(f); }); + std::error_code ec; + // Start the child process if (inputFile) { @@ -101,7 +113,8 @@ public: bp::env = bp::environment(boost::this_process::environment()), bp::std_in = inputFile.get(), bp::std_out = outStream, - bp::std_err = errStream + bp::std_err = errStream, + bp::error = ec ); } else @@ -110,12 +123,19 @@ public: bp::exe = boost::filesystem::absolute(executable).string(), bp::args = args, bp::env = bp::environment(boost::this_process::environment()), - bp::std_in = boost::process::close, + bp::std_in = bp::close, bp::std_out = outStream, - bp::std_err = errStream + bp::std_err = errStream, + bp::error = ec ); } + if (ec) + { + TC_LOG_ERROR(logger, R"(>> Failed to start process "{}": {})", executable, ec.message()); + return EXIT_FAILURE; + } + std::future stdOutReader = std::async(std::launch::async, [&] { std::string line; @@ -138,7 +158,6 @@ public: } }); - std::error_code ec; my_child->wait(ec); int32 const result = !ec && !was_terminated ? my_child->exit_code() : EXIT_FAILURE; my_child.reset(); @@ -146,11 +165,8 @@ public: stdOutReader.wait(); stdErrReader.wait(); - if (!is_secure) - { - TC_LOG_TRACE(logger, ">> Process \"{}\" finished with return value {}.", - executable, result); - } + TC_LOG_TRACE(logger, R"(>> Process "{}" finished with return value {}.)", + executable, result); return result; } @@ -180,7 +196,7 @@ public: } }; -int StartProcess(std::string executable, std::vector args, +int32 StartProcess(std::string executable, std::vector args, std::string logger, std::string input_file, bool secure) { AsyncProcessResultImplementation handle( -- cgit v1.2.3