Dep: Switch to boost process instead of old standalone version

This commit is contained in:
Shauren
2021-02-19 15:21:15 +01:00
committed by Ovahlord
parent 0299fa4fe1
commit fbffbbbe14
91 changed files with 63 additions and 4690 deletions

View File

@@ -60,7 +60,6 @@ target_include_directories(common
target_link_libraries(common
PRIVATE
process
trinity-core-interface
PUBLIC
boost

View File

@@ -19,13 +19,18 @@
#include "Errors.h"
#include "Log.h"
#include "Optional.h"
#include "Util.h"
#include <boost/algorithm/string/join.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/process.hpp>
#include <boost/process/args.hpp>
#include <boost/process/child.hpp>
#include <boost/process/env.hpp>
#include <boost/process/exe.hpp>
#include <boost/process/io.hpp>
#include <boost/process/pipe.hpp>
#include <boost/process/search_path.hpp>
using namespace boost::process;
using namespace boost::process::initializers;
using namespace boost::iostreams;
namespace Trinity
@@ -46,7 +51,10 @@ public:
std::streamsize write(char const* str, std::streamsize size)
{
callback_(std::string(str, size));
std::string consoleStr(str, size);
std::string utf8;
if (consoleToUtf8(consoleStr, utf8))
callback_(utf8);
return size;
}
};
@@ -60,62 +68,60 @@ auto MakeTCLogSink(T&& callback)
template<typename T>
static int CreateChildProcess(T waiter, std::string const& executable,
std::vector<std::string> const& args,
std::vector<std::string> const& argsVector,
std::string const& logger, std::string const& input,
bool secure)
{
auto outPipe = create_pipe();
auto errPipe = create_pipe();
Optional<file_descriptor_source> inputSource;
ipstream outStream;
ipstream errStream;
if (!secure)
{
TC_LOG_TRACE(logger, "Starting process \"%s\" with arguments: \"%s\".",
executable.c_str(), boost::algorithm::join(args, " ").c_str());
executable.c_str(), boost::algorithm::join(argsVector, " ").c_str());
}
// Start the child process
child c = [&]
child c = [&]()
{
if (!input.empty())
{
inputSource = file_descriptor_source(input);
// With binding stdin
return execute(run_exe(boost::filesystem::absolute(executable)),
set_args(args),
inherit_env(),
bind_stdin(*inputSource),
bind_stdout(file_descriptor_sink(outPipe.sink, close_handle)),
bind_stderr(file_descriptor_sink(errPipe.sink, close_handle)));
return child{
exe = boost::filesystem::absolute(executable).string(),
args = argsVector,
env = environment(boost::this_process::environment()),
std_in = input,
std_out = outStream,
std_err = errStream
};
}
else
{
// Without binding stdin
return execute(run_exe(boost::filesystem::absolute(executable)),
set_args(args),
inherit_env(),
bind_stdout(file_descriptor_sink(outPipe.sink, close_handle)),
bind_stderr(file_descriptor_sink(errPipe.sink, close_handle)));
return child{
exe = boost::filesystem::absolute(executable).string(),
args = argsVector,
env = environment(boost::this_process::environment()),
std_in = boost::process::close,
std_out = outStream,
std_err = errStream
};
}
}();
file_descriptor_source outFd(outPipe.source, close_handle);
file_descriptor_source errFd(errPipe.source, close_handle);
auto outInfo = MakeTCLogSink([&](std::string msg)
auto outInfo = MakeTCLogSink([&](std::string const& msg)
{
TC_LOG_INFO(logger, "%s", msg.c_str());
});
auto outError = MakeTCLogSink([&](std::string msg)
auto outError = MakeTCLogSink([&](std::string const& msg)
{
TC_LOG_ERROR(logger, "%s", msg.c_str());
});
copy(outFd, outInfo);
copy(errFd, outError);
copy(outStream, outInfo);
copy(errStream, outError);
// Call the waiter in the current scope to prevent
// the streams from closing too early on leaving the scope.
@@ -127,9 +133,6 @@ static int CreateChildProcess(T waiter, std::string const& executable,
executable.c_str(), result);
}
if (inputSource)
inputSource->close();
return result;
}
@@ -140,7 +143,8 @@ int StartProcess(std::string const& executable, std::vector<std::string> const&
{
try
{
return wait_for_exit(c);
c.wait();
return c.exit_code();
}
catch (...)
{
@@ -188,7 +192,8 @@ public:
try
{
result = wait_for_exit(c);
c.wait();
result = c.exit_code();
}
catch (...)
{
@@ -217,12 +222,12 @@ public:
/// Tries to terminate the process
void Terminate() override
{
if (!my_child)
if (my_child)
{
was_terminated = true;
try
{
terminate(my_child->get());
my_child->get().terminate();
}
catch(...)
{
@@ -247,7 +252,7 @@ std::string SearchExecutableInPath(std::string const& filename)
{
try
{
return search_path(filename);
return search_path(filename).string();
}
catch (...)
{

View File

@@ -371,7 +371,7 @@ bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr)
return true;
}
bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str)
bool WStrToUtf8(wchar_t const* wstr, size_t size, std::string& utf8str)
{
try
{

View File

@@ -114,7 +114,7 @@ inline bool Utf8toWStr(const std::string& utf8str, wchar_t* wstr, size_t& wsize)
TC_COMMON_API bool WStrToUtf8(std::wstring const& wstr, std::string& utf8str);
// size==real string size
TC_COMMON_API bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str);
TC_COMMON_API bool WStrToUtf8(wchar_t const* wstr, size_t size, std::string& utf8str);
// set string to "" if invalid utf8 sequence
TC_COMMON_API size_t utf8length(std::string& utf8str);