diff --git a/src/proc.rs b/src/proc.rs index 2ab1e04..763f211 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -1,10 +1,19 @@ +use std::path::PathBuf; + +#[derive(Debug, Clone)] +pub struct Process { + pub pid: i32, + pub comm: String, + pub exec: Option, +} + #[cfg(not(target_os = "linux"))] -pub fn get_process(_port: u16) -> Option<(i32, String)> { +pub fn get_process(_port: u16) -> Option { None } #[cfg(target_os = "linux")] -pub fn get_process(port: u16) -> Option<(i32, String)> { +pub fn get_process(port: u16) -> Option { let all_procs = match procfs::process::all_processes() { Error(e) => { rust_util::warning!("Get procfs all processes failed: {}", e); @@ -19,7 +28,7 @@ pub fn get_process(port: u16) -> Option<(i32, String)> { for fd in fds { if let Ok(fd) = fd { if let procfs::process::FDTarget::Socket(inode) = fd.target { - map.insert(inode, stat.clone()); + map.insert(inode, (stat.clone(), process.exe().ok())); } } } @@ -35,8 +44,12 @@ pub fn get_process(port: u16) -> Option<(i32, String)> { }; for entry in tcp.into_iter() { if port == entry.local_address.port() { - if let Some(stat) = map.get(&entry.inode) { - return Some((stat.pid, stat.comm)); + if let Some((stat, exec)) = map.get(&entry.inode) { + return Some(Process { + pid: stat.pid, + comm: stat.comm, + exec, + }); } else { rust_util::warning!("Cannot get process by port: {}, inode: {}", port, entry.inode); return None;