From d60f710c31394b3ee7af2a1be0f37a713a9565bd Mon Sep 17 00:00:00 2001 From: SteveLauC Date: Mon, 10 Jun 2024 14:39:22 +0800 Subject: [PATCH] refactor: I/O safety for sys/fanotify.rs (#2443) * refactor: I/O safety for sys/fanotify.rs * chore: correct changelog entry pr number --- changelog/2443.changed.md | 1 + src/fcntl.rs | 6 ------ src/sys/fanotify.rs | 9 ++++----- test/sys/test_fanotify.rs | 7 ++++--- 4 files changed, 9 insertions(+), 14 deletions(-) create mode 100644 changelog/2443.changed.md diff --git a/changelog/2443.changed.md b/changelog/2443.changed.md new file mode 100644 index 0000000000..da55512c4b --- /dev/null +++ b/changelog/2443.changed.md @@ -0,0 +1 @@ +Module sys/fanotify now adopts I/O safety diff --git a/src/fcntl.rs b/src/fcntl.rs index a8fc3a91c5..0ab79b402c 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -231,12 +231,6 @@ libc_bitflags!( } ); -/// Computes the raw fd consumed by a function of the form `*at`. -#[cfg(all(feature = "fanotify", target_os = "linux"))] -pub(crate) fn at_rawfd(fd: Option) -> RawFd { - fd.unwrap_or(libc::AT_FDCWD) -} - feature! { #![feature = "fs"] diff --git a/src/sys/fanotify.rs b/src/sys/fanotify.rs index c3b7fbd6d4..f838f1629f 100644 --- a/src/sys/fanotify.rs +++ b/src/sys/fanotify.rs @@ -11,7 +11,7 @@ //! [fanotify(7)](https://man7.org/linux/man-pages/man7/fanotify.7.html). use crate::errno::Errno; -use crate::fcntl::{at_rawfd, OFlag}; +use crate::fcntl::OFlag; use crate::unistd::{close, read, write}; use crate::{NixPath, Result}; use std::marker::PhantomData; @@ -321,16 +321,15 @@ impl Fanotify { } /// Add, remove, or modify an fanotify mark on a filesystem object. - /// If `dirfd` is `None`, `AT_FDCWD` is used. /// /// Returns a Result containing either `()` on success or errno otherwise. /// /// For more information, see [fanotify_mark(2)](https://man7.org/linux/man-pages/man7/fanotify_mark.2.html). - pub fn mark( + pub fn mark( &self, flags: MarkFlags, mask: MaskFlags, - dirfd: Option, + dirfd: Fd, path: Option<&P>, ) -> Result<()> { let res = crate::with_opt_nix_path(path, |p| unsafe { @@ -338,7 +337,7 @@ impl Fanotify { self.fd.as_raw_fd(), flags.bits(), mask.bits(), - at_rawfd(dirfd), + dirfd.as_fd().as_raw_fd(), p, ) })?; diff --git a/test/sys/test_fanotify.rs b/test/sys/test_fanotify.rs index 13ec945913..04b39c4db4 100644 --- a/test/sys/test_fanotify.rs +++ b/test/sys/test_fanotify.rs @@ -1,5 +1,6 @@ use crate::*; use nix::errno::Errno; +use nix::fcntl::AT_FDCWD; use nix::sys::fanotify::{ EventFFlags, Fanotify, FanotifyResponse, InitFlags, MarkFlags, MaskFlags, Response, @@ -36,7 +37,7 @@ fn test_fanotify_notifications() { .mark( MarkFlags::FAN_MARK_ADD, MaskFlags::FAN_OPEN | MaskFlags::FAN_MODIFY | MaskFlags::FAN_CLOSE, - None, + AT_FDCWD, Some(&tempfile), ) .unwrap(); @@ -99,7 +100,7 @@ fn test_fanotify_responses() { .mark( MarkFlags::FAN_MARK_ADD, MaskFlags::FAN_OPEN_PERM, - None, + AT_FDCWD, Some(&tempfile), ) .unwrap(); @@ -182,7 +183,7 @@ fn test_fanotify_overflow() { .mark( MarkFlags::FAN_MARK_ADD, MaskFlags::FAN_OPEN, - None, + AT_FDCWD, Some(&tempfile), ) .unwrap();