1 #ifndef RADARELAB_UTILS_SUBPROCESS_H
2 #define RADARELAB_UTILS_SUBPROCESS_H
10 namespace subprocess {
38 bool m_terminated =
false;
39 int m_stdin[2] = { -1, -1 };
40 int m_stdout[2] = { -1, -1 };
41 int m_stderr[2] = { -1, -1 };
42 Redirect m_stdin_action = Redirect::UNCHANGED;
43 Redirect m_stdout_action = Redirect::UNCHANGED;
44 Redirect m_stderr_action = Redirect::UNCHANGED;
47 virtual void pre_fork();
50 virtual void post_fork_parent();
53 virtual void post_fork_child();
60 virtual int main() noexcept = 0;
64 bool close_fds = true;
70 std::vector<
int> pass_fds;
76 bool start_new_session = false;
79 int get_stdin() const;
81 int get_stdout() const;
83 int get_stderr() const;
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);
106 Child(const Child&) = delete;
107 Child(Child&&) = delete;
110 Child& operator=(const Child&) = delete;
111 Child& operator=(Child&&) = delete;
117 pid_t pid()
const {
return m_pid; }
123 int returncode()
const;
126 int raw_returncode()
const {
return m_returncode; }
129 bool started()
const {
return m_pid != 0; }
132 bool terminated()
const {
return m_terminated; }
141 void send_signal(
int sig);
150 static std::string format_raw_returncode(
int raw_returncode);
154 class Popen :
public Child
157 int main() noexcept override;
161 std::vector<std::
string> args;
163 std::
string executable;
165 std::vector<std::
string> env;
170 Popen(std::initializer_list<std::
string> args);
173 void copy_env_from_parent();
175 void setenv(const std::
string& key, const std::
string& val);