Merge pull request #1031 from MihailRis/subinstance-interaction

sub-instances interaction
This commit is contained in:
MihailRis 2026-10-12 22:09:37 +03:00 • committed by GitHub
commit c3271b0632
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 749 additions and 142 deletions

View file

@ -0,0 +1,31 @@
app.create_memory_device("memtest")
local script_path = "config:subprocess.lua"
local log_path = "config:subprocess.log"
file.write(script_path, [[
debug.log("hello from sub-instance")
sleep(1)
]])
local handle = app.start_background_instance(script_path, log_path)
assert(handle >= 0)
local stdin = app.get_stdin_stream(handle)
assert(stdin ~= nil)
stdin:write_line("sub_instance_test")
stdin:flush()
app.sleep_until(function() return not app.is_instance_alive(handle) end)
assert(not app.is_instance_alive(handle))
local stdout = app.get_stdout_stream(handle)
assert(stdout ~= nil)
local output = table.concat(table.map(stdout:read(1e6), function(_, line) return "<sub>"..line end), '\n')
print(output)
assert(output and output:find("hello from sub-instance", 1, true) ~= nil)
assert(output and output:find('stdin-cmd] unknown command "sub_instance_test"', 1, true) ~= nil)
stdout:close()
file.remove(script_path)
assert(not app.terminate_instance(handle))

View file

@ -27,6 +27,7 @@ local function tb_frame_tostring(frame)
end
local __vc__app_script_coroutine
local __vc_process_streams = { stdin = {}, stdout = {} }
local function complete_app_lib(app)
local __app_load_content = app.load_content
@ -35,6 +36,45 @@ local function complete_app_lib(app)
local __app_tick = coroutine.yield
local __app_set_setting = app.set_setting
local __app_quit = app.quit
local __app_get_stdout_stream = app.get_stdout_stream
local __app_get_stdin_stream = app.get_stdin_stream
local io_stream = require "core:io_stream"
local stdio_stream_lib = {
read = file.__read_descriptor,
write = file.__write_descriptor,
seek = file.__seek_descriptor,
tell = file.__tell_descriptor,
flush = file.__flush_descriptor,
available = file.__available_descriptor,
is_alive = file.__has_descriptor,
close = file.__close_descriptor,
}
local function get_process_stream(kind, handle, getter)
local cache = __vc_process_streams[kind]
local stream = cache[handle]
if stream and stream:is_alive() then
return stream
end
cache[handle] = nil
local descriptor = getter(handle)
if descriptor == nil then
return nil
end
stream = io_stream.new(descriptor, false, stdio_stream_lib)
cache[handle] = stream
return stream
end
app.get_stdout_stream = function(handle)
return get_process_stream("stdout", handle, __app_get_stdout_stream)
end
app.get_stdin_stream = function(handle)
return get_process_stream("stdin", handle, __app_get_stdin_stream)
end
app.sleep = sleep
app.script = __VC_SCRIPT_NAME

View file

