libwreport  3.42
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::subprocess {
9 
10 enum class Redirect {
14  PIPE,
15 
17  DEVNULL,
18 
20  STDOUT,
21 
23  FD,
24 
26  UNCHANGED,
27 };
28 
29 class Child
30 {
31 protected:
32  pid_t m_pid = 0;
33  int m_returncode = 0;
34  bool m_terminated = false;
35  int m_stdin[2] = {-1, -1};
36  int m_stdout[2] = {-1, -1};
37  int m_stderr[2] = {-1, -1};
38  Redirect m_stdin_action = Redirect::UNCHANGED;
39  Redirect m_stdout_action = Redirect::UNCHANGED;
40  Redirect m_stderr_action = Redirect::UNCHANGED;
41 
43  virtual void pre_fork();
44 
46  virtual void post_fork_parent();
47 
49  virtual void post_fork_child();
50 
56  virtual int main() noexcept = 0;
57 
58 public:
60  bool close_fds = true;
61 
66  std::vector<int> pass_fds = std::vector<int>();
67 
69  std::string cwd = std::string();
70 
72  bool start_new_session = false;
73 
76  int get_stdin() const;
79  int get_stdout() const;
82  int get_stderr() const;
83 
85  void set_stdin(int fd);
87  void set_stdin(Redirect val);
89  void set_stdout(int fd);
91  void set_stdout(Redirect val);
93  void set_stderr(int fd);
95  void set_stderr(Redirect val);
96 
98  void close_stdin();
100  void close_stdout();
102  void close_stderr();
103 
104  Child() = default;
105  Child(const Child&) = delete;
106  Child(Child&&) = delete;
107  virtual ~Child();
108 
109  Child& operator=(const Child&) = delete;
110  Child& operator=(Child&&) = delete;
111 
113  void fork();
114 
116  pid_t pid() const { return m_pid; }
117 
122  int returncode() const;
123 
125  int raw_returncode() const { return m_returncode; }
126 
128  bool started() const { return m_pid != 0; }
129 
131  bool terminated() const { return m_terminated; }
132 
134  bool poll();
135 
137  int wait();
138 
147  bool wait(int msecs);
148 
150  void send_signal(int sig);
151 
153  void terminate();
154 
156  void kill();
157 
159  static std::string format_raw_returncode(int raw_returncode);
160 };
161 
162 class Popen : public Child
163 {
164 protected:
165  int main() noexcept override;
166 
167 public:
169  std::vector<std::string> args;
172  std::string executable;
174  std::vector<std::string> env;
175 
176  using Child::Child;
177 
178  Popen() = default;
179  Popen(std::initializer_list<std::string> args);
180 
182  void copy_env_from_parent();
183 
184  void setenv(const std::string& key, const std::string& val);
185 };
186 
187 } // namespace wreport::subprocess
188 
189 #endif
int get_stdin() const
Return the file descriptor to the stdin pipe to the child process, if configured, else -1...
std::string executable
pathname to the executable of the child process, defaults to args[0] if empty
Definition: subprocess.h:172
Definition: subprocess.h:162
int raw_returncode() const
Return the raw return code as returned by wait(2)
Definition: subprocess.h:125
std::vector< std::string > args
argv of the child process
Definition: subprocess.h:169
void close_stderr()
Close the pipe from the child process stderr.
std::string cwd
Change to this directory in the child process.
Definition: subprocess.h:69
int returncode() const
Return the return code of the subprocess; this is undefined if it has not terminated yet...
void close_stdin()
Close the pipe to the child process stdin.
void set_stderr(int fd)
Request to redirect the child stderr to this given file descriptor.
bool start_new_session
If true, call setsid() in the child process.
Definition: subprocess.h:72
bool terminated() const
Return true if the process has terminated.
Definition: subprocess.h:131
static std::string format_raw_returncode(int raw_returncode)
Format the status code returned by wait(2)
void fork()
Start the child process.
void send_signal(int sig)
Send the given signal to the process.
virtual void post_fork_child()
Function called after fork in the child process.
pid_t pid() const
Return the PID of the subprocess, or 0 if it has not started yet.
Definition: subprocess.h:116
int get_stdout() const
Return the file descriptor to the stdout pipe from the child process, if configured, else -1.
void kill()
Send SIGKILL to the process.
bool started() const
Return true if the process has started.
Definition: subprocess.h:128
bool poll()
Check if the process has terminated. Returns true if it has.
Definition: subprocess.h:8
int wait()
Wait for the child process to terminate and return its return code.
bool close_fds
After fork, close all file descriptors >=2 in the child.
Definition: subprocess.h:60
virtual void pre_fork()
Function called before forking.
virtual int main() noexcept=0
Main function called in the child process.
void set_stdout(int fd)
Request to redirect the child stdout to this given file descriptor.
void copy_env_from_parent()
Override env with the contents of environment.
std::vector< std::string > env
environment variables to use for the child process
Definition: subprocess.h:174
Definition: subprocess.h:29
int get_stderr() const
Return the file descriptor to the stderr pipe from the child process, if configured, else -1.
std::vector< int > pass_fds
Do not close these file descriptors in the child process (implies close_fds = true) ...
Definition: subprocess.h:66
int main() noexcept override
Main function called in the child process.
void close_stdout()
Close the pipe from the child process stdout.
void set_stdin(int fd)
Request to redirect the child stdin to this given file descriptor.
virtual void post_fork_parent()
Function called after fork in the parent process.
void terminate()
Send SIGTERM to the process.