MatN work for versatile appimage creation for all types of os
[goodguy/cinelerra.git] / cinelerra-5.1 / tools / makeappimagetool / includes / process.h
1 // system headers
2 #include <unordered_map>
3 #include <vector>
4 #include <signal.h>
5
6 // local headers
7 #include "subprocess.h"
8
9 namespace linuxdeploy {
10     namespace subprocess {
11         class process {
12         private:
13             // child process ID
14             int child_pid_ = -1;
15
16             // pipes to child process's stdout/stderr
17             int stdout_fd_ = -1;
18             int stderr_fd_ = -1;
19
20             // process exited
21             bool exited_ = false;
22             // exit code -- will be initialized by close()
23             int exit_code_ = -1;
24
25             // these constants help make the pipe code more readable
26             static constexpr int READ_END_ = 0, WRITE_END_ = 1;
27
28             static std::vector<char*> make_args_vector_(const std::vector<std::string>& args);
29
30             static std::vector<char*> make_env_vector_(const subprocess_env_map_t& env);
31
32             static int check_waitpid_status_(int status);
33
34             static void close_pipe_fd_(int fd);
35
36         public:
37             /**
38              * Create a child process.
39              * @param args parameters for process
40              * @param env additional environment variables (current environment will be copied)
41              */
42             process(std::initializer_list<std::string> args, const subprocess_env_map_t& env);
43
44             /**
45              * Create a child process.
46              * @param args parameters for process
47              * @param env additional environment variables (current environment will be copied)
48              */
49             process(const std::vector<std::string>& args, const subprocess_env_map_t& env);
50
51             ~process();
52
53             /**
54              * @return child process's ID
55              */
56             int pid() const;
57
58             /**
59              * @return child process's stdout file descriptor ID
60              */
61             int stdout_fd() const;
62
63
64             /**
65              * @return child process's stderr file descriptor ID
66              */
67             int stderr_fd() const;
68
69             /**
70              * Close all pipes and wait for process to exit.
71              * If process is not running any more, just returns exit code.
72              * @return child process's exit code
73              */
74             int close();
75
76             /**
77              * Kill underlying process with given signal. By default, SIGTERM is used to end the process.
78              */
79             void kill(int signal = SIGTERM) const;
80
81             /**
82              * Check whether process is still alive. Use close() to fetch exit code.
83              * @return true while process is alive, false otherwise
84              */
85             bool is_running();
86         };
87     }
88 }