#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Void::Process { inline constexpr std::size_t kMaxSpawnInputBytes = 256ULL * 1024ULL; inline constexpr std::size_t kDefaultMaxStdoutBytes = 1024ULL * 1024ULL; inline constexpr std::size_t kDefaultMaxStderrBytes = 1024ULL * 1024ULL; inline constexpr std::size_t kDefaultMaxLineBytes = 64ULL * 1024ULL; inline constexpr std::size_t kDefaultMaxStdinBytes = 4ULL * 1024ULL * 1024ULL; /// Captured output from a completed subprocess. struct Output { int exit_code{0}; std::string out; ///< captured stdout std::string err; ///< captured stderr }; /// Live line callbacks for run_streaming() and communicate_blocking(). struct StreamCallbacks { std::function on_stdout; std::function on_stderr; }; enum class OverflowPolicy { fail_and_terminate, drain_and_truncate, }; enum class EnvMode { inherit, allowlist, explicit_only, }; struct EnvironmentPolicy { EnvMode mode{EnvMode::inherit}; std::vector allowlist{}; std::vector> set_vars{}; std::vector unset_keys{}; std::vector redacted_keys{}; }; struct CaptureLimits { std::size_t max_stdout_bytes{kDefaultMaxStdoutBytes}; std::size_t max_stderr_bytes{kDefaultMaxStderrBytes}; std::size_t max_line_bytes{kDefaultMaxLineBytes}; std::size_t max_stdin_bytes{kDefaultMaxStdinBytes}; OverflowPolicy overflow_policy{OverflowPolicy::fail_and_terminate}; }; struct SpawnOptions { std::filesystem::path executable{}; std::vector arguments{}; std::optional working_directory{}; EnvironmentPolicy environment{}; bool stdin_pipe{true}; bool stdout_pipe{true}; bool stderr_pipe{true}; bool search_path{true}; bool new_process_group{true}; std::chrono::milliseconds kill_grace{std::chrono::milliseconds{2000}}; }; struct ExitStatus { bool exited{false}; int code{-1}; int signal{0}; int raw_status{0}; }; struct CommunicateResult { ExitStatus status{}; std::string out{}; std::string err{}; bool stdout_truncated{false}; bool stderr_truncated{false}; bool timed_out{false}; bool cancelled{false}; bool overflowed{false}; }; class Child { public: Child() noexcept = default; Child(long long pid, long long pgid, bool owns_process_group, Void::System::OwnedFd stdin_fd, Void::System::OwnedFd stdout_fd, Void::System::OwnedFd stderr_fd, std::chrono::milliseconds kill_grace) noexcept; ~Child() noexcept; Child(const Child&) = delete; Child& operator=(const Child&) = delete; Child(Child&& other) noexcept; Child& operator=(Child&& other) noexcept; [[nodiscard]] bool valid() const noexcept; [[nodiscard]] bool running() const noexcept; [[nodiscard]] long long pid() const noexcept; [[nodiscard]] long long pgid() const noexcept; [[nodiscard]] bool owns_process_group() const noexcept; [[nodiscard]] int stdin_fd() const noexcept; [[nodiscard]] int stdout_fd() const noexcept; [[nodiscard]] int stderr_fd() const noexcept; [[nodiscard]] Void::System::OwnedFd take_stdin_fd() noexcept; [[nodiscard]] Void::System::OwnedFd take_stdout_fd() noexcept; [[nodiscard]] Void::System::OwnedFd take_stderr_fd() noexcept; void close_stdin() noexcept; [[nodiscard]] Void::Result::Result send_signal(int sig) noexcept; [[nodiscard]] Void::Result::Result terminate(std::chrono::milliseconds grace = std::chrono::milliseconds{-1}); [[nodiscard]] Void::Result::Result, Void::Result::Error> try_wait(); [[nodiscard]] Void::Result::Result wait_blocking(std::chrono::milliseconds timeout = std::chrono::milliseconds{0}, std::stop_token stop = {}, std::chrono::milliseconds kill_grace = std::chrono::milliseconds{-1}); private: long long pid_{-1}; long long pgid_{-1}; bool owns_group_{false}; bool reaped_{true}; ExitStatus cached_status_{}; Void::System::OwnedFd stdin_{}; Void::System::OwnedFd stdout_{}; Void::System::OwnedFd stderr_{}; std::chrono::milliseconds kill_grace_{std::chrono::milliseconds{2000}}; void reset_and_reap() noexcept; }; [[nodiscard]] Void::Result::Result spawn(const SpawnOptions& options); [[nodiscard]] Void::Result::Result communicate_blocking(Child& child, std::string_view stdin_data = {}, CaptureLimits limits = {}, std::chrono::milliseconds timeout = std::chrono::milliseconds{0}, std::stop_token stop = {}, const StreamCallbacks* callbacks = nullptr); /// Run an executable synchronously, capturing stdout and stderr separately. [[nodiscard]] Void::Result::Result run(const std::filesystem::path& executable, std::vector args = {}, std::string_view stdin_data = {}, std::chrono::milliseconds timeout = std::chrono::milliseconds{0}); /// Like run(), but invokes `callbacks` line-by-line as stdout/stderr arrive. [[nodiscard]] Void::Result::Result run_streaming( const std::filesystem::path& executable, std::vector args, const StreamCallbacks& callbacks, std::string_view stdin_data = {}, std::chrono::milliseconds timeout = std::chrono::milliseconds{0}); /// Run a command through /bin/sh -c "command". [[nodiscard]] Void::Result::Result shell(std::string_view command, std::chrono::milliseconds timeout = std::chrono::milliseconds{0}); /// Replace the current process image while preserving its PID and inherited /// environment. [[nodiscard]] Void::Result::Result reexec(const std::filesystem::path& executable, const std::vector& arguments); /// Replace the current process image with explicit open-descriptor cleanup policy. [[nodiscard]] Void::Result::Result reexec(const std::filesystem::path& executable, const std::vector& arguments, bool close_extra_fds); } // namespace Void::Process