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