Skip to content

Commit

Permalink
refactor: I/O safety for sys/fanotify.rs (#2443)
Browse files Browse the repository at this point in the history
* refactor: I/O safety for sys/fanotify.rs

* chore: correct changelog entry pr number
  • Loading branch information
SteveLauC committed Jun 10, 2024
1 parent 458013d commit d60f710
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog/2443.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Module sys/fanotify now adopts I/O safety
6 changes: 0 additions & 6 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>) -> RawFd {
fd.unwrap_or(libc::AT_FDCWD)
}

feature! {
#![feature = "fs"]

Expand Down
9 changes: 4 additions & 5 deletions src/sys/fanotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -321,24 +321,23 @@ 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<P: ?Sized + NixPath>(
pub fn mark<Fd: std::os::fd::AsFd, P: ?Sized + NixPath>(
&self,
flags: MarkFlags,
mask: MaskFlags,
dirfd: Option<RawFd>,
dirfd: Fd,
path: Option<&P>,
) -> Result<()> {
let res = crate::with_opt_nix_path(path, |p| unsafe {
libc::fanotify_mark(
self.fd.as_raw_fd(),
flags.bits(),
mask.bits(),
at_rawfd(dirfd),
dirfd.as_fd().as_raw_fd(),
p,
)
})?;
Expand Down
7 changes: 4 additions & 3 deletions test/sys/test_fanotify.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -99,7 +100,7 @@ fn test_fanotify_responses() {
.mark(
MarkFlags::FAN_MARK_ADD,
MaskFlags::FAN_OPEN_PERM,
None,
AT_FDCWD,
Some(&tempfile),
)
.unwrap();
Expand Down Expand Up @@ -182,7 +183,7 @@ fn test_fanotify_overflow() {
.mark(
MarkFlags::FAN_MARK_ADD,
MaskFlags::FAN_OPEN,
None,
AT_FDCWD,
Some(&tempfile),
)
.unwrap();
Expand Down

0 comments on commit d60f710

Please sign in to comment.