]> git.ipfire.org Git - thirdparty/man-pages.git/blobdiff - man7/fanotify.7
Start of man-pages-5.02: updating .Announce and .lsm files
[thirdparty/man-pages.git] / man7 / fanotify.7
index efb20bb9c2942a9dc29230fd0c080aa433ef4f3b..74246063e5c613e02b45246630b8f7e0aaf5cd3e 100644 (file)
@@ -1,4 +1,5 @@
 .\" Copyright (C) 2013, Heinrich Schuchardt <xypron.glpk@gmx.de>
+.\" and Copyright (C) 2014, Michael Kerrisk <mtk.manpages@gmail.com>
 .\"
 .\" %%%LICENSE_START(VERBATIM)
 .\" Permission is granted to make and distribute verbatim copies of this
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
 .\" %%%LICENSE_END
-.TH FANOTIFY 7 2014-04-24 "Linux" "Linux Programmer's Manual"
+.TH FANOTIFY 7 2019-03-06 "Linux" "Linux Programmer's Manual"
 .SH NAME
 fanotify \- monitoring filesystem events
 .SH DESCRIPTION
-The
-.B fanotify
-API provides notification and interception of filesystem events.
+The fanotify API provides notification and interception of
+filesystem events.
 Use cases include virus scanning and hierarchical storage management.
 Currently, only a limited set of events is supported.
-In particular there is no support for create, delete, and move events.
-
+In particular, there is no support for create, delete, and move events.
+(See
+.BR inotify (7)
+for details of an API that does notify those events.)
+.PP
 Additional capabilities compared to the
 .BR inotify (7)
-API are monitoring of complete mounts, access permission decisions, and the
+API include the ability to monitor all of the objects
+in a mounted filesystem,
+the ability to make access permission decisions, and the
 possibility to read or modify files before access by other applications.
-
+.PP
 The following system calls are used with this API:
 .BR fanotify_init (2),
 .BR fanotify_mark (2),
-.BR poll (2),
-.BR ppoll (2),
 .BR read (2),
 .BR write (2),
 and
 .BR close (2).
-.PP
+.SS fanotify_init(), fanotify_mark(), and notification groups
+The
 .BR fanotify_init (2)
-creates and initializes an fanotify notification group and returns a file
-descriptor referring to it.
+system call creates and initializes an fanotify notification group
+and returns a file descriptor referring to it.
 .PP
-An fanotify notification group is an internal object of the kernel which holds
-a list of files, directories, and mount points for which events shall be
-created.
+An fanotify notification group is a kernel-internal object that holds
+a list of files, directories, filesystems, and mount points for which
+events shall be created.
 .PP
-For each entry in an fanotify notification group, two bit masks exist.
-One mask (the mark mask) defines file activities for which an event shall be
-created.
-Another mask (the ignore mask) defines activities for which no event shall be
-generated.
-Having these two types of masks permits a mount point or directory to be
-marked for receiving events, while at the same time ignoring events for
-specific objects under that mount point or directory.
+For each entry in an fanotify notification group, two bit masks exist: the
+.I mark
+mask and the
+.I ignore
+mask.
+The mark mask defines file activities for which an event shall be created.
+The ignore mask defines activities for which no event shall be generated.
+Having these two types of masks permits a filesystem, mount point, or
+directory to be marked for receiving events, while at the same time
+ignoring events for specific objects under a mount point or directory.
+.PP
+The
+.BR fanotify_mark (2)
+system call adds a file, directory, filesystem or mount point to a
+notification group and specifies which events
+shall be reported (or ignored), or removes or modifies such an entry.
 .PP
 A possible usage of the ignore mask is for a file cache.
 Events of interest for a file cache are modification of a file and closing
 of the same.
 Hence, the cached directory or mount point is to be marked to receive these
 events.
-After receiving the first event informing that a file has been modified, the
-corresponding cache entry will be invalidated.
-No further modification events for this file are of interest until the file is
-closed.
+After receiving the first event informing that a file has been modified,
+the corresponding cache entry will be invalidated.
+No further modification events for this file are of interest until the file
+is closed.
 Hence, the modify event can be added to the ignore mask.
-Upon receiving the closed event, the modify event can be removed from the
+Upon receiving the close event, the modify event can be removed from the
 ignore mask and the file cache entry can be updated.
 .PP
