Skip to content

Commit 5c7a82a

Browse files
committed
linux: add fanotify(7) API bindings.
The `fanotify` API[0] is a linux-specific API for notification and interception of filesystem events. In some ways it is similar to `inotify`, but with different advantages/tradeoffs. It is particularly well suited to full filesystem/mount monitoring (vs per directory) and for allowing/denying access to files (`inotify` lacks this capability). The `fanotify` API has been updated several times since it was enabled in Linux 2.6.37. Presently I've only included support for the original `fanotify` features, and the `FAN_MARK_FILESYSTEM` addition made in Linux 4.20. There are subsequent updates in 5.0 and 5.1 not covered in this initial commit. This commit adds the relevant constants and types from `uapi/linux/fanotify.h`[1] and two new functions (`fanotify_init`[2] and `fanotify_wrap`[3]) to `src/unix/linux_like/linux/mod.rs`. While I believe this API is also present on Android I have presently limited my attention to Linux. Although this commit focuses on Linux 4.20.x's `fanotify` API/constants I have skipped adding constants for `FAN_ALL_CLASS_BITS`, `FAN_ALL_INIT_FLAGS`, `FAN_ALL_MARK_FLAGS`, `FAN_ALL_EVENTS`, `FAN_ALL_PERM_EVENTS` and `FAN_ALL_OUTGOING_EVENTS` even though they are present in this kernel version's headers. These defines were deprecated[4] in later releases with instructions to not use them in new programs or extend them with new values. It would be a shame for new Rust programs to use deprecated #defines! [0]: http://man7.org/linux/man-pages/man7/fanotify.7.html [1]: https://github.com/torvalds/linux/blob/d54f4fba889b205e9cd8239182ca5d27d0ac3bc2/include/uapi/linux/fanotify.h [2]: http://man7.org/linux/man-pages/man2/fanotify_init.2.html [3]: http://man7.org/linux/man-pages/man2/fanotify_mark.2.html [4]: torvalds/linux@23c9dee#diff-4c9ca62be6bf38cc08f7ea9daf16e379
1 parent 08e8a3a commit 5c7a82a

File tree

6 files changed

+93
-0
lines changed

6 files changed

+93
-0
lines changed

libc-test/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,7 @@ fn test_linux(target: &str) {
22582258
"linux/sockios.h",
22592259
"linux/vm_sockets.h",
22602260
"sys/auxv.h",
2261+
"sys/fanotify.h",
22612262
}
22622263

22632264
// note: aio.h must be included before sys/mount.h

src/unix/linux_like/linux/align.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ macro_rules! expand_align {
3939
#[doc(hidden)]
4040
size: [u8; ::__SIZEOF_PTHREAD_CONDATTR_T],
4141
}
42+
43+
#[repr(align(8))]
44+
pub struct fanotify_event_metadata {
45+
pub event_len: __u32,
46+
pub vers: __u8,
47+
pub reserved: __u8,
48+
pub metadata_len: __u16,
49+
pub mask: __u64,
50+
pub fd: ::c_int,
51+
pub pid: ::c_int,
52+
}
4253
}
4354

4455
s_no_extra_traits! {

src/unix/linux_like/linux/gnu/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,13 @@ extern "C" {
12061206
len: ::size_t,
12071207
flags: ::c_uint,
12081208
) -> ::ssize_t;
1209+
pub fn fanotify_mark(
1210+
fd: ::c_int,
1211+
flags: ::c_uint,
1212+
mask: u64,
1213+
dirfd: ::c_int,
1214+
path: *const ::c_char,
1215+
) -> ::c_int;
12091216
}
12101217

12111218
#[link(name = "util")]

src/unix/linux_like/linux/mod.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ s! {
477477
pub len: u32
478478
}
479479

