@@ -16,7 +16,7 @@ use log::error;
1616use crate :: {
1717 event:: Event ,
1818 notification:: { Notification , NotificationLevel } ,
19- pid:: ConnectionMap ,
19+ pid:: { tcp :: TcpConnectionMap , udp :: UdpConnectionMap , ConnectionMap } ,
2020} ;
2121use mio:: { unix:: SourceFd , Events , Interest , Poll , Token } ;
2222
@@ -115,9 +115,44 @@ pub fn load_pid(
115115 let pid = u32:: from_ne_bytes ( pid) ;
116116
117117 let fd_dir = format ! ( "/proc/{}/fd" , pid) ;
118- if let Ok ( _fds ) = fs:: read_dir ( & fd_dir) {
118+ if let Ok ( fds ) = fs:: read_dir ( & fd_dir) {
119119 let mut map = pid_map. lock ( ) . unwrap ( ) ;
120- * map = ConnectionMap :: new ( ) ;
120+ let tcp_inode_map = TcpConnectionMap :: inode_map ( ) ;
121+ let udp_inode_map = UdpConnectionMap :: inode_map ( ) ;
122+
123+ for fd in fds. flatten ( ) {
124+ let link_path = fd. path ( ) ;
125+
126+ if let Ok ( link_target) = fs:: read_link ( & link_path) {
127+ if let Some ( inode_str) = link_target. to_str ( ) {
128+ if inode_str. starts_with ( "socket:[" )
129+ && inode_str. ends_with ( ']' )
130+ {
131+ if let Ok ( inode) = inode_str[ 8 ..inode_str. len ( ) - 1 ]
132+ . parse :: < usize > ( )
133+ {
134+ if let Some ( connection_hash) =
135+ tcp_inode_map. get ( & inode)
136+ {
137+ map. tcp . map . insert (
138+ * connection_hash,
139+ pid. try_into ( ) . unwrap ( ) ,
140+ ) ;
141+ }
142+
143+ if let Some ( connection_hash) =
144+ udp_inode_map. get ( & inode)
145+ {
146+ map. udp . map . insert (
147+ * connection_hash,
148+ pid. try_into ( ) . unwrap ( ) ,
149+ ) ;
150+ }
151+ }
152+ }
153+ }
154+ }
155+ }
121156 }
122157 }
123158 }
0 commit comments