From af5ccae79673a2c2b474eb6bd8f6ddf6fd58773d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 11 Oct 2026 20:40:24 +0300 Subject: [PATCH] feat: sub-instance stdin/stdout descriptors --- src/interfaces/SystemProcess.hpp | 11 + src/logic/scripting/lua/libs/libapp.cpp | 2 +- src/util/platform.cpp | 272 ++++++++++++++++-------- src/util/platform.hpp | 4 +- 4 files changed, 203 insertions(+), 86 deletions(-) create mode 100644 src/interfaces/SystemProcess.hpp diff --git a/src/interfaces/SystemProcess.hpp b/src/interfaces/SystemProcess.hpp new file mode 100644 index 000000000..dffb2634b --- /dev/null +++ b/src/interfaces/SystemProcess.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "interfaces/Process.hpp" + +class SystemProcess : public Process { +public: + virtual ~SystemProcess() override = default; + + virtual int getStdinDescriptor() const = 0; + virtual int getStdoutDescriptor() const = 0; +}; diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index 0058fcada..e08c6d948 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -25,7 +25,7 @@ using namespace scripting; namespace { - static std::array, MAX_SUBPROCESSES> processes; + static std::array, MAX_SUBPROCESSES> processes; } /// @brief Check if content is loaded diff --git a/src/util/platform.cpp b/src/util/platform.cpp index 278fbaa1b..b76b9afc9 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -1,7 +1,6 @@ #include "platform.hpp" #include - #include #include #include @@ -11,6 +10,10 @@ #include "typedefs.hpp" #include "debug/Logger.hpp" #include "frontend/locale.hpp" +#include "interfaces/SystemProcess.hpp" +#include "logic/scripting/io_descriptors.hpp" +#include "io/fd_istream.hpp" +#include "io/fd_ostream.hpp" #ifdef _WIN32 #include @@ -71,23 +74,17 @@ void platform::sleep(size_t millis) { return tc.wPeriodMin; }(); - // Set the timer resolution to the minimum for the Sleep call timeBeginPeriod(periodMin); - - // Wait... Sleep(static_cast(millis)); - - // Reset the timer resolution back to the system default timeEndPeriod(periodMin); } int platform::get_process_id() { - return GetCurrentProcessId(); + return GetCurrentProcessId(); } bool platform::open_url(const std::string& url) { if (url.empty()) return false; - // UTF-8 → UTF-16 int wlen = MultiByteToWideChar(CP_UTF8, 0, url.c_str(), -1, nullptr, 0); if (wlen <= 0) return false; @@ -105,11 +102,10 @@ bool platform::open_url(const std::string& url) { void platform::configure_encoding() { } - + std::string platform::detect_locale() { const char* const programLocaleName = setlocale(LC_ALL, nullptr); - const char* const preferredLocaleName = - setlocale(LC_ALL, ""); // locale name format: ll_CC.encoding + const char* const preferredLocaleName = setlocale(LC_ALL, ""); if (programLocaleName && preferredLocaleName) { setlocale(LC_ALL, programLocaleName); @@ -224,32 +220,44 @@ std::filesystem::path platform::get_executable_path() { #endif } -class SystemProcess final : public Process { +class SystemProcessImpl final : public SystemProcess { public: #ifdef __linux__ - SystemProcess(pid_t pid) : pid(pid) { + SystemProcessImpl( + pid_t pid, int stdinDescriptor = -1, int stdoutDescriptor = -1 + ) + : pid(pid), + stdinDescriptor(stdinDescriptor), + stdoutDescriptor(stdoutDescriptor) { } - ~SystemProcess() { + ~SystemProcessImpl() { terminate(); } #elif defined(_WIN32) - SystemProcess(const PROCESS_INFORMATION& pi) - : processHandle(pi.hProcess), pid(pi.dwProcessId) { + SystemProcessImpl( + const PROCESS_INFORMATION& pi, + int stdinDescriptor = -1, + int stdoutDescriptor = -1 + ) + : processHandle(pi.hProcess), + pid(pi.dwProcessId), + stdinDescriptor(stdinDescriptor), + stdoutDescriptor(stdoutDescriptor) { if (pi.hThread != nullptr) { CloseHandle(pi.hThread); } } - ~SystemProcess() { + ~SystemProcessImpl() { terminate(); } #endif - SystemProcess(const SystemProcess&) = delete; - SystemProcess(SystemProcess&&) noexcept = default; - SystemProcess& operator=(const SystemProcess&) = delete; + SystemProcessImpl(const SystemProcessImpl&) = delete; + SystemProcessImpl(SystemProcessImpl&&) noexcept = default; + SystemProcessImpl& operator=(const SystemProcessImpl&) = delete; - SystemProcess& operator=(SystemProcess&& other) noexcept { + SystemProcessImpl& operator=(SystemProcessImpl&& other) noexcept { if (this != &other) { #ifdef __linux__ pid = other.pid; @@ -262,16 +270,36 @@ public: other.processHandle = nullptr; other.pid = 0; #endif + stdinDescriptor = other.stdinDescriptor; + stdoutDescriptor = other.stdoutDescriptor; + other.stdinDescriptor = -1; + other.stdoutDescriptor = -1; } return *this; } bool isActive() const override { #ifdef __linux__ - if (kill(pid, 0) == 0) { + if (pid <= 0) { + return false; + } + + // fixme: ugly ugly ugly + int status = 0; + pid_t result = waitpid(pid, &status, WNOHANG); + if (result == -1) { + if (errno == ECHILD) { + const_cast(this)->pid = 0; + return false; + } + return false; + } + if (result == 0) { return true; } - return errno != ESRCH; + + const_cast(this)->pid = 0; + return false; #elif defined(_WIN32) if (processHandle == nullptr) { return false; @@ -283,6 +311,7 @@ public: return false; #elif defined(__APPLE__) // FIXME: completely implement SystemProcess for MacOS +#else return false; #endif } @@ -291,7 +320,11 @@ public: void waitForEnd() override { #ifdef __linux__ + if (pid <= 0) { + return; + } waitpid(pid, nullptr, 0); + pid = 0; #elif defined(_WIN32) if (processHandle != nullptr) { WaitForSingleObject(processHandle, INFINITE); @@ -301,6 +334,9 @@ public: void terminate() final override { #ifdef __linux__ + if (pid <= 0) { + return; + } logger.info() << "terminating " << pid; kill(pid, SIGTERM); #elif defined(_WIN32) @@ -310,16 +346,29 @@ public: } #endif } + + int getStdinDescriptor() const override { + return stdinDescriptor; + } + + int getStdoutDescriptor() const override { + return stdoutDescriptor; + } + private: #ifdef __linux__ pid_t pid; + int stdinDescriptor; + int stdoutDescriptor; #elif defined(_WIN32) HANDLE processHandle = nullptr; DWORD pid = 0; + int stdinDescriptor; + int stdoutDescriptor; #endif }; -std::unique_ptr platform::new_engine_instance( +std::unique_ptr platform::new_engine_instance( const std::vector& args, std::filesystem::path outputFile, bool subProcess @@ -334,7 +383,7 @@ std::unique_ptr platform::new_engine_instance( } auto toWString = [](const std::string& src) -> std::wstring { - if (src.empty()) + if (src.empty()) return L""; int size = MultiByteToWideChar(CP_UTF8, 0, src.c_str(), -1, nullptr, 0); if (size == 0) { @@ -358,10 +407,81 @@ std::unique_ptr platform::new_engine_instance( auto commandString = toWString(ss.str()); STARTUPINFOW si = { sizeof(si) }; + PROCESS_INFORMATION pi = { 0 }; + + if (subProcess) { + SECURITY_ATTRIBUTES pipeAttributes{}; + pipeAttributes.nLength = sizeof(pipeAttributes); + pipeAttributes.bInheritHandle = TRUE; + + HANDLE stdinRead = nullptr; + HANDLE stdinWrite = nullptr; + HANDLE stdoutRead = nullptr; + HANDLE stdoutWrite = nullptr; + + if (!CreatePipe(&stdinRead, &stdinWrite, &pipeAttributes, 0) || + !CreatePipe(&stdoutRead, &stdoutWrite, &pipeAttributes, 0)) { + throw std::runtime_error( + "CreatePipe failed with code: " + std::to_string(GetLastError()) + ); + } + + if (!SetHandleInformation(stdinWrite, HANDLE_FLAG_INHERIT, 0) || + !SetHandleInformation(stdoutRead, HANDLE_FLAG_INHERIT, 0)) { + CloseHandle(stdinRead); + CloseHandle(stdinWrite); + CloseHandle(stdoutRead); + CloseHandle(stdoutWrite); + throw std::runtime_error( + "SetHandleInformation failed with code: " + std::to_string(GetLastError()) + ); + } + + si.dwFlags = STARTF_USESTDHANDLES; + si.hStdInput = stdinRead; + si.hStdOutput = stdoutWrite; + si.hStdError = stdoutWrite; + + const DWORD flags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS; + const BOOL success = CreateProcessW( + nullptr, + commandString.data(), + nullptr, + nullptr, + TRUE, + flags, + nullptr, + nullptr, + &si, + &pi + ); + + CloseHandle(stdinRead); + CloseHandle(stdoutWrite); + + if (!success) { + CloseHandle(stdinWrite); + CloseHandle(stdoutRead); + throw std::runtime_error( + "starting an engine instance failed with code: " + + std::to_string(GetLastError()) + ); + } + + int stdinDescriptor = scripting::io_descriptors::add_descriptor( + std::make_unique(stdinWrite) + ); + int stdoutDescriptor = scripting::io_descriptors::add_descriptor( + std::make_unique(stdoutRead) + ); + return std::make_unique(pi, stdinDescriptor, stdoutDescriptor); + } + if (!outputFile.empty()) { si.dwFlags |= STARTF_USESTDHANDLES; + const auto outputPath = toWString(outputFile.u8string()); si.hStdOutput = CreateFileW( - toWString(outputFile.empty() ? outputFile.u8string() : "NUL"s).data(), + outputPath.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, nullptr, @@ -371,15 +491,13 @@ std::unique_ptr platform::new_engine_instance( ); if (si.hStdOutput == INVALID_HANDLE_VALUE) { throw std::runtime_error( - "CreateFileW failed with code: " + - std::to_string(GetLastError()) + "CreateFileW failed with code: " + std::to_string(GetLastError()) ); } } - PROCESS_INFORMATION pi = { 0 }; - DWORD flags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS; - // | CREATE_NO_WINDOW; - BOOL success = CreateProcessW( + + const DWORD flags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS; + const BOOL success = CreateProcessW( nullptr, commandString.data(), nullptr, @@ -391,38 +509,6 @@ std::unique_ptr platform::new_engine_instance( &si, &pi ); - if (subProcess) { - HANDLE job = CreateJobObjectW(nullptr, nullptr); - - JOBOBJECT_EXTENDED_LIMIT_INFORMATION info{}; - info.BasicLimitInformation.LimitFlags = - JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; - - try { - if (!SetInformationJobObject( - job, - JobObjectExtendedLimitInformation, - &info, - sizeof(info))) { - throw std::runtime_error( - "SetInformationJobObject failed with code: " + - std::to_string(GetLastError()) - ); - } - if (!AssignProcessToJobObject(job, pi.hProcess)) { - throw std::runtime_error( - "SetInformationJobObject failed with code: " + - std::to_string(GetLastError()) - ); - } - return std::make_unique(pi); - } catch (const std::exception& err) { - CloseHandle(job); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - return nullptr; - } - } if (success) { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); @@ -433,29 +519,41 @@ std::unique_ptr platform::new_engine_instance( ); } #else - -// TODO: implement #ifndef __APPLE__ if (subProcess) { - pid_t pid = fork(); - - if (outputFile.empty()) { - outputFile = "/dev/null"; + int stdinPipe[2] = {-1, -1}; + int stdoutPipe[2] = {-1, -1}; + if (pipe(stdinPipe) != 0 || pipe(stdoutPipe) != 0) { + if (stdinPipe[0] >= 0) { + close(stdinPipe[0]); + } + if (stdinPipe[1] >= 0) { + close(stdinPipe[1]); + } + if (stdoutPipe[0] >= 0) { + close(stdoutPipe[0]); + } + if (stdoutPipe[1] >= 0) { + close(stdoutPipe[1]); + } + throw std::runtime_error("failed to create subprocess pipes"); } - int fd = open( - outputFile.string().c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644 - ); + + pid_t pid = fork(); if (pid == 0) { prctl(PR_SET_PDEATHSIG, SIGTERM); - if (getppid() == 1) { _exit(1); } - dup2(fd, STDOUT_FILENO); - dup2(fd, STDERR_FILENO); - close(fd); + close(stdinPipe[1]); + close(stdoutPipe[0]); + dup2(stdinPipe[0], STDIN_FILENO); + dup2(stdoutPipe[1], STDOUT_FILENO); + dup2(stdoutPipe[1], STDERR_FILENO); + close(stdinPipe[0]); + close(stdoutPipe[1]); std::vector argv(args.size() + 2); argv[0] = const_cast(executable.c_str()); @@ -464,15 +562,23 @@ std::unique_ptr platform::new_engine_instance( } argv[args.size() + 1] = nullptr; - prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); + prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); execvp(executable.c_str(), const_cast(argv.data())); - _exit(127); - } else { - close(fd); - usleep(1000); // waiting for execvp call - return std::make_unique(pid); } + + close(stdinPipe[0]); + close(stdoutPipe[1]); + + const int stdinDescriptorId = scripting::io_descriptors::add_descriptor( + std::make_unique(stdinPipe[1]) + ); + const int stdoutDescriptorId = scripting::io_descriptors::add_descriptor( + std::make_unique(stdoutPipe[0]) + ); + + usleep(1000); // waiting for execvp call + return std::make_unique(pid, stdinDescriptorId, stdoutDescriptorId); } #endif diff --git a/src/util/platform.hpp b/src/util/platform.hpp index 18dc052b6..457642fd3 100644 --- a/src/util/platform.hpp +++ b/src/util/platform.hpp @@ -5,7 +5,7 @@ #include #include -#include "interfaces/Process.hpp" +#include "interfaces/SystemProcess.hpp" namespace platform { void configure_encoding(); @@ -21,7 +21,7 @@ namespace platform { /// @brief Get current process running executable path std::filesystem::path get_executable_path(); /// @brief Run a separate engine instance with specified arguments - std::unique_ptr new_engine_instance( + std::unique_ptr new_engine_instance( const std::vector& args, std::filesystem::path outputFile, bool subProcess