feat: sub-instance stdin/stdout descriptors

This commit is contained in:
MihailRis 2026-10-11 20:40:24 +03:00
parent 314f38c607
commit af5ccae796
4 changed files with 203 additions and 86 deletions

View file

@ -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;
};

View file

@ -25,7 +25,7 @@
using namespace scripting; using namespace scripting;
namespace { namespace {
static std::array<std::unique_ptr<Process>, MAX_SUBPROCESSES> processes; static std::array<std::unique_ptr<SystemProcess>, MAX_SUBPROCESSES> processes;
} }
/// @brief Check if content is loaded /// @brief Check if content is loaded

View file

@ -1,7 +1,6 @@
#include "platform.hpp" #include "platform.hpp"
#include <time.h> #include <time.h>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -11,6 +10,10 @@
#include "typedefs.hpp" #include "typedefs.hpp"
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "frontend/locale.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 #ifdef _WIN32
#include <Windows.h> #include <Windows.h>
@ -71,13 +74,8 @@ void platform::sleep(size_t millis) {
return tc.wPeriodMin; return tc.wPeriodMin;
}(); }();
// Set the timer resolution to the minimum for the Sleep call
timeBeginPeriod(periodMin); timeBeginPeriod(periodMin);
// Wait...
Sleep(static_cast<DWORD>(millis)); Sleep(static_cast<DWORD>(millis));
// Reset the timer resolution back to the system default
timeEndPeriod(periodMin); timeEndPeriod(periodMin);
} }
@ -87,7 +85,6 @@ int platform::get_process_id() {
bool platform::open_url(const std::string& url) { bool platform::open_url(const std::string& url) {
if (url.empty()) return false; if (url.empty()) return false;
// UTF-8 → UTF-16
int wlen = MultiByteToWideChar(CP_UTF8, 0, url.c_str(), -1, nullptr, 0); int wlen = MultiByteToWideChar(CP_UTF8, 0, url.c_str(), -1, nullptr, 0);
if (wlen <= 0) return false; if (wlen <= 0) return false;
@ -108,8 +105,7 @@ void platform::configure_encoding() {
std::string platform::detect_locale() { std::string platform::detect_locale() {
const char* const programLocaleName = setlocale(LC_ALL, nullptr); const char* const programLocaleName = setlocale(LC_ALL, nullptr);
const char* const preferredLocaleName = const char* const preferredLocaleName = setlocale(LC_ALL, "");
setlocale(LC_ALL, ""); // locale name format: ll_CC.encoding
if (programLocaleName && preferredLocaleName) { if (programLocaleName && preferredLocaleName) {
setlocale(LC_ALL, programLocaleName); setlocale(LC_ALL, programLocaleName);
@ -224,32 +220,44 @@ std::filesystem::path platform::get_executable_path() {
#endif #endif
} }
class SystemProcess final : public Process { class SystemProcessImpl final : public SystemProcess {
public: public:
#ifdef __linux__ #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(); terminate();
} }
#elif defined(_WIN32) #elif defined(_WIN32)
SystemProcess(const PROCESS_INFORMATION& pi) SystemProcessImpl(
: processHandle(pi.hProcess), pid(pi.dwProcessId) { const PROCESS_INFORMATION& pi,
int stdinDescriptor = -1,
int stdoutDescriptor = -1
)
: processHandle(pi.hProcess),
pid(pi.dwProcessId),
stdinDescriptor(stdinDescriptor),
stdoutDescriptor(stdoutDescriptor) {
if (pi.hThread != nullptr) { if (pi.hThread != nullptr) {
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
} }
} }
~SystemProcess() { ~SystemProcessImpl() {
terminate(); terminate();
} }
#endif #endif
SystemProcess(const SystemProcess&) = delete; SystemProcessImpl(const SystemProcessImpl&) = delete;
SystemProcess(SystemProcess&&) noexcept = default; SystemProcessImpl(SystemProcessImpl&&) noexcept = default;
SystemProcess& operator=(const SystemProcess&) = delete; SystemProcessImpl& operator=(const SystemProcessImpl&) = delete;
SystemProcess& operator=(SystemProcess&& other) noexcept { SystemProcessImpl& operator=(SystemProcessImpl&& other) noexcept {
if (this != &other) { if (this != &other) {
#ifdef __linux__ #ifdef __linux__
pid = other.pid; pid = other.pid;
@ -262,16 +270,36 @@ public:
other.processHandle = nullptr; other.processHandle = nullptr;
other.pid = 0; other.pid = 0;
#endif #endif
stdinDescriptor = other.stdinDescriptor;
stdoutDescriptor = other.stdoutDescriptor;
other.stdinDescriptor = -1;
other.stdoutDescriptor = -1;
} }
return *this; return *this;
} }
bool isActive() const override { bool isActive() const override {
#ifdef __linux__ #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<SystemProcessImpl*>(this)->pid = 0;
return false;
}
return false;
}
if (result == 0) {
return true; return true;
} }
return errno != ESRCH;
const_cast<SystemProcessImpl*>(this)->pid = 0;
return false;
#elif defined(_WIN32) #elif defined(_WIN32)
if (processHandle == nullptr) { if (processHandle == nullptr) {
return false; return false;
@ -283,6 +311,7 @@ public:
return false; return false;
#elif defined(__APPLE__) #elif defined(__APPLE__)
// FIXME: completely implement SystemProcess for MacOS // FIXME: completely implement SystemProcess for MacOS
#else
return false; return false;
#endif #endif
} }
@ -291,7 +320,11 @@ public:
void waitForEnd() override { void waitForEnd() override {
#ifdef __linux__ #ifdef __linux__
if (pid <= 0) {
return;
}
waitpid(pid, nullptr, 0); waitpid(pid, nullptr, 0);
pid = 0;
#elif defined(_WIN32) #elif defined(_WIN32)
if (processHandle != nullptr) { if (processHandle != nullptr) {
WaitForSingleObject(processHandle, INFINITE); WaitForSingleObject(processHandle, INFINITE);
@ -301,6 +334,9 @@ public:
void terminate() final override { void terminate() final override {
#ifdef __linux__ #ifdef __linux__
if (pid <= 0) {
return;
}
logger.info() << "terminating " << pid; logger.info() << "terminating " << pid;
kill(pid, SIGTERM); kill(pid, SIGTERM);
#elif defined(_WIN32) #elif defined(_WIN32)
@ -310,16 +346,29 @@ public:
} }
#endif #endif
} }
int getStdinDescriptor() const override {
return stdinDescriptor;
}
int getStdoutDescriptor() const override {
return stdoutDescriptor;
}
private: private:
#ifdef __linux__ #ifdef __linux__
pid_t pid; pid_t pid;
int stdinDescriptor;
int stdoutDescriptor;
#elif defined(_WIN32) #elif defined(_WIN32)
HANDLE processHandle = nullptr; HANDLE processHandle = nullptr;
DWORD pid = 0; DWORD pid = 0;
int stdinDescriptor;
int stdoutDescriptor;
#endif #endif
}; };
std::unique_ptr<Process> platform::new_engine_instance( std::unique_ptr<SystemProcess> platform::new_engine_instance(
const std::vector<std::string>& args, const std::vector<std::string>& args,
std::filesystem::path outputFile, std::filesystem::path outputFile,
bool subProcess bool subProcess
@ -358,10 +407,81 @@ std::unique_ptr<Process> platform::new_engine_instance(
auto commandString = toWString(ss.str()); auto commandString = toWString(ss.str());
STARTUPINFOW si = { sizeof(si) }; 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<fd_ostream>(stdinWrite)
);
int stdoutDescriptor = scripting::io_descriptors::add_descriptor(
std::make_unique<fd_istream>(stdoutRead)
);
return std::make_unique<SystemProcessImpl>(pi, stdinDescriptor, stdoutDescriptor);
}
if (!outputFile.empty()) { if (!outputFile.empty()) {
si.dwFlags |= STARTF_USESTDHANDLES; si.dwFlags |= STARTF_USESTDHANDLES;
const auto outputPath = toWString(outputFile.u8string());
si.hStdOutput = CreateFileW( si.hStdOutput = CreateFileW(
toWString(outputFile.empty() ? outputFile.u8string() : "NUL"s).data(), outputPath.c_str(),
GENERIC_WRITE, GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ, FILE_SHARE_WRITE | FILE_SHARE_READ,
nullptr, nullptr,
@ -371,15 +491,13 @@ std::unique_ptr<Process> platform::new_engine_instance(
); );
if (si.hStdOutput == INVALID_HANDLE_VALUE) { if (si.hStdOutput == INVALID_HANDLE_VALUE) {
throw std::runtime_error( throw std::runtime_error(
"CreateFileW failed with code: " + "CreateFileW failed with code: " + std::to_string(GetLastError())
std::to_string(GetLastError())
); );
} }
} }
PROCESS_INFORMATION pi = { 0 };
DWORD flags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS; const DWORD flags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS;
// | CREATE_NO_WINDOW; const BOOL success = CreateProcessW(
BOOL success = CreateProcessW(
nullptr, nullptr,
commandString.data(), commandString.data(),
nullptr, nullptr,
@ -391,38 +509,6 @@ std::unique_ptr<Process> platform::new_engine_instance(
&si, &si,
&pi &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<SystemProcess>(pi);
} catch (const std::exception& err) {
CloseHandle(job);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return nullptr;
}
}
if (success) { if (success) {
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
@ -433,29 +519,41 @@ std::unique_ptr<Process> platform::new_engine_instance(
); );
} }
#else #else
// TODO: implement
#ifndef __APPLE__ #ifndef __APPLE__
if (subProcess) { if (subProcess) {
pid_t pid = fork(); int stdinPipe[2] = {-1, -1};
int stdoutPipe[2] = {-1, -1};
if (outputFile.empty()) { if (pipe(stdinPipe) != 0 || pipe(stdoutPipe) != 0) {
outputFile = "/dev/null"; 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) { if (pid == 0) {
prctl(PR_SET_PDEATHSIG, SIGTERM); prctl(PR_SET_PDEATHSIG, SIGTERM);
if (getppid() == 1) { if (getppid() == 1) {
_exit(1); _exit(1);
} }
dup2(fd, STDOUT_FILENO); close(stdinPipe[1]);
dup2(fd, STDERR_FILENO); close(stdoutPipe[0]);
close(fd); dup2(stdinPipe[0], STDIN_FILENO);
dup2(stdoutPipe[1], STDOUT_FILENO);
dup2(stdoutPipe[1], STDERR_FILENO);
close(stdinPipe[0]);
close(stdoutPipe[1]);
std::vector<const char*> argv(args.size() + 2); std::vector<const char*> argv(args.size() + 2);
argv[0] = const_cast<char*>(executable.c_str()); argv[0] = const_cast<char*>(executable.c_str());
@ -466,13 +564,21 @@ std::unique_ptr<Process> platform::new_engine_instance(
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<char**>(argv.data())); execvp(executable.c_str(), const_cast<char**>(argv.data()));
_exit(127); _exit(127);
} else {
close(fd);
usleep(1000); // waiting for execvp call
return std::make_unique<SystemProcess>(pid);
} }
close(stdinPipe[0]);
close(stdoutPipe[1]);
const int stdinDescriptorId = scripting::io_descriptors::add_descriptor(
std::make_unique<fd_ostream>(stdinPipe[1])
);
const int stdoutDescriptorId = scripting::io_descriptors::add_descriptor(
std::make_unique<fd_istream>(stdoutPipe[0])
);
usleep(1000); // waiting for execvp call
return std::make_unique<SystemProcessImpl>(pid, stdinDescriptorId, stdoutDescriptorId);
} }
#endif #endif

View file

@ -5,7 +5,7 @@
#include <memory> #include <memory>
#include <filesystem> #include <filesystem>
#include "interfaces/Process.hpp" #include "interfaces/SystemProcess.hpp"
namespace platform { namespace platform {
void configure_encoding(); void configure_encoding();
@ -21,7 +21,7 @@ namespace platform {
/// @brief Get current process running executable path /// @brief Get current process running executable path
std::filesystem::path get_executable_path(); std::filesystem::path get_executable_path();
/// @brief Run a separate engine instance with specified arguments /// @brief Run a separate engine instance with specified arguments
std::unique_ptr<Process> new_engine_instance( std::unique_ptr<SystemProcess> new_engine_instance(
const std::vector<std::string>& args, const std::vector<std::string>& args,
std::filesystem::path outputFile, std::filesystem::path outputFile,
bool subProcess bool subProcess