NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
neforce::process类 参考

进程管理类 更多...

#include <process.hpp>

struct  memory_info
 进程内存信息 更多...
struct  time_info
 进程时间信息 更多...
struct  shell_result
 shell 命令执行结果 更多...

Public 类型

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类型

Public 成员函数

 process ()=default
 默认构造
 ~process ()
 析构
 process (process &&other) noexcept
 移动构造
processoperator= (process &&other) noexcept
 移动赋值
processset_work_dir (const string &dir)
 设置子进程工作目录
processset_env (const string &key, const string &value)
 设置子进程环境变量
processset_capture_stdout (bool v=true)
 捕获标准输出
processset_capture_stderr (bool v=true)
 捕获标准错误
processset_stdin_data (const string &data)
 预设要写入子进程 stdin 的数据
processset_stdout_file (const string &file_path)
 将 stdout 重定向到文件
processset_stderr_file (const string &file_path)
 将 stderr 重定向到文件
processset_external_stdin (pipe &external_pipe)
 设置外部管道作为 stdin(用于进程链)
processset_external_stdout (pipe &external_pipe)
 将 stdout 连接到外部管道(供下游进程读取)
processset_external_stderr (pipe &external_pipe)
 将 stderr 连接到外部管道(供下游进程读取)
void start (const string &executable, const vector< string > &args={})
 启动子进程
void start_elevated (const string &executable, const vector< string > &args={}, elevation_tool tool=elevation_tool::auto_)
 以管理员/root 权限启动子进程
int wait (int timeout_ms=-1)
 等待子进程退出
void terminate ()
 终止子进程(SIGTERM → 短暂等待 → SIGKILL)
void suspend ()
 挂起子进程
void resume ()
 恢复子进程
void close () noexcept
 显式关闭进程句柄并清理资源
bool is_running () const
 子进程是否正在运行
state get_state () const
 获取子进程状态
memory_info get_memory_info () const
 获取子进程内存信息
native_id_type id () const noexcept
 子进程 ID
int exit_code () const noexcept
 子进程退出码(仅在 wait() 返回后有效)
const stringstdout_output () const noexcept
 获取捕获的标准输出
const stringstderr_output () const noexcept
 获取捕获的标准错误
void write_stdin (const string &data)
 向子进程的 stdin 写入数据
void close_stdin ()
 关闭子进程的 stdin

静态 Public 成员函数

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 写入、工作目录和环境变量设置, 外部管道连接、文件重定向和进程链。

.set_work_dir("/tmp");
p.start("/bin/ls", {"-la"});
int rc = p.wait(5000);
string out = p.stdout_output();
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)
设置子进程工作目录
process()=default
默认构造
const string & stdout_output() const noexcept
获取捕获的标准输出

在文件 process.hpp65 行定义.

成员枚举类型说明

◆ elevation_tool

提权工具选择

枚举值
auto_ 

Windows 忽略此选项;Linux 自动尝试 pkexec,回退至 sudo

sudo 

Linux 强制使用 sudo 提权

pkexec 

Linux 强制使用 pkexec 提权

在文件 process.hpp150 行定义.

◆ permission

enum class neforce::process::permission
strong

进程权限枚举

枚举值
read 

读取权限

write 

写入权限

execute 

执行权限

terminate 

终止权限

query_info 

查询信息权限

all 

所有权限

在文件 process.hpp128 行定义.

◆ privilege_level

进程特权级别

枚举值
privileged 

具有管理员/root 权限

not_privileged 

普通权限

unknown 

无法确定权限级别

在文件 process.hpp141 行定义.

◆ state

enum class neforce::process::state
strong

进程状态枚举

枚举值
running 

进程正在运行

suspended 

进程已被挂起

stopped 

进程已停止

exited 

进程已退出

unknown 

进程状态未知

在文件 process.hpp116 行定义.

◆ std_stream

enum class neforce::process::std_stream
strong

标准流标识

枚举值
stdin 

标准输入

stdout 

标准输出

stderr 

标准错误

在文件 process.hpp81 行定义.

构造及析构函数说明

◆ process()

◆ ~process()

neforce::process::~process ( )

析构

自动终止未退出的子进程并清理资源

引用了 process().

成员函数说明

◆ capture()

string neforce::process::capture ( const string & executable,
const vector< string > & args = {},
int timeout_ms = -1 )
static

执行进程并捕获 stdout 输出

参数
executable可执行文件路径
args命令行参数
timeout_ms超时毫秒,-1 无限等待
返回
标准输出字符串
异常
process_exception超时或执行失败

◆ chain() [1/2]

void neforce::process::chain ( process & first,
process & second )
static

管道连接两个进程(A 的 stdout -> B 的 stdin)

参数
first上游进程(已配置但未启动)
second下游进程(已配置但未启动)

内部创建管道并将 first 的 stdout 连接到 second 的 stdin。 管道生命周期由下游进程管理。

引用了 process().

◆ chain() [2/2]

void neforce::process::chain ( vector< process * > & processes)
static

管道连接多个进程

参数
processes要连接的进程列表(已配置但未启动)

