feat: update procfs

This commit is contained in:
2022-07-29 00:41:29 +08:00
parent ffb630bb64
commit 925f186817

View File

@@ -1,10 +1,19 @@
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct Process {
pub pid: i32,
pub comm: String,
pub exec: Option<PathBuf>,
}
#[cfg(not(target_os = "linux"))]
pub fn get_process(_port: u16) -> Option<(i32, String)> {
pub fn get_process(_port: u16) -> Option<Process> {
None
}
#[cfg(target_os = "linux")]
pub fn get_process(port: u16) -> Option<(i32, String)> {
pub fn get_process(port: u16) -> Option<Process> {
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;