Files
Build-System/src/Platform/Linux/ProcessSupervisor.cpp
T
faumaray e333bbe84b
VoidBuildSystem CI / 1 · Requirements (push) Successful in 6s
VoidBuildSystem CI / 2C · Windows native build (debug) (push) Failing after 1m32s
VoidBuildSystem CI / 2C · Windows native build (sanitizers) (push) Failing after 18s
VoidBuildSystem CI / 2C · Windows native build (relwithdebinfo) (push) Failing after 3m4s
VoidBuildSystem CI / 2B · Windows Wine build (debug) (push) Successful in 6m20s
VoidBuildSystem CI / 2A · Linux build (debug) (push) Successful in 6m32s
VoidBuildSystem CI / 3C · Windows native test (debug) (push) Failing after 1m45s
VoidBuildSystem CI / 2B · Windows Wine build (sanitizers) (push) Successful in 6m55s
VoidBuildSystem CI / 2A · Linux build (sanitizers) (push) Successful in 6m59s
VoidBuildSystem CI / 2A · Linux build (relwithdebinfo) (push) Successful in 6m59s
VoidBuildSystem CI / 3C · Windows native test (sanitizers) (push) Failing after 15s
VoidBuildSystem CI / 3C · Windows native test (relwithdebinfo) (push) Failing after 1m47s
VoidBuildSystem CI / 3A · Linux test (sanitizers) (push) Successful in 2m4s
VoidBuildSystem CI / 4B · Windows package (debug) (push) Successful in 1m6s
VoidBuildSystem CI / 4B · Windows package (sanitizers) (push) Successful in 12s
VoidBuildSystem CI / 2B · Windows Wine build (relwithdebinfo) (push) Successful in 4m2s
VoidBuildSystem CI / 4B · Windows package (relwithdebinfo) (push) Successful in 1m44s
VoidBuildSystem CI / 3B · Windows Wine test (sanitizers) (push) Successful in 3m16s
VoidBuildSystem CI / 3B · Windows Wine test (debug) (push) Successful in 3m21s
VoidBuildSystem CI / 3A · Linux test (relwithdebinfo) (push) Failing after 6m45s
VoidBuildSystem CI / 3A · Linux test (debug) (push) Failing after 6m54s
VoidBuildSystem CI / 4A · Linux package (debug) (push) Skipped
VoidBuildSystem CI / 4A · Linux package (relwithdebinfo) (push) Skipped
VoidBuildSystem CI / 4A · Linux package (sanitizers) (push) Skipped
VoidBuildSystem CI / 5 · Release (push) Skipped
VoidBuildSystem CI / 3B · Windows Wine test (relwithdebinfo) (push) Successful in 3m42s
feat(platform): build on native Windows hosts
Bump VoidLibrary to 482a3ea (Windows target support: qualified
Configuration/Crypto/System/Archive, Windows DirectoryWatcher,
TempDir/TempFile, process pipes) and port VBS's own host backends:

