Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #84 by handle error more gracefully #88

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions src/os/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,47 @@ 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());

if let Ok(all_procs) = procfs::process::all_processes() {
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());
}
}
}
}
}

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);
};
if let Ok(tcp) = ::procfs::net::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);
};
}
}

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);
};
if let Ok(udp) = ::procfs::net::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);
};
}
}
OpenSockets {
sockets_to_procs: open_sockets,
Expand Down