1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Low level function mapping for fanotify
use crate::;
use c_void;
use ;
// Used for docs test
use crate*;
/// Get current platform sizeof of [`fanotify_event_metadata`].
const FAN_EVENT_METADATA_LEN: usize = ;
/// Get current platform sizeof of [`fanotify_event_with_fid`].
const FAN_EVENT_METADATA_FID_LEN: usize = ;
/// Get current platform size of [`fanotify_response`]
const FAN_WRITE_RESPONSE_LEN: usize = ;
/// Length of memory to be allocated for read buffer
pub static mut FAN_EVENT_BUFFER_LEN: Mutex = new;
/// Initializes a new fanotify group and returns a
/// file descriptor [`Fd`] for the event queue associated
/// with the group.
///
/// The file descriptor is used in calls to [`mark()`] to
/// specify the files, directories, mounts, or filesystems for which
/// fanotify events shall be created. These events are received by
/// reading from the file descriptor. Some events are only
/// informative, indicating that a file has been accessed. Other
/// events can be used to determine whether another application is
/// permitted to access a file or directory. Permission to access
/// filesystem objects is granted by writing to the file descriptor.
///
/// Multiple programs may be using the fanotify interface at the same
/// time to monitor the same files.
///
/// The number of fanotify groups per user is limited to 128.
/// This limit cannot be overridden.
///
/// Calling [`init()`] requires the `CAP_SYS_ADMIN` capability.
/// This constraint might be relaxed in future versions of the fanotify kernel API.
/// Therefore, certain additional capability checks have been
/// implemented as indicated below.
///
/// The flags argument contains a multi-bit field defining the
/// notification class of the listening application and further
/// single bit fields specifying the behavior of the file descriptor.
///
/// If multiple listeners for permission events exist, the
/// notification class is used to establish the sequence in which the
/// listeners receive the events.
///
/// # Arguments
/// * `flags` - Sets the notification group, can be mask of <br>
/// * [`FAN_CLASS_PRE_CONTENT`]
/// * [`FAN_CLASS_CONTENT`]
/// * [`FAN_CLASS_NOTIF`]
///
/// The following bits can additionally be set in `flags`: <br>
/// * [`FAN_CLOEXEC`]
/// * [`FAN_NONBLOCK`]
/// * [`FAN_UNLIMITED_QUEUE`]
/// * [`FAN_UNLIMITED_MARKS`]
/// * [`FAN_ENABLE_AUDIT`]
/// * [`FAN_REPORT_FID`]
/// * [`FAN_REPORT_DIR_FID`]
/// * [`FAN_REPORT_NAME`]
/// * [`FAN_REPORT_DFID_NAME`]
/// * `event_f_flags` - Defines the file status flags that
/// will be set on the open file descriptions that are created for
/// fanotify events. For details of these flags, see the description
/// of the flags values in open(2). `event_f_flags` includes a multi-
/// bit field for the access mode. This field can take the following
/// values:
/// * [`O_RDONLY`]
/// * [`O_WRONLY`]
/// * [`O_RDWR`]
///
/// Additional bits can be set in `event_f_flags`.
/// * [`O_LARGEFILE`]
/// * [`O_CLOEXEC`]
/// * [`O_APPEND`]
/// * [`O_DSYNC`]
/// * [`O_NOATIME`]
/// * [`O_NONBLOCK`]
///
/// # Example
/// This example may thorw error due to absence of `CAP_SYS_ADMIN` [capabilitity](https://man7.org/linux/man-pages/man7/capabilities.7.html)
/// ```rust
/// # use naughtyfy::flags::*;
/// # use naughtyfy::api::*;
/// let fd = init(FAN_CLASS_NOTIF | FAN_NONBLOCK, O_RDONLY);
/// match fd {
/// Ok(fd) => {
/// assert!(fd.is_valid());
/// }
/// Err(e) => {
/// eprintln!("Cannot get fd due to {e}");
///
/// }
/// }
/// ```
///
/// Adds, removes, or modifies an fanotify mark on a
/// filesystem object. The caller must have read permission on the
/// filesystem object that is to be marked.
///
/// # Arguments
/// * `fd` - Refrence to [`Fd`] returned by [`init()`]
/// * `flags` - Bit mask describing the modification to perform. <br>
/// It must include **exactly one** of the following values:
/// * [`FAN_MARK_ADD`]
/// * [`FAN_MARK_REMOVE`]
/// * [`FAN_MARK_FLUSH`]
///
/// In addition, zero or more of the following values may be ORed
/// into flags:
/// * [`FAN_MARK_DONT_FOLLOW`]
/// * [`FAN_MARK_ONLYDIR`]
/// * [`FAN_MARK_MOUNT`]
/// * [`FAN_MARK_FILESYSTEM`]
/// * [`FAN_MARK_IGNORED_MASK`]
/// * [`FAN_MARK_IGNORED_SURV_MODIFY`]
/// * `mask` - Which events shall be listened for (or which shall be ignored). <br>
/// It is a bit mask composed of the following values:
/// * [`FAN_ACCESS`]
/// * [`FAN_MODIFY`]
/// * [`FAN_CLOSE_WRITE`]
/// * [`FAN_CLOSE_NOWRITE`]
/// * [`FAN_OPEN`]
/// * [`FAN_OPEN_EXEC`]
/// * [`FAN_ATTRIB`]
/// * [`FAN_CREATE`]
/// * [`FAN_DELETE`]
/// * [`FAN_DELETE_SELF`]
/// * [`FAN_MOVED_FROM`]
/// * [`FAN_MOVED_TO`]
/// * [`FAN_MOVE_SELF`]
/// * [`FAN_OPEN_PERM`]
/// * [`FAN_OPEN_EXEC_PERM`]
/// * [`FAN_ACCESS_PERM`]
/// * [`FAN_ONDIR`]
/// * [`FAN_EVENT_ON_CHILD`]
/// * [`FAN_CLOSE`]
/// * [`FAN_MOVE`]
/// * `dirfd` - Defines the filesystem object to be marked.
/// * `path` - Filesystem path of file or diretory.
///
/// The filesystem object to be marked is determined by the file
/// descriptor dirfd and the pathname specified in path:
///
/// * If pathname is `NULL`, dirfd defines the filesystem object to be
/// marked.
/// * If pathname is `NULL`, and dirfd takes the special value
/// [`AT_FDCWD`], the current working directory is to be marked.
/// * If pathname is absolute, it defines the filesystem object to
/// be marked, and dirfd is ignored.
/// * If pathname is relative, and dirfd does not have the value
/// [`AT_FDCWD`], then the filesystem object to be marked is
/// determined by interpreting pathname relative the directory
/// referred to by dirfd.
/// * If pathname is relative, and dirfd has the value [`AT_FDCWD`],
/// then the filesystem object to be marked is determined by
/// interpreting pathname relative to the current working
/// directory.
///
/// # Example
/// This example may throw error due to absence of `CAP_SYS_ADMIN` [capabilitity](https://man7.org/linux/man-pages/man7/capabilities.7.html)
/// ```rust
/// # use naughtyfy::flags::*;
/// # use naughtyfy::types::*;
/// # use naughtyfy::api::*;
/// let fd = &init(FAN_CLASS_NOTIF, 0);
/// match fd {
/// Ok(fd) => {
/// let m = mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_ACCESS, AT_FDCWD, "./");
/// assert!(m.is_ok());
/// assert!(fd.is_valid());
/// }
/// Err(e) => {
/// // This can fail for multiple reason, most common being privileges.
/// eprintln!("Cannot get fd due to {e}");
///
/// }
/// }
/// ```
Sized + Path>
/// This function attempts to read from a file descriptor `fanotify_fd`
/// into a `Vec<fanotify_event_metadata>` and return a Result.
///
/// # Note
/// [`close()`] is called on metadata's fd when [`fanotify_event_metadata`]
/// is dropped. No need to explicitly call [`close()`] on every fd.
///
/// # Argument
/// * `fd` - Refrence to [`Fd`] returned by [`init()`]
///
/// # Example
/// This example may throw error due to absence of `CAP_SYS_ADMIN` [capabilitity](https://man7.org/linux/man-pages/man7/capabilities.7.html)
/// ```rust
/// # use naughtyfy::flags::*;
/// # use naughtyfy::types::*;
/// # use naughtyfy::api::*;
/// let fd = &init(FAN_CLASS_NOTIF, 0);
/// match fd {
/// Ok(fd) => {
/// let m = mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_ACCESS, AT_FDCWD, "./");
/// let res = read(fd);
/// assert!(res.is_ok());
/// for meta in res.unwrap() {
/// close(meta.fd).unwrap();
/// }
/// }
/// Err(e) => {
/// // This can fail for multiple reason, most common being privileges.
/// eprintln!("Cannot get fd due to {e}");
///
/// }
/// }
/// ```
/// This function attempts to read from a file descriptor `fanotify_fd`
/// and performs `process_metadata` on [`fanotify_event_metadata`] recieved after read.
/// returns `Result<(),FanotifyError>`.
///
/// This function closes `metadata.fd` after calling `process_metadata`
///
/// # Argument
/// * `fd` - Refrence to [`Fd`] returned by [`init()`]
/// * `process_metadata` - Function / Closure for processing [`fanotify_event_metadata`].
///
/// # Example
/// This example may throw error due to absence of `CAP_SYS_ADMIN` [capabilitity](https://man7.org/linux/man-pages/man7/capabilities.7.html)
/// ```rust
/// # use naughtyfy::flags::*;
/// # use naughtyfy::types::*;
/// # use naughtyfy::api::*;
/// fn procedure(md: &fanotify_event_metadata) {
/// println!("{md:#?}");
/// }
///
/// fn main() {
/// let fd = &init(FAN_CLASS_NOTIF, 0);
/// match fd {
/// Ok(fd) => {
/// let m = mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_ACCESS, AT_FDCWD, "./");
/// let res = read(fd);
/// assert!(res.is_ok());
/// read_do(fd,procedure);
/// }
/// Err(e) => {
/// // This can fail for multiple reason, most common being privileges.
/// eprintln!("Cannot get fd due to {e}");
///
/// }
/// }
/// }
/// ```
/// This function attempts to read from a file descriptor `fanotify_fd`
/// into a [`Vec`] of [`fanotify_event_with_fid`] which was initilated with
/// [`FAN_REPORT_FID`] or [`FAN_REPORT_DIR_FID`] flag. Returns the vector wrapped in `Result`.
///
/// # Important
/// Use this only when `fd` is initialized with [`FAN_REPORT_FID`] or [`FAN_REPORT_DIR_FID`] flag.
///
/// # Argument
/// * `fd` - Refrence to [`Fd`] returned by [`init()`]
/// This function attempts to read from a file descriptor `fanotify_fd`
/// which was initilated with [`FAN_REPORT_FID`] or [`FAN_REPORT_DIR_FID`] flag
/// and performs `process_metadata_fid` on [`fanotify_event_with_fid`]
/// recieved after read. Returns `Result<(),FanotifyError>`.
///
/// # Argument
/// * `fd` - Refrence to [`Fd`] returned by [`init()`].
/// * `process_metadata` - Function / Closure for processing [`fanotify_event_with_fid`].
/// Writes up to count bytes from the buffer starting at buf
/// to the file referred to by the file descriptor fd.
///
/// The number of bytes written may be less than count if, for
/// example, there is insufficient space on the underlying physical
/// medium, or the `RLIMIT_FSIZE` resource limit is encountered,
/// or the call was interrupted by a signal handler
/// after having written less than count bytes.
///
/// For a seekable file (i.e., one to which lseek(2) may be applied,
/// for example, a regular file) writing takes place at the file
/// offset, and the file offset is incremented by the number of bytes
/// actually written. If the file was open(2)ed with O_APPEND, the
/// file offset is first set to the end of the file before writing.
/// The adjustment of the file offset and the write operation are
/// performed as an atomic step.
///
/// POSIX requires that a read(2) that can be proved to occur after a
/// write() has returned will return the new data. Note that not all
/// filesystems are POSIX conforming.
///
/// According to POSIX.1, if count is greater than SSIZE_MAX, the
/// result is implementation-defined; see NOTES for the upper limit
/// on Linux.
///
/// # Argument
/// * `fd` - Refrence to [`Fd`] returned by [`init()`].
/// * `response` - This is a struct of type [`fanotify_response`]
/// that specifies how to deal with the request.
///
/// # Example
/// ```rust
/// # use naughtyfy::flags::*;
/// # use naughtyfy::types::*;
/// # use naughtyfy::api::*;
/// let fd = &init(FAN_CLOEXEC | FAN_CLASS_CONTENT, O_RDONLY | O_LARGEFILE);
/// match fd {
/// Ok(fd) => {
/// let m = mark(
/// fd,
/// FAN_MARK_ADD | FAN_MARK_MOUNT,
/// FAN_OPEN_PERM | FAN_CLOSE_WRITE,
/// AT_FDCWD,
/// "/tmp",
/// );
/// assert!(m.is_ok());
/// assert!(fd.is_valid());
///
/// let events = read(fd).unwrap();
/// if events.len() > 1 {
/// for event in events {
/// println!("{event:#?}");
/// write(
/// fd,
/// &fanotify_response {
/// fd: event.fd,
/// // Allowig all events
/// response: FAN_ALLOW,
/// },
/// )
/// .unwrap();
/// }
/// }
/// }
/// Err(e) => {
/// // This can fail for multiple reason, most common being privileges.
/// eprintln!("Cannot get fd due to {e}");
///
/// }
/// }
/// ```
/// Closes the file descriptor returned by [`init()`] or [`read()`]
///
/// # Argument
/// * `fd` - file descriptor in raw form ([`RawFd`])`