Skip to content

Commit 46b18b8

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 276eaa2 commit 46b18b8

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-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/gnu/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,13 @@ extern "C" {
11981198
pub fn ntp_adjtime(buf: *mut timex) -> ::c_int;
11991199
#[link_name = "ntp_gettimex"]
12001200
pub fn ntp_gettime(buf: *mut ntptimeval) -> ::c_int;
1201+
pub fn fanotify_mark(
1202+
fd: ::c_int,
1203+
flags: ::c_uint,
1204+
mask: u64,
1205+
dirfd: ::c_int,
1206+
path: *const ::c_char,
1207+
) -> ::c_int;
12011208
}
12021209

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

src/unix/linux_like/linux/mod.rs

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

480+
#[repr(align(8))]
481+
pub struct fanotify_event_metadata {
482+
pub event_len: __u32,
483+
pub vers: __u8,
484+
pub reserved: __u8,
485+
pub metadata_len: __u16,
486+
pub mask: __u64,
487+
pub fd: ::c_int,
488+
pub pid: ::c_int,
489+
}
490+
491+
pub struct fanotify_response {
492+
pub fd: ::c_int,
493+
pub response: __u32,
494+
}
495+
480496
pub struct sockaddr_vm {
481497
pub svm_family: ::sa_family_t,
482498
pub svm_reserved1: ::c_ushort,
@@ -2417,6 +2433,53 @@ pub const IN_ALL_EVENTS: u32 = IN_ACCESS
24172433
pub const IN_CLOEXEC: ::c_int = O_CLOEXEC;
24182434
pub const IN_NONBLOCK: ::c_int = O_NONBLOCK;
24192435

2436+
// uapi/linux/fanotify.h
2437+
pub const FAN_ACCESS: u64 = 0x0000_0001;
2438+
pub const FAN_MODIFY: u64 = 0x0000_0002;
2439+
pub const FAN_CLOSE_WRITE: u64 = 0x0000_0008;
2440+
pub const FAN_CLOSE_NOWRITE: u64 = 0x0000_0010;
2441+
pub const FAN_OPEN: u64 = 0x0000_0020;
2442+
2443+
pub const FAN_Q_OVERFLOW: u64 = 0x0000_4000;
2444+
2445+
pub const FAN_OPEN_PERM: u64 = 0x0001_0000;
2446+
pub const FAN_ACCESS_PERM: u64 = 0x0002_0000;
2447+
2448+
pub const FAN_ONDIR: u64 = 0x4000_0000;
2449+
2450+
pub const FAN_EVENT_ON_CHILD: u64 = 0x0800_0000;
2451+
2452+
pub const FAN_CLOSE: u64 = FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE;
2453+
2454+
pub const FAN_CLOEXEC: ::c_uint = 0x0000_0001;
2455+
pub const FAN_NONBLOCK: ::c_uint = 0x0000_0002;
2456+
2457+
pub const FAN_CLASS_NOTIF: ::c_uint = 0x0000_0000;
2458+
pub const FAN_CLASS_CONTENT: ::c_uint = 0x0000_0004;
2459+
pub const FAN_CLASS_PRE_CONTENT: ::c_uint = 0x0000_0008;
2460+
2461+
pub const FAN_UNLIMITED_QUEUE: ::c_uint = 0x0000_0010;
2462+
pub const FAN_UNLIMITED_MARKS: ::c_uint = 0x0000_0020;
2463+
2464+
pub const FAN_MARK_ADD: ::c_uint = 0x0000_0001;
2465+
pub const FAN_MARK_REMOVE: ::c_uint = 0x0000_0002;
2466+
pub const FAN_MARK_DONT_FOLLOW: ::c_uint = 0x0000_0004;
2467+
pub const FAN_MARK_ONLYDIR: ::c_uint = 0x0000_0008;
2468+
pub const FAN_MARK_INODE: ::c_uint = 0x0000_0000;
2469+
pub const FAN_MARK_MOUNT: ::c_uint = 0x0000_0010;
2470+
// NOTE: FAN_MARK_FILESYSTEM requires Linux Kernel >= 4.20.0
2471+
pub const FAN_MARK_FILESYSTEM: ::c_uint = 0x0000_0100;
2472+
pub const FAN_MARK_IGNORED_MASK: ::c_uint = 0x0000_0020;
2473+
pub const FAN_MARK_IGNORED_SURV_MODIFY: ::c_uint = 0x0000_0040;
2474+
pub const FAN_MARK_FLUSH: ::c_uint = 0x0000_0080;
2475+
2476+
pub const FANOTIFY_METADATA_VERSION: u8 = 3;
2477+
2478+
pub const FAN_ALLOW: u32 = 0x01;
2479+
pub const FAN_DENY: u32 = 0x02;
2480+
2481+
pub const FAN_NOFD: ::c_int = -1;
2482+
24202483
pub const FUTEX_WAIT: ::c_int = 0;
24212484
pub const FUTEX_WAKE: ::c_int = 1;
24222485
pub const FUTEX_FD: ::c_int = 2;
@@ -3328,6 +3391,7 @@ extern "C" {
33283391
path: *const ::c_char,
33293392
mask: u32,
33303393
) -> ::c_int;
3394+
pub fn fanotify_init(flags: ::c_uint, event_f_flags: ::c_uint) -> ::c_int;
33313395
}
33323396

33333397
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! {

0 commit comments

Comments
 (0)