Skip to content

Commit

Permalink
Fix issue #84 by handle error more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxp1998 committed Jan 8, 2020
1 parent c2d18bd commit 0e625ed
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions src/os/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,56 @@ use crate::OpenSockets;
pub(crate) fn get_open_sockets() -> OpenSockets {
let mut open_sockets = HashMap::new();
let mut connections = std::vec::Vec::new();
let all_procs = procfs::process::all_processes().unwrap();

let mut inode_to_procname = HashMap::new();
for process in all_procs {
if let Ok(fds) = process.fd() {
let procname = process.stat.comm;
for fd in fds {
if let FDTarget::Socket(inode) = fd.target {
inode_to_procname.insert(inode, procname.clone());

match procfs::process::all_processes() {
Ok(all_procs) => {
for process in all_procs {
if let Ok(fds) = process.fd() {
let procname = process.stat.comm;
for fd in fds {
if let FDTarget::Socket(inode) = fd.target {
inode_to_procname.insert(inode, procname.clone());
}
}
}
}
}
},
Err(_) => (),
}

let tcp = ::procfs::net::tcp().unwrap();
for entry in tcp.into_iter() {
let local_port = entry.local_address.port();
let local_ip = entry.local_address.ip();
if let (Some(connection), Some(procname)) = (
Connection::new(entry.remote_address, local_ip, local_port, Protocol::Tcp),
inode_to_procname.get(&entry.inode),
) {
open_sockets.insert(connection.local_socket, procname.clone());
connections.push(connection);
};
match ::procfs::net::tcp() {
Ok(tcp) => {
for entry in tcp.into_iter() {
let local_port = entry.local_address.port();
let local_ip = entry.local_address.ip();
if let (Some(connection), Some(procname)) = (
Connection::new(entry.remote_address, local_ip, local_port, Protocol::Tcp),
inode_to_procname.get(&entry.inode),
) {
open_sockets.insert(connection.local_socket, procname.clone());
connections.push(connection);
};
}
},
Err(_) => (),
}

let udp = ::procfs::net::udp().unwrap();
for entry in udp.into_iter() {
let local_port = entry.local_address.port();
let local_ip = entry.local_address.ip();
if let (Some(connection), Some(procname)) = (
Connection::new(entry.remote_address, local_ip, local_port, Protocol::Udp),
inode_to_procname.get(&entry.inode),
) {
open_sockets.insert(connection.local_socket, procname.clone());
connections.push(connection);
};
match ::procfs::net::udp() {
Ok(udp) => {
for entry in udp.into_iter() {
let local_port = entry.local_address.port();
let local_ip = entry.local_address.ip();
if let (Some(connection), Some(procname)) = (
Connection::new(entry.remote_address, local_ip, local_port, Protocol::Udp),
inode_to_procname.get(&entry.inode),
) {
open_sockets.insert(connection.local_socket, procname.clone());
connections.push(connection);
};
}
},
Err(_) => (),
}
OpenSockets {
sockets_to_procs: open_sockets,
Expand Down

0 comments on commit 0e625ed

Please sign in to comment.