2 .\" Copyright (C) 2006, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH INOTIFY 7 2014-05-08 "Linux" "Linux Programmer's Manual"
28 inotify \- monitoring filesystem events
32 API provides a mechanism for monitoring filesystem events.
33 Inotify can be used to monitor individual files,
34 or to monitor directories.
35 When a directory is monitored, inotify will return events
36 for the directory itself, and for files inside the directory.
38 The following system calls are used with this API:
41 creates an inotify instance and returns a file descriptor
42 referring to the inotify instance.
49 argument that provides access to some extra functionality.
51 .BR inotify_add_watch (2)
52 manipulates the "watch list" associated with an inotify instance.
53 Each item ("watch") in the watch list specifies the pathname of
55 along with some set of events that the kernel should monitor for the
56 file referred to by that pathname.
57 .BR inotify_add_watch (2)
58 either creates a new watch item, or modifies an existing watch.
59 Each watch has a unique "watch descriptor", an integer
61 .BR inotify_add_watch (2)
62 when the watch is created.
64 When events occur for monitored files and directories,
65 those events are made available to the application as structured data that
66 can be read from the inotify file descriptor using
70 .BR inotify_rm_watch (2)
71 removes an item from an inotify watch list.
73 When all file descriptors referring to an inotify
74 instance have been closed (using
76 the underlying object and its resources are
77 freed for reuse by the kernel;
78 all associated watches are automatically freed.
80 With careful programming,
81 an application can use inotify to efficiently monitor and cache
82 the state of a set of filesystem objects.
83 However, robust applications should allow for the fact that bugs
84 in the monitoring logic or races of the kind described below
85 may leave the cache inconsistent with the filesystem state.
86 It is probably wise to to do some consistency checking,
87 and rebuild the cache when inconsistencies are detected.
88 .SS Reading events from an inotify file descriptor
89 To determine what events have occurred, an application
91 from the inotify file descriptor.
92 If no events have so far occurred, then,
93 assuming a blocking file descriptor,
95 will block until at least one event occurs
96 (unless interrupted by a signal,
97 in which case the call fails with the error
104 returns a buffer containing one or more of the following structures:
108 struct inotify_event {
109 int wd; /* Watch descriptor */
110 .\" FIXME . The type of the 'wd' field should probably be "int32_t".
111 .\" I submitted a patch to fix this. See the LKML thread
112 .\" "[patch] Fix type errors in inotify interfaces", 18 Nov 2008
113 .\" Glibc bug filed: http://sources.redhat.com/bugzilla/show_bug.cgi?id=7040
114 uint32_t mask; /* Mask of events */
115 uint32_t cookie; /* Unique cookie associating related
116 events (for rename(2)) */
117 uint32_t len; /* Size of \fIname\fP field */
118 char name[]; /* Optional null-terminated name */
124 identifies the watch for which this event occurs.
125 It is one of the watch descriptors returned by a previous call to
126 .BR inotify_add_watch (2).
129 contains bits that describe the event that occurred (see below).
132 is a unique integer that connects related events.
133 Currently this is used only for rename events, and
134 allows the resulting pair of
138 events to be connected by the application.
139 For all other event types,
145 field is present only when an event is returned
146 for a file inside a watched directory;
147 it identifies the file pathname relative to the watched directory.
148 This pathname is null-terminated,
149 and may include further null bytes (\(aq\\0\(aq) to align subsequent reads to a
150 suitable address boundary.
154 field counts all of the bytes in
156 including the null bytes;
160 .IR "sizeof(struct inotify_event)+len" .
162 The behavior when the buffer given to
164 is too small to return information about the next event depends
165 on the kernel version: in kernels before 2.6.21,
167 returns 0; since kernel 2.6.21,
171 Specifying a buffer of size
173 sizeof(struct inotify_event) + NAME_MAX + 1
175 will be sufficient to read at least one event.
178 .BR inotify_add_watch (2)
184 structure returned when
186 an inotify file descriptor are both bit masks identifying
188 The following bits can be specified in
191 .BR inotify_add_watch (2)
192 and may be returned in the
199 File was accessed (e.g.,
204 Metadata changed\(emfor example, permissions (e.g.,
210 link count (since Linux 2.6.25; e.g.,
215 and user/group ID (e.g.,
218 .BR IN_CLOSE_WRITE " (*)"
219 File opened for writing was closed.
221 .BR IN_CLOSE_NOWRITE " (*)"
222 File not opened for writing was closed.
225 File/directory created in watched directory (e.g.,
232 on a UNIX domain socket).
235 File/directory deleted from watched directory.
238 Watched file/directory was itself deleted.
239 (This event also occurs if an object is moved to another filesystem,
242 in effect copies the file to the other filesystem and
243 then deletes it from the original filesystem.)
246 event will subsequently be generated for the watch descriptor.
249 File was modified (e.g.,
254 Watched file/directory was itself moved.
256 .BR IN_MOVED_FROM " (*)"
257 Generated for the directory containing the old filename
258 when a file is renamed.
260 .BR IN_MOVED_TO " (*)"
261 Generated for the directory containing the new filename
262 when a file is renamed.
268 When monitoring a directory,
269 the events marked with an asterisk (*) above can occur for
270 files in the directory, in which case the
272 field in the returned
274 structure identifies the name of the file within the directory.
278 macro is defined as a bit mask of all of the above events.
279 This macro can be used as the
281 argument when calling
282 .BR inotify_add_watch (2).
284 Two additional convenience macros are defined:
289 .BR "IN_MOVED_FROM | IN_MOVED_TO" .
293 .BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" .
296 The following further bits can be specified in
299 .BR inotify_add_watch (2):
302 .BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
305 if it is a symbolic link.
307 .BR IN_EXCL_UNLINK " (since Linux 2.6.36)"
308 .\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
309 By default, when watching events on the children of a directory,
310 events are generated for children even after they have been unlinked
312 This can result in large numbers of uninteresting events for
313 some applications (e.g., if watching
315 in which many applications create temporary files whose
316 names are immediately unlinked).
319 changes the default behavior,
320 so that events are not generated for children after
321 they have been unlinked from the watched directory.
324 Add (OR) events to watch mask for this pathname if
325 it already exists (instead of replacing mask).
330 for one event, then remove from
333 .BR IN_ONLYDIR " (since Linux 2.6.15)"
336 if it is a directory.
339 The following bits may be set in the
346 Watch was removed explicitly
347 .RB ( inotify_rm_watch (2))
348 or automatically (file was deleted, or filesystem was unmounted).
352 Subject of this event is a directory.
355 Event queue overflowed
357 is \-1 for this event).
360 Filesystem containing watched object was unmounted.
363 event will subsequently be generated for the watch descriptor.
366 Suppose an application is watching the directory
371 The examples below show some events that will be generated
372 for these two objects.
375 fd = open("dir/myfile", O_RDWR);
383 read(fd, buf, count);
391 write(fd, buf, count);
416 Suppose an application is watching the directories
422 The following examples show some events that may be generated.
425 link("dir1/myfile", "dir2/new");
435 rename("dir1/myfile", "dir2/myfile");
452 events will have the same
461 are (the only) links to the same file, and an application is watching
467 Executing the following calls in the order given below will generate
468 the following events:
476 (because its link count changes)
496 Suppose an application is watching the directory
498 and (the empty) directory
500 The following examples show some events that may be generated.
503 mkdir("dir/new", mode);
505 .B "IN_CREATE | IN_ISDIR"
517 .B "IN_DELETE | IN_ISDIR"
522 The following interfaces can be used to limit the amount of
523 kernel memory consumed by inotify:
525 .I /proc/sys/fs/inotify/max_queued_events
526 The value in this file is used when an application calls
528 to set an upper limit on the number of events that can be
529 queued to the corresponding inotify instance.
530 Events in excess of this limit are dropped, but an
532 event is always generated.
534 .I /proc/sys/fs/inotify/max_user_instances
535 This specifies an upper limit on the number of inotify instances
536 that can be created per real user ID.
538 .I /proc/sys/fs/inotify/max_user_watches
539 This specifies an upper limit on the number of watches
540 that can be created per real user ID.
542 Inotify was merged into the 2.6.13 Linux kernel.
543 The required library interfaces were added to glibc in version 2.4.
544 .RB ( IN_DONT_FOLLOW ,
548 were added in glibc version 2.5.)
550 The inotify API is Linux-specific.
552 Inotify file descriptors can be monitored using
557 When an event is available, the file descriptor indicates as readable.
560 signal-driven I/O notification is available for inotify file descriptors;
561 see the discussion of
573 structure (described in
575 that is passed to the signal handler has the following fields set:
577 is set to the inotify file descriptor number;
579 is set to the signal number;
588 If successive output inotify events produced on the
589 inotify file descriptor are identical (same
595 then they are coalesced into a single event if the
596 older event has not yet been read (but see BUGS).
597 This reduces the amount of kernel memory required for the event queue,
598 but also means that an application can't use inotify to reliably count
601 The events returned by reading from an inotify file descriptor
602 form an ordered queue.
603 Thus, for example, it is guaranteed that when renaming from
604 one directory to another, events will be produced in the
605 correct order on the inotify file descriptor.
610 returns the number of bytes available to read from an
611 inotify file descriptor.
612 .SS Limitations and caveats
613 The inotify API provides no information about the user or process that
614 triggered the inotify event.
615 In particular, there is no easy
616 way for a process that is monitoring events via inotify
617 to distinguish events that it triggers
618 itself from those that are triggered by other processes.
620 Inotify reports only events that a user-space program triggers through
622 As a result, it does not catch remote events that occur
623 on network filesystems.
624 (Applications must fall back to polling the filesystem
625 to catch such events.)
626 Furthermore, various pseudo-filesystems such as
631 are not monitorable with inotify.
633 The inotify API does not report file accesses and modifications that
639 The inotify API identifies affected files by filename.
640 However, by the time an application processes an inotify event,
641 the filename may already have been deleted or renamed.
643 The inotify API identifies events via watch descriptors.
644 It is the application's responsibility to cache a mapping
645 (if one is needed) between watch descriptors and pathnames.
646 Be aware that directory renamings may affect multiple cached pathnames.
648 Inotify monitoring of directories is not recursive:
649 to monitor subdirectories under a directory,
650 additional watches must be created.
651 This can take a significant amount time for large directory trees.
653 If monitoring an entire directory subtree,
654 and a new subdirectory is created in that tree or an existing directory
655 is renamed into that tree,
656 be aware that by the time you create a watch for the new subdirectory,
657 new files (and subdirectories) may already exist inside the subdirectory.
658 Therefore, you may want to scan the contents of the subdirectory
659 immediately after adding the watch (and, if desired,
660 recursively add watches for any subdirectories that it contains).
662 Note that the event queue can overflow.
663 In this case, events are lost.
664 Robust applications should handle the possibility of
665 lost events gracefully.
666 For example, it may be necessary to rebuild part or all of
667 the application cache.
668 (One simple, but possibly expensive,
669 approach is to close the inotify file descriptor, empty the cache,
670 create a new inotify file descriptor,
671 and then re-create watches and cache entries
672 for the objects to be monitored.)
673 .SS Dealing with rename() events
678 event pair that is generated by
680 can be matched up via their shared cookie value.
681 However, the task of matching has some challenges.
683 These two events are usually consecutive in the event stream available
684 when reading from the inotify file descriptor.
685 However, this is not guaranteed.
686 If multiple processes are triggering events for monitored objects,
687 then (on rare occasions) an arbitrary number of
688 other events may appear between the
698 event pair generated by
700 is thus inherently racy.
701 (Don't forget that if an object is renamed outside of a monitored directory,
702 there may not even be an
705 Heuristic approaches (e.g., assume the events are always consecutive)
706 can be used to ensure a match in most cases,
707 but will inevitably miss some cases,
708 causing the application to perceive the
712 events as being unrelated.
713 If watch descriptors are destroyed and re-created as a result,
714 then those watch descriptors will be inconsistent with
715 the watch descriptors in any pending events.
716 (Re-creating the inotify file descriptor and rebuilding the cache may
717 be useful to deal with this scenario.)
719 Applications should also allow for the possibility that the
721 event was the last event that could fit in the buffer
722 returned by the current call to
726 event might be fetched only on the next
729 .\" FIXME kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
730 .\" implies that unmount events were buggy 2.6.11 to 2.6.36
732 In kernels before 2.6.16, the
737 As originally designed and implemented, the
739 flag did not cause an
741 event to be generated when the watch was dropped after one event.
742 However, as an unintended effect of other changes,
743 since Linux 2.6.36, an
745 event is generated in this case.
747 Before kernel 2.6.25,
748 .\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2
749 the kernel code that was intended to coalesce successive identical events
750 (i.e., the two most recent events could potentially be coalesced
751 if the older had not yet been read)
752 instead checked if the most recent event could be coalesced with the
757 .BR inotifywatch (1),
758 .BR inotify_add_watch (2),
759 .BR inotify_init (2),
760 .BR inotify_init1 (2),
761 .BR inotify_rm_watch (2),
766 .IR Documentation/filesystems/inotify.txt
767 in the Linux kernel source tree
769 This page is part of release 3.67 of the Linux
772 A description of the project,
773 information about reporting bugs,
774 and the latest version of this page,
776 \%http://www.kernel.org/doc/man\-pages/.