480+
pub struct fanotify_response {
481+
pub fd: ::c_int,
482+
pub response: __u32,
483+
}
484+
480485
pub struct sockaddr_vm {
481486
pub svm_family: ::sa_family_t,
482487
pub svm_reserved1: ::c_ushort,
@@ -2417,6 +2422,53 @@ pub const IN_ALL_EVENTS: u32 = IN_ACCESS
24172422
pub const IN_CLOEXEC: ::c_int = O_CLOEXEC;
24182423
pub const IN_NONBLOCK: ::c_int = O_NONBLOCK;
24192424

2425+
// uapi/linux/fanotify.h
2426+
pub const FAN_ACCESS: u64 = 0x0000_0001;
2427+
pub const FAN_MODIFY: u64 = 0x0000_0002;
2428+
pub const FAN_CLOSE_WRITE: u64 = 0x0000_0008;
2429+
pub const FAN_CLOSE_NOWRITE: u64 = 0x0000_0010;
2430+
pub const FAN_OPEN: u64 = 0x0000_0020;
2431+
2432+
pub const FAN_Q_OVERFLOW: u64 = 0x0000_4000;
2433+
2434+
pub const FAN_OPEN_PERM: u64 = 0x0001_0000;
2435+
pub const FAN_ACCESS_PERM: u64 = 0x0002_0000;
2436+
2437+
pub const FAN_ONDIR: u64 = 0x4000_0000;
2438+
2439+
pub const FAN_EVENT_ON_CHILD: u64 = 0x0800_0000;
2440+
2441+
pub const FAN_CLOSE: u64 = FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE;
2442+
2443+
pub const FAN_CLOEXEC: ::c_uint = 0x0000_0001;
2444+
pub const FAN_NONBLOCK: ::c_uint = 0x0000_0002;
2445+
2446+
pub const FAN_CLASS_NOTIF: ::c_uint = 0x0000_0000;
2447+
pub const FAN_CLASS_CONTENT: ::c_uint = 0x0000_0004;
2448+
pub const FAN_CLASS_PRE_CONTENT: ::c_uint = 0x0000_0008;
2449+
2450+
pub const FAN_UNLIMITED_QUEUE: ::c_uint = 0x0000_0010;
2451+
pub const FAN_UNLIMITED_MARKS: ::c_uint = 0x0000_0020;
2452+
2453+
pub const FAN_MARK_ADD: ::c_uint = 0x0000_0001;
2454+
pub const FAN_MARK_REMOVE: ::c_uint = 0x0000_0002;
2455+
pub const FAN_MARK_DONT_FOLLOW: ::c_uint = 0x0000_0004;
2456+
pub const FAN_MARK_ONLYDIR: ::c_uint = 0x0000_0008;
2457+
pub const FAN_MARK_INODE: ::c_uint = 0x0000_0000;
2458+
pub const FAN_MARK_MOUNT: ::c_uint = 0x0000_0010;
2459+
// NOTE: FAN_MARK_FILESYSTEM requires Linux Kernel >= 4.20.0
2460+
pub const FAN_MARK_FILESYSTEM: ::c_uint = 0x0000_0100;
2461+
pub const FAN_MARK_IGNORED_MASK: ::c_uint = 0x0000_0020;
2462+
pub const FAN_MARK_IGNORED_SURV_MODIFY: ::c_uint = 0x0000_0040;
2463+
pub const FAN_MARK_FLUSH: ::c_uint = 0x0000_0080;
2464+
2465+
pub const FANOTIFY_METADATA_VERSION: u8 = 3;
2466+
2467+
pub const FAN_ALLOW: u32 = 0x01;
2468+
pub const FAN_DENY: u32 = 0x02;
2469+
2470+
pub const FAN_NOFD: ::c_int = -1;
2471+
24202472
pub const FUTEX_WAIT: ::c_int = 0;
24212473
pub const FUTEX_WAKE: ::c_int = 1;
24222474
pub const FUTEX_FD: ::c_int = 2;
@@ -3304,6 +3356,7 @@ extern "C" {
33043356
path: *const ::c_char,
33053357
mask: u32,
33063358
) -> ::c_int;
3359+
pub fn fanotify_init(flags: ::c_uint, event_f_flags: ::c_uint) -> ::c_int;
33073360
}
33083361

33093362
cfg_if! {

src/unix/linux_like/linux/musl/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,16 @@ extern "C" {
420420
needle: *const ::c_void,
421421
needlelen: ::size_t,
422422
) -> *mut ::c_void;
423+
// Musl targets need the `mask` argument of `fanotify_mark` be specified
424+
// `::c_ulonglong` instead of `u64` or there will be a type mismatch between
425+
// `long long unsigned int` and the expected `uint64_t`.
426+
pub fn fanotify_mark(
427+
fd: ::c_int,
428+
flags: ::c_uint,
429+
mask: ::c_ulonglong,
430+
dirfd: ::c_int,
431+
path: *const ::c_char,
432+
) -> ::c_int;
423433
}
424434

425435
cfg_if! {

src/unix/linux_like/linux/no_align.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ macro_rules! expand_align {
3535
__align: [::c_int; 0],
3636
size: [u8; ::__SIZEOF_PTHREAD_CONDATTR_T],
3737
}
38+
39+
pub struct fanotify_event_metadata {
40+
__align: [::c_long; 0],
41+
pub event_len: __u32,
42+
pub vers: __u8,
43+
pub reserved: __u8,
44+
pub metadata_len: __u16,
45+
pub mask: __u64,
46+
pub fd: ::c_int,
47+
pub pid: ::c_int,
48+
}
3849
}
3950

4051
s_no_extra_traits! {

0 commit comments

Comments
 (0)