diff options
author | Shauren <shauren.trinity@gmail.com> | 2024-09-23 14:17:33 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2025-04-13 19:05:27 +0200 |
commit | 9c22e1df93b36beb4b880c0276873d5d102ddfce (patch) | |
tree | 8a2ddc2bcd2601ba81d06c6b303368d4a5d06d27 /src/common | |
parent | 25be3cdc3b2b8d3410800c0934a697f00e264394 (diff) |
Core/Common: Catch and log child process creation errors
(cherry picked from commit fff12eafdf971e88ff9b3abe2c6e0a62e050b450)
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Utilities/StartProcess.cpp | 42 |
1 files changed, 29 insertions, 13 deletions
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 <boost/process/args.hpp> #include <boost/process/child.hpp> #include <boost/process/env.hpp> +#include <boost/process/error.hpp> #include <boost/process/exe.hpp> #include <boost/process/io.hpp> #include <boost/process/pipe.hpp> #include <boost/process/search_path.hpp> +#undef BOOST_ALLOW_DEPRECATED_HEADERS +#endif #include <fmt/ranges.h> 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<FILE>(!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<void> 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<std::string> args, +int32 StartProcess(std::string executable, std::vector<std::string> args, std::string logger, std::string input_file, bool secure) { AsyncProcessResultImplementation handle( |