2023-09-18 20:13:29 -07:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-05-06 12:01:56 -07:00
|
|
|
|
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};
|
2023-09-18 20:13:29 -07:00
|
|
|
|
|
|
|
use crate::errors::*;
|
|
|
|
|
2022-12-19 16:20:46 -08:00
|
|
|
|
2022-12-22 16:36:32 -08:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
|
2022-12-19 16:20:46 -08:00
|
|
|
pub struct Client {
|
|
|
|
pub pid: u32,
|
2023-09-18 20:13:29 -07:00
|
|
|
pub exe: Option<PathBuf>,
|
2022-12-19 16:20:46 -08:00
|
|
|
}
|
2022-12-03 21:47:09 -08:00
|
|
|
|
|
|
|
|
2023-09-23 11:10:07 -07:00
|
|
|
pub fn get_process_parent_info(pid: u32) -> Result<Client, ClientInfoError> {
|
2023-09-18 20:13:29 -07:00
|
|
|
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
|
|
|
}
|