uvw  2.10.0
process.h
1 #ifndef UVW_PROCESS_INCLUDE_H
2 #define UVW_PROCESS_INCLUDE_H
3 
4 
5 #include <utility>
6 #include <memory>
7 #include <string>
8 #include <vector>
9 #include <uv.h>
10 #include "handle.hpp"
11 #include "stream.h"
12 #include "util.h"
13 #include "loop.h"
14 
15 
16 namespace uvw {
17 
18 
19 namespace details {
20 
21 
22 enum class UVProcessFlags: std::underlying_type_t<uv_process_flags> {
23  SETUID = UV_PROCESS_SETUID,
24  SETGID = UV_PROCESS_SETGID,
25  WINDOWS_VERBATIM_ARGUMENTS = UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS,
26  DETACHED = UV_PROCESS_DETACHED,
27  WINDOWS_HIDE = UV_PROCESS_WINDOWS_HIDE,
28  WINDOWS_HIDE_CONSOLE = UV_PROCESS_WINDOWS_HIDE_CONSOLE,
29  WINDOWS_HIDE_GUI = UV_PROCESS_WINDOWS_HIDE_GUI
30 };
31 
32 
33 enum class UVStdIOFlags: std::underlying_type_t<uv_stdio_flags> {
34  IGNORE_STREAM = UV_IGNORE,
35  CREATE_PIPE = UV_CREATE_PIPE,
36  INHERIT_FD = UV_INHERIT_FD,
37  INHERIT_STREAM = UV_INHERIT_STREAM,
38  READABLE_PIPE = UV_READABLE_PIPE,
39  WRITABLE_PIPE = UV_WRITABLE_PIPE,
40  OVERLAPPED_PIPE = UV_OVERLAPPED_PIPE
41 };
42 
43 
44 }
45 
46 
52 struct ExitEvent {
53  explicit ExitEvent(int64_t code, int sig) noexcept;
54 
55  int64_t status;
56  int signal;
57 };
58 
65 class ProcessHandle final: public Handle<ProcessHandle, uv_process_t> {
66  static void exitCallback(uv_process_t *handle, int64_t exitStatus, int termSignal);
67 
68 public:
69  using Process = details::UVProcessFlags;
70  using StdIO = details::UVStdIOFlags;
71 
72  ProcessHandle(ConstructorAccess ca, std::shared_ptr<Loop> ref);
73 
88  static void disableStdIOInheritance() noexcept;
89 
96  static bool kill(int pid, int signum) noexcept;
97 
102  bool init();
103 
118  void spawn(const char *file, char **args, char **env = nullptr);
119 
124  void kill(int signum);
125 
133  int pid() noexcept;
134 
140  ProcessHandle & cwd(const std::string &path) noexcept;
141 
162  ProcessHandle & flags(Flags<Process> flags) noexcept;
163 
185  template<typename T, typename U>
186  ProcessHandle & stdio(StreamHandle<T, U> &stream, Flags<StdIO> flags) {
187  uv_stdio_container_t container;
188  Flags<StdIO>::Type fgs = flags;
189  container.flags = static_cast<uv_stdio_flags>(fgs);
190  container.data.stream = get<uv_stream_t>(stream);
191  poStreamStdio.push_back(std::move(container));
192  return *this;
193  }
194 
222 
229 
236 
237 private:
238  std::string poCwd;
239  Flags<Process> poFlags;
240  std::vector<uv_stdio_container_t> poFdStdio;
241  std::vector<uv_stdio_container_t> poStreamStdio;
242  Uid poUid;
243  Gid poGid;
244 };
245 
246 
247 }
248 
249 
250 #ifndef UVW_AS_LIB
251 #include "process.cpp"
252 #endif
253 
254 
255 #endif // UVW_PROCESS_INCLUDE_H
Utility class to handle flags.
Definition: util.h:82
Handle base class.
Definition: handle.hpp:30
OSFileDescriptor fd() const
Gets the platform dependent file descriptor equivalent.
Definition: handle.hpp:267
The ProcessHandle handle.
Definition: process.h:65
ProcessHandle & uid(Uid id)
Sets the child process' user id.
ProcessHandle & stdio(FileHandle fd, Flags< StdIO > flags)
Makes a file descriptor available to the child process.
bool init()
Initializes the handle.
ProcessHandle & cwd(const std::string &path) noexcept
Sets the current working directory for the subprocess.
int pid() noexcept
Gets the PID of the spawned process.
ProcessHandle & stdio(StreamHandle< T, U > &stream, Flags< StdIO > flags)
Makes a stdio handle available to the child process.
Definition: process.h:186
static void disableStdIOInheritance() noexcept
Disables inheritance for file descriptors/handles.
void spawn(const char *file, char **args, char **env=nullptr)
spawn Starts the process.
ProcessHandle & flags(Flags< Process > flags) noexcept
Sets flags that control how spawn() behaves.
ProcessHandle & gid(Gid id)
Sets the child process' group id.
static bool kill(int pid, int signum) noexcept
kill Sends the specified signal to the given PID.
std::shared_ptr< R > data() const
Gets user-defined data. uvw won't use this field in any case.
Definition: resource.hpp:52
The StreamHandle handle.
Definition: stream.h:128
uvw default namespace.
Definition: async.h:10
uv_uid_t Uid
Definition: util.h:201
details::UVTypeWrapper< uv_file > FileHandle
Definition: util.h:189
uv_gid_t Gid
Definition: util.h:202
ExitEvent event.
Definition: process.h:52
int64_t status
Definition: process.h:55