feat(daemon,platform): add Unix-socket daemon core, split Linux platform backends, and fix CMake feature-carrier translation
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 7s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 7s
This commit is contained in:
1 parent
55ea305528
commit
0bde74f739
20 files changed
+1194
-21
No files matched your search
@@ -0,0 +1,318 @@
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
#include <format>
|
||||
#include <set>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
|
||||
#if !defined(__linux__)
|
||||
#error \
|
||||
"VoidBuildSystem's process and file backends currently support Linux hosts only (Track C2/C3 ports are planned)"
|
||||
#endif
|
||||
|
||||
namespace Void::Build::Platform {
|
||||
namespace {
|
||||
|
||||
Void::Result::Error sys_err(int err, std::string message) {
|
||||
return Void::System::make_system_error(Void::System::map_errno_to_system_code(err),
|
||||
std::move(message), err);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<FileLock, Void::Result::Error>
|
||||
FileLock::acquire(const std::filesystem::path &lock_path, LockMode mode, bool non_blocking) {
|
||||
std::error_code ec;
|
||||
if (lock_path.has_parent_path()) {
|
||||
std::filesystem::create_directories(lock_path.parent_path(), ec);
|
||||
if (ec) {
|
||||
return std::unexpected{
|
||||
sys_err(ec.value(), std::format("FileLock: failed creating parent for '{}': {}",
|
||||
lock_path.string(), ec.message()))};
|
||||
}
|
||||
}
|
||||
const auto vl_mode = (mode == LockMode::Exclusive) ? Void::FileSystem::LockMode::Exclusive
|
||||
: Void::FileSystem::LockMode::Shared;
|
||||
if (non_blocking) {
|
||||
auto res = Void::FileSystem::FileLock::try_lock(lock_path, vl_mode);
|
||||
if (!res) {
|
||||
return std::unexpected{std::move(res.error())};
|
||||
}
|
||||
if (!res->has_value()) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::busy,
|
||||
std::format("FileLock: '{}' is already locked", lock_path.string()), EWOULDBLOCK)};
|
||||
}
|
||||
return FileLock{std::move(**res)};
|
||||
}
|
||||
auto res = Void::FileSystem::FileLock::lock(lock_path, vl_mode);
|
||||
if (!res) {
|
||||
return std::unexpected{std::move(res.error())};
|
||||
}
|
||||
return FileLock{std::move(*res)};
|
||||
}
|
||||
|
||||
Void::Result::Result<FileMetadata, Void::Result::Error>
|
||||
stat_lstat(const std::filesystem::path &path) {
|
||||
struct stat st{};
|
||||
if (::lstat(path.c_str(), &st) != 0) {
|
||||
const int err = errno;
|
||||
if (err == ENOENT || err == ENOTDIR) {
|
||||
FileMetadata missing;
|
||||
missing.exists = false;
|
||||
return missing;
|
||||
}
|
||||
return std::unexpected{
|
||||
sys_err(err, std::format("stat_lstat: lstat failed for '{}'", path.string()))};
|
||||
}
|
||||
|
||||
FileMetadata meta;
|
||||
meta.exists = true;
|
||||
meta.is_regular = S_ISREG(st.st_mode);
|
||||
meta.is_directory = S_ISDIR(st.st_mode);
|
||||
meta.is_symlink = S_ISLNK(st.st_mode);
|
||||
meta.dev = static_cast<std::uint64_t>(st.st_dev);
|
||||
meta.ino = static_cast<std::uint64_t>(st.st_ino);
|
||||
meta.size = static_cast<std::uint64_t>(st.st_size);
|
||||
meta.nlink = static_cast<std::uint64_t>(st.st_nlink);
|
||||
meta.mode = static_cast<std::uint32_t>(st.st_mode & 07777u);
|
||||
meta.mtime_sec = static_cast<std::int64_t>(st.st_mtim.tv_sec);
|
||||
meta.mtime_nsec = static_cast<std::int64_t>(st.st_mtim.tv_nsec);
|
||||
|
||||
if (meta.is_symlink) {
|
||||
std::size_t buf_size =
|
||||
st.st_size > 0 ? static_cast<std::size_t>(st.st_size) + 1 : std::size_t{256};
|
||||
for (int attempt = 0; attempt < 8; ++attempt) {
|
||||
std::string target(buf_size, '\0');
|
||||
const ssize_t n = ::readlink(path.c_str(), target.data(), target.size());
|
||||
if (n < 0) {
|
||||
const int err = errno;
|
||||
return std::unexpected{sys_err(
|
||||
err, std::format("stat_lstat: readlink failed for '{}'", path.string()))};
|
||||
}
|
||||
if (static_cast<std::size_t>(n) < target.size()) {
|
||||
target.resize(static_cast<std::size_t>(n));
|
||||
meta.symlink_target = std::move(target);
|
||||
break;
|
||||
}
|
||||
buf_size *= 2;
|
||||
}
|
||||
}
|
||||
return meta;
|
||||
}
|
||||
|
||||
Void::Result::Result<bool, Void::Result::Error>
|
||||
copy_isolated(const std::filesystem::path &src, const std::filesystem::path &dst,
|
||||
std::optional<std::uint32_t> mode_override) {
|
||||
std::error_code ec;
|
||||
if (dst.has_parent_path()) {
|
||||
std::filesystem::create_directories(dst.parent_path(), ec);
|
||||
if (ec) {
|
||||
return std::unexpected{sys_err(
|
||||
ec.value(), std::format("copy_isolated: cannot create parent dir for '{}': {}",
|
||||
dst.string(), ec.message()))};
|
||||
}
|
||||
}
|
||||
Void::FileSystem::CopyOptions opts;
|
||||
opts.overwrite = true;
|
||||
opts.preserve_permissions = !mode_override.has_value();
|
||||
opts.durability = Void::FileSystem::ReplaceDurability::atomic_visibility;
|
||||
|
||||
auto res = Void::FileSystem::reflink_copy_file(src, dst, opts);
|
||||
if (!res) {
|
||||
return std::unexpected{std::move(res.error())};
|
||||
}
|
||||
if (mode_override) {
|
||||
(void)::chmod(dst.c_str(), static_cast<mode_t>(*mode_override));
|
||||
}
|
||||
return res->method == Void::FileSystem::CopyMethod::Reflink;
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
set_canonical_mtime(const std::filesystem::path &path, std::int64_t epoch_seconds) {
|
||||
return Void::FileSystem::set_canonical_mtime(path, epoch_seconds, 0);
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
atomic_write_bytes(const std::filesystem::path &path, std::span<const std::byte> bytes,
|
||||
bool durable_sync, unsigned mode) {
|
||||
std::error_code ec;
|
||||
if (path.has_parent_path()) {
|
||||
std::filesystem::create_directories(path.parent_path(), ec);
|
||||
if (ec) {
|
||||
return std::unexpected{sys_err(
|
||||
ec.value(), std::format("atomic_write_bytes: create_directories failed for '{}'",
|
||||
path.string()))};
|
||||
}
|
||||
}
|
||||
Void::FileSystem::ReplaceOptions opts;
|
||||
opts.durability = durable_sync ? Void::FileSystem::ReplaceDurability::durable_sync
|
||||
: Void::FileSystem::ReplaceDurability::atomic_visibility;
|
||||
opts.mode = mode;
|
||||
auto res = Void::FileSystem::replace_file(path, bytes, opts);
|
||||
if (!res) {
|
||||
return std::unexpected{std::move(res.error().error)};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> atomic_write_file(const std::filesystem::path &path,
|
||||
std::string_view content,
|
||||
bool durable_sync,
|
||||
unsigned mode) {
|
||||
return atomic_write_bytes(path,
|
||||
std::as_bytes(std::span<const char>{content.data(), content.size()}),
|
||||
durable_sync, mode);
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
fsync_directory(const std::filesystem::path &dir_path) {
|
||||
const int fd = ::open(dir_path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
const int err = errno;
|
||||
return std::unexpected{
|
||||
sys_err(err, std::format("fsync_directory: open failed for '{}'", dir_path.string()))};
|
||||
}
|
||||
Void::System::OwnedFd guard{fd};
|
||||
for (;;) {
|
||||
if (::fsync(fd) == 0) {
|
||||
return {};
|
||||
}
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
const int err = errno;
|
||||
if (err == EINVAL || err == EROFS) {
|
||||
return {};
|
||||
}
|
||||
return std::unexpected{
|
||||
sys_err(err, std::format("fsync_directory: fsync failed for '{}'", dir_path.string()))};
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
atomic_swap_symlink(const std::filesystem::path &link_path, const std::filesystem::path &target) {
|
||||
std::error_code ec;
|
||||
const auto parent =
|
||||
link_path.has_parent_path() ? link_path.parent_path() : std::filesystem::path{"."};
|
||||
std::filesystem::create_directories(parent, ec);
|
||||
if (ec) {
|
||||
return std::unexpected{sys_err(
|
||||
ec.value(), std::format("atomic_swap_symlink: create_directories failed for '{}'",
|
||||
parent.string()))};
|
||||
}
|
||||
return Void::FileSystem::atomic_symlink_swap(link_path, target,
|
||||
Void::FileSystem::ReplaceDurability::durable_sync);
|
||||
}
|
||||
|
||||
bool path_is_within(const std::filesystem::path &root, const std::filesystem::path &path) {
|
||||
const auto norm_root = root.lexically_normal();
|
||||
const auto norm_path = path.lexically_normal();
|
||||
auto r_it = norm_root.begin();
|
||||
auto p_it = norm_path.begin();
|
||||
for (; r_it != norm_root.end(); ++r_it, ++p_it) {
|
||||
if (r_it->empty() || *r_it == ".") {
|
||||
continue;
|
||||
}
|
||||
if (p_it == norm_path.end() || *r_it != *p_it) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::filesystem::path, Void::Result::Error>
|
||||
resolve_confined_path(const std::filesystem::path &root, const std::filesystem::path &candidate,
|
||||
const std::vector<std::filesystem::path> &allowed_external_roots) {
|
||||
std::error_code ec;
|
||||
const auto canonical_root = std::filesystem::weakly_canonical(root, ec);
|
||||
if (ec) {
|
||||
return std::unexpected{
|
||||
sys_err(ec.value(), std::format("resolve_confined_path: cannot canonicalize root '{}'",
|
||||
root.string()))};
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> allowed{canonical_root};
|
||||
for (const auto &ext : allowed_external_roots) {
|
||||
const auto ce = std::filesystem::weakly_canonical(ext, ec);
|
||||
if (!ec) {
|
||||
allowed.push_back(ce);
|
||||
}
|
||||
}
|
||||
|
||||
std::filesystem::path current =
|
||||
candidate.is_absolute() ? candidate : (canonical_root / candidate);
|
||||
current = current.lexically_normal();
|
||||
|
||||
constexpr int kMaxSymlinkHops = 40;
|
||||
int hops = 0;
|
||||
std::set<std::filesystem::path> seen;
|
||||
|
||||
for (;;) {
|
||||
std::filesystem::path acc;
|
||||
bool followed_symlink = false;
|
||||
auto it = current.begin();
|
||||
for (; it != current.end(); ++it) {
|
||||
if (*it == ".") {
|
||||
continue;
|
||||
}
|
||||
if (*it == "..") {
|
||||
acc = acc.parent_path();
|
||||
continue;
|
||||
}
|
||||
acc /= *it;
|
||||
auto meta = stat_lstat(acc);
|
||||
if (!meta) {
|
||||
return std::unexpected{meta.error()};
|
||||
}
|
||||
if (!meta->exists) {
|
||||
++it;
|
||||
for (; it != current.end(); ++it) {
|
||||
acc /= *it;
|
||||
}
|
||||
current = acc.lexically_normal();
|
||||
break;
|
||||
}
|
||||
if (meta->is_symlink) {
|
||||
if (++hops > kMaxSymlinkHops || !seen.insert(acc).second) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
std::format("resolve_confined_path: symlink loop detected at '{}'",
|
||||
acc.string()),
|
||||
ELOOP)};
|
||||
}
|
||||
std::filesystem::path target_path{meta->symlink_target};
|
||||
std::filesystem::path rebuilt =
|
||||
target_path.is_absolute() ? target_path : (acc.parent_path() / target_path);
|
||||
++it;
|
||||
for (; it != current.end(); ++it) {
|
||||
rebuilt /= *it;
|
||||
}
|
||||
current = rebuilt.lexically_normal();
|
||||
followed_symlink = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!followed_symlink) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &allowed_root : allowed) {
|
||||
if (path_is_within(allowed_root, current)) {
|
||||
return current;
|
||||
}
|
||||
}
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::permission_denied,
|
||||
std::format("resolve_confined_path: '{}' escapes declared roots (root='{}')",
|
||||
candidate.string(), canonical_root.string()),
|
||||
EACCES)};
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Platform
|
||||
@@ -0,0 +1,204 @@
|
||||
#include <Void/Build/Platform/ProcessSupervisor.hpp>
|
||||
|
||||
#include <Void/Configuration/Env.hpp>
|
||||
#include <Void/System/Process.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
|
||||
#if !defined(__linux__)
|
||||
#error \
|
||||
"VoidBuildSystem's process and file backends currently support Linux hosts only (Track C2/C3 ports are planned)"
|
||||
#endif
|
||||
|
||||
namespace Void::Build::Platform {
|
||||
namespace {
|
||||
|
||||
bool is_executable_file(const std::filesystem::path &p) {
|
||||
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;
|
||||
}
|
||||
|
||||
void ensure_subreaper() noexcept {
|
||||
static std::once_flag once;
|
||||
std::call_once(once, [] { (void)Void::Process::enable_subreaper(); });
|
||||
}
|
||||
|
||||
} // 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('/') != std::string::npos) {
|
||||
std::error_code ec;
|
||||
const auto abs = std::filesystem::absolute(executable, ec);
|
||||
if (!ec && is_executable_file(abs)) {
|
||||
return abs.lexically_normal();
|
||||
}
|
||||
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 colon = path_env.find(':', start);
|
||||
const auto segment = path_env.substr(
|
||||
start, colon == std::string_view::npos ? std::string_view::npos : colon - start);
|
||||
const std::filesystem::path dir =
|
||||
segment.empty() ? std::filesystem::path{"."} : std::filesystem::path{segment};
|
||||
const auto candidate = dir / executable;
|
||||
if (is_executable_file(candidate)) {
|
||||
std::error_code ec;
|
||||
auto abs = std::filesystem::absolute(candidate, ec);
|
||||
return ec ? candidate : abs.lexically_normal();
|
||||
}
|
||||
if (colon == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
start = colon + 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;
|
||||
const pid_t pgid = static_cast<pid_t>(child->pgid());
|
||||
if (!group_timed_out && !group_cancelled && !comm->overflowed) {
|
||||
while (child->process_group_alive()) {
|
||||
// Reap any adopted descendants in this process group (subreaper) so zombies clear.
|
||||
int wstatus = 0;
|
||||
while (pgid > 0 && ::waitpid(-pgid, &wstatus, WNOHANG) > 0) {
|
||||
}
|
||||
if (!child->process_group_alive()) {
|
||||
break;
|
||||
}
|
||||
if (stop.stop_requested()) {
|
||||
group_cancelled = true;
|
||||
break;
|
||||
}
|
||||
if (has_timeout && std::chrono::steady_clock::now() >= deadline) {
|
||||
group_timed_out = true;
|
||||
break;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{10});
|
||||
}
|
||||
}
|
||||
|
||||
if (group_cancelled || group_timed_out || comm->overflowed || child->process_group_alive()) {
|
||||
(void)child->terminate(request.kill_grace);
|
||||
int wstatus = 0;
|
||||
while (pgid > 0 && ::waitpid(-pgid, &wstatus, WNOHANG) > 0) {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user