uvw  2.12.1
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 "handle.hpp"
10 #include "loop.h"
11 #include "stream.h"
12 #include "util.h"
13 
14 namespace uvw {
15 
16 namespace details {
17 
18 enum class UVProcessFlags : std::underlying_type_t<uv_process_flags> {
19  SETUID = UV_PROCESS_SETUID,
20  SETGID = UV_PROCESS_SETGID,
21  WINDOWS_VERBATIM_ARGUMENTS = UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS,
22  DETACHED = UV_PROCESS_DETACHED,
23  WINDOWS_HIDE = UV_PROCESS_WINDOWS_HIDE,
24  WINDOWS_HIDE_CONSOLE = UV_PROCESS_WINDOWS_HIDE_CONSOLE,
25  WINDOWS_HIDE_GUI = UV_PROCESS_WINDOWS_HIDE_GUI
26 };
27 
28 enum class UVStdIOFlags : std::underlying_type_t<uv_stdio_flags> {
29  IGNORE_STREAM = UV_IGNORE,
30  CREATE_PIPE = UV_CREATE_PIPE,
31  INHERIT_FD = UV_INHERIT_FD,
32  INHERIT_STREAM = UV_INHERIT_STREAM,
33  READABLE_PIPE = UV_READABLE_PIPE,
34  WRITABLE_PIPE = UV_WRITABLE_PIPE,
35  OVERLAPPED_PIPE = UV_OVERLAPPED_PIPE
36 };
37 
38 } // namespace details
39 
45 struct ExitEvent {
46  explicit ExitEvent(int64_t code, int sig) noexcept;
47 
48  int64_t status;
49  int signal;
50 };
51 
58 class ProcessHandle final: public Handle<ProcessHandle, uv_process_t> {
59  static void exitCallback(uv_process_t *handle, int64_t exitStatus, int termSignal);
60 
61 public:
62  using Process = details::UVProcessFlags;
63  using StdIO = details::UVStdIOFlags;
64 
65  ProcessHandle(ConstructorAccess ca, std::shared_ptr<Loop> ref);
66 
81  static void disableStdIOInheritance() noexcept;
82 
89  static bool kill(int pid, int signum) noexcept;
90 
95  bool init();
96 
111  void spawn(const char *file, char **args, char **env = nullptr);
112 
117  void kill(int signum);
118 
126  int pid() noexcept;
127 
133  ProcessHandle &cwd(const std::string &path) noexcept;
134 
155  ProcessHandle &flags(Flags<Process> flags) noexcept;
156 
178  template<typename T, typename U>
179  ProcessHandle &stdio(StreamHandle<T, U> &stream, Flags<StdIO> flags) {
180  uv_stdio_container_t container;
181  Flags<StdIO>::Type fgs = flags;
182  container.flags = static_cast<uv_stdio_flags>(fgs);
183  container.data.stream = get<uv_stream_t>(stream);
184  poStreamStdio.push_back(std::move(container));
185  return *this;
186  }
187 
215 
222 
229 
230 private:
231  std::string poCwd;
232  Flags<Process> poFlags;
233  std::vector<uv_stdio_container_t> poFdStdio;
234  std::vector<uv_stdio_container_t> poStreamStdio;
235  Uid poUid;
236  Gid poGid;
237 };
238 
239 } // namespace uvw
240 
241 #ifndef UVW_AS_LIB
242 # include "process.cpp"
243 #endif
244 
245 #endif // UVW_PROCESS_INCLUDE_H
Utility class to handle flags.
Definition: util.h:79
Handle base class.
Definition: handle.hpp:26
OSFileDescriptor fd() const
Gets the platform dependent file descriptor equivalent.
Definition: handle.hpp:260
The ProcessHandle handle.
Definition: process.h:58
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:179
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:48
The StreamHandle handle.
Definition: stream.h:113
uvw default namespace.
Definition: async.h:8
uv_uid_t Uid
Definition: util.h:216
details::UVTypeWrapper< uv_file > FileHandle
Definition: util.h:204
uv_gid_t Gid
Definition: util.h:217
ExitEvent event.
Definition: process.h:45
int64_t status
Definition: process.h:48