uvw 3.1.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 _UVW_ENUM = 0
29};
30
31enum class uvw_stdio_flags : std::underlying_type_t<uv_stdio_flags> {
32 IGNORE_STREAM = UV_IGNORE,
33 CREATE_PIPE = UV_CREATE_PIPE,
34 INHERIT_FD = UV_INHERIT_FD,
35 INHERIT_STREAM = UV_INHERIT_STREAM,
36 READABLE_PIPE = UV_READABLE_PIPE,
37 WRITABLE_PIPE = UV_WRITABLE_PIPE,
38 OVERLAPPED_PIPE = UV_OVERLAPPED_PIPE,
39 _UVW_ENUM = 0
40};
41
42} // namespace details
43
45struct exit_event {
46 explicit exit_event(int64_t code, int sig) noexcept;
47
48 int64_t status;
49 int signal;
50};
51
58class process_handle final: public handle<process_handle, uv_process_t, exit_event> {
59 static void exit_callback(uv_process_t *hndl, int64_t exit_status, int term_signal);
60
61public:
62 using process_flags = details::uvw_process_flags;
63 using stdio_flags = details::uvw_stdio_flags;
64
65 process_handle(loop::token token, std::shared_ptr<loop> ref);
66
81 static void disable_stdio_inheritance() noexcept;
82
89 static bool kill(int pid, int signum) noexcept;
90
95 int init() final;
96
109 int spawn(const char *file, char **args, char **env = nullptr);
110
116 int kill(int signum);
117
125 int pid() noexcept;
126
132 process_handle &cwd(const std::string &path) noexcept;
133
154 process_handle &flags(process_flags flags) noexcept;
155
177 template<typename T, typename U, typename... E>
178 process_handle &stdio(stream_handle<T, U, E...> &stream, stdio_flags flags) {
179 uv_stdio_container_t container;
180 container.flags = static_cast<uv_stdio_flags>(flags);
181 container.data.stream = reinterpret_cast<uv_stream_t *>(stream.raw());
182 po_stream_stdio.push_back(std::move(container));
183 return *this;
184 }
185
213
220
227
228private:
229 std::string po_cwd;
230 process_flags po_flags;
231 std::vector<uv_stdio_container_t> po_fd_stdio;
232 std::vector<uv_stdio_container_t> po_stream_stdio;
233 uid_type po_uid;
234 gid_type po_gid;
235};
236
237} // namespace uvw
238
239#ifndef UVW_AS_LIB
240# include "process.cpp"
241#endif
242
243#endif // UVW_PROCESS_INCLUDE_H
Handle base class.
Definition: handle.hpp:23
os_file_descriptor fd() const
Gets the platform dependent file descriptor equivalent.
Definition: handle.hpp:241
The process handle.
Definition: process.h:58
process_handle & stdio(file_handle fd, stdio_flags flags)
Makes a file descriptor available to the child process.
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.
int init() final
Initializes the handle.
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:178
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.
std::shared_ptr< R > data() const
Gets user-defined data. uvw won't use this field in any case.
Definition: resource.hpp:47
The stream handle.
Definition: stream.h:108
uvw default namespace.
Definition: async.h:8
uv_uid_t uid_type
Definition: util.h:98
details::uv_type_wrapper< uv_file > file_handle
Definition: util.h:85
uv_gid_t gid_type
Definition: util.h:99
Exit event.
Definition: process.h:45
int64_t status
Definition: process.h:48