- Toolchain: Windows/macOS host names and architectures without uname.
- Platform FileOps: Win32 stat metadata, CreateFileW+LockFileEx file
  locks (Void FileLock is POSIX-only, Void/Library#66), no-op
  directory fsync, GetModuleFileNameW executable path, mtime via
  std::filesystem, and a file-copy fallback for the compdb alias.
- ProcessSupervisor: Windows PATH/PATHEXT resolution and no POSIX
  waitpid reaping (job objects own descendants).
- Snapshot: directory listing through std::filesystem on Windows.
- Remote cache/registry client/CDash: curl or Winsock-based HTTP on
  Windows (Void::Net is not qualified there yet).
- Daemon: explicit unsupported errors on Windows (named-pipe
  transport is Track C3); the registry service stays POSIX-only and
  is excluded from the Windows build.
- StateStore/Sbom/Main/Graph: portable stat, time and path handling.

Verified: MinGW-w64 GCC 16.2 builds vbs, vbs_describe, vbs_benchmarks
and the sdk/core/v1/selfhost test executables; vbs runs under Wine
and the SDK suite passes 17/17. The Linux suite stays green (8/8).
2026-12-06 02:01:24 +03:00

286 lines
10 KiB
C++

#include <Void/Build/Platform/ProcessSupervisor.hpp>
#include <Void/Configuration/Env.hpp>
#include <Void/System/Process.hpp>
#include <Void/System/Types.hpp>
#include <algorithm>
#include <chrono>
#include <mutex>
#include <thread>
#if defined(_WIN32)
#include <cwctype>
#else
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
#if !defined(__linux__) && !defined(_WIN32)
#error \
"VoidBuildSystem's process and file backends currently support Linux and Windows hosts only (Track C2/C3 ports are planned)"
#endif
namespace Void::Build::Platform {
namespace {
#if defined(_WIN32)
constexpr char kPathSeparator = ';';
#else
constexpr char kPathSeparator = ':';
#endif
bool is_executable_file(const std::filesystem::path &p) {
#if defined(_WIN32)
std::error_code ec;
if (!std::filesystem::is_regular_file(p, ec)) {
return false;
}
std::string ext = p.extension().string();
std::ranges::transform(ext, ext.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return ext.empty() || ext == ".exe" || ext == ".com" || ext == ".bat" || ext == ".cmd";
#else
struct stat st{};
if (::stat(p.c_str(), &st) != 0) {
return false;
}
return S_ISREG(st.st_mode) && ::access(p.c_str(), X_OK) == 0;
#endif
}
std::optional<std::filesystem::path> resolve_candidate(const std::filesystem::path &candidate) {
std::vector<std::filesystem::path> names{candidate};
#if defined(_WIN32)
if (!candidate.has_extension()) {
for (const char *ext : {".exe", ".cmd", ".bat", ".com"}) {
names.push_back(candidate.string() + ext);
}
}
#endif
for (const auto &name : names) {
if (is_executable_file(name)) {
std::error_code ec;
auto abs = std::filesystem::absolute(name, ec);
return ec ? name : abs.lexically_normal();
}
}
return std::nullopt;
}
void ensure_subreaper() noexcept {
#if !defined(_WIN32)
static std::once_flag once;
std::call_once(once, [] { (void)Void::Process::enable_subreaper(); });
#endif
}
} // namespace
std::optional<std::filesystem::path>
resolve_executable(const std::filesystem::path &executable,
std::optional<std::string_view> path_override) {
if (executable.empty()) {
return std::nullopt;
}
if (executable.is_absolute() ||
executable.string().find_first_of(std::string_view{"/\\"}) != std::string::npos) {
std::error_code ec;
const auto abs = std::filesystem::absolute(executable, ec);
if (!ec) {
if (auto resolved = resolve_candidate(abs); resolved) {
return resolved;
}
}
return std::nullopt;
}
std::string owned_env;
std::string_view path_env;
if (path_override) {
path_env = *path_override;
} else if (auto env_val = Void::Configuration::get_env("PATH"); env_val) {
owned_env = std::move(*env_val);
path_env = owned_env;
}
std::size_t start = 0;
while (start <= path_env.size()) {
const auto separator = path_env.find(kPathSeparator, start);
auto segment = path_env.substr(
start, separator == std::string_view::npos ? std::string_view::npos : separator - start);
// Windows PATH entries may be quoted.
if (segment.size() >= 2 && segment.front() == '"' && segment.back() == '"') {
segment.remove_prefix(1);
segment.remove_suffix(1);
}
const std::filesystem::path dir =
segment.empty() ? std::filesystem::path{"."} : std::filesystem::path{segment};
if (auto resolved = resolve_candidate(dir / executable); resolved) {
return resolved;
}
if (separator == std::string_view::npos) {
break;
}
start = separator + 1;
}
return std::nullopt;
}
Void::Result::Result<ProcessResult, Void::Result::Error>
run_supervised(const ProcessRequest &request, std::stop_token stop) {
if (request.executable.empty()) {
return std::unexpected{
Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument,
"run_supervised: executable path must not be empty")};
}
ensure_subreaper();
// Delegate process spawning, streaming, timeout/cancellation, and robust -pgid
// descendant SIGTERM -> SIGKILL supervision directly to Void::Process (VL-001).
Void::Process::SpawnOptions opts;
opts.executable = request.executable;
opts.arguments = request.arguments;
opts.working_directory = request.working_directory;
opts.search_path = request.search_path;
opts.new_process_group = true;
opts.kill_grace = request.kill_grace;
opts.stdin_pipe = true;
opts.stdout_pipe = true;
opts.stderr_pipe = true;
switch (request.environment.mode) {
case EnvPolicyMode::Inherit:
opts.environment.mode = Void::Process::EnvMode::inherit;
break;
case EnvPolicyMode::Allowlist:
opts.environment.mode = Void::Process::EnvMode::allowlist;
break;
case EnvPolicyMode::ExplicitOnly:
opts.environment.mode = Void::Process::EnvMode::explicit_only;
break;
}
opts.environment.allowlist = request.environment.allowlist;
opts.environment.unset_keys = request.environment.unset_keys;
for (const auto &[k, v] : request.environment.set_vars) {
opts.environment.set_vars.emplace_back(k, v);
}
auto child = Void::Process::spawn(opts);
if (!child) {
ProcessResult fail;
fail.exec_failed = true;
fail.exec_error_message = child.error().message();
return fail;
}
const auto start_time = std::chrono::steady_clock::now();
const bool has_timeout = request.timeout.count() > 0;
const auto deadline = start_time + request.timeout;
Void::Process::CaptureLimits limits;
limits.max_stdout_bytes = request.max_stdout_bytes;
limits.max_stderr_bytes = request.max_stderr_bytes;
limits.max_line_bytes = 0; // Do not reject long compiler/linker diagnostic lines.
limits.max_stdin_bytes = std::max(limits.max_stdin_bytes, request.stdin_data.size() + 1);
limits.overflow_policy = request.fail_on_overflow
? Void::Process::OverflowPolicy::fail_and_terminate
: Void::Process::OverflowPolicy::drain_and_truncate;
Void::Process::StreamCallbacks callbacks;
const Void::Process::StreamCallbacks *cb_ptr = nullptr;
if (request.on_stdout_chunk || request.on_stderr_chunk) {
callbacks.on_stdout = request.on_stdout_chunk;
callbacks.on_stderr = request.on_stderr_chunk;
cb_ptr = &callbacks;
}
auto comm = Void::Process::communicate_blocking(*child, request.stdin_data, limits,
request.timeout, stop, cb_ptr);
if (!comm) {
(void)child->terminate(request.kill_grace);
return std::unexpected{std::move(comm.error())};
}
// If the leader exited cleanly while descendants in its process group are still alive
// (e.g. background child with redirected stdio), supervise the process group until it
// drains, times out, or is cancelled, then escalate via SIGTERM -> kill_grace -> SIGKILL.
bool group_timed_out = comm->timed_out;
bool group_cancelled = comm->cancelled;
bool descendants_terminated = false;
#if !defined(_WIN32)
const pid_t pgid = static_cast<pid_t>(child->pgid());
#endif
if (!group_timed_out && !group_cancelled && !comm->overflowed) {
const auto descendant_deadline =
std::chrono::steady_clock::now() + request.descendant_grace;
while (child->process_group_alive()) {
#if !defined(_WIN32)
// Reap any adopted descendants in this process group (subreaper) so zombies clear.
int wstatus = 0;
while (pgid > 0 && ::waitpid(-pgid, &wstatus, WNOHANG) > 0) {
}
#endif
if (!child->process_group_alive()) {
break;
}
if (stop.stop_requested()) {
group_cancelled = true;
break;
}
const auto now = std::chrono::steady_clock::now();
if (has_timeout && now >= deadline) {
group_timed_out = true;
break;
}
// Orphans must not keep the supervisor (or a test run) alive forever.
if (request.descendant_grace.count() > 0 && now >= descendant_deadline) {
descendants_terminated = true;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds{10});
}
}
if (group_cancelled || group_timed_out || descendants_terminated || comm->overflowed ||
child->process_group_alive()) {
(void)child->terminate(request.kill_grace);
#if !defined(_WIN32)
// Reap the whole group until it is really gone (bounded), so no zombie or orphan is
// left behind after test supervision.
const auto reap_deadline =
std::chrono::steady_clock::now() + request.kill_grace + std::chrono::seconds{2};
int wstatus = 0;
while (pgid > 0) {
const pid_t reaped = ::waitpid(-pgid, &wstatus, WNOHANG);
if (reaped < 0) { // ECHILD: no descendants left.
break;
}
if (reaped == 0) {
if (std::chrono::steady_clock::now() >= reap_deadline) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds{5});
continue;
}
}
#endif
}
ProcessResult result;
result.exited = comm->status.exited;
result.exit_code = comm->status.code;
result.term_signal = comm->status.signal;
result.stdout_data = std::move(comm->out);
result.stderr_data = std::move(comm->err);
result.stdout_truncated = comm->stdout_truncated;
result.stderr_truncated = comm->stderr_truncated;
result.timed_out = group_timed_out;
result.cancelled = group_cancelled;
result.overflowed = comm->overflowed;
return result;
}
} // namespace Void::Build::Platform