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.",
const char*
type = static_type,
29 const int code = 0) noexcept :
30 system_exception(info,
type,
code) {}
32 explicit process_exception(
const exception& e) :
33 system_exception(e) {}
35 ~process_exception()
override =
default;
36 static constexpr auto static_type =
"process_exception";
69#ifdef NEFORCE_PLATFORM_WINDOWS
156 void close_handles() noexcept;
160 bool started_{
false};
161 bool finished_{
false};
164 vector<pair<string, string>> env_vars_;
165 bool capture_stdout_{
false};
166 bool capture_stderr_{
false};
176 thread reader_thread_;
177 atomic<bool> reader_running_{
false};
179#ifdef NEFORCE_PLATFORM_WINDOWS
180 void* process_handle_{
nullptr};
181 void* thread_handle_{
nullptr};
260 elevation_tool tool = elevation_tool::auto_);
295 NEFORCE_NODISCARD
int exit_code() const noexcept {
return exit_code_; }
298 NEFORCE_NODISCARD
const string&
stdout_output() const noexcept {
return stdout_buf_; }
301 NEFORCE_NODISCARD
const string&
stderr_output() const noexcept {
return stderr_buf_; }
372NEFORCE_END_NAMESPACE__
static privilege_level get_privilege_level(native_id_type process_id)
获取指定进程的特权级别
void start_elevated(const string &executable, const vector< string > &args={}, elevation_tool tool=elevation_tool::auto_)
以管理员/root 权限启动子进程
int wait(int timeout_ms=-1)
等待子进程退出
process & set_stdin_data(const string &data)
预设要写入子进程 stdin 的数据
static shell_result execute_shell(const string &command, int timeout_ms=-1)
执行 shell 命令并获取输出
memory_info get_memory_info() const
获取子进程内存信息
void close() noexcept
显式关闭进程句柄并清理资源
void close_stdin()
关闭子进程的 stdin
process & set_capture_stdout(bool v=true)
捕获标准输出
static shell_result execute_elevated_shell(const string &command, int timeout_ms=-1, elevation_tool tool=elevation_tool::auto_)
以管理员/root 权限执行 shell 命令
static native_id_type current_id() noexcept
获取当前进程 ID
bool is_running() const
子进程是否正在运行
void start(const string &executable, const vector< string > &args={})
启动子进程
int exit_code() const noexcept
子进程退出码(仅在 wait() 返回后有效)
const string & stderr_output() const noexcept
获取捕获的标准错误
process & operator=(process &&other) noexcept
移动赋值
static privilege_level current_privilege_level() noexcept
获取当前进程的特权级别
process & set_work_dir(const string &dir)
设置子进程工作目录
static bool check_permission(native_id_type process_id, permission perm)
检查对指定进程的访问权限
state get_state() const
获取子进程状态
void write_stdin(const string &data)
向子进程的 stdin 写入数据
process & set_env(const string &key, const string &value)
设置子进程环境变量
const string & stdout_output() const noexcept
获取捕获的标准输出
process & set_capture_stderr(bool v=true)
捕获标准错误
unsigned long native_id_type
进程ID类型
void terminate()
终止子进程(SIGTERM → 短暂等待 → SIGKILL)
process(process &&other) noexcept
移动构造
static string name(native_id_type process_id)
根据进程 ID 获取进程名称
unsigned long long uint64_t
64位无符号整数类型
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
const char * type() const noexcept
获取异常类型
int code() const noexcept
获取异常码
size_t peak_pagefile_usage
size_t peak_working_set_size