-The entries in the fanotify notification groups refer to files and directories
-via their inode number and to mounts via their mount ID.
-If files or directories are renamed or moved, the respective entries survive.
-If files or directories are deleted or mounts are unmounted, the corresponding
-entries are deleted.
+The entries in the fanotify notification groups refer to files and
+directories via their inode number and to mounts via their mount ID.
+If files or directories are renamed or moved within the same mount,
+the respective entries survive.
+If files or directories are deleted or moved to another mount or if
+filesystems or mounts are unmounted, the corresponding entries are deleted.
+.SS The event queue
+As events occur on the filesystem objects monitored by a notification group,
+the fanotify system generates events that are collected in a queue.
+These events can then be read (using
+.BR read (2)
+or similar)
+from the fanotify file descriptor
+returned by
+.BR fanotify_init (2).
 .PP
-Two types of events exist: notification events and permission events.
-Notification events are only informative and require no action to be taken by
-the receiving application except for closing the file descriptor passed in the
-event.
-Permission events are requests to the receiving application to decide whether
-permission for a file access shall be granted.
+Two types of events are generated:
+.I notification
+events and
+.I permission
+events.
+Notification events are merely informative
+and require no action to be taken by
+the receiving application except for closing the file descriptor passed
+in the event (see below).
+Permission events are requests to the receiving application to decide
+whether permission for a file access shall be granted.
 For these events, the recipient must write a response which decides whether
 access is granted or not.
 .PP
-When all file descriptors referring to the fanotify notification group are
-closed, the fanotify group is released and its resources are freed for reuse by
-the kernel.
-.PP
-.BR fanotify_mark (2)
-adds a file, directory, or mount to the group and specifies which events
-shall be reported (or ignored), or removes or modifies such an entry.
-.PP
-When an fanotify event occurs, the fanotify file descriptor indicates as
-readable when passed to
-.BR epoll (7),
-.BR poll (2),
-or
-.BR select (2).
-.PP
-All events for an fanotify group are collected in a queue.
-Consecutive events for the same filesystem object and originating from the
-same process may be merged into a single event, with the exception that two
-permission events are never merged into one queue entry.
-Queue entries for notification events are removed when the event has been
-read.
-Queue entries for permission events are removed when the permission
-decision has been taken by writing to the fanotify file descriptor.
-.PP
+An event is removed from the event queue of the fanotify group
+when it has been read.
+Permission events that have been read are kept in an internal list of the
+fanotify group until either a permission decision has been taken by
+writing to the fanotify file descriptor or the fanotify file descriptor
+is closed.
+.SS Reading fanotify events
 Calling
 .BR read (2)
 for the file descriptor returned by
@@ -127,16 +137,13 @@ is not specified in the call to
 until either a file event occurs or the call is interrupted by a signal
 (see
 .BR signal (7)).
-
-The return value of
-.BR read (2)
-is the length of the filled buffer, or \-1 in case of an error.
+.PP
 After a successful
-.BR read(2),
+.BR read (2),
 the read buffer contains one or more of the following structures:
-
+.PP
 .in +4n
