@@ -8,44 +8,56 @@ use crate::OpenSockets;
88pub ( crate ) fn get_open_sockets ( ) -> OpenSockets {
99 let mut open_sockets = HashMap :: new ( ) ;
1010 let mut connections = std:: vec:: Vec :: new ( ) ;
11- let all_procs = procfs:: process:: all_processes ( ) . unwrap ( ) ;
12-
1311 let mut inode_to_procname = HashMap :: new ( ) ;
14- for process in all_procs {
15- if let Ok ( fds) = process. fd ( ) {
16- let procname = process. stat . comm ;
17- for fd in fds {
18- if let FDTarget :: Socket ( inode) = fd. target {
19- inode_to_procname. insert ( inode, procname. clone ( ) ) ;
12+
13+ match procfs:: process:: all_processes ( ) {
14+ Ok ( all_procs) => {
15+ for process in all_procs {
16+ if let Ok ( fds) = process. fd ( ) {
17+ let procname = process. stat . comm ;
18+ for fd in fds {
19+ if let FDTarget :: Socket ( inode) = fd. target {
20+ inode_to_procname. insert ( inode, procname. clone ( ) ) ;
21+ }
22+ }
2023 }
2124 }
22- }
25+ } ,
26+ Err ( _) => ( ) ,
2327 }
2428
25- let tcp = :: procfs:: net:: tcp ( ) . unwrap ( ) ;
26- for entry in tcp. into_iter ( ) {
27- let local_port = entry. local_address . port ( ) ;
28- let local_ip = entry. local_address . ip ( ) ;
29- if let ( Some ( connection) , Some ( procname) ) = (
30- Connection :: new ( entry. remote_address , local_ip, local_port, Protocol :: Tcp ) ,
31- inode_to_procname. get ( & entry. inode ) ,
32- ) {
33- open_sockets. insert ( connection. local_socket , procname. clone ( ) ) ;
34- connections. push ( connection) ;
35- } ;
29+ match :: procfs:: net:: tcp ( ) {
30+ Ok ( tcp) => {
31+ for entry in tcp. into_iter ( ) {
32+ let local_port = entry. local_address . port ( ) ;
33+ let local_ip = entry. local_address . ip ( ) ;
34+ if let ( Some ( connection) , Some ( procname) ) = (
35+ Connection :: new ( entry. remote_address , local_ip, local_port, Protocol :: Tcp ) ,
36+ inode_to_procname. get ( & entry. inode ) ,
37+ ) {
38+ open_sockets. insert ( connection. local_socket , procname. clone ( ) ) ;
39+ connections. push ( connection) ;
40+ } ;
41+ }
42+ } ,
43+ Err ( _) => ( ) ,
3644 }
3745
38- let udp = :: procfs:: net:: udp ( ) . unwrap ( ) ;
39- for entry in udp. into_iter ( ) {
40- let local_port = entry. local_address . port ( ) ;
41- let local_ip = entry. local_address . ip ( ) ;
42- if let ( Some ( connection) , Some ( procname) ) = (
43- Connection :: new ( entry. remote_address , local_ip, local_port, Protocol :: Udp ) ,
44- inode_to_procname. get ( & entry. inode ) ,
45- ) {
46- open_sockets. insert ( connection. local_socket , procname. clone ( ) ) ;
47- connections. push ( connection) ;
48- } ;
46+ match :: procfs:: net:: udp ( ) {
47+ Ok ( udp) => {
48+ for entry in udp. into_iter ( ) {
49+ let local_port = entry. local_address . port ( ) ;
50+ let local_ip = entry. local_address . ip ( ) ;
51+ if let ( Some ( connection) , Some ( procname) ) = (
52+ Connection :: new ( entry. remote_address , local_ip, local_port, Protocol :: Udp ) ,
53+ inode_to_procname. get ( & entry. inode ) ,
54+ ) {
55+ open_sockets. insert ( connection. local_socket , procname. clone ( ) ) ;
56+ connections. push ( connection) ;
57+ } ;
58+ }
59+ } ,
60+ Err ( _) => ( ) ,
4961 }
5062 OpenSockets {
5163 sockets_to_procs : open_sockets,
0 commit comments