libwreport  3.40
subprocess.h
1 #ifndef WREPORT_SUBPROCESS_H
2 #define WREPORT_SUBPROCESS_H
3 
4 #include <string>
5 #include <sys/types.h>
6 #include <vector>
7 
8 namespace wreport {
9 namespace subprocess {
10 
11 enum class Redirect {
15  PIPE,
16 
18  DEVNULL,
19 
21  STDOUT,
22 
24  FD,
25 
27  UNCHANGED,
28 };
29 
30 class Child
31 {
32 protected:
33  pid_t m_pid = 0;
34  int m_returncode = 0;
35  bool m_terminated = false;
36  int m_stdin[2] = {-1, -1};
37  int m_stdout[2] = {-1, -1};
38  int m_stderr[2] = {-1, -1};
39  Redirect m_stdin_action = Redirect::UNCHANGED;
40  Redirect m_stdout_action = Redirect::UNCHANGED;
41  Redirect m_stderr_action = Redirect::UNCHANGED;
42 
44  virtual void pre_fork();
45 
47  virtual void post_fork_parent();
48 
50  virtual void post_fork_child();
51 
57  virtual int main() noexcept = 0;
58 
59 public:
61  bool close_fds = true;
62 
67  std::vector<int> pass_fds = std::vector<int>();
68 
70  std::string cwd = std::string();
71 
73  bool start_new_session = false;
74 
77  int get_stdin() const;
80  int get_stdout() const;
83  int get_stderr() const;
84 
86  void set_stdin(int fd);
88  void set_stdin(Redirect val);
90  void set_stdout(int fd);
92  void set_stdout(Redirect val);
94  void set_stderr(int fd);
96  void set_stderr(Redirect val);
97 
99  void close_stdin();
101  void close_stdout();
103  void close_stderr();
104 
105  Child() = default;
106  Child(const Child&) = delete;
107  Child(Child&&) = delete;
108  virtual ~Child();
109 
110  Child& operator=(const Child&) = delete;
111  Child& operator=(Child&&) = delete;
112 
114  void fork();
115 
117  pid_t pid() const { return m_pid; }
118 
123  int returncode() const;
124 
126  int raw_returncode() const { return m_returncode; }
127 
129  bool started() const { return m_pid != 0; }
130 
132  bool terminated() const { return m_terminated; }
133 
135  bool poll();
136 
138  int wait();
139 
148  bool wait(int msecs);
149 
151  void send_signal(int sig);
152 
154  void terminate();
155 
157  void kill();
158 
160  static std::string format_raw_returncode(int raw_returncode);
161 };
162 
163 class Popen : public Child
164 {
165 protected:
166  int main() noexcept override;
167 
168 public:
170  std::vector<std::string> args;
173  std::string executable;
175  std::vector<std::string> env;
176 
177  using Child::Child;
178 
179  Popen() = default;
180  Popen(std::initializer_list<std::string> args);
181 
184 
185  void setenv(const std::string& key, const std::string& val);
186 };
187 
188 } // namespace subprocess
189 } // namespace wreport
190 
191 #endif
Definition: subprocess.h:31
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:67
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:70
bool start_new_session
If true, call setsid() in the child process.
Definition: subprocess.h:73
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:117
bool started() const
Return true if the process has started.
Definition: subprocess.h:129
bool terminated() const
Return true if the process has terminated.
Definition: subprocess.h:132
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:126
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:61
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:173
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:175
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