将进程列表中的进程按顺序用管道串联: processes[0] stdout -> processes[1] stdin, processes[1] stdout -> processes[2] stdin, ...

◆ check_permission()

bool neforce::process::check_permission ( native_id_type process_id,
permission permission )
staticnodiscard

检查对指定进程的访问权限

参数
process_id目标进程 ID
permission要检查的权限
返回
是否拥有该权限

引用了 check_permission().

被这些函数引用 check_permission().

◆ execute_elevated_shell()

shell_result neforce::process::execute_elevated_shell ( const string & command,
int timeout_ms = -1,
elevation_tool tool = elevation_tool::auto_ )
static

以管理员/root 权限执行 shell 命令

参数
command命令字符串
timeout_ms超时毫秒,-1 无限等待
tool提权工具选择
返回
包含退出码和空输出的 shell_result
异常
process_exception执行失败或超时
注解
不捕获输出

引用了 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
异常
process_exception执行失败或超时

◆ get_memory_info()

memory_info neforce::process::get_memory_info ( native_id_type process_id)
staticnodiscard

查询任意进程的内存信息

参数
process_id目标进程 ID

引用了 get_memory_info().

◆ get_privilege_level()

privilege_level neforce::process::get_privilege_level ( native_id_type process_id)
staticnodiscard

获取指定进程的特权级别

参数
process_id目标进程 ID

引用了 get_privilege_level().

被这些函数引用 get_privilege_level().

◆ get_state()

state neforce::process::get_state ( native_id_type process_id)
staticnodiscard

查询任意进程的运行状态

参数
process_id目标进程 ID

引用了 get_state().

◆ name()

string neforce::process::name ( native_id_type process_id)
staticnodiscard

根据进程 ID 获取进程名称

参数
process_id目标进程 ID
返回
进程名称,失败返回空字符串

引用了 name().

被这些函数引用 name().

◆ search_path()

string neforce::process::search_path ( const string & executable)
staticnodiscard

在 PATH 环境变量中搜索可执行文件

参数
executable可执行文件名
返回
可执行文件完整路径,未找到返回空字符串

引用了 search_path().

被这些函数引用 search_path().

◆ 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)

捕获标准输出

参数
v是否捕获

引用了 process().

◆ set_env()

process & neforce::process::set_env ( const string & key,
const string & value )

设置子进程环境变量

参数
key变量名
value变量值

引用了 process().

◆ set_external_stderr()

process & neforce::process::set_external_stderr ( pipe & external_pipe)

将 stderr 连接到外部管道(供下游进程读取)

参数
external_pipe外部管道对象
返回
自身引用
注解
该管道生命周期由调用者管理

引用了 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 连接到外部管道(供下游进程读取)

参数
external_pipe外部管道对象
返回
自身引用
注解
该管道生命周期由调用者管理

引用了 process().

◆ set_stderr_file()

process & neforce::process::set_stderr_file ( const string & file_path)

将 stderr 重定向到文件

参数
file_path文件路径
返回
自身引用

引用了 process().

◆ set_stdin_data()

process & neforce::process::set_stdin_data ( const string & data)

预设要写入子进程 stdin 的数据

参数
data数据内容,在子进程启动后自动写入并关闭 stdin

引用了 neforce::data() , 以及 process().

◆ set_stdout_file()

process & neforce::process::set_stdout_file ( const string & file_path)

将 stdout 重定向到文件

参数
file_path文件路径
返回
自身引用

引用了 process().

◆ set_work_dir()

process & neforce::process::set_work_dir ( const string & dir)

设置子进程工作目录

参数
dir工作目录路径

引用了 process().

◆ spawn()

native_id_type neforce::process::spawn ( const string & executable,
const vector< string > & args = {} )
static

启动子进程并分离(fire-and-forget)

参数
executable可执行文件路径
args命令行参数
返回
子进程 ID

启动后立即分离,不等待。进程在后台运行。

◆ start()

void neforce::process::start ( const string & executable,
const vector< string > & args = {} )

启动子进程

参数
executable可执行文件路径
args命令行参数列表
异常
process_exception启动失败时抛出

◆ start_elevated()

void neforce::process::start_elevated ( const string & executable,
const vector< string > & args = {},
elevation_tool tool = elevation_tool::auto_ )

以管理员/root 权限启动子进程

参数
executable可执行文件路径
args命令行参数列表
tool提权工具
异常
process_exception启动失败或已设置了管道捕获时抛出
注解
不捕获输出

◆ system()

int neforce::process::system ( const string & executable,
const vector< string > & args = {},
int timeout_ms = -1 )
static

同步运行进程并返回退出码

参数
executable可执行文件路径
args命令行参数
timeout_ms超时毫秒,-1 无限等待
返回
退出码
异常
process_exception超时或执行失败

◆ wait()

int neforce::process::wait ( int timeout_ms = -1)

等待子进程退出

参数
timeout_ms超时毫秒数,-1 表示无限等待
返回
退出码,-1 超时
异常
process_exception等待失败时抛出

该类的文档由以下文件生成: