78#include <unordered_map>
373 void start(
const std::string& program,
const std::vector<std::string>& arguments = {});
432 void close(
bool force_kill_on_timeout =
false);
485 size_t read_stdout(std::vector<uint8_t>& buffer,
size_t max_size);
494 size_t read_stderr(std::vector<uint8_t>& buffer,
size_t max_size);
579 static int execute(
const std::string& program,
const std::vector<std::string>& arguments = {},
580 int timeout_ms = kDefaultExecuteTimeoutMs);
593 static bool start_detached(
const std::string& program,
const std::vector<std::string>& arguments = {});
596 struct ReadResult final {
597 bool has_stdout_data{
false};
598 bool has_stderr_data{
false};
599 bool truncated{
false};
600 bool read_error{
false};
603 void set_state(State state);
605 void set_error(Error error);
609 ReadResult read_from_pipes();
611 void report_read_result(
const ReadResult& result);
613 void read_from_pipes_with_lock();
617 bool start_program(
const std::string& program,
const std::vector<std::string>& arguments);
619 void monitor_thread();
621 void start_monitor_thread();
623 void stop_monitor_thread();
625 void handle_process_exit(
int exit_code, ExitStatus status);
627 void invoke_callbacks_outside_lock(Error error_to_report,
bool has_finished,
int exit_code_to_report,
628 ExitStatus exit_status_to_report, State state_to_report,
bool has_state_changed,
629 bool has_stdout_data,
bool has_stderr_data);
631 std::unique_ptr<struct ProcessImpl> impl_;
size_t read_stderr(std::vector< uint8_t > &buffer, size_t max_size)
Reads up to max_size bytes from stderr into buffer.
static constexpr int kDestructorWaitTimeoutMs
Time the destructor waits for the child to exit before force-killing in milliseconds.
Definition process.h:188
static constexpr int kDefaultWriteTimeoutMs
Default timeout for write() in milliseconds.
Definition process.h:178
bool wait_for_ready_read(int msecs=kDefaultWaitTimeoutMs)
Blocks until new data is available on any pipe.
size_t write(const std::vector< uint8_t > &buffer, int timeout_ms=kDefaultWriteTimeoutMs)
Writes buffer to the child's stdin.
Mode get_process_mode() const
Returns the configured I/O channel mode.
std::unordered_map< std::string, std::string > EnvironmentMap
Environment variable map type for set_environment().
Definition process.h:140
~Process()
Destructor. Closes the process and waits up to kDestructorWaitTimeoutMs.
Error get_error() const
Returns the last error code.
static constexpr int kDefaultExecuteTimeoutMs
Default timeout for the synchronous execute() helper in milliseconds.
Definition process.h:183
bool can_read_line_stdout() const
Returns true if a complete newline-terminated line is available on stdout.
std::function< void(int, ExitStatus)> FinishedCallback
Callback type invoked when the process exits.
Definition process.h:153
void register_ready_read_stderr_callback(ReadyReadCallback &&callback)
Registers a callback invoked when new stderr data is available.
std::function< void(Error)> ErrorCallback
Callback type invoked when an error occurs.
Definition process.h:145
void set_max_buffer_size(size_t size)
Sets the maximum buffer size for stdout and stderr capture.
EnvironmentMap get_environment() const
Returns the configured environment map.
ExitStatus get_exit_status() const
Returns how the child process exited.
void register_error_callback(ErrorCallback &&callback)
Registers a callback for error events.
void set_inherit_environment(bool inherit)
Controls whether the child inherits the parent's environment.
int64_t get_process_id() const
Returns the operating-system process ID of the child.
Mode
I/O channel routing mode.
Definition process.h:129
@ kForwardedMode
Both stdout and stderr forwarded to the parent process.
Definition process.h:132
@ kMergedMode
stderr merged into stdout pipe
Definition process.h:131
@ kForwardedErrorMode
stderr forwarded; stdout buffered separately
Definition process.h:134
@ kSeparateMode
stdout and stderr buffered as separate pipes
Definition process.h:130
@ kForwardedOutputMode
stdout forwarded; stderr buffered separately
Definition process.h:133
void start_command(const std::string &command)
Parses and launches a shell command string.
static constexpr int kDefaultWaitTimeoutMs
Default timeout for wait_for_started() and wait_for_finished() in milliseconds.
Definition process.h:173
void close_write_channel()
Closes the write channel (stdin pipe), signalling EOF to the child.
Process & operator=(Process &&other) noexcept=delete
size_t get_max_buffer_size() const
Returns the configured maximum buffer size.
static constexpr int kInfinite
Sentinel wait timeout meaning wait indefinitely.
Definition process.h:168
bool wait_for_finished(int msecs=kDefaultWaitTimeoutMs)
Blocks until the child process exits.
static bool start_detached(const std::string &program, const std::vector< std::string > &arguments={})
Starts a program in the background and returns immediately.
bool read_line_stderr(std::string &line)
Reads one line from stderr into line.
bool get_inherit_environment() const
Returns whether the child inherits the parent environment.
Process(Process &&other) noexcept=delete
Process()
Constructs a Process object. No child is started yet.
ExitStatus
How the child process exited.
Definition process.h:107
@ kNormalExitStatus
Exited normally (via exit() or return from main).
Definition process.h:108
@ kCrashExitStatus
Killed by a signal or crashed.
Definition process.h:109
bool read_line_stdout(std::string &line)
Reads one line from stdout into line.
bool read_all_output(std::string &str)
Reads all available stdout data into str.
size_t bytes_available_stderr() const
Returns the number of bytes available to read from stderr.
void set_process_mode(Mode mode)
Sets the I/O channel routing mode.
bool read_all_error(std::string &str)
Reads all available stderr data into str.
void register_ready_read_stdout_callback(ReadyReadCallback &&callback)
Registers a callback invoked when new stdout data is available.
void register_state_changed_callback(StateChangedCallback &&callback)
Registers a callback invoked when the process state changes.
bool is_running() const
Returns true if the child process is currently running.
size_t read_stdout(std::vector< uint8_t > &buffer, size_t max_size)
Reads up to max_size bytes from stdout into buffer.
bool read_all(std::vector< uint8_t > &buffer)
Reads all available stdout and stderr data into buffer (byte vector overload).
size_t bytes_available_stdout() const
Returns the number of bytes available to read from stdout.
void close(bool force_kill_on_timeout=false)
Calls terminate() and optionally force-kills after a timeout.
std::string get_working_directory() const
Returns the configured working directory.
bool read_all_error(std::vector< uint8_t > &buffer)
Reads all available stderr data into buffer (byte vector overload).
State get_state() const
Returns the current state of the child process.
void set_environment(const EnvironmentMap &env_map)
Sets the environment variables for the child process.
std::function< void(State)> StateChangedCallback
Callback type invoked when the process state changes.
Definition process.h:163
int get_exit_code() const
Returns the exit code of the child process.
void set_working_directory(const std::string &dir)
Sets the working directory for the child process.
bool read_all_output(std::vector< uint8_t > &buffer)
Reads all available stdout data into buffer (byte vector overload).
void start(const std::string &program, const std::vector< std::string > &arguments={})
Launches the child process.
size_t write(const std::string &str, int timeout_ms=kDefaultWriteTimeoutMs)
Writes a string to the child's stdin.
std::function< void()> ReadyReadCallback
Callback type invoked when new data is available on a pipe.
Definition process.h:158
void kill()
Forcefully kills the child process (SIGKILL / TerminateProcess).
State
Lifecycle state of the child process.
Definition process.h:98
@ kStartingState
start() called; waiting for exec to complete
Definition process.h:100
@ kRunningState
Successfully running.
Definition process.h:101
@ kNotRunningState
Not started or has exited.
Definition process.h:99
void register_finished_callback(FinishedCallback &&callback)
Registers a callback invoked when the child exits.
bool read_all(std::string &str)
Reads all available stdout and stderr data into str.
void terminate()
Requests child termination.
Error
Error codes set on failure.
Definition process.h:115
@ kUnknownError
Unknown error.
Definition process.h:117
@ kTimedOutError
A wait operation timed out.
Definition process.h:120
@ kBufferOverflowError
Output exceeded max_buffer_size.
Definition process.h:123
@ kCrashedError
Process crashed.
Definition process.h:119
@ kWriteError
Write to stdin failed.
Definition process.h:121
@ kNoError
No error.
Definition process.h:116
@ kStartError
Failed to start the process (e.g., file not found).
Definition process.h:118
@ kReadError
Read from stdout/stderr failed.
Definition process.h:122
bool wait_for_started(int msecs=kDefaultWaitTimeoutMs)
Blocks until the child process enters kRunningState.
static int execute(const std::string &program, const std::vector< std::string > &arguments={}, int timeout_ms=kDefaultExecuteTimeoutMs)
Synchronously executes a program and waits for it to finish.
bool can_read_line_stderr() const
Returns true if a complete newline-terminated line is available on stderr.
Platform-independent macro definitions for the VLink library.
#define VLINK_EXPORT
Definition macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184