@ -28,6 +28,7 @@ void cmd::start_stdin_cmd_reader(Engine& engine) {
if (line.empty()) {
continue;
}
logger.debug() << "input received: " << line;
engine.postRunnable([line, &interpreter] () {
try {
auto result = interpreter.execute(line);

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

132
src/io/fd_istream.hpp Normal file
View file

@ -0,0 +1,132 @@
#pragma once
#include <array>
#include <istream>
#include <streambuf>
#include <vector>
#ifdef _WIN32
#include <Windows.h>
#endif
#ifndef _WIN32
#include <unistd.h>
#endif
class fd_istreambuf final : public std::streambuf {
static constexpr size_t BUFFER_SIZE = 4096;
public:
#ifdef _WIN32
explicit fd_istreambuf(HANDLE handle)
: handle(handle) {
setg(buffer.data(), buffer.data(), buffer.data());
}
~fd_istreambuf() override {
if (handle != nullptr && handle != INVALID_HANDLE_VALUE) {
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
}
}
protected:
int_type underflow() override {
if (handle == nullptr || handle == INVALID_HANDLE_VALUE) {
return traits_type::eof();
}
if (gptr() != egptr()) {
return traits_type::to_int_type(*gptr());
}
std::array<char, BUFFER_SIZE> chunk {};
DWORD readCount = 0;
if (!ReadFile(
handle,
chunk.data(),
static_cast<DWORD>(chunk.size()),
&readCount,
nullptr
) ||
readCount == 0) {
return traits_type::eof();
}
buffer.assign(chunk.begin(), chunk.begin() + readCount);
setg(buffer.data(), buffer.data(), buffer.data() + buffer.size());
return traits_type::to_int_type(*gptr());
}
pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode)
override {
return pos_type(off_type(-1));
}
pos_type seekpos(pos_type, std::ios_base::openmode) override {
return pos_type(off_type(-1));
}
private:
HANDLE handle;
std::vector<char> buffer;
#else
explicit fd_istreambuf(int fd)
: fd(fd) {
setg(buffer.data(), buffer.data(), buffer.data());
}
~fd_istreambuf() override {
if (fd >= 0) {
::close(fd);
fd = -1;
}
}
protected:
int_type underflow() override {
if (fd < 0) {
return traits_type::eof();
}
if (gptr() != egptr()) {
return traits_type::to_int_type(*gptr());
}
buffer.resize(BUFFER_SIZE);
const auto count = ::read(fd, buffer.data(), buffer.size());
if (count <= 0) {
return traits_type::eof();
}
buffer.resize(static_cast<size_t>(count));
setg(buffer.data(), buffer.data(), buffer.data() + buffer.size());
return traits_type::to_int_type(*gptr());
}
pos_type seekoff(
off_type,
std::ios_base::seekdir,
std::ios_base::openmode
) override {
return pos_type(off_type(-1));
}
pos_type seekpos(pos_type, std::ios_base::openmode) override {
return pos_type(off_type(-1));
}
private:
int fd;
std::vector<char> buffer;
#endif
};
class fd_istream final : public std::istream {
public:
#ifdef _WIN32
explicit fd_istream(HANDLE handle)
: std::istream(&buf), buf(handle) {}
#else
explicit fd_istream(int fd)
: std::istream(&buf), buf(fd) {}
#endif
private:
fd_istreambuf buf;
};

158
src/io/fd_ostream.hpp Normal file
View file

@ -0,0 +1,158 @@
#pragma once
#include <array>
#include <iostream>
#include <streambuf>
#ifdef _WIN32
#include <Windows.h>
#endif
#ifndef _WIN32
#include <unistd.h>
#endif
class fd_ostreambuf final : public std::streambuf {
static constexpr size_t BUFFER_SIZE = 4096;
public:
#ifdef _WIN32
explicit fd_ostreambuf(HANDLE handle) : handle(handle) {
setp(buffer.data(), buffer.data() + buffer.size());
}
~fd_ostreambuf() override {
fd_ostreambuf::sync();
if (handle != nullptr && handle != INVALID_HANDLE_VALUE) {
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
}
}
protected:
int_type overflow(int_type ch) override {
if (ch == traits_type::eof()) {
return traits_type::not_eof(ch);
}
if (pptr() == epptr()) {
if (sync() != 0) {
return traits_type::eof();
}
}
*pptr() = traits_type::to_char_type(ch);
pbump(1);
return ch;
}
int sync() override {
if (handle == nullptr || handle == INVALID_HANDLE_VALUE) {
return -1;
}
const auto size = static_cast<size_t>(pptr() - pbase());
if (size == 0) {
return 0;
}
DWORD written = 0;
if (!WriteFile(
handle, pbase(), static_cast<DWORD>(size), &written, nullptr
)) {
return -1;
}
setp(buffer.data(), buffer.data() + buffer.size());
return 0;
}
pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode)
override {
return pos_type(off_type(-1));
}
pos_type seekpos(pos_type, std::ios_base::openmode) override {
return pos_type(off_type(-1));
}
private:
HANDLE handle;
std::array<char, BUFFER_SIZE> buffer {};
#else
explicit fd_ostreambuf(int fd) : fd(fd) {
setp(buffer.data(), buffer.data() + buffer.size());
}
~fd_ostreambuf() override {
fd_ostreambuf::sync();
if (fd >= 0) {
::close(fd);
fd = -1;
}
}
protected:
int_type overflow(int_type ch) override {
if (ch == traits_type::eof()) {
return traits_type::not_eof(ch);
}
if (pptr() == epptr()) {
if (sync() != 0) {
return traits_type::eof();
}
}
*pptr() = traits_type::to_char_type(ch);
pbump(1);
return ch;
}
int sync() override {
if (fd < 0) {
return -1;
}
const auto size = static_cast<size_t>(pptr() - pbase());
if (size == 0) {
return 0;
}
char* ptr = pbase();
size_t remaining = size;
while (remaining > 0) {
const auto written = ::write(fd, ptr, remaining);
if (written < 0) {
if (errno == EINTR) {
continue;
}
return -1;
}
ptr += written;
remaining -= static_cast<size_t>(written);
}
setp(buffer.data(), buffer.data() + buffer.size());
return 0;
}
pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode)
override {
return pos_type(off_type(-1));
}
pos_type seekpos(pos_type, std::ios_base::openmode) override {
return pos_type(off_type(-1));
}
private:
int fd;
std::array<char, BUFFER_SIZE> buffer {};
#endif
};
class fd_ostream final : public std::ostream {
public:
#ifdef _WIN32
explicit fd_ostream(HANDLE handle) : std::ostream(&buf), buf(handle) {
}
#else
explicit fd_ostream(int fd) : std::ostream(&buf), buf(fd) {
}
#endif
private:
fd_ostreambuf buf;
};

View file

@ -19,6 +19,18 @@ namespace {
std::unique_ptr<std::ostream> out;
};
std::vector<std::optional<StreamDescriptor>> descriptors;
int add_descriptor(StreamDescriptor descriptor) {
for (int i = 0; i < static_cast<int>(descriptors.size()); ++i) {
if (!descriptors[i].has_value()) {
descriptors[i] = std::move(descriptor);
return i;
}
}
descriptors.emplace_back(std::move(descriptor));
return static_cast<int>(descriptors.size() - 1);
}
}
std::istream* io_descriptors::get_input(int id) {
@ -121,6 +133,18 @@ void io_descriptors::close(int id) {
::descriptors[id] = std::nullopt;
}
int io_descriptors::add_descriptor(std::unique_ptr<std::istream> stream) {
return add_descriptor(StreamDescriptor {
std::move(stream), nullptr
});
}
int io_descriptors::add_descriptor(std::unique_ptr<std::ostream> stream) {
return add_descriptor(StreamDescriptor {
nullptr, std::move(stream)
});
}
int io_descriptors::open_descriptor(const io::path& path, bool write, bool read) {
std::unique_ptr<std::istream> in;
std::unique_ptr<std::ostream> out;
@ -138,15 +162,9 @@ int io_descriptors::open_descriptor(const io::path& path, bool write, bool read)
return -1;
}
for (int i = 0; i < static_cast<int>(descriptors.size()); ++i) {
if (!descriptors[i].has_value()) {
descriptors[i] = StreamDescriptor{ std::move(in), std::move(out) };
return i;
}
}
::descriptors.emplace_back(StreamDescriptor{ std::move(in), std::move(out) });
return static_cast<int>(::descriptors.size() - 1);
return add_descriptor(StreamDescriptor{
std::move(in), std::move(out)
});
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <istream>
#include <memory>
#include <ostream>
#include "io/fwd.hpp"
@ -22,6 +23,8 @@ namespace scripting::io_descriptors {
void close(int id);
int open_descriptor(const io::path& path, bool write, bool read);
int add_descriptor(std::unique_ptr<std::istream> stream);
int add_descriptor(std::unique_ptr<std::ostream> stream);
void close_all_descriptors();
}

View file

@ -21,11 +21,13 @@
#include "world/Level.hpp"
#include <array>
#include <unordered_map>
using namespace scripting;
namespace {
static std::array<std::unique_ptr<Process>, MAX_SUBPROCESSES> processes;
static std::unordered_map<int, std::unique_ptr<SystemProcess>> processes;
static int next_process_id = 1;
}
/// @brief Check if content is loaded
@ -429,6 +431,7 @@ static int l_start_background_instance(lua::State* L) {
"--sub-depth", std::to_string(engine->getCoreParameters()
.subProcessDepth + 1),
"--log", io::resolve(outputPath).u8string(),
"--stdin-cmd",
};
args.emplace_back("--project");
args.emplace_back(io::resolve(engine->getProject().path).u8string());
@ -444,25 +447,17 @@ static int l_start_background_instance(lua::State* L) {
}
}
int handle = -1;
for (int i = 0; i < ::processes.size(); i++) {
if (!::processes[i] || !::processes[i]->isActive()) {
handle = i;
break;
}
}
if (handle == -1) {
if (::processes.size() >= MAX_SUBPROCESSES) {
throw std::runtime_error("sub-processes limit exceeded");
}
::processes[handle] =
platform::new_engine_instance(std::move(args), "", true);
return lua::pushinteger(L, handle);
int id = ::next_process_id++;
::processes[id] = platform::new_engine_instance(std::move(args), "", true);
return lua::pushinteger(L, id);
}
static int l_is_instance_alive(lua::State* L) {
int handle = lua::tointeger(L, 1);
if (handle < 0 || handle >= ::processes.size()) {
if (handle < 0 || ::processes.find(handle) == ::processes.end()) {
throw std::runtime_error("invalid process handle");
}
auto& process = ::processes[handle];
@ -471,16 +466,42 @@ static int l_is_instance_alive(lua::State* L) {
static int l_terminate_instance(lua::State* L) {
int handle = lua::tointeger(L, 1);
if (handle < 0 || handle >= ::processes.size()) {
if (handle < 0 || ::processes.find(handle) == ::processes.end()) {
throw std::runtime_error("invalid process handle");
}
auto it = ::processes.find(handle);
if (it->second == nullptr) {
::processes.erase(it);
return lua::pushboolean(L, false);
}
bool active = it->second->isActive();
it->second.reset();
::processes.erase(it);
return lua::pushboolean(L, active);
}
static int l_get_stdin_stream(lua::State* L) {
int handle = lua::tointeger(L, 1);
if (handle < 0 || ::processes.find(handle) == ::processes.end()) {
throw std::runtime_error("invalid process handle");
}
auto& process = ::processes[handle];
if (process == nullptr) {
return lua::pushboolean(L, false);
return 0;
}
bool active = process->isActive();
process.reset();
return lua::pushboolean(L, active);
return lua::pushinteger(L, process->getStdinDescriptor());
}
static int l_get_stdout_stream(lua::State* L) {
int handle = lua::tointeger(L, 1);
if (handle < 0 || ::processes.find(handle) == ::processes.end()) {
throw std::runtime_error("invalid process handle");
}
auto& process = ::processes[handle];
if (process == nullptr) {
return 0;
}
return lua::pushinteger(L, process->getStdoutDescriptor());
}
const luaL_Reg applib[] = {
@ -515,9 +536,12 @@ const luaL_Reg applib[] = {
/// other
{"get_version", lua::wrap<l_get_version>},
{"create_memory_device", lua::wrap<l_create_memory_device>},
// sub-instances
{"start_debug_instance", lua::wrap<l_start_debug_instance>},
{"start_background_instance", lua::wrap<l_start_background_instance>},
{"is_instance_alive", lua::wrap<l_is_instance_alive>},
{"terminate_instance", lua::wrap<l_terminate_instance>},
{"get_stdin_stream", lua::wrap<l_get_stdin_stream>},
{"get_stdout_stream", lua::wrap<l_get_stdout_stream>},
{nullptr, nullptr}
};

View file

@ -33,6 +33,9 @@ int main(int argc, char** argv) {
std::cerr << err.what() << std::endl;
return EXIT_FAILURE;
}
#ifndef _WIN32
std::signal(SIGPIPE, SIG_IGN);
#endif
std::signal(SIGTERM, sigterm_handler);
#ifdef NDEBUG
std::signal(SIGINT, sigterm_handler);

View file

@ -1,7 +1,6 @@
#include "platform.hpp"
#include <time.h>
#include <iomanip>
#include <iostream>
#include <sstream>
@ -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 <Windows.h>
@ -22,9 +25,13 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <cerrno>
#include <cstring>
#endif
#ifdef __linux__
#include <sys/prctl.h>
#endif
#if defined(__linux__) || defined(__APPLE__)
#include <sys/wait.h>
#endif
@ -71,23 +78,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<DWORD>(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 +106,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,34 +224,46 @@ 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) {
#if defined(__linux__) || defined(__APPLE__)
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__
#if defined(__linux__) || defined(__APPLE__)
pid = other.pid;
#elif defined(_WIN32)
if (processHandle != nullptr) {
@ -262,16 +274,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 defined(__linux__) || defined(__APPLE__)
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 errno != ESRCH;
const_cast<SystemProcessImpl*>(this)->pid = 0;
return false;
#elif defined(_WIN32)
if (processHandle == nullptr) {
return false;
@ -283,6 +315,7 @@ public:
return false;
#elif defined(__APPLE__)
// FIXME: completely implement SystemProcess for MacOS
#else
return false;
#endif
}
@ -290,8 +323,12 @@ public:
void update() override {}
void waitForEnd() override {
#ifdef __linux__
#if defined(__linux__) || defined(__APPLE__)
if (pid <= 0) {
return;
}
waitpid(pid, nullptr, 0);
pid = 0;
#elif defined(_WIN32)
if (processHandle != nullptr) {
WaitForSingleObject(processHandle, INFINITE);
@ -300,7 +337,10 @@ public:
};
void terminate() final override {
#ifdef __linux__
#if defined(__linux__) || defined(__APPLE__)
if (pid <= 0) {
return;
}
logger.info() << "terminating " << pid;
kill(pid, SIGTERM);
#elif defined(_WIN32)
@ -310,16 +350,149 @@ public:
}
#endif
}
int getStdinDescriptor() const override {
return stdinDescriptor;
}
int getStdoutDescriptor() const override {
return stdoutDescriptor;
}
private:
#ifdef __linux__
#if defined(__linux__) || defined(__APPLE__)
pid_t pid;
int stdinDescriptor;
int stdoutDescriptor;
#elif defined(_WIN32)
HANDLE processHandle = nullptr;
DWORD pid = 0;
int stdinDescriptor;
int stdoutDescriptor;
#endif
};
std::unique_ptr<Process> platform::new_engine_instance(
#ifndef _WIN32
static void close_fd(int& fd) {
if (fd >= 0) {
close(fd);
fd = -1;
}
}
static bool make_cloexec_pipe(int fds[2]) {
#ifdef __linux__
return pipe2(fds, O_CLOEXEC) == 0;
#else
if (pipe(fds) != 0) {
return false;
}
fcntl(fds[0], F_SETFD, FD_CLOEXEC);
fcntl(fds[1], F_SETFD, FD_CLOEXEC);
return true;
#endif
}
static void report_error_and_exit(int fd) {
int err = errno;
[[maybe_unused]] ssize_t _ = write(fd, &err, sizeof(err));
_exit(127);
}
// TODO: make sub-process die with parent in MacOS
static std::unique_ptr<SystemProcess> start_engine_subprocess(
const std::filesystem::path& executable,
const std::vector<std::string>& args
) {
int stdinPipe[2] = {-1, -1};
int stdoutPipe[2] = {-1, -1};
int execPipe[2] = {-1, -1};
auto closeAll = [&]() {
for (int* fd :
{&stdinPipe[0],
&stdinPipe[1],
&stdoutPipe[0],
&stdoutPipe[1],
&execPipe[0],
&execPipe[1]}) {
close_fd(*fd);
}
};
if (!make_cloexec_pipe(stdinPipe) || !make_cloexec_pipe(stdoutPipe) ||
!make_cloexec_pipe(execPipe)) {
closeAll();
throw std::runtime_error("failed to create subprocess pipes");
}
std::string path = executable.string();
std::vector<char*> argv;
argv.reserve(args.size() + 2);
argv.push_back(path.data());
for (const auto& arg : args) {
argv.push_back(const_cast<char*>(arg.c_str()));
}
argv.push_back(nullptr);
#ifdef __linux__
const pid_t parentPid = getpid();
#endif
const pid_t pid = fork();
if (pid < 0) {
int err = errno;
closeAll();
throw std::runtime_error("fork failed: "s + std::strerror(err));
}
if (pid == 0) {
#ifdef __linux__
prctl(PR_SET_PDEATHSIG, SIGTERM);
if (getppid() != parentPid) {
_exit(1);
}
prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
#endif
if (dup2(stdinPipe[0], STDIN_FILENO) < 0 ||
dup2(stdoutPipe[1], STDOUT_FILENO) < 0 ||
dup2(stdoutPipe[1], STDERR_FILENO) < 0) {
report_error_and_exit(execPipe[1]);
}
execv(argv[0], argv.data());
report_error_and_exit(execPipe[1]);
}
close_fd(stdinPipe[0]);
close_fd(stdoutPipe[1]);
close_fd(execPipe[1]);
int childErrno = 0;
ssize_t n;
do {
n = read(execPipe[0], &childErrno, sizeof(childErrno));
} while (n < 0 && errno == EINTR);
close_fd(execPipe[0]);
if (n > 0) {
waitpid(pid, nullptr, 0);
closeAll();
throw std::runtime_error(
"starting an engine instance failed: "s + std::strerror(childErrno)
);
}
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])
);
return std::make_unique<SystemProcessImpl>(
pid, stdinDescriptorId, stdoutDescriptorId
);
}
#endif
std::unique_ptr<SystemProcess> platform::new_engine_instance(
const std::vector<std::string>& args,
std::filesystem::path outputFile,
bool subProcess
@ -334,7 +507,7 @@ std::unique_ptr<Process> 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 +531,81 @@ std::unique_ptr<Process> 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<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()) {
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 +615,13 @@ std::unique_ptr<Process> 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 +633,6 @@ std::unique_ptr<Process> 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<SystemProcess>(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,51 +643,12 @@ std::unique_ptr<Process> platform::new_engine_instance(
);
}
#else
// TODO: implement
#ifndef __APPLE__
if (subProcess) {
pid_t pid = fork();
if (outputFile.empty()) {
outputFile = "/dev/null";
}
int fd = open(
outputFile.string().c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644
);
if (pid == 0) {
prctl(PR_SET_PDEATHSIG, SIGTERM);
if (getppid() == 1) {
_exit(1);
}
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
std::vector<const char*> argv(args.size() + 2);
argv[0] = const_cast<char*>(executable.c_str());
for (int i = 0; i < args.size(); i++) {
argv[i + 1] = args[i].c_str();
}
argv[args.size() + 1] = nullptr;
prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
execvp(executable.c_str(), const_cast<char**>(argv.data()));
_exit(127);
} else {
close(fd);
usleep(1000); // waiting for execvp call
return std::make_unique<SystemProcess>(pid);
}
return start_engine_subprocess(executable, args);
}
#endif
std::stringstream ss;
ss << executable;
ss << util::quote(executable.u8string());
for (int i = 0; i < args.size(); i++) {
ss << " " << util::quote(args[i]);
}
@ -501,7 +672,22 @@ std::unique_ptr<Process> platform::new_engine_instance(
bool platform::stdin_has_data() {
#ifdef _WIN32
return _kbhit();
const HANDLE inputHandle = GetStdHandle(STD_INPUT_HANDLE);
if (inputHandle == nullptr || inputHandle == INVALID_HANDLE_VALUE) {
return false;
}
DWORD mode = 0;
if (GetConsoleMode(inputHandle, &mode)) {
return _kbhit() != 0;
}
DWORD available = 0;
if (!PeekNamedPipe(inputHandle, nullptr, 0, nullptr, &available, nullptr)) {
const DWORD error = GetLastError();
return error != ERROR_BROKEN_PIPE && error != ERROR_INVALID_HANDLE;
}
return available > 0;
#else
struct pollfd fds;
fds.fd = STDIN_FILENO;

View file

@ -5,7 +5,7 @@
#include <memory>
#include <filesystem>
#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<Process> new_engine_instance(
std::unique_ptr<SystemProcess> new_engine_instance(
const std::vector<std::string>& args,
std::filesystem::path outputFile,
bool subProcess