creddy/src-tauri/src/clientinfo.rs

36 lines
981 B
Rust
Raw Normal View History

use std::path::{Path, PathBuf};
2022-12-20 13:01:44 -08:00
use sysinfo::{System, SystemExt, Pid, PidExt, ProcessExt};
2022-12-19 16:20:46 -08:00
use serde::{Serialize, Deserialize};
use crate::errors::*;
2022-12-19 16:20:46 -08:00
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
2022-12-19 16:20:46 -08:00
pub struct Client {
pub pid: u32,
pub exe: Option<PathBuf>,
2022-12-19 16:20:46 -08:00
}
2022-12-03 21:47:09 -08:00
pub fn get_process_parent_info(pid: u32) -> Result<Client, ClientInfoError> {
let sys_pid = Pid::from_u32(pid);
let mut sys = System::new();
sys.refresh_process(sys_pid);
let proc = sys.process(sys_pid)
.ok_or(ClientInfoError::ProcessNotFound)?;
let parent_pid_sys = proc.parent()
.ok_or(ClientInfoError::ParentPidNotFound)?;
sys.refresh_process(parent_pid_sys);
let parent = sys.process(parent_pid_sys)
.ok_or(ClientInfoError::ParentProcessNotFound)?;
let exe = match parent.exe() {
p if p == Path::new("") => None,
p => Some(PathBuf::from(p)),
};
Ok(Client { pid: parent_pid_sys.as_u32(), exe })
2022-12-03 21:47:09 -08:00
}