|
| 1 | +use std::{ |
| 2 | + fs::{self, File}, |
| 3 | + os::fd::AsRawFd, |
| 4 | + sync::{atomic::AtomicBool, Arc, Mutex}, |
| 5 | + thread, |
| 6 | + time::Duration, |
| 7 | +}; |
| 8 | + |
| 9 | +use aya::{ |
| 10 | + include_bytes_aligned, |
| 11 | + programs::{CgroupAttachMode, CgroupSockAddr}, |
| 12 | + EbpfLoader, |
| 13 | +}; |
| 14 | +use log::error; |
| 15 | + |
| 16 | +use crate::{ |
| 17 | + event::Event, |
| 18 | + notification::{Notification, NotificationLevel}, |
| 19 | + pid::ConnectionMap, |
| 20 | +}; |
| 21 | +use mio::{unix::SourceFd, Events, Interest, Poll, Token}; |
| 22 | + |
| 23 | +use super::RingBuffer; |
| 24 | + |
| 25 | +pub fn load_pid( |
| 26 | + pid_map: Arc<Mutex<ConnectionMap>>, |
| 27 | + notification_sender: kanal::Sender<Event>, |
| 28 | + terminate: Arc<AtomicBool>, |
| 29 | +) { |
| 30 | + thread::spawn({ |
| 31 | + move || { |
| 32 | + let rlim = libc::rlimit { |
| 33 | + rlim_cur: libc::RLIM_INFINITY, |
| 34 | + rlim_max: libc::RLIM_INFINITY, |
| 35 | + }; |
| 36 | + |
| 37 | + unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) }; |
| 38 | + |
| 39 | + #[cfg(debug_assertions)] |
| 40 | + let mut bpf = match EbpfLoader::new().load(include_bytes_aligned!( |
| 41 | + "../../../target/bpfel-unknown-none/debug/oryx" |
| 42 | + )) { |
| 43 | + Ok(v) => v, |
| 44 | + Err(e) => { |
| 45 | + error!("Failed to load the pid eBPF bytecode. {}", e); |
| 46 | + Notification::send( |
| 47 | + "Failed to load the pid eBPF bytecode", |
| 48 | + NotificationLevel::Error, |
| 49 | + notification_sender, |
| 50 | + ) |
| 51 | + .unwrap(); |
| 52 | + return; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + #[cfg(not(debug_assertions))] |
| 57 | + let mut bpf = match EbpfLoader::new().load(include_bytes_aligned!( |
| 58 | + "../../../target/bpfel-unknown-none/debug/oryx" |
| 59 | + )) { |
| 60 | + Ok(v) => v, |
| 61 | + Err(e) => { |
| 62 | + error!("Failed to load the pid eBPF bytecode. {}", e); |
| 63 | + Notification::send( |
| 64 | + "Failed to load the pid eBPF bytecode", |
| 65 | + NotificationLevel::Error, |
| 66 | + notification_sender, |
| 67 | + ) |
| 68 | + .unwrap(); |
| 69 | + return; |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + let sock_connect: &mut CgroupSockAddr = bpf |
| 74 | + .program_mut("socket_connect") |
| 75 | + .unwrap() |
| 76 | + .try_into() |
| 77 | + .unwrap(); |
| 78 | + sock_connect.load().unwrap(); |
| 79 | + let file = File::open("/sys/fs/cgroup/user.slice").unwrap(); |
| 80 | + |
| 81 | + sock_connect.attach(file, CgroupAttachMode::Single).unwrap(); |
| 82 | + |
| 83 | + let mut poll = Poll::new().unwrap(); |
| 84 | + let mut events = Events::with_capacity(128); |
| 85 | + |
| 86 | + let mut ring_buf = RingBuffer::new(&mut bpf, "PID_DATA"); |
| 87 | + |
| 88 | + poll.registry() |
| 89 | + .register( |
| 90 | + &mut SourceFd(&ring_buf.buffer.as_raw_fd()), |
| 91 | + Token(0), |
| 92 | + Interest::READABLE, |
| 93 | + ) |
| 94 | + .unwrap(); |
| 95 | + |
| 96 | + loop { |
| 97 | + poll.poll(&mut events, Some(Duration::from_millis(100))) |
| 98 | + .unwrap(); |
| 99 | + if terminate.load(std::sync::atomic::Ordering::Relaxed) { |
| 100 | + break; |
| 101 | + } |
| 102 | + for event in &events { |
| 103 | + if terminate.load(std::sync::atomic::Ordering::Relaxed) { |
| 104 | + break; |
| 105 | + } |
| 106 | + if event.token() == Token(0) && event.is_readable() { |
| 107 | + if terminate.load(std::sync::atomic::Ordering::Relaxed) { |
| 108 | + break; |
| 109 | + } |
| 110 | + while let Some(item) = ring_buf.next() { |
| 111 | + if terminate.load(std::sync::atomic::Ordering::Relaxed) { |
| 112 | + break; |
| 113 | + } |
| 114 | + let pid: [u8; 4] = item.to_owned().try_into().unwrap(); |
| 115 | + let pid = u32::from_ne_bytes(pid); |
| 116 | + |
| 117 | + let fd_dir = format!("/proc/{}/fd", pid); |
| 118 | + if let Ok(_fds) = fs::read_dir(&fd_dir) { |
| 119 | + let mut map = pid_map.lock().unwrap(); |
| 120 | + *map = ConnectionMap::new(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + let _ = poll |
| 128 | + .registry() |
| 129 | + .deregister(&mut SourceFd(&ring_buf.buffer.as_raw_fd())); |
| 130 | + } |
| 131 | + }); |
| 132 | +} |
0 commit comments