-.nf
+.EX
 struct fanotify_event_metadata {
     __u32 event_len;
     __u8 vers;
@@ -146,27 +153,39 @@ struct fanotify_event_metadata {
     __s32 fd;
     __s32 pid;
 };
-.fi
+.EE
 .in
-
-.TP 15
+.PP
+For performance reasons, it is recommended to use a large
+buffer size (for example, 4096 bytes),
+so that multiple events can be retrieved by a single
+.BR read (2).
+.PP
+The return value of
+.BR read (2)
+is the number of bytes placed in the buffer,
+or \-1 in case of an error (but see BUGS).
+.PP
+The fields of the
+.I fanotify_event_metadata
+structure are as follows:
+.TP
 .I event_len
-This is the length of the data for the current event and the offset to the next
-event in the buffer.
-In the current implementation the value of
+This is the length of the data for the current event and the offset
+to the next event in the buffer.
+In the current implementation, the value of
 .I event_len
 is always
 .BR FAN_EVENT_METADATA_LEN .
-In principal the API design would allow to return variable length structures.
-Therefore, and for performance reasons, it is recommended to use a larger
-buffer size when reading, for example 4096 bytes.
+However, the API is designed to allow
+variable-length structures to be returned in the future.
 .TP
 .I vers
-This field holds a version number for the structures.
+This field holds a version number for the structure.
 It must be compared to
 .B FANOTIFY_METADATA_VERSION
-to verify that the structures at runtime match the structures at compile
-time.
+to verify that the structures returned at run time match
+the structures defined at compile time.
 In case of a mismatch, the application should abandon trying to use the
 fanotify file descriptor.
 .TP
@@ -175,39 +194,61 @@ This field is not used.
 .TP
 .I metadata_len
 This is the length of the structure.
-The field was introduced to facilitate the implementation of optional headers
-per event type.
+The field was introduced to facilitate the implementation of
+optional headers per event type.
 No such optional headers exist in the current implementation.
 .TP
 .I mask
-This is a bit mask describing the event.
+This is a bit mask describing the event (see below).
 .TP
 .I fd
 This is an open file descriptor for the object being accessed, or
 .B FAN_NOFD
 if a queue overflow occurred.
-The file descriptor can be used to access the contents of the monitored file or
-directory.
-It has internal the flag
+The file descriptor can be used to access the contents
+of the monitored file or directory.
+The reading application is responsible for closing this file descriptor.
+.IP
+When calling
+.BR fanotify_init (2),
+the caller may specify (via the
+.I event_f_flags
+argument) various file status flags that are to be set
+on the open file description that corresponds to this file descriptor.
+In addition, the (kernel-internal)
 .B FMODE_NONOTIFY
-set.
+file status flag is set on the open file description.
 This flag suppresses fanotify event generation.
 Hence, when the receiver of the fanotify event accesses the notified file or
 directory using this file descriptor, no additional events will be created.
-The reading application is responsible for closing the file descriptor.
 .TP
 .I pid
-This is the ID of the process that caused the event.
-A program listening to fanotify events can compare this PID to the PID returned
-by
+If flag
+.B FAN_REPORT_TID
+was set in
+.BR fanotify_init (2),
+this is the TID of the thread that caused the event.
+Otherwise, this the PID of the process that caused the event.
+.PP
+A program listening to fanotify events can compare this PID
+to the PID returned by
 .BR getpid (2),
-to determine whether the event is caused by the listener itself, or is due to a
-file access by another program.
+to determine whether the event is caused by the listener itself,
+or is due to a file access by another process.
 .PP
 The bit mask in
 .I mask
-signals which events have occurred for a single filesystem object.
-More than one of the following flags can be set at once in the bit mask.
+indicates which events have occurred for a single filesystem object.
+Multiple bits may be set in this mask,
+if more than one event occurred for the monitored filesystem object.
+In particular,
+consecutive events for the same filesystem object and originating from the
+same process may be merged into a single event, with the exception that two
+permission events are never merged into one queue entry.
+.PP
+The bits that may appear in
+.I mask
+are as follows:
 .TP
 .B FAN_ACCESS
 A file or a directory (but see BUGS) was accessed (read).
@@ -215,6 +256,12 @@ A file or a directory (but see BUGS) was accessed (read).
 .B FAN_OPEN
 A file or a directory was opened.
 .TP
+.B FAN_OPEN_EXEC
+A file was opened with the intent to be executed.
+See NOTES in
+.BR fanotify_mark (2)
+for additional details.
+.TP
 .B FAN_MODIFY
 A file was modified.
 .TP
@@ -226,40 +273,51 @@ or
 was closed.
 .TP
 .B FAN_CLOSE_NOWRITE
-A file that was only opened for reading
+A file or directory that was opened read-only
 .RB ( O_RDONLY )
-or a directory was closed.
+was closed.
 .TP
 .B FAN_Q_OVERFLOW
 The event queue exceeded the limit of 16384 entries.
-This limit can be overridden in the call to
-.BR fanotify_init (2)
-by setting the flag
-.BR FAN_UNLIMITED_QUEUE .
+This limit can be overridden by specifying the
+.BR FAN_UNLIMITED_QUEUE
+flag when calling
+.BR fanotify_init (2).
 .TP
 .B FAN_ACCESS_PERM
 An application wants to read a file or directory, for example using
 .BR read (2)
 or
 .BR readdir (2).
-The reader must write a response that determines whether the permission to
+The reader must write a response (as described below)
+that determines whether the permission to
 access the filesystem object shall be granted.
 .TP
 .B FAN_OPEN_PERM
 An application wants to open a file or directory.
 The reader must write a response that determines whether the permission to
 open the filesystem object shall be granted.
+.TP
+.B FAN_OPEN_EXEC_PERM
+An application wants to open a file for execution.
+The reader must write a response that determines whether the permission to
+open the filesystem object for execution shall be granted.
+See NOTES in
+.BR fanotify_mark (2)
+for additional details.
 .PP
 To check for any close event, the following bit mask may be used:
 .TP
 .B FAN_CLOSE
-A file was closed
-(FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE).
+A file was closed.
+This is a synonym for:
+.IP
+    FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE
 .PP
-The following macros are provided to iterate over a buffer containing fanotify
-event metadata returned by
+The following macros are provided to iterate over a buffer containing
+fanotify event metadata returned by a
 .BR read (2)
-from an fanotify file descriptor.
+from an fanotify file descriptor:
 .TP
 .B FAN_EVENT_OK(meta, len)
 This macro checks the remaining length
@@ -271,30 +329,55 @@ against the length of the metadata structure and the
 field of the first metadata structure in the buffer.
 .TP
 .B FAN_EVENT_NEXT(meta, len)
-This macro sets the pointer
-.I meta
-to the next metadata structure using the length indicated in the
+This macro uses the length indicated in the
 .I event_len
-field of the metadata structure and reduces the remaining length of the
-buffer
-.IT len .
+field of the metadata structure pointed to by
+.IR meta
+to calculate the address of the next metadata structure that follows
+.IR meta .
+.I len
+is the number of bytes of metadata that currently remain in the buffer.
+The macro returns a pointer to the next metadata structure that follows
+.IR meta ,
+and reduces
+.I len
+by the number of bytes in the metadata structure that
+has been skipped over (i.e., it subtracts
+.IR meta\->event_len
+from
+.IR len ).
 .PP
+In addition, there is:
+.TP
+.B FAN_EVENT_METADATA_LEN
+This macro returns the size (in bytes) of the structure
+.IR fanotify_event_metadata .
+This is the minimum size (and currently the only size) of any event metadata.
+.\"
+.SS Monitoring an fanotify file descriptor for events
+When an fanotify event occurs, the fanotify file descriptor indicates as
+readable when passed to
+.BR epoll (7),
+.BR poll (2),
+or
+.BR select (2).
+.SS Dealing with permission events
 For permission events, the application must
 .BR write (2)
 a structure of the following form to the
-.B fanotify
-file descriptor:
-
+fanotify file descriptor:
+.PP
 .in +4n
-.nf
+.EX
 struct fanotify_response {
     __s32 fd;
     __u32 response;
 };
-.fi
+.EE
 .in
-
-.TP 15
+.PP
+The fields of this structure are as follows:
+.TP
 .I fd
 This is the file descriptor from the structure
 .IR fanotify_event_metadata .
@@ -307,32 +390,35 @@ to allow the file operation or
 .B FAN_DENY
 to deny the file operation.
 .PP
-If access has been denied, the requesting application call will receive an
-error
-.BR EPERM .
-.PP
-To end listening, it is sufficient to
-.BR close (2)
-the fanotify file descriptor.
-The outstanding permission events will be set to allowed, and all resources
-will be returned to the kernel.
+If access is denied, the requesting application call will receive an
+.BR EPERM
+error.
+.SS Closing the fanotify file descriptor
 .PP
+When all file descriptors referring to the fanotify notification group are
+closed, the fanotify group is released and its resources
+are freed for reuse by the kernel.
+Upon
+.BR close (2),
+outstanding permission events will be set to allowed.
+.SS /proc/[pid]/fdinfo
 The file
-.I /proc/<pid>/fdinfo/<fd>
+.I /proc/[pid]/fdinfo/[fd]
 contains information about fanotify marks for file descriptor
 .I fd
 of process
 .IR pid .
 See
-.I Documentation/filesystems/proc.txt
+.BR proc (5)
 for details.
 .SH ERRORS
 In addition to the usual errors for
 .BR read (2),
-the following errors can occur when reading from the fanotify file descriptor:
+the following errors can occur when reading from the
+fanotify file descriptor:
 .TP
 .B EINVAL
-The buffer is too short to hold the event.
+The buffer is too small to hold the event.
 .TP
 .B EMFILE
 The per-process limit on the number of open files has been reached.
@@ -342,16 +428,15 @@ in
 .BR getrlimit (2).
 .TP
 .B ENFILE
-The system-wide limit on the number of open files has been reached.
+The system-wide limit on the total number of open files has been reached.
 See
 .I /proc/sys/fs/file-max
 in
 .BR proc (5).
 .TP
 .B ETXTBSY
-A write enabled file descriptor shall be created for a file that is executing.
 This error is returned by
-.BR read (2),
+.BR read (2)
 if
 .B O_RDWR
 or
@@ -360,15 +445,15 @@ was specified in the
 .I event_f_flags
 argument when calling
 .BR fanotify_init (2)
-and the event occured for a monitored file that is currently being execuded.
+and an event occurred for a monitored file that is currently being executed.
 .PP
 In addition to the usual errors for
 .BR write (2),
 the following errors can occur when writing to the fanotify file descriptor:
 .TP
 .B EINVAL
-Fanotify access permissions are not enabled in the kernel configuration or the
-value of
+Fanotify access permissions are not enabled in the kernel configuration
+or the value of
 .I response
 in the response structure is not valid.
 .TP
@@ -376,13 +461,13 @@ in the response structure is not valid.
 The file descriptor
 .I fd
 in the response structure is not valid.
-This might occur because the file was already deleted by another thread or
-process.
+This may occur when a response for the permission event has already been
+written.
 .SH VERSIONS
 The fanotify API was introduced in version 2.6.36 of the Linux kernel and
 enabled in version 2.6.37.
 Fdinfo support was added in version 3.8.
-.SH "CONFORMING TO"
+.SH CONFORMING TO
 The fanotify API is Linux-specific.
 .SH NOTES
 The fanotify API is available only if the kernel was built with the
@@ -394,7 +479,8 @@ configuration option is enabled.
 .SS Limitations and caveats
 Fanotify reports only events that a user-space program triggers through the
 filesystem API.
-As a result, it does not catch remote events that occur on network filesystems.
+As a result,
+it does not catch remote events that occur on network filesystems.
 .PP
 The fanotify API does not report file accesses and modifications that
 may occur because of
@@ -408,43 +494,72 @@ read, and closed.
 Adding, removing, or changing children of a marked directory does not create
 events for the monitored directory itself.
 .PP
-Fanotify monitoring of directories is not recursive: to monitor subdirectories
-under a directory, additional marks must be created.
-(But note that the fanotify API provides no way of  detecting when a
-subdirectory has been created under a marked directory, which makes recursive
-monitoring difficult.)
+Fanotify monitoring of directories is not recursive:
+to monitor subdirectories under a directory,
+additional marks must be created.
+(But note that the fanotify API provides no way of detecting when a
+subdirectory has been created under a marked directory,
+which makes recursive monitoring difficult.)
 Monitoring mounts offers the capability to monitor a whole directory tree.
+Monitoring filesystems offers the capability to monitor changes made from
+any mount of a filesystem instance.
 .PP
 The event queue can overflow.
 In this case, events are lost.
 .SH BUGS
-As of Linux 3.15,
-the following bugs existed:
+Before Linux 3.19,
+.BR fallocate (2)
+did not generate fanotify events.
+Since Linux 3.19,
+.\" commit 820c12d5d6c0890bc93dd63893924a13041fdc35
+calls to
+.BR fallocate (2)
+generate
+.B FAN_MODIFY
+events.
+.PP
+As of Linux 3.17,
+the following bugs exist:
 .IP * 3
-.\" FIXME: Patch is in linux-next-20140424.
-.BR readdir (2)
-does not create a
-.B FAN_ACCESS
-event.
+On Linux, a filesystem object may be accessible through multiple paths,
+for example, a part of a filesystem may be remounted using the
+.IR \-\-bind
+option of
+.BR mount (8).
+A listener that marked a mount will be notified only of events that were
+triggered for a filesystem object using the same mount.
+Any other event will pass unnoticed.
 .IP *
-.\" FIXME: A patch was proposed.
-When an event is generated, no check is made to see whether the user ID of the
-receiving process has authorization to read or write the file before passing a
-file descriptor for that file in
+.\" FIXME . A patch was proposed.
+When an event is generated,
+no check is made to see whether the user ID of the
+receiving process has authorization to read or write the file
+before passing a file descriptor for that file.
 This poses a security risk, when the
 .B CAP_SYS_ADMIN
 capability is set for programs executed by unprivileged users.
+.IP *
+If a call to
+.BR read (2)
+processes multiple events from the fanotify queue and an error occurs,
+the return value will be the total length of the events successfully
+copied to the user-space buffer before the error occurred.
+The return value will not be \-1, and
+.I errno
+will not be set.
+Thus, the reading application has no way to detect the error.
 .SH EXAMPLE
 The following program demonstrates the usage of the fanotify API.
-It marks the mount passed as argument and waits for events of type
-.B FAN_PERM_OPEN
+It marks the mount point passed as a command-line argument
+and waits for events of type
+.B FAN_OPEN_PERM
 and
 .BR FAN_CLOSE_WRITE .
 When a permission event occurs, a
 .B FAN_ALLOW
 response is given.
 .PP
-The following output was recorded while editing file
+The following output was recorded while editing the file
 .IR /home/user/temp/notes .
 Before the file was opened, a
 .B FAN_OPEN_PERM
@@ -455,7 +570,7 @@ event occurred.
 Execution of the program ends when the user presses the ENTER key.
 .SS Example output
 .in +4n
-.nf
+.EX
 # ./fanotify_example /home
 Press enter key to terminate.
 Listening for events.
@@ -463,11 +578,12 @@ FAN_OPEN_PERM: File /home/user/temp/notes
 FAN_CLOSE_WRITE: File /home/user/temp/notes
 
 Listening for events stopped.
-.fi
+.EE
 .in
 .SS Program source
-.nf
-#define _GNU_SOURCE // needed for O_LARGEFILE
+\&
+.EX
+#define _GNU_SOURCE     /* Needed to get O_LARGEFILE definition */
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
@@ -479,22 +595,22 @@ Listening for events stopped.
 
 /* Read all available fanotify events from the file descriptor 'fd' */
 
-void
+static void
 handle_events(int fd)
 {
     const struct fanotify_event_metadata *metadata;
-    char buf[4096];
+    struct fanotify_event_metadata buf[200];
     ssize_t len;
     char path[PATH_MAX];
     ssize_t path_len;
     char procfd_path[PATH_MAX];
     struct fanotify_response response;
 
-    /* Loop while events can be read from fanotify file descriptor. */
+    /* Loop while events can be read from fanotify file descriptor */
 
     for(;;) {
 
-        /* Read some events. */
+        /* Read some events */
 
         len = read(fd, (void *) &buf, sizeof(buf));
         if (len == \-1 && errno != EAGAIN) {
@@ -502,52 +618,52 @@ handle_events(int fd)
             exit(EXIT_FAILURE);
         }
 
-        /* Check if end of available data reached. */
+        /* Check if end of available data reached */
 
         if (len <= 0)
             break;
 
-        /* Point to the first event in the buffer. */
+        /* Point to the first event in the buffer */
 
-        metadata = (struct fanotify_event_metadata *) buf;
+        metadata = buf;
 
-        /* Loop over all events in the buffer. */
+        /* Loop over all events in the buffer */
 
         while (FAN_EVENT_OK(metadata, len)) {
 
-            /* Check that run time and compile time structures
-               match. */
+            /* Check that run\-time and compile\-time structures match */
 
             if (metadata\->vers != FANOTIFY_METADATA_VERSION) {
                 fprintf(stderr,
-                        "Mismatch of fanotify metadata version.\\n");
+                        "Mismatch of fanotify metadata version.\en");
                 exit(EXIT_FAILURE);
             }
 
-            /* Check that the event contains a file descriptor. */
+            /* metadata\->fd contains either FAN_NOFD, indicating a
+               queue overflow, or a file descriptor (a nonnegative
+               integer). Here, we simply ignore queue overflow. */
 
             if (metadata\->fd >= 0) {
 
-                /* Handle open permission event. */
+                /* Handle open permission event */
 
                 if (metadata\->mask & FAN_OPEN_PERM) {
                     printf("FAN_OPEN_PERM: ");
 
-                    /* Allow file to be opened. */
+                    /* Allow file to be opened */
 
                     response.fd = metadata\->fd;
                     response.response = FAN_ALLOW;
-                    write(fd, &response, sizeof(
-                              struct fanotify_response));
+                    write(fd, &response,
+                          sizeof(struct fanotify_response));
                 }
 
-                /* Handle closing of writable file event. */
+                /* Handle closing of writable file event */
 
-                if (metadata\->mask & FAN_CLOSE_WRITE) {
+                if (metadata\->mask & FAN_CLOSE_WRITE)
                     printf("FAN_CLOSE_WRITE: ");
-                }
 
-                /* Determine path of the file accessed. */
+                /* Retrieve and print pathname of the accessed file */
 
                 snprintf(procfd_path, sizeof(procfd_path),
                          "/proc/self/fd/%d", metadata\->fd);
@@ -558,16 +674,15 @@ handle_events(int fd)
                     exit(EXIT_FAILURE);
                 }
 
-                path[path_len] = '\\0';
-                printf("File %s", path);
+                path[path_len] = '\e0';
+                printf("File %s\en", path);
 
-                /* Close the file descriptor of the event. */
+                /* Close the file descriptor of the event */
 
                 close(metadata\->fd);
-                printf("\\n");
             }
 
-            /* Forward pointer to next event. */
+            /* Advance to next event */
 
             metadata = FAN_EVENT_NEXT(metadata, len);
         }
@@ -582,16 +697,16 @@ main(int argc, char *argv[])
     nfds_t nfds;
     struct pollfd fds[2];
 
-    /* Check mount point is supplied. */
+    /* Check mount point is supplied */
 
     if (argc != 2) {
-        printf("Usage: %s MOUNT\\n", argv[0]);
+        fprintf(stderr, "Usage: %s MOUNT\en", argv[0]);
         exit(EXIT_FAILURE);
     }
 
-    printf("Press enter key to terminate.\\n");
+    printf("Press enter key to terminate.\en");
 
-    /* Create the file descriptor for accessing the fanotify API. */
+    /* Create the file descriptor for accessing the fanotify API */
 
     fd = fanotify_init(FAN_CLOEXEC | FAN_CLASS_CONTENT | FAN_NONBLOCK,
                        O_RDONLY | O_LARGEFILE);
@@ -600,70 +715,70 @@ main(int argc, char *argv[])
         exit(EXIT_FAILURE);
     }
 
