Skip to content

Commit d60f710

Browse files
authored
refactor: I/O safety for sys/fanotify.rs (#2443)
* refactor: I/O safety for sys/fanotify.rs * chore: correct changelog entry pr number
1 parent 458013d commit d60f710

File tree

4 files changed

+9
-14
lines changed

4 files changed

+9
-14
lines changed

changelog/2443.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Module sys/fanotify now adopts I/O safety

src/fcntl.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,6 @@ libc_bitflags!(
231231
}
232232
);
233233

234-
/// Computes the raw fd consumed by a function of the form `*at`.
235-
#[cfg(all(feature = "fanotify", target_os = "linux"))]
236-
pub(crate) fn at_rawfd(fd: Option<RawFd>) -> RawFd {
237-
fd.unwrap_or(libc::AT_FDCWD)
238-
}
239-
240234
feature! {
241235
#![feature = "fs"]
242236

src/sys/fanotify.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! [fanotify(7)](https://man7.org/linux/man-pages/man7/fanotify.7.html).
1212
1313
use crate::errno::Errno;
14-
use crate::fcntl::{at_rawfd, OFlag};
14+
use crate::fcntl::OFlag;
1515
use crate::unistd::{close, read, write};
1616
use crate::{NixPath, Result};
1717
use std::marker::PhantomData;
@@ -321,24 +321,23 @@ impl Fanotify {
321321
}
322322

323323
/// Add, remove, or modify an fanotify mark on a filesystem object.
324-
/// If `dirfd` is `None`, `AT_FDCWD` is used.
325324
///
326325
/// Returns a Result containing either `()` on success or errno otherwise.
327326
///
328327
/// For more information, see [fanotify_mark(2)](https://man7.org/linux/man-pages/man7/fanotify_mark.2.html).
329-
pub fn mark<P: ?Sized + NixPath>(
328+
pub fn mark<Fd: std::os::fd::AsFd, P: ?Sized + NixPath>(
330329
&self,
331330
flags: MarkFlags,
332331
mask: MaskFlags,
333-
dirfd: Option<RawFd>,
332+
dirfd: Fd,
334333
path: Option<&P>,
335334
) -> Result<()> {
336335
let res = crate::with_opt_nix_path(path, |p| unsafe {
337336
libc::fanotify_mark(
338337
self.fd.as_raw_fd(),
339338
flags.bits(),
340339
mask.bits(),
341-
at_rawfd(dirfd),
340+
dirfd.as_fd().as_raw_fd(),
342341
p,
343342
)
344343
})?;

test/sys/test_fanotify.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::*;
22
use nix::errno::Errno;
3+
use nix::fcntl::AT_FDCWD;
34
use nix::sys::fanotify::{
45
EventFFlags, Fanotify, FanotifyResponse, InitFlags, MarkFlags, MaskFlags,
56
Response,
@@ -36,7 +37,7 @@ fn test_fanotify_notifications() {
3637
.mark(
3738
MarkFlags::FAN_MARK_ADD,
3839
MaskFlags::FAN_OPEN | MaskFlags::FAN_MODIFY | MaskFlags::FAN_CLOSE,
39-
None,
40+
AT_FDCWD,
4041
Some(&tempfile),
4142
)
4243
.unwrap();
@@ -99,7 +100,7 @@ fn test_fanotify_responses() {
99100
.mark(
100101
MarkFlags::FAN_MARK_ADD,
101102
MaskFlags::FAN_OPEN_PERM,
102-
None,
103+
AT_FDCWD,
103104
Some(&tempfile),
104105
)
105106
.unwrap();
@@ -182,7 +183,7 @@ fn test_fanotify_overflow() {
182183
.mark(
183184
MarkFlags::FAN_MARK_ADD,
184185
MaskFlags::FAN_OPEN,
185-
None,
186+
AT_FDCWD,
186187
Some(&tempfile),
187188
)
188189
.unwrap();

0 commit comments

Comments
 (0)