libfilezilla
process.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_PROCESS_HEADER
2 #define LIBFILEZILLA_PROCESS_HEADER
3 
4 #include "libfilezilla.hpp"
5 #include "fsresult.hpp"
6 #include "event.hpp"
7 
12 #include <vector>
13 
14 namespace fz {
15 class event_handler;
16 class impersonation_token;
17 class thread_pool;
18 
19 
25 {
27  read = 0x1,
28 
30  write = 0x2,
31 };
32 
34 struct process_event_type;
35 
36 class process;
37 
52 
59 class FZ_PUBLIC_SYMBOL process final
60 {
61 public:
64 
70 
71  ~process();
72  process(process const&) = delete;
73  process& operator=(process const&) = delete;
74 
76  enum class io_redirection {
77  redirect,
78  none,
79  closeall
80  };
81 
95  bool spawn(native_string const& cmd, std::vector<native_string> const& args = std::vector<native_string>(), io_redirection redirect_mode = io_redirection::redirect);
96 
97  bool spawn(std::vector<native_string> const& command_with_args, io_redirection redirect_mode = io_redirection::redirect);
98 
99 #if FZ_WINDOWS || FZ_UNIX
101  bool spawn(impersonation_token const& it, native_string const& cmd, std::vector<native_string> const& args, io_redirection redirect_mode = io_redirection::redirect);
102 #endif
103 
104 #ifndef FZ_WINDOWS
111  bool spawn(native_string const& cmd, std::vector<native_string> const& args, std::vector<int> const& extra_fds, io_redirection redirect_mode = io_redirection::redirect);
112 
113  bool spawn(impersonation_token const& it, native_string const& cmd, std::vector<native_string> const& args, std::vector<int> const& extra_fds, io_redirection redirect_mode = io_redirection::redirect);
114 #endif
115 
123  bool kill(bool wait = true, bool force = false);
124 
133  rwresult read(void* buffer, size_t len);
134 
142  rwresult write(void const* buffer, size_t len);
143 
144  inline rwresult write(std::string_view const& s) {
145  return write(s.data(), s.size());
146  }
147 
148 #if FZ_WINDOWS
152  void* handle() const;
153 #endif
154 
155 private:
156  class impl;
157  impl* impl_;
158 };
159 
160 
169 bool FZ_PUBLIC_SYMBOL spawn_detached_process(std::vector<native_string> const& cmd_with_args);
170 
171 #if !FZ_WINDOWS
181 class FZ_PUBLIC_SYMBOL forkblock final
182 {
183 public:
184  forkblock();
185  ~forkblock();
186 
187  forkblock(forkblock const&) = delete;
188  forkblock& operator=(forkblock const&) = delete;
189 };
190 #endif
191 
192 }
193 
194 #endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition: buffer.hpp:27
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
Impersonation tokens for a given user can be used to spawn processes running as that user.
Definition: impersonation.hpp:33
The process class manages an asynchronous process with redirected IO.
Definition: process.hpp:60
process(fz::thread_pool &pool, fz::event_handler &handler)
Creates instance with non-blocking event-based redirected communication.
rwresult write(void const *buffer, size_t len)
Write data data process.
rwresult read(void *buffer, size_t len)
Read data from process.
bool spawn(native_string const &cmd, std::vector< native_string > const &args=std::vector< native_string >(), io_redirection redirect_mode=io_redirection::redirect)
Start the process.
void * handle() const
Returns the HANDLE of the process.
process()
Creates instance for blocking I/O.
io_redirection
IO redirection modes.
Definition: process.hpp:76
bool spawn(impersonation_token const &it, native_string const &cmd, std::vector< native_string > const &args, io_redirection redirect_mode=io_redirection::redirect)
Creates a process running under the user represented by the impersonation token.
bool kill(bool wait=true, bool force=false)
Stops the spawned process.
Definition: fsresult.hpp:61
This is the recommended event class.
Definition: event.hpp:68
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:64
Declares event_base and simple_event<>
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition: apply.hpp:17
simple_event< process_event_type, process *, process_event_flag > process_event
Definition: process.hpp:36
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:33
bool spawn_detached_process(std::vector< native_string > const &cmd_with_args)
Starts a detached process.
process_event_flag
The type of a process event.
Definition: process.hpp:25
@ read
Data has become available.
@ write
data can be written.