Skip to content

Commit 1c9b6e6

Browse files
committed
refresh map when new socket is created
1 parent b90cad5 commit 1c9b6e6

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

oryx-tui/src/ebpf/pid.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use log::error;
1616
use crate::{
1717
event::Event,
1818
notification::{Notification, NotificationLevel},
19-
pid::ConnectionMap,
19+
pid::{tcp::TcpConnectionMap, udp::UdpConnectionMap, ConnectionMap},
2020
};
2121
use 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
}

oryx-tui/src/pid.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ fn decode_hex_port(hex_str: &str) -> Result<u16, ParseIntError> {
6565

6666
#[derive(Clone, Debug)]
6767
pub struct ConnectionMap {
68-
tcp: TcpConnectionMap,
69-
udp: UdpConnectionMap,
68+
pub tcp: TcpConnectionMap,
69+
pub udp: UdpConnectionMap,
7070
}
7171

7272
impl ConnectionMap {

oryx-tui/src/pid/tcp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct TcpConnectionMap {
1313
}
1414

1515
impl TcpConnectionMap {
16-
fn inode_map() -> HashMap<usize, u64> {
16+
pub fn inode_map() -> HashMap<usize, u64> {
1717
let mut map = HashMap::new();
1818
let mut file = File::open("/proc/net/tcp").unwrap();
1919
let mut buffer = String::new();

oryx-tui/src/pid/udp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct UdpConnectionMap {
1313
}
1414

1515
impl UdpConnectionMap {
16-
fn inode_map() -> HashMap<usize, u64> {
16+
pub fn inode_map() -> HashMap<usize, u64> {
1717
let mut map = HashMap::new();
1818
let mut file = File::open("/proc/net/udp").unwrap();
1919
let mut buffer = String::new();

0 commit comments

Comments
 (0)