1#ifndef WREPORT_SUBPROCESS_H
2#define WREPORT_SUBPROCESS_H
37 bool m_terminated =
false;
38 int m_stdin[2] = { -1, -1 };
39 int m_stdout[2] = { -1, -1 };
40 int m_stderr[2] = { -1, -1 };
41 Redirect m_stdin_action = Redirect::UNCHANGED;
42 Redirect m_stdout_action = Redirect::UNCHANGED;
43 Redirect m_stderr_action = Redirect::UNCHANGED;
59 virtual int main() noexcept = 0;
116 pid_t
pid()
const {
return m_pid; }
174 std::vector<std::
string>
env;
179 Popen(std::initializer_list<std::
string>
args);
184 void setenv(const std::
string& key, const std::
string& val);
Definition: subprocess.h:33
int get_stdout() const
Return the file descriptor to the stdout pipe from the child process, if configured,...
void close_stdout()
Close the pipe from the child process stdout.
void send_signal(int sig)
Send the given signal to the process.
virtual void pre_fork()
Function called before forking.
void set_stdin(int fd)
Request to redirect the child stdin to this given file descriptor.
void close_stderr()
Close the pipe from the child process stderr.
bool poll()
Check if the process has terminated. Returns true if it has.
std::vector< int > pass_fds
Do not close these file descriptors in the child process (implies close_fds = true)
Definition: subprocess.h:69
void set_stdout(int fd)
Request to redirect the child stdout to this given file descriptor.
std::string cwd
Change to this directory in the child process.
Definition: subprocess.h:72
bool start_new_session
If true, call setsid() in the child process.
Definition: subprocess.h:75
void kill()
Send SIGKILL to the process.
void close_stdin()
Close the pipe to the child process stdin.
pid_t pid() const
Return the PID of the subprocess, or 0 if it has not started yet.
Definition: subprocess.h:116
bool started() const
Return true if the process has started.
Definition: subprocess.h:128
bool terminated() const
Return true if the process has terminated.
Definition: subprocess.h:131
void fork()
Start the child process.
int get_stderr() const
Return the file descriptor to the stderr pipe from the child process, if configured,...
int wait()
Wait for the child process to terminate and return its return code.
virtual void post_fork_parent()
Function called after fork in the parent process.
static std::string format_raw_returncode(int raw_returncode)
Format the status code returned by wait(2)
int returncode() const
Return the return code of the subprocess; this is undefined if it has not terminated yet.
int raw_returncode() const
Return the raw return code as returned by wait(2)
Definition: subprocess.h:125
bool wait(int msecs)
Wait for the child process to terminate.
bool close_fds
After fork, close all file descriptors >=2 in the child.
Definition: subprocess.h:63
void terminate()
Send SIGTERM to the process.
virtual void post_fork_child()
Function called after fork in the child process.
int get_stdin() const
Return the file descriptor to the stdin pipe to the child process, if configured, else -1.
void set_stderr(int fd)
Request to redirect the child stderr to this given file descriptor.
virtual int main() noexcept=0
Main function called in the child process.
Definition: subprocess.h:164
std::string executable
pathname to the executable of the child process, defaults to args[0] if empty
Definition: subprocess.h:172
std::vector< std::string > args
argv of the child process
Definition: subprocess.h:170
std::vector< std::string > env
environment variables to use for the child process
Definition: subprocess.h:174
void copy_env_from_parent()
Override env with the contents of environment.
int main() noexcept override
Main function called in the child process.
String functions.
Definition: benchmark.h:13