进程管理类
更多...
#include <process.hpp>
|
| enum class | std_stream { stdin
, stdout
, stderr
} |
| | 标准流标识 更多...
|
| enum class | state {
running
, suspended
, stopped
, exited
,
unknown
} |
| | 进程状态枚举 更多...
|
| enum class | permission {
read = 0x01
, write = 0x02
, execute = 0x04
, terminate = 0x08
,
query_info = 0x10
, all = 0xFF
} |
| | 进程权限枚举 更多...
|
| enum class | privilege_level { privileged
, not_privileged
, unknown
} |
| | 进程特权级别 更多...
|
| enum class | elevation_tool { auto_
, sudo
, pkexec
} |
| | 提权工具选择 更多...
|
|
using | native_id_type |
| | 进程ID类型
|
|
| static void | chain (process &first, process &second) |
| | 管道连接两个进程(A 的 stdout -> B 的 stdin)
|
| static void | chain (vector< process * > &processes) |
| | 管道连接多个进程
|
| static shell_result | execute_shell (const string &command, int timeout_ms=-1) |
| | 执行 shell 命令并获取输出
|
| 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 | spawn (const string &executable, const vector< string > &args={}) |
| | 启动子进程并分离(fire-and-forget)
|
| static int | system (const string &executable, const vector< string > &args={}, int timeout_ms=-1) |
| | 同步运行进程并返回退出码
|
| static string | capture (const string &executable, const vector< string > &args={}, int timeout_ms=-1) |
| | 执行进程并捕获 stdout 输出
|
|
static native_id_type | current_id () noexcept |
| | 获取当前进程 ID
|
|
static privilege_level | current_privilege_level () noexcept |
| | 获取当前进程的特权级别
|
| static string | name (native_id_type process_id) |
| | 根据进程 ID 获取进程名称
|
| static memory_info | get_memory_info (native_id_type process_id) |
| | 查询任意进程的内存信息
|
| static state | get_state (native_id_type process_id) |
| | 查询任意进程的运行状态
|
| static privilege_level | get_privilege_level (native_id_type process_id) |
| | 获取指定进程的特权级别
|
| static bool | check_permission (native_id_type process_id, permission permission) |
| | 检查对指定进程的访问权限
|
| static string | search_path (const string &executable) |
| | 在 PATH 环境变量中搜索可执行文件
|
进程管理类
支持 stdout/stderr 分离捕获、stdin 写入、工作目录和环境变量设置, 外部管道连接、文件重定向和进程链。
p.
start(
"/bin/ls", {
"-la"});
process & set_capture_stderr(bool v=true)
捕获标准错误
process & set_capture_stdout(bool v=true)
捕获标准输出
void start(const string &executable, const vector< string > &args={})
启动子进程
int wait(int timeout_ms=-1)
等待子进程退出
process & set_work_dir(const string &dir)
设置子进程工作目录
const string & stdout_output() const noexcept
获取捕获的标准输出
在文件 process.hpp 第 65 行定义.
◆ elevation_tool
提权工具选择
| 枚举值 |
|---|
| auto_ | Windows 忽略此选项;Linux 自动尝试 pkexec,回退至 sudo
|
| sudo | Linux 强制使用 sudo 提权
|
| pkexec | Linux 强制使用 pkexec 提权
|
在文件 process.hpp 第 150 行定义.
◆ permission
进程权限枚举
| 枚举值 |
|---|
| read | 读取权限
|
| write | 写入权限
|
| execute | 执行权限
|
| terminate | 终止权限
|
| query_info | 查询信息权限
|
| all | 所有权限
|
在文件 process.hpp 第 128 行定义.
◆ privilege_level
进程特权级别
| 枚举值 |
|---|
| privileged | 具有管理员/root 权限
|
| not_privileged | 普通权限
|
| unknown | 无法确定权限级别
|
在文件 process.hpp 第 141 行定义.
◆ state
进程状态枚举
| 枚举值 |
|---|
| running | 进程正在运行
|
| suspended | 进程已被挂起
|
| stopped | 进程已停止
|
| exited | 进程已退出
|
| unknown | 进程状态未知
|
在文件 process.hpp 第 116 行定义.
◆ std_stream
标准流标识
| 枚举值 |
|---|
| stdin | 标准输入
|
| stdout | 标准输出
|
| stderr | 标准错误
|
在文件 process.hpp 第 81 行定义.
◆ process()
| neforce::process::process |
( |
| ) |
|
|
default |
默认构造
不启动任何进程
被这些函数引用 chain(), operator=(), process(), set_capture_stderr(), set_capture_stdout(), set_env(), set_external_stderr(), set_external_stdin(), set_external_stdout(), set_stderr_file(), set_stdin_data(), set_stdout_file(), set_work_dir() , 以及 ~process().
◆ ~process()
| neforce::process::~process |
( |
| ) |
|
◆ capture()
执行进程并捕获 stdout 输出
- 参数
-
| executable | 可执行文件路径 |
| args | 命令行参数 |
| timeout_ms | 超时毫秒,-1 无限等待 |
- 返回
- 标准输出字符串
- 异常
-
◆ chain() [1/2]
管道连接两个进程(A 的 stdout -> B 的 stdin)
- 参数
-
| first | 上游进程(已配置但未启动) |
| second | 下游进程(已配置但未启动) |
内部创建管道并将 first 的 stdout 连接到 second 的 stdin。 管道生命周期由下游进程管理。
引用了 process().
◆ chain() [2/2]
管道连接多个进程
- 参数
-
| processes | 要连接的进程列表(已配置但未启动) |
将进程列表中的进程按顺序用管道串联: processes[0] stdout -> processes[1] stdin, processes[1] stdout -> processes[2] stdin, ...
◆ check_permission()
◆ execute_elevated_shell()
以管理员/root 权限执行 shell 命令
- 参数
-
| command | 命令字符串 |
| timeout_ms | 超时毫秒,-1 无限等待 |
| tool | 提权工具选择 |
- 返回
- 包含退出码和空输出的 shell_result
- 异常
-
- 注解
- 不捕获输出
引用了 auto_.
◆ execute_shell()
| shell_result neforce::process::execute_shell |
( |
const string & | command, |
|
|
int | timeout_ms = -1 ) |
|
staticnodiscard |
执行 shell 命令并获取输出
- 参数
-
| command | 命令字符串 |
| timeout_ms | 超时毫秒,-1 无限等待 |
- 返回
- 包含退出码和合并的 stdout+stderr 的 shell_result
- 异常
-
◆ get_memory_info()
◆ get_privilege_level()
◆ get_state()
◆ name()
根据进程 ID 获取进程名称
- 参数
-
- 返回
- 进程名称,失败返回空字符串
引用了 name().
被这些函数引用 name().
◆ search_path()
| string neforce::process::search_path |
( |
const string & | executable | ) |
|
|
staticnodiscard |
◆ set_capture_stderr()
| process & neforce::process::set_capture_stderr |
( |
bool | v = true | ) |
|
捕获标准错误
- 参数
-
| v | 是否捕获,为 true 时 stderr 写入独立缓冲区 |
引用了 process().
◆ set_capture_stdout()
| process & neforce::process::set_capture_stdout |
( |
bool | v = true | ) |
|
◆ set_env()
◆ set_external_stderr()
| process & neforce::process::set_external_stderr |
( |
pipe & | external_pipe | ) |
|
将 stderr 连接到外部管道(供下游进程读取)
- 参数
-
- 返回
- 自身引用
- 注解
- 该管道生命周期由调用者管理
引用了 process().
◆ set_external_stdin()
| process & neforce::process::set_external_stdin |
( |
pipe & | external_pipe | ) |
|
设置外部管道作为 stdin(用于进程链)
- 参数
-
| external_pipe | 上游进程的 stdout 连接的管道 |
- 返回
- 自身引用
- 注解
- 该管道生命周期由调用者管理,process 不拥有所有权
引用了 process().
◆ set_external_stdout()
| process & neforce::process::set_external_stdout |
( |
pipe & | external_pipe | ) |
|
将 stdout 连接到外部管道(供下游进程读取)
- 参数
-
- 返回
- 自身引用
- 注解
- 该管道生命周期由调用者管理
引用了 process().
◆ set_stderr_file()
| process & neforce::process::set_stderr_file |
( |
const string & | file_path | ) |
|
◆ set_stdin_data()
| process & neforce::process::set_stdin_data |
( |
const string & | data | ) |
|
◆ set_stdout_file()
| process & neforce::process::set_stdout_file |
( |
const string & | file_path | ) |
|
◆ set_work_dir()
◆ spawn()
启动子进程并分离(fire-and-forget)
- 参数
-
| executable | 可执行文件路径 |
| args | 命令行参数 |
- 返回
- 子进程 ID
启动后立即分离,不等待。进程在后台运行。
◆ start()
| void neforce::process::start |
( |
const string & | executable, |
|
|
const vector< string > & | args = {} ) |
启动子进程
- 参数
-
| executable | 可执行文件路径 |
| args | 命令行参数列表 |
- 异常
-
◆ start_elevated()
以管理员/root 权限启动子进程
- 参数
-
| executable | 可执行文件路径 |
| args | 命令行参数列表 |
| tool | 提权工具 |
- 异常
-
- 注解
- 不捕获输出
◆ system()
| int neforce::process::system |
( |
const string & | executable, |
|
|
const vector< string > & | args = {}, |
|
|
int | timeout_ms = -1 ) |
|
static |
同步运行进程并返回退出码
- 参数
-
| executable | 可执行文件路径 |
| args | 命令行参数 |
| timeout_ms | 超时毫秒,-1 无限等待 |
- 返回
- 退出码
- 异常
-
◆ wait()
| int neforce::process::wait |
( |
int | timeout_ms = -1 | ) |
|
等待子进程退出
- 参数
-
| timeout_ms | 超时毫秒数,-1 表示无限等待 |
- 返回
- 退出码,-1 超时
- 异常
-
该类的文档由以下文件生成: