From 0e625ed2c775e2e2bf4bb9b8472009fcc8367757 Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Tue, 7 Jan 2020 20:27:45 -0500 Subject: [PATCH] Fix issue #84 by handle error more gracefully --- src/os/linux.rs | 74 ++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/src/os/linux.rs b/src/os/linux.rs index 9ca566cfc..b6a8a9f92 100644 --- a/src/os/linux.rs +++ b/src/os/linux.rs @@ -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,