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