WPILibC++  2018.4.1-20180820040250-1165-g0b8f4b5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Process.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_UV_PROCESS_H_
9 #define WPIUTIL_WPI_UV_PROCESS_H_
10 
11 #include <uv.h>
12 
13 #include <memory>
14 #include <string>
15 
16 #include "wpi/Signal.h"
17 #include "wpi/SmallVector.h"
18 #include "wpi/Twine.h"
19 #include "wpi/uv/Handle.h"
20 
21 namespace wpi {
22 namespace uv {
23 
24 class Loop;
25 class Pipe;
26 
30 class ProcessOptions final {
31  friend class Process;
32 
33  public:
39  ProcessOptions& SetEnv(char** env) {
40  m_env = env;
41  return *this;
42  }
43 
48  ProcessOptions& SetCwd(const Twine& cwd) {
49  m_cwd = cwd.str();
50  return *this;
51  }
52 
57  ProcessOptions& SetUid(uv_uid_t uid) noexcept {
58  m_uid = uid;
59  m_flags |= UV_PROCESS_SETUID;
60  return *this;
61  }
62 
67  ProcessOptions& SetGid(uv_gid_t gid) noexcept {
68  m_gid = gid;
69  m_flags |= UV_PROCESS_SETGID;
70  return *this;
71  }
72 
77  ProcessOptions& SetFlags(unsigned int flags) noexcept {
78  m_flags |= flags;
79  return *this;
80  }
81 
86  ProcessOptions& ClearFlags(unsigned int flags) noexcept {
87  m_flags &= ~flags;
88  return *this;
89  }
90 
95  ProcessOptions& StdioIgnore(size_t index);
96 
102  ProcessOptions& StdioInherit(size_t index, int fd);
103 
109  ProcessOptions& StdioInherit(size_t index, Pipe& pipe);
110 
118  ProcessOptions& StdioCreatePipe(size_t index, Pipe& pipe, unsigned int flags);
119 
120  private:
121  char** m_env = nullptr;
122  std::string m_cwd;
123  unsigned int m_flags = 0;
124  struct StdioContainer : public uv_stdio_container_t {
125  StdioContainer() {
126  flags = UV_IGNORE;
127  data.fd = 0;
128  }
129  };
130  SmallVector<StdioContainer, 4> m_stdio;
131  uv_uid_t m_uid = 0;
132  uv_gid_t m_gid = 0;
133 };
134 
140 class Process final : public HandleImpl<Process, uv_process_t> {
141  struct private_init {};
142 
143  public:
144  explicit Process(const private_init&) {}
145  ~Process() noexcept override = default;
146 
156  static void DisableStdioInheritance() { uv_disable_stdio_inheritance(); }
157 
172  static std::shared_ptr<Process> Spawn(
173  Loop& loop, const Twine& file, char** args,
174  const ProcessOptions& options = ProcessOptions{});
175 
190  static std::shared_ptr<Process> Spawn(
191  const std::shared_ptr<Loop>& loop, const Twine& file, char** args,
192  const ProcessOptions& options = ProcessOptions{}) {
193  return Spawn(*loop, file, args, options);
194  }
195 
200  void Kill(int signum) { Invoke(&uv_process_kill, GetRaw(), signum); }
201 
208  static int Kill(int pid, int signum) noexcept { return uv_kill(pid, signum); }
209 
214  uv_pid_t GetPid() const noexcept { return GetRaw()->pid; }
215 
221 };
222 
223 } // namespace uv
224 } // namespace wpi
225 
226 #endif // WPIUTIL_WPI_UV_PROCESS_H_
uv_pid_t GetPid() const noexcept
Get the process ID.
Definition: Process.h:214
ProcessOptions & SetGid(uv_gid_t gid) noexcept
Set the child process' group id.
Definition: Process.h:67
uv_process_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:261
Handle.
Definition: Handle.h:246
static int Kill(int pid, int signum) noexcept
Sends the specified signal to the given PID.
Definition: Process.h:208
static void DisableStdioInheritance()
Disables inheritance for file descriptors / handles that this process inherited from its parent...
Definition: Process.h:156
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
sig::Signal< int64_t, int > exited
Signal generated when the process exits.
Definition: Process.h:220
Process handle.
Definition: Process.h:140
ProcessOptions & SetEnv(char **env)
Set environment variables for the subprocess.
Definition: Process.h:39
Pipe handle.
Definition: Pipe.h:31
ProcessOptions & SetCwd(const Twine &cwd)
Set the current working directory for the subprocess.
Definition: Process.h:48
ProcessOptions & StdioCreatePipe(size_t index, Pipe &pipe, unsigned int flags)
Create a pipe between the child and the parent.
std::string str() const
Return the twine contents as a std::string.
ProcessOptions & StdioInherit(size_t index, int fd)
Inherit a file descriptor from the parent process.
static std::shared_ptr< Process > Spawn(const std::shared_ptr< Loop > &loop, const Twine &file, char **args, const ProcessOptions &options=ProcessOptions{})
Starts a process.
Definition: Process.h:190
ProcessOptions & StdioIgnore(size_t index)
Explicitly ignore a stdio.
Event loop.
Definition: Loop.h:37
Definition: uv.h:865
Process options.
Definition: Process.h:30
ProcessOptions & SetUid(uv_uid_t uid) noexcept
Set the child process' user id.
Definition: Process.h:57
static std::shared_ptr< Process > Spawn(Loop &loop, const Twine &file, char **args, const ProcessOptions &options=ProcessOptions{})
Starts a process.
void Kill(int signum)
Sends the specified signal to the process.
Definition: Process.h:200
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
ProcessOptions & SetFlags(unsigned int flags) noexcept
Set flags.
Definition: Process.h:77
ProcessOptions & ClearFlags(unsigned int flags) noexcept
Clear flags.
Definition: Process.h:86