]> git.ipfire.org Git - thirdparty/man-pages.git/blobdiff - man7/inotify.7
signal.7: Some reworking of Michal Sekletar's text
[thirdparty/man-pages.git] / man7 / inotify.7
index 55e5cab60288fd1b4436cbeb216046e2ca1a4706..032d78d25123a3148dbdf0509b8befb441eaa81b 100644 (file)
@@ -24,7 +24,7 @@
 .\" the source, must acknowledge the copyright and authors of this work.
 .\" %%%LICENSE_END
 .\"
-.TH INOTIFY 7 2014-05-23 "Linux" "Linux Programmer's Manual"
+.TH INOTIFY 7 2019-03-06 "Linux" "Linux Programmer's Manual"
 .SH NAME
 inotify \- monitoring filesystem events
 .SH DESCRIPTION
@@ -35,7 +35,7 @@ Inotify can be used to monitor individual files,
 or to monitor directories.
 When a directory is monitored, inotify will return events
 for the directory itself, and for files inside the directory.
-
+.PP
 The following system calls are used with this API:
 .IP * 3
 .BR inotify_init (2)
@@ -77,14 +77,14 @@ instance have been closed (using
 the underlying object and its resources are
 freed for reuse by the kernel;
 all associated watches are automatically freed.
-
+.PP
 With careful programming,
 an application can use inotify to efficiently monitor and cache
 the state of a set of filesystem objects.
 However, robust applications should allow for the fact that bugs
 in the monitoring logic or races of the kind described below
 may leave the cache inconsistent with the filesystem state.
-It is probably wise to to do some consistency checking,
+It is probably wise to do some consistency checking,
 and rebuild the cache when inconsistencies are detected.
 .SS Reading events from an inotify file descriptor
 To determine what events have occurred, an application
@@ -99,39 +99,39 @@ in which case the call fails with the error
 .BR EINTR ;
 see
 .BR signal (7)).
-
+.PP
 Each successful
 .BR read (2)
 returns a buffer containing one or more of the following structures:
+.PP
 .in +4n
-.nf
-
+.EX
 struct inotify_event {
     int      wd;       /* Watch descriptor */
 .\" FIXME . The type of the 'wd' field should probably be "int32_t".
 .\" I submitted a patch to fix this.  See the LKML thread
 .\" "[patch] Fix type errors in inotify interfaces", 18 Nov 2008
 .\" Glibc bug filed: http://sources.redhat.com/bugzilla/show_bug.cgi?id=7040
-    uint32_t mask;     /* Mask of events */
+    uint32_t mask;     /* Mask describing event */
     uint32_t cookie;   /* Unique cookie associating related
                           events (for rename(2)) */
     uint32_t len;      /* Size of \fIname\fP field */
     char     name[];   /* Optional null-terminated name */
 };
-.fi
+.EE
 .in
-
+.PP
 .I wd
 identifies the watch for which this event occurs.
 It is one of the watch descriptors returned by a previous call to
 .BR inotify_add_watch (2).
-
+.PP
 .I mask
 contains bits that describe the event that occurred (see below).
-
+.PP
 .I cookie
 is a unique integer that connects related events.
-Currently this is used only for rename events, and
+Currently, this is used only for rename events, and
 allows the resulting pair of
 .B IN_MOVED_FROM
 and
@@ -140,16 +140,16 @@ events to be connected by the application.
 For all other event types,
 .I cookie
 is set to 0.
-
+.PP
 The
 .I name
 field is present only when an event is returned
 for a file inside a watched directory;
