]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/fanotify.7
fanotify.7: srcfix: Add FIXME
[thirdparty/man-pages.git] / man7 / fanotify.7
1 .\" Copyright (C) 2013, Heinrich Schuchardt <xypron.glpk@gmx.de>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of
9 .\" this manual under the conditions for verbatim copying, provided that
10 .\" the entire resulting derived work is distributed under the terms of
11 .\" a permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume.
15 .\" no responsibility for errors or omissions, or for damages resulting.
16 .\" from the use of the information contained herein. The author(s) may.
17 .\" not have taken the same level of care in the production of this.
18 .\" manual, which is licensed free of charge, as they might when working.
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .TH FANOTIFY 7 2014-04-24 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 fanotify \- monitoring filesystem events
27 .SH DESCRIPTION
28 The fanotify API provides notification and interception of filesystem events.
29 Use cases include virus scanning and hierarchical storage management.
30 Currently, only a limited set of events is supported.
31 In particular, there is no support for create, delete, and move events.
32 (See
33 .BR inotify (7)
34 for details of an API that does notify those events.)
35
36 Additional capabilities compared to the
37 .BR inotify (7)
38 API are monitoring of complete mounts, access permission decisions, and the
39 possibility to read or modify files before access by other applications.
40
41 The following system calls are used with this API:
42 .BR fanotify_init (2),
43 .BR fanotify_mark (2),
44 .BR poll (2),
45 .BR ppoll (2),
46 .BR read (2),
47 .BR write (2),
48 and
49 .BR close (2).
50 .SS fanotify_init(), fanotify_mark(), and notification groups
51 The
52 .BR fanotify_init (2)
53 system call creates and initializes an fanotify notification group
54 and returns a file descriptor referring to it.
55 .PP
56 An fanotify notification group is a kernel-internal object that holds
57 a list of files, directories, and mount points for which events shall be
58 created.
59 .PP
60 For each entry in an fanotify notification group, two bit masks exist: the
61 .I mark
62 mask and the
63 .I ignore
64 mask.
65 The mark mask defines file activities for which an event shall be created.
66 The ignore mask defines activities for which no event shall be generated.
67 Having these two types of masks permits a mount point or directory to be
68 marked for receiving events, while at the same time ignoring events for
69 specific objects under that mount point or directory.
70 .PP
71 The
72 .BR fanotify_mark (2)
73 system call adds a file, directory, or mount to a notification group
74 and specifies which events
75 shall be reported (or ignored), or removes or modifies such an entry.
76 .PP
77 A possible usage of the ignore mask is for a file cache.
78 Events of interest for a file cache are modification of a file and closing
79 of the same.
80 Hence, the cached directory or mount point is to be marked to receive these
81 events.
82 After receiving the first event informing that a file has been modified, the
83 corresponding cache entry will be invalidated.
84 No further modification events for this file are of interest until the file is
85 closed.
86 Hence, the modify event can be added to the ignore mask.
87 Upon receiving the closed event, the modify event can be removed from the
88 ignore mask and the file cache entry can be updated.
89 .PP
90 The entries in the fanotify notification groups refer to files and directories
91 via their inode number and to mounts via their mount ID.
92 If files or directories are renamed or moved, the respective entries survive.
93 If files or directories are deleted or mounts are unmounted, the corresponding
94 entries are deleted.
95 .SS The event queue
96 As events occur on the filesystem objects monitired by a notification group,
97 the fanotify system generates events that are collected in a queue.
98 These events can then be read (using
99 .BR read (2)
100 or similar)
101 from the fanotify file descriptor
102 returned by
103 .BR fanotify_init (2).
104
105 Two types of events are generated:
106 notification events and permission events.
107 Notification events are merely informative
108 and require no action to be taken by
109 the receiving application except for closing the file descriptor passed in the
110 event.
111 Permission events are requests to the receiving application to decide whether
112 permission for a file access shall be granted.
113 For these events, the recipient must write a response which decides whether
114 access is granted or not.
115
116 Queue entries for notification events are removed when the event has been
117 read.
118 Queue entries for permission events are removed when the permission
119 decision has been taken by writing to the fanotify file descriptor.
120 .SS Reading fanotify events
121 Calling
122 .BR read (2)
123 for the file descriptor returned by
124 .BR fanotify_init (2)
125 blocks (if the flag
126 .B FAN_NONBLOCK
127 is not specified in the call to
128 .BR fanotify_init (2))
129 until either a file event occurs or the call is interrupted by a signal
130 (see
131 .BR signal (7)).
132
133 The return value of
134 .BR read (2)
135 is the length of the filled buffer, or \-1 in case of an error.
136 After a successful
137 .BR read (2),
138 the read buffer contains one or more of the following structures:
139
140 .in +4n
141 .nf
142 struct fanotify_event_metadata {
143 __u32 event_len;
144 __u8 vers;
145 __u8 reserved;
146 __u16 metadata_len;
147 __aligned_u64 mask;
148 __s32 fd;
149 __s32 pid;
150 };
151 .fi
152 .in
153 .PP
154 The fields of this structure as follows:
155 .TP
156 .I event_len
157 This is the length of the data for the current event and the offset to the next
158 event in the buffer.
159 In the current implementation, the value of
160 .I event_len
161 is always
162 .BR FAN_EVENT_METADATA_LEN .
163 In principle, the API design would allow to return variable-length structures.
164 Therefore, and for performance reasons, it is recommended to use a larger
165 buffer size when reading, for example 4096 bytes.
166 .TP
167 .I vers
168 This field holds a version number for the structure.
169 It must be compared to
170 .B FANOTIFY_METADATA_VERSION
171 to verify that the structures returned at runtime match
172 the structures defined at compile time.
173 In case of a mismatch, the application should abandon trying to use the
174 fanotify file descriptor.
175 .TP
176 .I reserved
177 This field is not used.
178 .TP
179 .I metadata_len
180 This is the length of the structure.
181 The field was introduced to facilitate the implementation of optional headers
182 per event type.
183 No such optional headers exist in the current implementation.
184 .TP
185 .I mask
186 This is a bit mask describing the event.
187 .TP
188 .I fd
189 This is an open file descriptor for the object being accessed, or
190 .B FAN_NOFD
191 if a queue overflow occurred.
192 The file descriptor can be used to access the contents of the monitored file or
193 directory.
194 The
195 .B FMODE_NONOTIFY
196 file status flag is set on the corresponding open file description.
197 This flag suppresses fanotify event generation.
198 Hence, when the receiver of the fanotify event accesses the notified file or
199 directory using this file descriptor, no additional events will be created.
200 The reading application is responsible for closing the file descriptor.
201 .TP
202 .I pid
203 This is the ID of the process that caused the event.
204 A program listening to fanotify events can compare this PID to the PID returned
205 by
206 .BR getpid (2),
207 to determine whether the event is caused by the listener itself, or is due to a
208 file access by another program.
209 .PP
210 The bit mask in
211 .I mask
212 signals which events have occurred for a single filesystem object.
213 Multiple bits may be set in this mask,
214 if more than one event occurred for the monitored filesystem obect.
215 In particular,
216 consecutive events for the same filesystem object and originating from the
217 same process may be merged into a single event, with the exception that two
218 permission events are never merged into one queue entry.
219 .PP
220 The bits that may appear in
221 .I mask
222 are as follows:
223 .TP
224 .B FAN_ACCESS
225 A file or a directory (but see BUGS) was accessed (read).
226 .TP
227 .B FAN_OPEN
228 A file or a directory was opened.
229 .TP
230 .B FAN_MODIFY
231 A file was modified.
232 .TP
233 .B FAN_CLOSE_WRITE
234 A file that was opened for writing
235 .RB ( O_WRONLY
236 or
237 .BR O_RDWR )
238 was closed.
239 .TP
240 .B FAN_CLOSE_NOWRITE
241 A file or directory that was opened read-only
242 .RB ( O_RDONLY )
243 was closed.
244 .TP
245 .B FAN_Q_OVERFLOW
246 The event queue exceeded the limit of 16384 entries.
247 This limit can be overridden in the call to
248 .BR fanotify_init (2)
249 by setting the flag
250 .BR FAN_UNLIMITED_QUEUE .
251 .TP
252 .B FAN_ACCESS_PERM
253 An application wants to read a file or directory, for example using
254 .BR read (2)
255 or
256 .BR readdir (2).
257 The reader must write a response that determines whether the permission to
258 access the filesystem object shall be granted.
259 .TP
260 .B FAN_OPEN_PERM
261 An application wants to open a file or directory.
262 The reader must write a response that determines whether the permission to
263 open the filesystem object shall be granted.
264 .PP
265 To check for any close event, the following bit mask may be used:
266 .TP
267 .B FAN_CLOSE
268 A file was closed.
269 This is a synonym for;
270
271 FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE
272 .PP
273 The following macros are provided to iterate over a buffer containing fanotify
274 event metadata returned by a
275 .BR read (2)
276 from an fanotify file descriptor.
277 .TP
278 .B FAN_EVENT_OK(meta, len)
279 This macro checks the remaining length
280 .I len
281 of the buffer
282 .I meta
283 against the length of the metadata structure and the
284 .I event_len
285 field of the first metadata structure in the buffer.
286 .TP
287 .B FAN_EVENT_NEXT(meta, len)
288 This macro sets the pointer
289 .I meta
290 to the next metadata structure using the length indicated in the
291 .I event_len
292 field of the metadata structure and reduces the remaining length of the
293 buffer
294 .IR len .
295 .SS Monitoring an fanotify file descriptor for events
296 When an fanotify event occurs, the fanotify file descriptor indicates as
297 readable when passed to
298 .BR epoll (7),
299 .BR poll (2),
300 or
301 .BR select (2).
302 .SS Dealing with permission events
303 For permission events, the application must
304 .BR write (2)
305 a structure of the following form to the
306 fanotify file descriptor:
307
308 .in +4n
309 .nf
310 struct fanotify_response {
311 __s32 fd;
312 __u32 response;
313 };
314 .fi
315 .in
316 .PP
317 The fields of this structure are as follows:
318 .TP
319 .I fd
320 This is the file descriptor from the structure
321 .IR fanotify_event_metadata .
322 .TP
323 .I response
324 This field indicates whether or not the permission is to be granted.
325 Its value must be either
326 .B FAN_ALLOW
327 to allow the file operation or
328 .B FAN_DENY
329 to deny the file operation.
330 .PP
331 If access is denied, the requesting application call will receive an
332 .BR EPERM
333 error.
334 .SS Closing the fanotify file descriptor
335 .PP
336 When all file descriptors referring to the fanotify notification group are
337 closed, the fanotify group is released and its resources
338 are freed for reuse by the kernel.
339 Upon
340 .BR close (2),
341 outstanding permission events will be set to allowed.
342 .SS /proc/[pid]/fdinfo
343 The file
344 .I /proc/[pid]/fdinfo/[fd]
345 contains information about fanotify marks for file descriptor
346 .I fd
347 of process
348 .IR pid .
349 See the kernel source file
350 .I Documentation/filesystems/proc.txt
351 for details.
352 .SH ERRORS
353 In addition to the usual errors for
354 .BR read (2),
355 the following errors can occur when reading from the fanotify file descriptor:
356 .TP
357 .B EINVAL
358 The buffer is too short to hold the event.
359 .TP
360 .B EMFILE
361 The per-process limit on the number of open files has been reached.
362 See the description of
363 .B RLIMIT_NOFILE
364 in
365 .BR getrlimit (2).
366 .TP
367 .B ENFILE
368 The system-wide limit on the number of open files has been reached.
369 See
370 .I /proc/sys/fs/file-max
371 in
372 .BR proc (5).
373 .TP
374 .B ETXTBSY
375 This error is returned by
376 .BR read (2)
377 if
378 .B O_RDWR
379 or
380 .B O_WRONLY
381 was specified in the
382 .I event_f_flags
383 argument when calling
384 .BR fanotify_init (2)
385 and an event occurred for a monitored file that is currently being executed.
386 .PP
387 In addition to the usual errors for
388 .BR write (2),
389 the following errors can occur when writing to the fanotify file descriptor:
390 .TP
391 .B EINVAL
392 Fanotify access permissions are not enabled in the kernel configuration or the
393 value of
394 .I response
395 in the response structure is not valid.
396 .TP
397 .B ENOENT
398 The file descriptor
399 .I fd
400 in the response structure is not valid.
401 This might occur because the file was already deleted by another thread or
402 process.
403 .SH VERSIONS
404 The fanotify API was introduced in version 2.6.36 of the Linux kernel and
405 enabled in version 2.6.37.
406 Fdinfo support was added in version 3.8.
407 .SH "CONFORMING TO"
408 The fanotify API is Linux-specific.
409 .SH NOTES
410 The fanotify API is available only if the kernel was built with the
411 .B CONFIG_FANOTIFY
412 configuration option enabled.
413 In addition, fanotify permission handling is available only if the
414 .B CONFIG_FANOTIFY_ACCESS_PERMISSIONS
415 configuration option is enabled.
416 .SS Limitations and caveats
417 Fanotify reports only events that a user-space program triggers through the
418 filesystem API.
419 As a result, it does not catch remote events that occur on network filesystems.
420 .PP
421 The fanotify API does not report file accesses and modifications that
422 may occur because of
423 .BR mmap (2),
424 .BR msync (2),
425 and
426 .BR munmap (2).
427 .PP
428 Events for directories are created only if the directory itself is opened,
429 read, and closed.
430 Adding, removing, or changing children of a marked directory does not create
431 events for the monitored directory itself.
432 .PP
433 Fanotify monitoring of directories is not recursive: to monitor subdirectories
434 under a directory, additional marks must be created.
435 (But note that the fanotify API provides no way of detecting when a
436 subdirectory has been created under a marked directory, which makes recursive
437 monitoring difficult.)
438 Monitoring mounts offers the capability to monitor a whole directory tree.
439 .PP
440 The event queue can overflow.
441 In this case, events are lost.
442 .SH BUGS
443 As of Linux 3.15,
444 the following bugs exist:
445 .IP * 3
446 .\" FIXME: Patch is in linux-next-20140424.
447 .BR readdir (2)
448 does not create a
449 .B FAN_ACCESS
450 event.
451 .IP *
452 .\" FIXME: A patch was proposed.
453 When an event is generated, no check is made to see whether the user ID of the
454 receiving process has authorization to read or write the file before passing a
455 file descriptor for that file.
456 This poses a security risk, when the
457 .B CAP_SYS_ADMIN
458 capability is set for programs executed by unprivileged users.
459 .SH EXAMPLE
460 The following program demonstrates the usage of the fanotify API.
461 It marks the mount point passed as command-line argument
462 and waits for events of type
463 .B FAN_PERM_OPEN
464 and
465 .BR FAN_CLOSE_WRITE .
466 When a permission event occurs, a
467 .B FAN_ALLOW
468 response is given.
469 .PP
470 The following output was recorded while editing the file
471 .IR /home/user/temp/notes .
472 Before the file was opened, a
473 .B FAN_OPEN_PERM
474 event occurred.
475 After the file was closed, a
476 .B FAN_CLOSE_WRITE
477 event occurred.
478 Execution of the program ends when the user presses the ENTER key.
479 .SS Example output
480 .in +4n
481 .nf
482 # ./fanotify_example /home
483 Press enter key to terminate.
484 Listening for events.
485 FAN_OPEN_PERM: File /home/user/temp/notes
486 FAN_CLOSE_WRITE: File /home/user/temp/notes
487
488 Listening for events stopped.
489 .fi
490 .in
491 .SS Program source
492 .nf
493 #define _GNU_SOURCE /* Needed to get O_LARGEFILE definition */
494 #include <errno.h>
495 #include <fcntl.h>
496 #include <limits.h>
497 #include <poll.h>
498 #include <stdio.h>
499 #include <stdlib.h>
500 #include <sys/fanotify.h>
501 #include <unistd.h>
502
503 /* Read all available fanotify events from the file descriptor 'fd' */
504
505 static void
506 handle_events(int fd)
507 {
508 const struct fanotify_event_metadata *metadata;
509 char buf[4096];
510 ssize_t len;
511 char path[PATH_MAX];
512 ssize_t path_len;
513 char procfd_path[PATH_MAX];
514 struct fanotify_response response;
515
516 /* Loop while events can be read from fanotify file descriptor */
517
518 for(;;) {
519
520 /* Read some events */
521
522 len = read(fd, (void *) &buf, sizeof(buf));
523 if (len == \-1 && errno != EAGAIN) {
524 perror("read");
525 exit(EXIT_FAILURE);
526 }
527
528 /* Check if end of available data reached */
529
530 if (len <= 0)
531 break;
532
533 /* Point to the first event in the buffer */
534
535 metadata = (struct fanotify_event_metadata *) buf;
536
537 /* Loop over all events in the buffer */
538
539 while (FAN_EVENT_OK(metadata, len)) {
540
541 /* Check that run\-time and compile\-time structures match */
542
543 if (metadata\->vers != FANOTIFY_METADATA_VERSION) {
544 fprintf(stderr,
545 "Mismatch of fanotify metadata version.\\n");
546 exit(EXIT_FAILURE);
547 }
548
549 /* Check that the event contains a file descriptor */
550 .\" FIXME Should the following line actually be:
551 .\" if (metadata\->fd != FANNOFD)
552 .\" ?
553 .\" Or, more generally, the program has a problem inasmuch as it is not
554 .\" handling queue overflow
555 if (metadata\->fd >= 0) {
556
557 /* Handle open permission event */
558
559 if (metadata\->mask & FAN_OPEN_PERM) {
560 printf("FAN_OPEN_PERM: ");
561
562 /* Allow file to be opened */
563
564 response.fd = metadata\->fd;
565 response.response = FAN_ALLOW;
566 write(fd, &response,
567 sizeof(struct fanotify_response));
568 }
569
570 /* Handle closing of writable file event */
571
572 if (metadata\->mask & FAN_CLOSE_WRITE)
573 printf("FAN_CLOSE_WRITE: ");
574
575 /* Retrieve and print pathname of the accessed file */
576
577 snprintf(procfd_path, sizeof(procfd_path),
578 "/proc/self/fd/%d", metadata\->fd);
579 path_len = readlink(procfd_path, path,
580 sizeof(path) \- 1);
581 if (path_len == \-1) {
582 perror("readlink");
583 exit(EXIT_FAILURE);
584 }
585
586 path[path_len] = '\\0';
587 printf("File %s\\n", path);
588
589 /* Close the file descriptor of the event */
590
591 close(metadata\->fd);
592 }
593
594 /* Advance to next event */
595
596 metadata = FAN_EVENT_NEXT(metadata, len);
597 }
598 }
599 }
600
601 int
602 main(int argc, char *argv[])
603 {
604 char buf;
605 int fd, poll_num;
606 nfds_t nfds;
607 struct pollfd fds[2];
608
609 /* Check mount point is supplied */
610
611 if (argc != 2) {
612 fprintf(stderr, "Usage: %s MOUNT\\n", argv[0]);
613 exit(EXIT_FAILURE);
614 }
615
616 printf("Press enter key to terminate.\\n");
617
618 /* Create the file descriptor for accessing the fanotify API */
619
620 fd = fanotify_init(FAN_CLOEXEC | FAN_CLASS_CONTENT | FAN_NONBLOCK,
621 O_RDONLY | O_LARGEFILE);
622 if (fd == \-1) {
623 perror("fanotify_init");
624 exit(EXIT_FAILURE);
625 }
626
627 /* Mark the mount for:
628 \- permission events before opening files
629 \- notification events after closing a write\-enabled
630 file descriptor */
631
632 if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT,
633 FAN_OPEN_PERM | FAN_CLOSE_WRITE, \-1,
634 argv[1]) == \-1) {
635 perror("fanotify_mark");
636 exit(EXIT_FAILURE);
637 }
638
639 /* Prepare for polling */
640
641 nfds = 2;
642
643 /* Console input */
644
645 fds[0].fd = STDIN_FILENO;
646 fds[0].events = POLLIN;
647
648 /* Fanotify input */
649
650 fds[1].fd = fd;
651 fds[1].events = POLLIN;
652
653 /* This is the loop to wait for incoming events */
654
655 printf("Listening for events.\\n");
656
657 while (1) {
658 poll_num = poll(fds, nfds, \-1);
659 if (poll_num == \-1) {
660 if (errno == EINTR) /* Interrupted by a signal */
661 continue; /* Restart poll() */
662
663 perror("poll"); /* Unexpected error */
664 exit(EXIT_FAILURE);
665 }
666
667 if (poll_num > 0) {
668 if (fds[0].revents & POLLIN) {
669
670 /* Console input is available: empty stdin and quit */
671
672 while (read(STDIN_FILENO, &buf, 1) > 0 && buf != '\\n')
673 continue;
674 break;
675 }
676
677 if (fds[1].revents & POLLIN) {
678
679 /* Fanotify events are available */
680
681 handle_events(fd);
682 }
683 }
684 }
685
686 printf("Listening for events stopped.\\n");
687 exit(EXIT_SUCCESS);
688 }
689 .fi
690 .SH "SEE ALSO"
691 .ad l
692 .BR fanotify_init (2),
693 .BR fanotify_mark (2),
694 .BR inotify (7)