xtap-core-lib 0.5.1

星TAP实验室通用 Rust 基座:跨平台路径/IO/硬件/MCP HTTP 服务/egui 主题与字体(features 按需裁剪)
Documentation
use crate::error::Result;
use anyhow::anyhow;
#[cfg(feature = "process")]
use sysinfo::{Pid, System};

/// 获取当前进程 PID
pub fn get_current_pid() -> u32 {
    std::process::id()
}

/// 检查进程是否在运行 (需要 process feature)
#[cfg(feature = "process")]
pub fn is_process_running(pid: u32) -> bool {
    let mut sys = System::new_all();
    sys.refresh_all();
    sys.process(Pid::from(pid as usize)).is_some()
}

/// 杀掉进程
#[cfg(feature = "process")]
pub fn kill_process(pid: u32) -> Result<()> {
    let mut sys = System::new_all();
    sys.refresh_all();
    if let Some(process) = sys.process(Pid::from(pid as usize)) {
        if process.kill() {
            Ok(())
        } else {
            Err(anyhow!("无法终止进程 {}", pid))
        }
    } else {
        Err(anyhow!("找不到进程 {}", pid))
    }
}