Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
child_process.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3
4#include <chrono>
5#include <cstdint>
6#include <filesystem>
7#include <functional>
8#include <memory>
9#include <optional>
10#include <span>
11#include <string>
12#include <string_view>
13#include <vector>
14
15namespace pulp::platform {
16
19 int exit_code = -1;
20 std::string stdout_output;
21 std::string stderr_output;
22 bool timed_out = false;
23 bool was_cancelled = false;
28};
29
32 std::string working_directory;
38 std::function<bool(int process_id)> suspended_process_validator;
39 int timeout_ms = 0;
40 size_t max_output_bytes = 1 << 20;
41 bool capture_stdout = true;
42 bool capture_stderr = true;
45 std::function<void(std::string_view line)> on_stdout_line;
48 std::function<void(std::string_view line)> on_stderr_line;
51};
52
59 std::function<std::optional<std::vector<std::uint8_t>>(int child_process_id)>;
60
64public:
70
71 std::intptr_t native_handle() const noexcept { return handle_; }
72 explicit operator bool() const noexcept { return handle_ >= 0; }
73
74private:
75 friend class ChildProcess;
76 explicit ChildProcessInputChannel(std::intptr_t handle) noexcept : handle_(handle) {}
77 std::intptr_t handle_ = -1;
78};
79
85 std::function<bool(int child_process_id, ChildProcessInputChannel channel,
86 std::chrono::steady_clock::time_point deadline)>;
87
92public:
96 ChildProcess& operator=(ChildProcess&&) noexcept;
97
98 ChildProcess(const ChildProcess&) = delete;
99 ChildProcess& operator=(const ChildProcess&) = delete;
100
102 static ProcessResult run(const std::string& command,
103 const std::vector<std::string>& args,
104 const ProcessOptions& options = {});
105
107 bool start(const std::string& command,
108 const std::vector<std::string>& args,
109 const ProcessOptions& options = {});
110
113 bool start_with_standard_input(const std::string& command, const std::vector<std::string>& args,
114 std::span<const std::uint8_t> bytes,
115 const ProcessOptions& options = {});
116
121 bool start_with_standard_input(const std::string& command, const std::vector<std::string>& args,
122 const StandardInputByteProvider& provider,
123 const ProcessOptions& options = {});
124
128 bool start_with_standard_input_channel(const std::string& command,
129 const std::vector<std::string>& args,
130 const StandardInputChannelSession& session,
131 const ProcessOptions& options = {});
132
134 bool is_running() const;
135
139 int process_id() const;
140
144 void cancel();
145
148
151
152private:
153 bool start_impl(const std::string& command, const std::vector<std::string>& args,
154 const ProcessOptions& options,
155 const std::span<const std::uint8_t>* standard_input,
156 const StandardInputByteProvider* standard_input_provider,
157 const StandardInputChannelSession* standard_input_session);
158
159 struct Impl;
160 std::unique_ptr<Impl> impl_;
161};
162
164ProcessResult exec(const std::string& command,
165 const std::vector<std::string>& args = {},
166 int timeout_ms = 30000);
167
169std::optional<std::filesystem::path> find_on_path(const std::string& binary_name);
170
171} // namespace pulp::platform
Move-only parent endpoint of the child's inherited standard-input channel.
Definition child_process.hpp:63
std::intptr_t native_handle() const noexcept
Definition child_process.hpp:71
ChildProcessInputChannel & operator=(const ChildProcessInputChannel &)=delete
ChildProcessInputChannel & operator=(ChildProcessInputChannel &&other) noexcept
friend class ChildProcess
Definition child_process.hpp:75
ChildProcessInputChannel(ChildProcessInputChannel &&other) noexcept
ChildProcessInputChannel(const ChildProcessInputChannel &)=delete
void cancel()
Request cancellation.
static ProcessResult run(const std::string &command, const std::vector< std::string > &args, const ProcessOptions &options={})
Blocking: run a command and return the result.
int process_id() const
Return the child process id, or -1 if no process has started.
bool start(const std::string &command, const std::vector< std::string > &args, const ProcessOptions &options={})
Non-blocking: start a command.
ChildProcess(ChildProcess &&) noexcept
bool start_with_standard_input(const std::string &command, const std::vector< std::string > &args, const StandardInputByteProvider &provider, const ProcessOptions &options={})
Spawn the child blocked on a private inherited standard-input pipe, then obtain the bytes from a prov...
bool is_running() const
Check if the started process is still running.
ProcessResult wait()
Wait for the process to complete and return the result.
std::string read_available_output()
Read any available output without blocking (for non-blocking mode).
bool start_with_standard_input(const std::string &command, const std::vector< std::string > &args, std::span< const std::uint8_t > bytes, const ProcessOptions &options={})
Start with bytes delivered through a private inherited standard-input pipe.
bool start_with_standard_input_channel(const std::string &command, const std::vector< std::string > &args, const StandardInputChannelSession &session, const ProcessOptions &options={})
Spawn with a private duplex standard-input channel and transfer the parent endpoint to a post-spawn s...
Device capability vocabulary: one tier ladder, one thermal ladder, and the projection from observed i...
Definition child_process.hpp:15
std::optional< std::filesystem::path > find_on_path(const std::string &binary_name)
Check if a binary exists on PATH. Returns the full path if found.
ProcessResult exec(const std::string &command, const std::vector< std::string > &args={}, int timeout_ms=30000)
Convenience: run a command and capture output (blocking).
std::function< std::optional< std::vector< std::uint8_t > >(int child_process_id)> StandardInputByteProvider
Produces private standard-input bytes after the child has been spawned.
Definition child_process.hpp:58
std::function< bool(int child_process_id, ChildProcessInputChannel channel, std::chrono::steady_clock::time_point deadline)> StandardInputChannelSession
Owns a post-spawn exchange over the child's private inherited input channel.
Definition child_process.hpp:84
STL namespace.
Options for child process execution.
Definition child_process.hpp:31
bool capture_stdout
Definition child_process.hpp:41
std::function< void(std::string_view line)> on_stderr_line
Called for each complete captured line on stderr while output is drained.
Definition child_process.hpp:48
int working_directory_descriptor
POSIX directory descriptor consumed atomically by spawn instead of resolving working_directory.
Definition child_process.hpp:35
bool capture_stderr
Definition child_process.hpp:42
int timeout_ms
0 = no timeout
Definition child_process.hpp:39
std::string working_directory
Definition child_process.hpp:32
std::function< bool(int process_id)> suspended_process_validator
macOS-only validation hook.
Definition child_process.hpp:38
size_t max_standard_input_provider_bytes
Cap provider-returned bytes.
Definition child_process.hpp:50
std::function< void(std::string_view line)> on_stdout_line
Called for each complete captured line on stdout while output is drained.
Definition child_process.hpp:45
int standard_input_timeout_ms
Bound inherited-input delivery.
Definition child_process.hpp:49
size_t max_output_bytes
1 MB default cap
Definition child_process.hpp:40
Result of a completed child process.
Definition child_process.hpp:18
bool timed_out
Definition child_process.hpp:22
bool output_limit_exceeded
At least one captured stream produced more than max_output_bytes.
Definition child_process.hpp:27
std::string stderr_output
Definition child_process.hpp:21
std::string stdout_output
Definition child_process.hpp:20
bool was_cancelled
Definition child_process.hpp:23
int exit_code
Definition child_process.hpp:19