|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | + |
| 3 | +// Include the bindings for event.h |
| 4 | +pub mod bindings { |
| 5 | + #![allow(non_upper_case_globals)] |
| 6 | + #![allow(non_camel_case_types)] |
| 7 | + #![allow(non_snake_case)] |
| 8 | + #![allow(dead_code)] |
| 9 | + |
| 10 | + include!(concat!(env!("OUT_DIR"), "/event.rs")); |
| 11 | +} |
| 12 | +use bindings::*; |
| 13 | + |
| 14 | +#[repr(u8)] |
| 15 | +#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)] |
| 16 | +pub enum EventType { |
| 17 | + Fork = EVENT_TYPE_FORK as u8, |
| 18 | + Exec = EVENT_TYPE_EXEC as u8, |
| 19 | + Exit = EVENT_TYPE_EXIT as u8, |
| 20 | +} |
| 21 | + |
| 22 | +impl From<u8> for EventType { |
| 23 | + fn from(val: u8) -> Self { |
| 24 | + match val as u32 { |
| 25 | + bindings::EVENT_TYPE_FORK => EventType::Fork, |
| 26 | + bindings::EVENT_TYPE_EXEC => EventType::Exec, |
| 27 | + bindings::EVENT_TYPE_EXIT => EventType::Exit, |
| 28 | + _ => panic!("Unknown event type: {val}"), |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[repr(C)] |
| 34 | +#[derive(Debug, Clone, Copy)] |
| 35 | +pub struct Event { |
| 36 | + pub event_type: u8, |
| 37 | + pub timestamp: u64, |
| 38 | + pub pid: u32, |
| 39 | + pub tid: u32, |
| 40 | + pub ppid: u32, |
| 41 | + pub comm: [u8; 16], |
| 42 | +} |
| 43 | + |
| 44 | +impl Event { |
| 45 | + /// Get the event type as an enum |
| 46 | + pub fn event_type(&self) -> EventType { |
| 47 | + EventType::from(self.event_type) |
| 48 | + } |
| 49 | + |
| 50 | + /// Get the command name as a string |
| 51 | + pub fn comm_str(&self) -> &str { |
| 52 | + let len = self.comm.iter().position(|&c| c == 0).unwrap_or(16); |
| 53 | + std::str::from_utf8(&self.comm[..len]).unwrap_or("<invalid>") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Static assertions for C/Rust ABI safety |
| 58 | +mod assertions { |
| 59 | + use super::*; |
| 60 | + use static_assertions::{assert_eq_align, assert_eq_size, const_assert_eq}; |
| 61 | + use std::mem::offset_of; |
| 62 | + |
| 63 | + assert_eq_size!(Event, bindings::event); |
| 64 | + assert_eq_align!(Event, bindings::event); |
| 65 | + |
| 66 | + const_assert_eq!( |
| 67 | + offset_of!(Event, timestamp), |
| 68 | + offset_of!(bindings::event, timestamp) |
| 69 | + ); |
| 70 | + const_assert_eq!(offset_of!(Event, pid), offset_of!(bindings::event, pid)); |
| 71 | + const_assert_eq!(offset_of!(Event, tid), offset_of!(bindings::event, tid)); |
| 72 | + const_assert_eq!( |
| 73 | + offset_of!(Event, event_type), |
| 74 | + offset_of!(bindings::event, event_type) |
| 75 | + ); |
| 76 | + const_assert_eq!(offset_of!(Event, comm), offset_of!(bindings::event, comm)); |
| 77 | +} |
0 commit comments