feat: update procfs
This commit is contained in:
23
src/proc.rs
23
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<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
pub fn get_process(_port: u16) -> Option<(i32, String)> {
|
pub fn get_process(_port: u16) -> Option<Process> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[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() {
|
let all_procs = match procfs::process::all_processes() {
|
||||||
Error(e) => {
|
Error(e) => {
|
||||||
rust_util::warning!("Get procfs all processes failed: {}", 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 {
|
for fd in fds {
|
||||||
if let Ok(fd) = fd {
|
if let Ok(fd) = fd {
|
||||||
if let procfs::process::FDTarget::Socket(inode) = fd.target {
|
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() {
|
for entry in tcp.into_iter() {
|
||||||
if port == entry.local_address.port() {
|
if port == entry.local_address.port() {
|
||||||
if let Some(stat) = map.get(&entry.inode) {
|
if let Some((stat, exec)) = map.get(&entry.inode) {
|
||||||
return Some((stat.pid, stat.comm));
|
return Some(Process {
|
||||||
|
pid: stat.pid,
|
||||||
|
comm: stat.comm,
|
||||||
|
exec,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
rust_util::warning!("Cannot get process by port: {}, inode: {}", port, entry.inode);
|
rust_util::warning!("Cannot get process by port: {}, inode: {}", port, entry.inode);
|
||||||
return None;
|
return None;
|
||||||
|
|||||||
Reference in New Issue
Block a user