uvw 3.4.0
Loading...
Searching...
No Matches
process.h
1#ifndef UVW_PROCESS_INCLUDE_H
2#define UVW_PROCESS_INCLUDE_H
3
4#include <memory>
5#include <string>
6#include <utility>
7#include <vector>
8#include <uv.h>
9#include "config.h"
10#include "enum.hpp"
11#include "handle.hpp"
12#include "loop.h"
13#include "stream.h"
14#include "util.h"
15
16namespace uvw {
17
18namespace details {
19
20enum class uvw_process_flags : std::underlying_type_t<uv_process_flags> {
21 SETUID = UV_PROCESS_SETUID,
22 SETGID = UV_PROCESS_SETGID,
23 WINDOWS_VERBATIM_ARGUMENTS = UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS,
24 DETACHED = UV_PROCESS_DETACHED,
25 WINDOWS_HIDE = UV_PROCESS_WINDOWS_HIDE,
26 WINDOWS_HIDE_CONSOLE = UV_PROCESS_WINDOWS_HIDE_CONSOLE,
27 WINDOWS_HIDE_GUI = UV_PROCESS_WINDOWS_HIDE_GUI,
28 WINDOWS_FILE_PATH_EXACT_NAME = UV_PROCESS_WINDOWS_FILE_PATH_EXACT_NAME,
29 _UVW_ENUM = 0
30};
31
32enum class uvw_stdio_flags : std::underlying_type_t<uv_stdio_flags> {
33 IGNORE_STREAM = UV_IGNORE,
34 CREATE_PIPE = UV_CREATE_PIPE,
35 INHERIT_FD = UV_INHERIT_FD,
36 INHERIT_STREAM = UV_INHERIT_STREAM,
37 READABLE_PIPE = UV_READABLE_PIPE,
38 WRITABLE_PIPE = UV_WRITABLE_PIPE,
39 OVERLAPPED_PIPE = UV_OVERLAPPED_PIPE,
40 _UVW_ENUM = 0
41};
42
43} // namespace details
44
46struct exit_event {
47 explicit exit_event(int64_t code, int sig) noexcept;
48
49 int64_t status;
50 int signal;
51};
52
59class process_handle final: public handle<process_handle, uv_process_t, exit_event> {
60 static void exit_callback(uv_process_t *hndl, int64_t exit_status, int term_signal);
61
62public:
63 using process_flags = details::uvw_process_flags;
64 using stdio_flags = details::uvw_stdio_flags;
65
66 process_handle(loop::token token, std::shared_ptr<loop> ref);
67
82 static void disable_stdio_inheritance() noexcept;
83
90 static bool kill(int pid, int signum) noexcept;
91
96 int init();
97
110 int spawn(const char *file, char **args, char **env = nullptr);
111
117 int kill(int signum);
118
126 int pid() noexcept;
127
133 process_handle &cwd(const std::string &path) noexcept;
134
156 process_handle &flags(process_flags flags) noexcept;
157
179 template<typename T, typename U, typename... E>
180 process_handle &stdio(stream_handle<T, U, E...> &stream, stdio_flags flags) {
181 uv_stdio_container_t container;
182 container.flags = static_cast<uv_stdio_flags>(flags);
183 container.data.stream = reinterpret_cast<uv_stream_t *>(stream.raw());
184 po_stream_stdio.push_back(std::move(container));
185 return *this;
186 }
187
214 process_handle &stdio(file_handle fd, stdio_flags flags);
215
221 process_handle &uid(uid_type id);
222
228 process_handle &gid(gid_type id);
229
230private:
231 std::string po_cwd;
232 process_flags po_flags;
233 std::vector<uv_stdio_container_t> po_fd_stdio;
234 std::vector<uv_stdio_container_t> po_stream_stdio;
235 uid_type po_uid;
236 gid_type po_gid;
237};
238
239} // namespace uvw
240
241#ifndef UVW_AS_LIB
242# include "process.cpp"
243#endif
244
245#endif // UVW_PROCESS_INCLUDE_H
Handle base class.
Definition handle.hpp:23
process_handle & stdio(file_handle fd, stdio_flags flags)
Makes a file descriptor available to the child process.
int init()
Initializes the handle.
process_handle & gid(gid_type id)
Sets the child process' group id.
int spawn(const char *file, char **args, char **env=nullptr)
spawn Starts the process.
process_handle & flags(process_flags flags) noexcept
Sets flags that control how spawn() behaves.
static bool kill(int pid, int signum) noexcept
kill Sends the specified signal to the given PID.
process_handle & uid(uid_type id)
Sets the child process' user id.
process_handle & stdio(stream_handle< T, U, E... > &stream, stdio_flags flags)
Makes a stdio handle available to the child process.
Definition process.h:180
int pid() noexcept
Gets the PID of the spawned process.
process_handle & cwd(const std::string &path) noexcept
Sets the current working directory for the subprocess.
static void disable_stdio_inheritance() noexcept
Disables inheritance for file descriptors/handles.
The stream handle.
Definition stream.h:108
uvw default namespace.
Definition async.h:8
uv_gid_t gid_type
Definition util.h:99
uv_uid_t uid_type
Definition util.h:98
details::uv_type_wrapper< uv_file > file_handle
Definition util.h:85
int64_t status
Definition process.h:49