use std::path::{Path, PathBuf}; use sysinfo::{System, SystemExt, Pid, PidExt, ProcessExt}; use serde::{Serialize, Deserialize}; use crate::errors::*; #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)] pub struct Client { pub pid: u32, pub exe: Option, } pub fn get_process_parent_info(pid: u32) -> Result { 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 }) }