1#ifndef NEFORCE_CORE_SYSTEM_PROCESS_HPP__
2#define NEFORCE_CORE_SYSTEM_PROCESS_HPP__
15NEFORCE_BEGIN_NAMESPACE__
27struct process_exception final : system_exception {
28 explicit process_exception(
const char* info =
"Process Operation Failed.",
30 system_exception(info, code) {}
32 explicit process_exception(
const exception& e) :
33 system_exception(e) {}
35 ~process_exception()
override =
default;
37 NEFORCE_NODISCARD
const char*
type() const noexcept
override {
return "process_exception"; }
71#ifdef NEFORCE_PLATFORM_WINDOWS
168 void close_handles() noexcept;
173 void detach_handles() noexcept;
177 bool started_{
false};
178 bool finished_{
false};
181 vector<pair<string, string>> env_vars_;
182 bool capture_stdout_{
false};
183 bool capture_stderr_{
false};
189 pipe* external_stdin_pipe_{
nullptr};
190 pipe* external_stdout_pipe_{
nullptr};
191 pipe* external_stderr_pipe_{
nullptr};
193 bool detached_{
false};
202 thread reader_thread_;
203 atomic<bool> reader_running_{
false};
205#ifdef NEFORCE_PLATFORM_WINDOWS
206 void* process_handle_{
nullptr};
207 void* thread_handle_{
nullptr};
344 elevation_tool tool = elevation_tool::auto_);
379 NEFORCE_NODISCARD
int exit_code() const noexcept {
return exit_code_; }
382 NEFORCE_NODISCARD
const string&
stdout_output() const noexcept {
return stdout_buf_; }
385 NEFORCE_NODISCARD
const string&
stderr_output() const noexcept {
return stderr_buf_; }
488 NEFORCE_NODISCARD static
string search_path(const
string& executable);
493NEFORCE_END_NAMESPACE__
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)
检查对指定进程的访问权限
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)
同步运行进程并返回退出码
process & set_external_stderr(pipe &external_pipe)
将 stderr 连接到外部管道(供下游进程读取)
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
显式关闭进程句柄并清理资源
memory_info get_memory_info() const
获取子进程内存信息
void close_stdin()
关闭子进程的 stdin
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 连接到外部管道(供下游进程读取)
static native_id_type current_id() noexcept
获取当前进程 ID
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()))
获取容器的底层数据指针
size_t peak_pagefile_usage
size_t peak_working_set_size
const char * type() const noexcept override
获取异常类型