-it identifies the file pathname relative to the watched directory.
-This pathname is null-terminated,
-and may include further null bytes (\(aq\\0\(aq) to align subsequent reads to a
+it identifies the filename within to the watched directory.
+This filename is null-terminated,
+and may include further null bytes (\(aq\e0\(aq) to align subsequent reads to a
 suitable address boundary.
-
+.PP
 The
 .I len
 field counts all of the bytes in
@@ -159,7 +159,7 @@ the length of each
 .I inotify_event
 structure is thus
 .IR "sizeof(struct inotify_event)+len" .
-
+.PP
 The behavior when the buffer given to
 .BR read (2)
 is too small to return information about the next event depends
@@ -170,9 +170,9 @@ returns 0; since kernel 2.6.21,
 fails with the error
 .BR EINVAL .
 Specifying a buffer of size
-
+.PP
     sizeof(struct inotify_event) + NAME_MAX + 1
-
+.PP
 will be sufficient to read at least one event.
 .SS inotify events
 The
@@ -196,7 +196,7 @@ field returned by
 .BR read (2):
 .RS 4
 .TP
-.BR IN_ACCESS " (*)"
+.BR IN_ACCESS " (+)"
 File was accessed (e.g.,
 .BR read (2),
 .BR execve (2)).
@@ -209,6 +209,10 @@ timestamps (e.g.,
 extended attributes
 .RB ( setxattr (2)),
 link count (since Linux 2.6.25; e.g.,
+.\" FIXME .
+.\" Events do not occur for link count changes on a file inside a monitored
+.\" directory. This differs from other metadata changes for files inside
+.\" a monitored directory.
 for the target of
 .BR link (2)
 and for
@@ -216,13 +220,13 @@ and for
 and user/group ID (e.g.,
 .BR chown (2)).
 .TP
-.BR IN_CLOSE_WRITE " (*)"
+.BR IN_CLOSE_WRITE " (+)"
 File opened for writing was closed.
 .TP
 .BR IN_CLOSE_NOWRITE " (*)"
-File not opened for writing was closed.
+File or directory not opened for writing was closed.
 .TP
-.BR IN_CREATE " (*)"
+.BR IN_CREATE " (+)"
 File/directory created in watched directory (e.g.,
 .BR open (2)
 .BR O_CREAT ,
@@ -232,7 +236,7 @@ File/directory created in watched directory (e.g.,
 .BR bind (2)
 on a UNIX domain socket).
 .TP
-.BR IN_DELETE " (*)"
+.BR IN_DELETE " (+)"
 File/directory deleted from watched directory.
 .TP
 .B IN_DELETE_SELF
@@ -246,7 +250,7 @@ In addition, an
 .B IN_IGNORED
 event will subsequently be generated for the watch descriptor.
 .TP
-.BR IN_MODIFY " (*)"
+.BR IN_MODIFY " (+)"
 File was modified (e.g.,
 .BR write (2),
 .BR truncate (2)).
@@ -254,21 +258,38 @@ File was modified (e.g.,
 .B IN_MOVE_SELF
 Watched file/directory was itself moved.
 .TP
-.BR IN_MOVED_FROM " (*)"
+.BR IN_MOVED_FROM " (+)"
 Generated for the directory containing the old filename
 when a file is renamed.
 .TP
-.BR IN_MOVED_TO " (*)"
+.BR IN_MOVED_TO " (+)"
 Generated for the directory containing the new filename
 when a file is renamed.
 .TP
 .BR IN_OPEN " (*)"
-File was opened.
+File or directory was opened.
 .RE
 .PP
-When monitoring a directory,
-the events marked with an asterisk (*) above can occur for
-files in the directory, in which case the
+Inotify monitoring is inode-based: when monitoring a file
+(but not when monitoring the directory containing a file),
+an event can be generated for activity on any link to the file
+(in the same or a different directory).
+.PP
+When monitoring a directory:
+.IP * 3
+the events marked above with an asterisk (*) can occur both
+for the directory itself and for objects inside the directory; and
+.IP *
+the events marked with a plus sign (+) occur only for objects
+inside the directory (not for the directory itself).
+.PP
+.IR Note :
+when monitoring a directory,
+events are not generated for the files inside the directory
+when the events are performed via a pathname (i.e., a link)
+that lies outside the monitored directory.
+.PP
+When events are generated for objects inside a watched directory, the
 .I name
 field in the returned
 .I inotify_event
@@ -281,7 +302,7 @@ This macro can be used as the
 .I mask
 argument when calling
 .BR inotify_add_watch (2).
-
+.PP
 Two additional convenience macros are defined:
 .RS 4
 .TP
@@ -322,19 +343,51 @@ so that events are not generated for children after
 they have been unlinked from the watched directory.
 .TP
 .B IN_MASK_ADD
-Add (OR) events to watch mask for this pathname if
-it already exists (instead of replacing mask).
+If a watch instance already exists for the filesystem object corresponding to
+.IR pathname ,
+add (OR) the events in
+.I mask
+to the watch mask (instead of replacing the mask);
+the error
+.B EINVAL
+results if
+.B IN_MASK_CREATE
+is also specified.
 .TP
 .B IN_ONESHOT
-Monitor
+Monitor the filesystem object corresponding to
 .I pathname
 for one event, then remove from
 watch list.
 .TP
 .BR IN_ONLYDIR " (since Linux 2.6.15)"
-Only watch
+Watch
+.I pathname
+only if it is a directory;
+the error
+.B ENOTDIR
+results if
+.I pathname
+is not a directory.
+Using this flag provides an application with a race-free way of
+ensuring that the monitored object is a directory.
+.TP
+.BR IN_MASK_CREATE " (since Linux 4.18)"
+Watch
+.I pathname
+only if it does not already have a watch associated with it;
+the error
+.B EEXIST
+results if
 .I pathname
-if it is a directory.
+is already being watched.
+.IP
+Using this flag provides an application with a way of ensuring
+that new watches do not modify existing ones.
+This is useful because multiple paths may refer to the same inode,
+and multiple calls to
+.BR inotify_add_watch (2)
+without this flag may clobber existing watch masks.
 .RE
 .PP
 The following bits may be set in the
@@ -556,7 +609,7 @@ Inotify file descriptors can be monitored using
 and
 .BR epoll (7).
 When an event is available, the file descriptor indicates as readable.
-
+.PP
 Since Linux 2.6.25,
 signal-driven I/O notification is available for inotify file descriptors;
 see the discussion of
@@ -585,7 +638,7 @@ and
 .B POLLIN
 is set in
 .IR si_band .
-
+.PP
 If successive output inotify events produced on the
 inotify file descriptor are identical (same
 .IR wd ,
@@ -598,13 +651,21 @@ older event has not yet been read (but see BUGS).
 This reduces the amount of kernel memory required for the event queue,
 but also means that an application can't use inotify to reliably count
 file events.
-
+.PP
 The events returned by reading from an inotify file descriptor
 form an ordered queue.
 Thus, for example, it is guaranteed that when renaming from
 one directory to another, events will be produced in the
 correct order on the inotify file descriptor.
-
+.PP
+The set of watch descriptors that is being monitored via
+an inotify file descriptor can be viewed via the entry for
+the inotify file descriptor in the process's
+.IR /proc/[pid]/fdinfo
+directory.
+See
+.BR proc (5)
+for further details.
 The
 .B FIONREAD
 .BR ioctl (2)
@@ -617,7 +678,7 @@ In particular, there is no easy
 way for a process that is monitoring events via inotify
 to distinguish events that it triggers
 itself from those that are triggered by other processes.
-
+.PP
 Inotify reports only events that a user-space program triggers through
 the filesystem API.
 As a result, it does not catch remote events that occur
@@ -630,27 +691,28 @@ Furthermore, various pseudo-filesystems such as
 and
 .IR /dev/pts
 are not monitorable with inotify.
-
+.PP
 The inotify API does not report file accesses and modifications that
 may occur because of
-.BR mmap (2)
+.BR mmap (2),
+.BR msync (2),
 and
-.BR msync (2).
-
+.BR munmap (2).
+.PP
 The inotify API identifies affected files by filename.
 However, by the time an application processes an inotify event,
 the filename may already have been deleted or renamed.
-
+.PP
 The inotify API identifies events via watch descriptors.
 It is the application's responsibility to cache a mapping
 (if one is needed) between watch descriptors and pathnames.
 Be aware that directory renamings may affect multiple cached pathnames.
-
+.PP
 Inotify monitoring of directories is not recursive:
 to monitor subdirectories under a directory,
 additional watches must be created.
 This can take a significant amount time for large directory trees.
-
+.PP
 If monitoring an entire directory subtree,
 and a new subdirectory is created in that tree or an existing directory
 is renamed into that tree,
@@ -659,7 +721,7 @@ new files (and subdirectories) may already exist inside the subdirectory.
 Therefore, you may want to scan the contents of the subdirectory
 immediately after adding the watch (and, if desired,
 recursively add watches for any subdirectories that it contains).
-
+.PP
 Note that the event queue can overflow.
 In this case, events are lost.
 Robust applications should handle the possibility of
@@ -671,6 +733,14 @@ approach is to close the inotify file descriptor, empty the cache,
 create a new inotify file descriptor,
 and then re-create watches and cache entries
 for the objects to be monitored.)
+.PP
+If a filesystem is mounted on top of a monitored directory,
+no event is generated, and no events are generated
+for objects immediately under the new mount point.
+If the filesystem is subsequently unmounted,
+events will subsequently be generated for the directory and
+the objects it contains.
+.\"
 .SS Dealing with rename() events
 As noted above, the
 .B IN_MOVED_FROM
@@ -680,7 +750,7 @@ event pair that is generated by
 .BR rename (2)
 can be matched up via their shared cookie value.
 However, the task of matching has some challenges.
-
+.PP
 These two events are usually consecutive in the event stream available
 when reading from the inotify file descriptor.
 However, this is not guaranteed.
@@ -691,7 +761,13 @@ other events may appear between the
 and
 .B IN_MOVED_TO
 events.
-
+Furthermore, it is not guaranteed that the event pair is atomically
+inserted into the queue: there may be a brief interval where the
+.B IN_MOVED_FROM
+has appeared, but the
+.B IN_MOVED_TO
+has not.
+.PP
 Matching up the
 .B IN_MOVED_FROM
 and
@@ -716,7 +792,7 @@ then those watch descriptors will be inconsistent with
 the watch descriptors in any pending events.
 (Re-creating the inotify file descriptor and rebuilding the cache may
 be useful to deal with this scenario.)
-
+.PP
 Applications should also allow for the possibility that the
 .B IN_MOVED_FROM
 event was the last event that could fit in the buffer
@@ -725,16 +801,34 @@ returned by the current call to
 and the accompanying
 .B IN_MOVED_TO
 event might be fetched only on the next
-.BR read (2).
+.BR read (2),
+which should be done with a (small) timeout to allow for the fact that
+insertion of the
+.BR IN_MOVED_FROM - IN_MOVED_TO
+event pair is not atomic,
+and also the possibility that there may not be any
+.B IN_MOVED_TO
+event.
 .SH BUGS
-.\" FIXME kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
+Before Linux 3.19,
+.BR fallocate (2)
+did not create any inotify events.
+Since Linux 3.19,
+.\" commit 820c12d5d6c0890bc93dd63893924a13041fdc35
+calls to
+.BR fallocate (2)
+generate
+.B IN_MODIFY
+events.
+.PP
+.\" FIXME . kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
 .\" implies that unmount events were buggy 2.6.11 to 2.6.36
 .\"
 In kernels before 2.6.16, the
 .B IN_ONESHOT
 .I mask
 flag does not work.
-
+.PP
 As originally designed and implemented, the
 .B IN_ONESHOT
 flag did not cause an
@@ -744,7 +838,7 @@ However, as an unintended effect of other changes,
 since Linux 2.6.36, an
 .B IN_IGNORED
 event is generated in this case.
-
+.PP
 Before kernel 2.6.25,
 .\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2
 the kernel code that was intended to coalesce successive identical events
@@ -753,12 +847,42 @@ if the older had not yet been read)
 instead checked if the most recent event could be coalesced with the
 .I oldest
 unread event.
+.PP
+When a watch descriptor is removed by calling
+.BR inotify_rm_watch (2)
+(or because a watch file is deleted or the filesystem
+that contains it is unmounted),
+any pending unread events for that watch descriptor remain available to read.
+As watch descriptors are subsequently allocated with
+.BR inotify_add_watch (2),
+the kernel cycles through the range of possible watch descriptors (0 to
+.BR INT_MAX )
+incrementally.
+When allocating a free watch descriptor, no check is made to see whether that
+watch descriptor number has any pending unread events in the inotify queue.
+Thus, it can happen that a watch descriptor is reallocated even
+when pending unread events exist for a previous incarnation of
+that watch descriptor number, with the result that the application
+might then read those events and interpret them as belonging to
+the file associated with the newly recycled watch descriptor.
+In practice, the likelihood of hitting this bug may be extremely low,
+since it requires that an application cycle through
+.B INT_MAX
+watch descriptors,
+release a watch descriptor while leaving unread events for that
+watch descriptor in the queue,
+and then recycle that watch descriptor.
+For this reason, and because there have been no reports
+of the bug occurring in real-world applications,
+as of Linux 3.15,
+.\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=77111
+no kernel changes have yet been made to eliminate this possible bug.
 .SH EXAMPLE
 The following program demonstrates the usage of the inotify API.
 It marks the directories passed as a command-line arguments
 and waits for events of type
 .BR IN_OPEN ,
-.BR IN_CLOSE_NOWRITE 
+.BR IN_CLOSE_NOWRITE
 and
 .BR IN_CLOSE_WRITE .
 .PP
@@ -778,8 +902,8 @@ event occurred.
 Execution of the program ended when the user pressed the ENTER key.
 .SS Example output
 .in +4n
-.nf
-$ ./inotify.7.example /tmp /home/user/temp
+.EX
+$ \fB./a.out /tmp /home/user/temp\fP
 Press enter key to terminate.
 Listening for events.
 IN_OPEN: /home/user/temp/foo [file]
@@ -788,10 +912,11 @@ IN_OPEN: /tmp/ [directory]
 IN_CLOSE_NOWRITE: /tmp/ [directory]
 
 Listening for events stopped.
-.fi
+.EE
 .in
 .SS Program source
-.nf
+\&
+.EX
 #include <errno.h>
 #include <poll.h>
 #include <stdio.h>
@@ -808,14 +933,14 @@ Listening for events stopped.
 static void
 handle_events(int fd, int *wd, int argc, char* argv[])
 {
-    /* Some systems cannot read integer variables if they are
-       not properly aligned. On other systems incorrect alignment
-       may decrease performance.
-       Hence, the buffer used for reading from the inotify file
-       descriptor should have the same alignment as
+    /* Some systems cannot read integer variables if they are not
+       properly aligned. On other systems, incorrect alignment may
+       decrease performance. Hence, the buffer used for reading from
+       the inotify file descriptor should have the same alignment as
        struct inotify_event. */
+
     char buf[4096]
-    __attribute__ ((aligned(__alignof__(struct inotify_event))));
+        __attribute__ ((aligned(__alignof__(struct inotify_event))));
     const struct inotify_event *event;
     int i;
     ssize_t len;
@@ -843,7 +968,7 @@ handle_events(int fd, int *wd, int argc, char* argv[])
         /* Loop over all events in the buffer */
 
         for (ptr = buf; ptr < buf + len;
-             ptr += sizeof(struct inotify_event) + event\->len) {
+                ptr += sizeof(struct inotify_event) + event\->len) {
 
             event = (const struct inotify_event *) ptr;
 
@@ -873,10 +998,9 @@ handle_events(int fd, int *wd, int argc, char* argv[])
             /* Print type of filesystem object */
 
             if (event\->mask & IN_ISDIR)
-                printf(" [directory]\\n");
+                printf(" [directory]\en");
             else
-                printf(" [file]\\n");
-
+                printf(" [file]\en");
         }
     }
 }
@@ -891,11 +1015,11 @@ main(int argc, char* argv[])
     struct pollfd fds[2];
 
     if (argc < 2) {
-        printf("Usage: %s PATH [PATH ...]\\n", argv[0]);
+        printf("Usage: %s PATH [PATH ...]\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 inotify API */
 
@@ -921,7 +1045,7 @@ main(int argc, char* argv[])
         wd[i] = inotify_add_watch(fd, argv[i],
                                   IN_OPEN | IN_CLOSE);
         if (wd[i] == \-1) {
-            fprintf(stderr, "Cannot watch '%s'\\n", argv[i]);
+            fprintf(stderr, "Cannot watch '%s'\en", argv[i]);
             perror("inotify_add_watch");
             exit(EXIT_FAILURE);
         }
@@ -943,7 +1067,7 @@ main(int argc, char* argv[])
 
     /* Wait for events and/or terminal input */
 
-    printf("Listening for events.\\n");
+    printf("Listening for events.\en");
     while (1) {
         poll_num = poll(fds, nfds, \-1);
         if (poll_num == \-1) {
@@ -952,32 +1076,37 @@ main(int argc, char* argv[])
             perror("poll");
             exit(EXIT_FAILURE);
         }
+
         if (poll_num > 0) {
 
             if (fds[0].revents & POLLIN) {
 
                 /* 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) {
 
                 /* Inotify events are available */
+
                 handle_events(fd, wd, argc, argv);
             }
         }
     }
 
+    printf("Listening for events stopped.\en");
+
     /* Close inotify file descriptor */
 
     close(fd);
+
     free(wd);
-    printf("Listening for events stopped.\\n");
     exit(EXIT_SUCCESS);
 }
-.fi
+.EE
 .SH SEE ALSO
 .BR inotifywait (1),
 .BR inotifywatch (1),
@@ -988,6 +1117,6 @@ main(int argc, char* argv[])
 .BR read (2),
 .BR stat (2),
 .BR fanotify (7)
-
+.PP
 .IR Documentation/filesystems/inotify.txt
 in the Linux kernel source tree