212 lines
7.1 KiB
C++
212 lines
7.1 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/System/Types.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
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<void(std::string_view line)> on_stdout;
|
|
std::function<void(std::string_view line)> 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<std::string> allowlist{};
|
|
std::vector<std::pair<std::string, std::string>> set_vars{};
|
|
std::vector<std::string> unset_keys{};
|
|
std::vector<std::string> 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<std::string> arguments{};
|
|
std::optional<std::filesystem::path> 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]] bool process_group_alive() 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<void, Void::Result::Error>
|
|
send_signal(int sig) noexcept;
|
|
|
|
[[nodiscard]] Void::Result::Result<ExitStatus, Void::Result::Error>
|
|
terminate(std::chrono::milliseconds grace = std::chrono::milliseconds{-1});
|
|
|
|
[[nodiscard]] Void::Result::Result<std::optional<ExitStatus>, Void::Result::Error>
|
|
try_wait();
|
|
|
|
[[nodiscard]] Void::Result::Result<ExitStatus, Void::Result::Error>
|
|
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<Child, Void::Result::Error>
|
|
spawn(const SpawnOptions& options);
|
|
|
|
[[nodiscard]] Void::Result::Result<CommunicateResult, Void::Result::Error>
|
|
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<Output, Void::Result::Error>
|
|
run(const std::filesystem::path& executable,
|
|
std::vector<std::string> 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<Output, Void::Result::Error>
|
|
run_streaming(
|
|
const std::filesystem::path& executable,
|
|
std::vector<std::string> 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<Output, Void::Result::Error>
|
|
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<void, Void::Result::Error>
|
|
reexec(const std::filesystem::path& executable,
|
|
const std::vector<std::string>& arguments);
|
|
|
|
/// Replace the current process image with explicit open-descriptor cleanup policy.
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
reexec(const std::filesystem::path& executable,
|
|
const std::vector<std::string>& arguments,
|
|
bool close_extra_fds);
|
|
|
|
/// Opt the current process into Linux PR_SET_CHILD_SUBREAPER so orphaned
|
|
/// descendant processes in spawned process groups reparent to this process
|
|
/// and can be reaped directly via waitpid(-pgid, ...).
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
enable_subreaper() noexcept;
|
|
|
|
} // namespace Void::Process
|