Merge issue-40-windows-process: Windows process and handle support (closes #40)

This commit is contained in:
faumaray committed 2026-12-03 16:11:40 +03:00
commit 62c2082fb3
7 files changed
+2754 -294

No files matched your search

+15 -1
View File
@@ -5,13 +5,27 @@ set_property(GLOBAL PROPERTY VOID_ACTIVE_PUBLIC_HEADERS "")
function(void_add_module MODULE_NAME)
cmake_parse_arguments(ARG "" ""
"INTERNAL_DEPS;EXTERNAL_DEPS;OPTIONAL_DEPS;DEPS_WIN32;DEPS_UNIX;OPTIONS_WIN32;OPTIONS_UNIX;EXTRA_HEADERS;EXCLUDE_HEADERS"
"INTERNAL_DEPS;EXTERNAL_DEPS;OPTIONAL_DEPS;DEPS_WIN32;DEPS_UNIX;OPTIONS_WIN32;OPTIONS_UNIX;EXTRA_HEADERS;EXCLUDE_HEADERS;EXCLUDE_SOURCES_WIN32;EXCLUDE_SOURCES_UNIX"
${ARGN})
set(TGT ${PROJECT_NAME}_${MODULE_NAME})
set(ALIAS ${PROJECT_NAME}::${MODULE_NAME})
file(GLOB_RECURSE SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/${MODULE_NAME}/*.cpp")
# Platform-specific source selection: a module may be fully portable at the
# header level while some translation units only build on one platform.
# Exclusions are relative to the module directory so callers do not depend
# on the global source layout.
if(WIN32 AND ARG_EXCLUDE_SOURCES_WIN32)
foreach(_excl IN LISTS ARG_EXCLUDE_SOURCES_WIN32)
list(REMOVE_ITEM SRC "${CMAKE_CURRENT_SOURCE_DIR}/${_excl}")
endforeach()
endif()
if(NOT WIN32 AND ARG_EXCLUDE_SOURCES_UNIX)
foreach(_excl IN LISTS ARG_EXCLUDE_SOURCES_UNIX)
list(REMOVE_ITEM SRC "${CMAKE_CURRENT_SOURCE_DIR}/${_excl}")
endforeach()
endif()
file(GLOB_RECURSE HDR CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/include/Void/${MODULE_NAME}/*.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/include/Void/${MODULE_NAME}/*.h")
+21 -1
View File
@@ -17,9 +17,23 @@
#include <utility>
#include <vector>
#ifdef _WIN32
#include <memory>
#endif
namespace Void::Process
{
#ifdef _WIN32
namespace Detail
{
/// Opaque owner of the native Windows process, job and pipe state. Defined by
/// the System module implementation; it is never an integer descriptor.
struct WindowsProcess;
struct ChildAccess;
} // namespace Detail
#endif
inline constexpr std::size_t kMaxSpawnInputBytes = 256ULL * 1024ULL;
inline constexpr std::size_t kDefaultMaxStdoutBytes = 1024ULL * 1024ULL;
inline constexpr std::size_t kDefaultMaxStderrBytes = 1024ULL * 1024ULL;
@@ -100,7 +114,7 @@ struct CommunicateResult {
class Child
{
public:
Child() noexcept = default;
Child() noexcept;
Child(long long pid,
long long pgid,
bool owns_process_group,
@@ -157,6 +171,12 @@ private:
Void::System::OwnedFd stderr_{};
std::chrono::milliseconds kill_grace_{std::chrono::milliseconds{2000}};
#ifdef _WIN32
std::unique_ptr<Detail::WindowsProcess> windows_;
friend struct Detail::ChildAccess;
friend class BinarySession;
#endif
void reset_and_reap() noexcept;
};
+6 -1
View File
@@ -1,3 +1,8 @@
void_add_module(IO
INTERNAL_DEPS Core Functional Time Memory Async
)
# The current native driver is epoll-based and only builds on Linux/Unix.
# Windows capability is not claimed; callers get a fail-closed boundary
# instead of a POSIX driver that cannot compile.
EXCLUDE_SOURCES_WIN32
EpollDriver.cpp
)
+103 -1
View File
@@ -24,6 +24,19 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#else
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <process.h>
#include <sys/stat.h>
#endif
#include <algorithm>
@@ -56,6 +69,26 @@ namespace Void::Net
std::atomic<std::uint64_t> g_dl_temp_seq{0};
#if defined(_WIN32)
// Win32 paths above MAX_PATH must use the extended-length prefix, and
// forward slashes are not accepted by the wide file APIs.
[[nodiscard]] std::filesystem::path
extended_path(const std::filesystem::path& path)
{
std::error_code ec;
const auto absolute = std::filesystem::absolute(path, ec);
std::wstring text = (ec ? path : absolute).wstring();
std::replace(text.begin(), text.end(), L'/', L'\\');
if (text.rfind(L"\\\\?\\", 0) == 0 || text.size() < MAX_PATH) {
return std::filesystem::path{text};
}
if (text.rfind(L"\\\\", 0) == 0) {
return std::filesystem::path{L"\\\\?\\UNC\\" + text.substr(2)};
}
return std::filesystem::path{L"\\\\?\\" + text};
}
#endif
[[nodiscard]] bool
equal_ascii_ci(std::string_view a, std::string_view b) noexcept
{
@@ -557,13 +590,26 @@ namespace Void::Net
}
const std::filesystem::path tmp_path =
parent / std::format(".void-dl-{}-{}",
#if defined(_WIN32)
static_cast<long long>(::_getpid()),
#else
static_cast<long long>(::getpid()),
#endif
g_dl_temp_seq.fetch_add(
1, std::memory_order_relaxed));
#if defined(_WIN32)
// Create exclusively next to the destination: an existing entry
// (including a reparse point) is never followed or truncated.
const int fd = ::_wopen(
extended_path(tmp_path).c_str(),
_O_WRONLY | _O_CREAT | _O_EXCL | _O_BINARY | _O_NOINHERIT,
_S_IREAD | _S_IWRITE);
#else
const int fd = ::open(
tmp_path.c_str(),
O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW,
0644);
#endif
if (fd < 0) {
return std::unexpected{make_net_error(
NetErrorCode::io_error,
@@ -593,10 +639,17 @@ namespace Void::Net
{
std::size_t off = 0;
while (off < data.size()) {
#if defined(_WIN32)
const int w = ::_write(
m_fd,
reinterpret_cast<const char*>(data.data()) + off,
static_cast<unsigned int>(data.size() - off));
#else
const ssize_t w = ::write(
m_fd,
reinterpret_cast<const char*>(data.data()) + off,
data.size() - off);
#endif
if (w < 0 && errno == EINTR) {
continue;
}
@@ -616,6 +669,50 @@ namespace Void::Net
[[nodiscard]] Void::Result::Result<void> commit()
{
#if defined(_WIN32)
if (::_commit(m_fd) != 0) {
return std::unexpected{make_net_error(
NetErrorCode::io_error,
std::format("Net: flush download file failed: {}",
std::strerror(errno)),
std::error_code{errno, std::generic_category()})};
}
const int raw = std::exchange(m_fd, -1);
if (::_close(raw) != 0) {
discard();
return std::unexpected{make_net_error(
NetErrorCode::io_error,
std::format("Net: close download file failed: {}",
std::strerror(errno)),
std::error_code{errno, std::generic_category()})};
}
// Never replace a reparse point: a hostile workspace entry
// could redirect the completed download outside the target.
const DWORD attributes =
::GetFileAttributesW(extended_path(m_dest).c_str());
if (attributes != INVALID_FILE_ATTRIBUTES &&
(attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) {
discard();
return std::unexpected{make_net_error(
NetErrorCode::io_error,
"Net: refusing to replace a reparse-point "
"download destination")};
}
if (::MoveFileExW(extended_path(m_tmp).c_str(),
extended_path(m_dest).c_str(),
MOVEFILE_REPLACE_EXISTING |
MOVEFILE_WRITE_THROUGH) == 0) {
const DWORD error = ::GetLastError();
discard();
return std::unexpected{make_net_error(
NetErrorCode::io_error,
std::format("Net: replace download file failed: "
"win32={}",
error),
std::error_code{static_cast<int>(error),
std::system_category()})};
}
#else
if (::fsync(m_fd) != 0) {
return std::unexpected{make_net_error(
NetErrorCode::io_error,
@@ -639,9 +736,10 @@ namespace Void::Net
return std::unexpected{make_net_error(
NetErrorCode::io_error,
std::format("Net: rename download file failed: {}",
ec.message()),
ec.message()),
ec)};
}
#endif
m_committed = true;
return {};
}
@@ -649,7 +747,11 @@ namespace Void::Net
void discard() noexcept
{
if (m_fd >= 0) {
#if defined(_WIN32)
(void)::_close(m_fd);
#else
(void)::close(m_fd);
#endif
m_fd = -1;
}
if (!m_committed && !m_tmp.empty()) {
+5
View File
@@ -1,5 +1,10 @@
void_add_module(Net
INTERNAL_DEPS Functional Text Time
# The HTTP server is POSIX-only. Windows builds get the client-side
# surface; a Windows server port is explicitly out of scope, and any
# server symbol left undefined fails closed at link time.
EXCLUDE_SOURCES_WIN32
HttpServer.cpp
EXTERNAL_DEPS
Boost::headers
Boost::beast
+4 -290
View File
@@ -20,295 +20,7 @@
#include <utility>
#include <vector>
#ifdef _WIN32
namespace Void::Process
{
Child::Child(long long,
long long,
bool,
Void::System::OwnedFd,
Void::System::OwnedFd,
Void::System::OwnedFd,
std::chrono::milliseconds) noexcept
{
}
Child::~Child() noexcept = default;
Child::Child(Child&&) noexcept = default;
Child& Child::operator=(Child&&) noexcept = default;
bool Child::valid() const noexcept { return false; }
bool Child::running() const noexcept { return false; }
bool Child::process_group_alive() const noexcept { return false; }
long long Child::pid() const noexcept { return -1; }
long long Child::pgid() const noexcept { return -1; }
bool Child::owns_process_group() const noexcept { return false; }
int Child::stdin_fd() const noexcept { return -1; }
int Child::stdout_fd() const noexcept { return -1; }
int Child::stderr_fd() const noexcept { return -1; }
Void::System::OwnedFd Child::take_stdin_fd() noexcept { return {}; }
Void::System::OwnedFd Child::take_stdout_fd() noexcept { return {}; }
Void::System::OwnedFd Child::take_stderr_fd() noexcept { return {}; }
void Child::close_stdin() noexcept {}
Void::Result::Result<void, Void::Result::Error> Child::send_signal(int) noexcept
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::Child::send_signal: not implemented on Windows")};
}
Void::Result::Result<ExitStatus, Void::Result::Error>
Child::terminate(std::chrono::milliseconds)
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::Child::terminate: not implemented on Windows")};
}
Void::Result::Result<std::optional<ExitStatus>, Void::Result::Error>
Child::try_wait()
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::Child::try_wait: not implemented on Windows")};
}
Void::Result::Result<ExitStatus, Void::Result::Error>
Child::wait_blocking(std::chrono::milliseconds,
std::stop_token,
std::chrono::milliseconds)
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::Child::wait_blocking: not implemented on Windows")};
}
Void::Result::Result<Child, Void::Result::Error> spawn(const SpawnOptions&)
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::spawn: not implemented on Windows")};
}
BinarySession::BinarySession(Child child, BinarySessionOptions options)
: child_(std::move(child)), options_(options)
{
}
BinarySession::~BinarySession() noexcept = default;
BinarySession::BinarySession(BinarySession&& other) noexcept
: child_(std::move(other.child_)),
stdin_(std::move(other.stdin_)),
stdout_(std::move(other.stdout_)),
stderr_(std::move(other.stderr_)),
stdout_buffer_(std::move(other.stdout_buffer_)),
stdout_offset_(std::exchange(other.stdout_offset_, 0)),
stderr_buffer_(std::move(other.stderr_buffer_)),
stderr_offset_(std::exchange(other.stderr_offset_, 0)),
stdout_eof_(other.stdout_eof_),
stderr_eof_(other.stderr_eof_),
stdin_closed_(other.stdin_closed_),
stdout_truncated_(other.stdout_truncated_),
stderr_truncated_(other.stderr_truncated_),
options_(other.options_),
cancelled_(other.cancelled_.exchange(false)),
status_(other.status_),
reaped_(other.reaped_)
{
other.stdout_eof_ = true;
other.stderr_eof_ = true;
other.stdin_closed_ = true;
other.reaped_ = true;
}
BinarySession& BinarySession::operator=(BinarySession&& other) noexcept
{
if (this != &other) {
child_ = std::move(other.child_);
stdin_ = std::move(other.stdin_);
stdout_ = std::move(other.stdout_);
stderr_ = std::move(other.stderr_);
stdout_buffer_ = std::move(other.stdout_buffer_);
stdout_offset_ = std::exchange(other.stdout_offset_, 0);
stderr_buffer_ = std::move(other.stderr_buffer_);
stderr_offset_ = std::exchange(other.stderr_offset_, 0);
stdout_eof_ = other.stdout_eof_;
stderr_eof_ = other.stderr_eof_;
stdin_closed_ = other.stdin_closed_;
stdout_truncated_ = other.stdout_truncated_;
stderr_truncated_ = other.stderr_truncated_;
options_ = other.options_;
cancelled_.store(other.cancelled_.exchange(false));
status_ = other.status_;
reaped_ = other.reaped_;
other.stdout_eof_ = true;
other.stderr_eof_ = true;
other.stdin_closed_ = true;
other.reaped_ = true;
}
return *this;
}
Void::Result::Result<BinarySession, Void::Result::Error>
BinarySession::spawn(const SpawnOptions&, BinarySessionOptions)
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::BinarySession: not implemented on Windows")};
}
bool BinarySession::valid() const noexcept { return false; }
Void::Result::Result<BinaryTransfer, BinaryError> BinarySession::write_stdin(
std::span<const std::byte>, std::chrono::milliseconds, std::stop_token)
{
return std::unexpected{BinaryError{
0,
false,
BinaryFailureCode::io_error,
Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::BinarySession::write_stdin: not implemented on Windows")}};
}
Void::Result::Result<BinaryTransfer, BinaryError> BinarySession::read_stdout(
std::span<std::byte>, std::chrono::milliseconds, std::stop_token)
{
return std::unexpected{BinaryError{
0,
false,
BinaryFailureCode::io_error,
Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::BinarySession::read_stdout: not implemented on Windows")}};
}
Void::Result::Result<BinaryTransfer, BinaryError> BinarySession::read_stderr(
std::span<std::byte>, std::chrono::milliseconds, std::stop_token)
{
return std::unexpected{BinaryError{
0,
false,
BinaryFailureCode::io_error,
Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::BinarySession::read_stderr: not implemented on Windows")}};
}
Void::Result::Result<void, Void::Result::Error> BinarySession::close_stdin()
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::BinarySession::close_stdin: not implemented on Windows")};
}
Void::Result::Result<ExitStatus, Void::Result::Error> BinarySession::wait(
std::chrono::milliseconds, std::stop_token)
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::BinarySession::wait: not implemented on Windows")};
}
Void::Result::Result<ExitStatus, Void::Result::Error> BinarySession::terminate(
std::chrono::milliseconds)
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::BinarySession::terminate: not implemented on Windows")};
}
BinarySession::PollFill BinarySession::poll_fill(int,
std::span<const std::byte>,
std::size_t&,
bool)
{
PollFill result;
result.failure = BinaryFailureCode::io_error;
result.error = Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::BinarySession: not implemented on Windows");
return result;
}
Void::Result::Result<BinaryTransfer, BinaryError> BinarySession::read_stream(
bool, std::span<std::byte>, std::chrono::milliseconds, std::stop_token)
{
return std::unexpected{BinaryError{
0,
false,
BinaryFailureCode::io_error,
Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::BinarySession: not implemented on Windows")}};
}
void BinarySession::note_exit() noexcept {}
Void::Result::Result<CommunicateResult, Void::Result::Error>
communicate_blocking(Child&,
std::string_view,
CaptureLimits,
std::chrono::milliseconds,
std::stop_token,
const StreamCallbacks*)
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::communicate_blocking: not implemented on Windows")};
}
Void::Result::Result<Output, Void::Result::Error> run(
const std::filesystem::path&,
std::vector<std::string>,
std::string_view,
std::chrono::milliseconds)
{
return Void::Result::fail("Process::run: not implemented on Windows");
}
Void::Result::Result<Output, Void::Result::Error> run_streaming(
const std::filesystem::path&,
std::vector<std::string>,
const StreamCallbacks&,
std::string_view,
std::chrono::milliseconds)
{
return Void::Result::fail(
"Process::run_streaming: not implemented on Windows");
}
Void::Result::Result<Output, Void::Result::Error>
shell(std::string_view, std::chrono::milliseconds)
{
return Void::Result::fail("Process::shell: not implemented on Windows");
}
Void::Result::Result<void, Void::Result::Error>
reexec(const std::filesystem::path&, const std::vector<std::string>&)
{
return Void::Result::fail("Process::reexec: not implemented on Windows");
}
Void::Result::Result<void, Void::Result::Error> reexec(
const std::filesystem::path&, const std::vector<std::string>&, bool)
{
return Void::Result::fail("Process::reexec: not implemented on Windows");
}
Void::Result::Result<void, Void::Result::Error> enable_subreaper() noexcept
{
return std::unexpected{Void::System::make_system_error(
Void::System::SystemErrorCode::unsupported,
"Process::enable_subreaper: not supported on Windows")};
}
} // namespace Void::Process
#else // POSIX
#ifndef _WIN32
#include <dirent.h>
#include <fcntl.h>
@@ -896,6 +608,8 @@ void emit_lines_from_offset(const std::string& buf,
} // namespace
Child::Child() noexcept = default;
Child::Child(long long pid,
long long pgid,
bool owns_process_group,
@@ -2511,4 +2225,4 @@ Void::Result::Result<void, Void::Result::Error> enable_subreaper() noexcept
} // namespace Void::Process
#endif // _WIN32
#endif // !_WIN32
File diff suppressed because it is too large. Load diff