NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
process.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_SYSTEM_PROCESS_HPP__
2#define NEFORCE_CORE_SYSTEM_PROCESS_HPP__
3
10
15NEFORCE_BEGIN_NAMESPACE__
16
22
27struct process_exception final : system_exception {
28 explicit process_exception(const char* info = "Process Operation Failed.",
29 const error_code code = last_error()) noexcept :
30 system_exception(info, code) {}
31
32 explicit process_exception(const exception& e) :
33 system_exception(e) {}
34
35 ~process_exception() override = default;
36
37 NEFORCE_NODISCARD const char* type() const noexcept override { return "process_exception"; }
38};
39 // Exceptions
41
47
65class NEFORCE_API process {
66public:
71#ifdef NEFORCE_PLATFORM_WINDOWS
72 unsigned long;
73#else
74 int;
75#endif
76
81 enum class std_stream {
82 stdin,
83 stdout,
84 stderr
85 };
86
93 struct memory_info {
94 size_t working_set_size{0};
96 size_t pagefile_usage{0};
98 };
99
111
116 enum class state {
117 running,
118 suspended,
119 stopped,
120 exited,
121 unknown
122 };
123
128 enum class permission {
129 read = 0x01,
130 write = 0x02,
131 execute = 0x04,
132 terminate = 0x08,
133 query_info = 0x10,
134 all = 0xFF
135 };
136
141 enum class privilege_level {
142 privileged,
143 not_privileged,
144 unknown
145 };
146
150 enum class elevation_tool {
151 auto_,
152 sudo,
153 pkexec
154 };
155
162 string output;
163 };
164
165private:
166 void reader_loop();
167
168 void close_handles() noexcept;
169
173 void detach_handles() noexcept;
174
175 native_id_type process_id_{0};
176 int exit_code_{-1};
177 bool started_{false};
178 bool finished_{false};
179
180 string work_dir_;
181 vector<pair<string, string>> env_vars_;
182 bool capture_stdout_{false};
183 bool capture_stderr_{false};
184 string stdin_data_;
185
186 string stdout_file_;
187 string stderr_file_;
188
189 pipe* external_stdin_pipe_{nullptr};
190 pipe* external_stdout_pipe_{nullptr};
191 pipe* external_stderr_pipe_{nullptr};
192
193 bool detached_{false};
194
195 pipe stdout_pipe_;
196 pipe stderr_pipe_;
197 pipe stdin_pipe_;
198
199 string stdout_buf_;
200 string stderr_buf_;
201
202 thread reader_thread_;
203 atomic<bool> reader_running_{false};
204
205#ifdef NEFORCE_PLATFORM_WINDOWS
206 void* process_handle_{nullptr};
207 void* thread_handle_{nullptr};
208#endif
209
210public:
216 process() = default;
217
224
225 process(const process&) = delete;
226 process& operator=(const process&) = delete;
227
231 process(process&& other) noexcept;
232
236 process& operator=(process&& other) noexcept;
237
242 process& set_work_dir(const string& dir);
243
249 process& set_env(const string& key, const string& value);
250
256
262
267 process& set_stdin_data(const string& data);
268
274 process& set_stdout_file(const string& file_path);
275
281 process& set_stderr_file(const string& file_path);
282
290
298
306
315 static void chain(process& first, process& second);
316
325 static void chain(vector<process*>& processes);
326
333 void start(const string& executable, const vector<string>& args = {});
334
343 void start_elevated(const string& executable, const vector<string>& args = {},
344 elevation_tool tool = elevation_tool::auto_);
345
352 int wait(int timeout_ms = -1);
353
355 void terminate();
356
358 void suspend();
359
361 void resume();
362
364 void close() noexcept;
365
367 NEFORCE_NODISCARD bool is_running() const;
368
370 NEFORCE_NODISCARD state get_state() const;
371
373 NEFORCE_NODISCARD memory_info get_memory_info() const;
374
376 NEFORCE_NODISCARD native_id_type id() const noexcept { return process_id_; }
377
379 NEFORCE_NODISCARD int exit_code() const noexcept { return exit_code_; }
380
382 NEFORCE_NODISCARD const string& stdout_output() const noexcept { return stdout_buf_; }
383
385 NEFORCE_NODISCARD const string& stderr_output() const noexcept { return stderr_buf_; }
386
388 void write_stdin(const string& data);
389
392
400 NEFORCE_NODISCARD static shell_result execute_shell(const string& command, int timeout_ms = -1);
401
411 static shell_result execute_elevated_shell(const string& command, int timeout_ms = -1,
413
422 static native_id_type spawn(const string& executable, const vector<string>& args = {});
423
432 static int system(const string& executable, const vector<string>& args = {}, int timeout_ms = -1);
433
442 static string capture(const string& executable, const vector<string>& args = {}, int timeout_ms = -1);
443
445 NEFORCE_NODISCARD static native_id_type current_id() noexcept;
446
448 NEFORCE_NODISCARD static privilege_level current_privilege_level() noexcept;
449
455 NEFORCE_NODISCARD static string name(native_id_type process_id);
456
461 NEFORCE_NODISCARD static memory_info get_memory_info(native_id_type process_id);
462
467 NEFORCE_NODISCARD static state get_state(native_id_type process_id);
468
473 NEFORCE_NODISCARD static privilege_level get_privilege_level(native_id_type process_id);
474
481 NEFORCE_NODISCARD static bool check_permission(native_id_type process_id, permission permission);
482
488 NEFORCE_NODISCARD static string search_path(const string& executable);
489};
490 // Process
492
493NEFORCE_END_NAMESPACE__
494#endif // NEFORCE_CORE_SYSTEM_PROCESS_HPP__
原子类型完整实现
匿名管道类
process & set_capture_stderr(bool v=true)
捕获标准错误
void terminate()
终止子进程(SIGTERM → 短暂等待 → SIGKILL)
static string name(native_id_type process_id)
根据进程 ID 获取进程名称
static string search_path(const string &executable)
在 PATH 环境变量中搜索可执行文件
void write_stdin(const string &data)
向子进程的 stdin 写入数据
static privilege_level get_privilege_level(native_id_type process_id)
获取指定进程的特权级别
static shell_result execute_shell(const string &command, int timeout_ms=-1)
执行 shell 命令并获取输出
process & set_stdout_file(const string &file_path)
将 stdout 重定向到文件
static void chain(vector< process * > &processes)
管道连接多个进程
unsigned long native_id_type
进程ID类型
static privilege_level current_privilege_level() noexcept
获取当前进程的特权级别
static bool check_permission(native_id_type process_id, permission permission)
检查对指定进程的访问权限
privilege_level
进程特权级别
process & set_capture_stdout(bool v=true)
捕获标准输出
process & operator=(process &&other) noexcept
移动赋值
process & set_stdin_data(const string &data)
预设要写入子进程 stdin 的数据
static int system(const string &executable, const vector< string > &args={}, int timeout_ms=-1)
同步运行进程并返回退出码
state
进程状态枚举
process & set_external_stderr(pipe &external_pipe)
将 stderr 连接到外部管道(供下游进程读取)
void suspend()
挂起子进程
static void chain(process &first, process &second)
管道连接两个进程(A 的 stdout -> B 的 stdin)
process & set_env(const string &key, const string &value)
设置子进程环境变量
static string capture(const string &executable, const vector< string > &args={}, int timeout_ms=-1)
执行进程并捕获 stdout 输出
const string & stderr_output() const noexcept
获取捕获的标准错误
bool is_running() const
子进程是否正在运行
void close() noexcept
显式关闭进程句柄并清理资源
void resume()
恢复子进程
permission
进程权限枚举
memory_info get_memory_info() const
获取子进程内存信息
void close_stdin()
关闭子进程的 stdin
std_stream
标准流标识
void start(const string &executable, const vector< string > &args={})
启动子进程
int wait(int timeout_ms=-1)
等待子进程退出
void start_elevated(const string &executable, const vector< string > &args={}, elevation_tool tool=elevation_tool::auto_)
以管理员/root 权限启动子进程
static shell_result execute_elevated_shell(const string &command, int timeout_ms=-1, elevation_tool tool=elevation_tool::auto_)
以管理员/root 权限执行 shell 命令
process & set_work_dir(const string &dir)
设置子进程工作目录
process & set_external_stdin(pipe &external_pipe)
设置外部管道作为 stdin(用于进程链)
process & set_stderr_file(const string &file_path)
将 stderr 重定向到文件
process & set_external_stdout(pipe &external_pipe)
将 stdout 连接到外部管道(供下游进程读取)
elevation_tool
提权工具选择
static native_id_type current_id() noexcept
获取当前进程 ID
process()=default
默认构造
static native_id_type spawn(const string &executable, const vector< string > &args={})
启动子进程并分离(fire-and-forget)
const string & stdout_output() const noexcept
获取捕获的标准输出
process(process &&other) noexcept
移动构造
int exit_code() const noexcept
子进程退出码(仅在 wait() 返回后有效)
state get_state() const
获取子进程状态
动态大小数组容器
unsigned long uint64_t
64位无符号整数类型
error_code last_error() noexcept
获取当前系统错误码
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
跨平台管道封装
shell 命令执行结果
const char * type() const noexcept override
获取异常类型
线程管理类
动态大小数组容器