-    /* Mark the mount for
+    /* Mark the mount for:
        \- permission events before opening files
-       \- notification events after closing a write enabled
-         file descriptor. */
+       \- notification events after closing a write\-enabled
+         file descriptor */
 
     if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT,
-                      FAN_OPEN_PERM | FAN_CLOSE_WRITE, \-1,
+                      FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD,
                       argv[1]) == \-1) {
         perror("fanotify_mark");
-        close(fd);
         exit(EXIT_FAILURE);
     }
 
-    /* Prepare for polling. */
+    /* Prepare for polling */
 
     nfds = 2;
 
-    /* Console input. */
+    /* Console input */
 
     fds[0].fd = STDIN_FILENO;
     fds[0].events = POLLIN;
 
-    /* Fanotify input. */
+    /* Fanotify input */
 
     fds[1].fd = fd;
     fds[1].events = POLLIN;
 
-    /* This is the loop to wait for incoming events. */
+    /* This is the loop to wait for incoming events */
+
+    printf("Listening for events.\en");
 
-    printf("Listening for events.\\n");
     while (1) {
         poll_num = poll(fds, nfds, \-1);
         if (poll_num == \-1) {
-            if (errno == EINTR)
-                continue;
-            perror("poll");
+            if (errno == EINTR)     /* Interrupted by a signal */
+                continue;           /* Restart poll() */
+
+            perror("poll");         /* Unexpected error */
             exit(EXIT_FAILURE);
         }
+
         if (poll_num > 0) {
             if (fds[0].revents & POLLIN) {
 
-                /* Console input is available. Empty stdin and quit. */
+                /* Console input is available: empty stdin and quit */
 
-                while (read(STDIN_FILENO, &buf, 1) > 0 && buf != '\\n')
+                while (read(STDIN_FILENO, &buf, 1) > 0 && buf != '\en')
                     continue;
                 break;
             }
+
             if (fds[1].revents & POLLIN) {
 
-                /* Fanotify events are available. */
+                /* Fanotify events are available */
 
                 handle_events(fd);
             }
         }
     }
 
-    /* Close fanotify file descriptor. */
-
-    close(fd);
-    printf("Listening for events stopped.\\n");
-    return EXIT_SUCCESS;
+    printf("Listening for events stopped.\en");
+    exit(EXIT_SUCCESS);
 }
-.fi
-.SH "SEE ALSO"
+.EE
+.SH SEE ALSO
 .ad l
 .BR fanotify_init (2),
 .BR fanotify_mark (2),