From 6cfdec1d3c145ca35c34b89d694d5fa2d63f90aa Mon Sep 17 00:00:00 2001 From: guochao Date: Thu, 3 Apr 2025 14:12:09 +0000 Subject: [PATCH] small step to implement extra metadatas --- fanotify-demo/src/main.rs | 8 +++++++- fanotify/src/fanotify.rs | 31 +++++++++++++++++++------------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/fanotify-demo/src/main.rs b/fanotify-demo/src/main.rs index 587817f..4be748b 100644 --- a/fanotify-demo/src/main.rs +++ b/fanotify-demo/src/main.rs @@ -159,7 +159,13 @@ fn main() -> Result<(), Error> { } for event in events.iter_mut() { let Some(fd) = event.fd() else { - warn!("queue full"); + if init_flags & (InitFlags::FAN_REPORT_FID | InitFlags::FAN_REPORT_DIR_FID) + != InitFlags::empty() + { + warn!("fid not implementd"); + } else { + warn!("queue full"); + } continue; }; let path = match std::fs::read_link(format!("/proc/self/fd/{}", fd.as_raw_fd())) { diff --git a/fanotify/src/fanotify.rs b/fanotify/src/fanotify.rs index c75a879..3fc7646 100644 --- a/fanotify/src/fanotify.rs +++ b/fanotify/src/fanotify.rs @@ -114,7 +114,7 @@ impl Fanotify { // + (meta) -> event_len // add event_len to move to next // ); offset += event.event_len as usize; - result.push(Event(event)); + result.push(Event::new(event)); } } @@ -136,35 +136,42 @@ impl Fanotify { } } -pub struct Event(pub libc::fanotify_event_metadata); +pub struct Event { + pub fanotify_event_metadata: libc::fanotify_event_metadata, +} impl Event { + fn new(fanotify_event_metadata: libc::fanotify_event_metadata) -> Self { + Self { + fanotify_event_metadata, + } + } // compatible to nix::sys::fanotify::FanotifyEvent pub fn metadata_version(&self) -> u8 { - self.0.vers + self.fanotify_event_metadata.vers } pub fn check_metadata_version(&self) -> bool { - self.0.vers == libc::FANOTIFY_METADATA_VERSION + self.fanotify_event_metadata.vers == libc::FANOTIFY_METADATA_VERSION } pub fn fd(&self) -> Option { - if self.0.fd == libc::FAN_NOFD { + if self.fanotify_event_metadata.fd == libc::FAN_NOFD { None } else { - Some(unsafe { BorrowedFd::borrow_raw(self.0.fd) }) + Some(unsafe { BorrowedFd::borrow_raw(self.fanotify_event_metadata.fd) }) } } pub fn pid(&self) -> i32 { - self.0.pid + self.fanotify_event_metadata.pid } pub fn mask(&self) -> MaskFlags { - MaskFlags::from_bits_truncate(self.0.mask) + MaskFlags::from_bits_truncate(self.fanotify_event_metadata.mask) } // sometimes we don't want to close the fd immediately, so we forget about it, store it somewhere, and drop it later // it is safe to just call this method without store it in variable, it will be dropped immediately due to the nature of rust pub fn forget_fd(&mut self) -> OwnedFd { - let fd = self.0.fd; - self.0.fd = libc::FAN_NOFD; + let fd = self.fanotify_event_metadata.fd; + self.fanotify_event_metadata.fd = libc::FAN_NOFD; unsafe { OwnedFd::from_raw_fd(fd) } } @@ -172,11 +179,11 @@ impl Event { impl Drop for Event { fn drop(&mut self) { - if self.0.fd == libc::FAN_NOFD { + if self.fanotify_event_metadata.fd == libc::FAN_NOFD { return; } - let e = unsafe { libc::close(self.0.fd) }; + let e = unsafe { libc::close(self.fanotify_event_metadata.fd) }; if !std::thread::panicking() && e == libc::EBADF { panic!("Closing an invalid file descriptor!"); };