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-04-01 "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.
79 .SS Reading events from an inotify file descriptor
80 To determine what events have occurred, an application
82 from the inotify file descriptor.
83 If no events have so far occurred, then,
84 assuming a blocking file descriptor,
86 will block until at least one event occurs
87 (unless interrupted by a signal,
88 in which case the call fails with the error
95 returns a buffer containing one or more of the following structures:
99 struct inotify_event {
100 int wd; /* Watch descriptor */
101 .\" FIXME . The type of the 'wd' field should probably be "int32_t".
102 .\" I submitted a patch to fix this. See the LKML thread
103 .\" "[patch] Fix type errors in inotify interfaces", 18 Nov 2008
104 .\" Glibc bug filed: http://sources.redhat.com/bugzilla/show_bug.cgi?id=7040
105 uint32_t mask; /* Mask of events */
106 uint32_t cookie; /* Unique cookie associating related
107 events (for rename(2)) */
108 uint32_t len; /* Size of \fIname\fP field */
109 char name[]; /* Optional null-terminated name */
115 identifies the watch for which this event occurs.
116 It is one of the watch descriptors returned by a previous call to
117 .BR inotify_add_watch (2).
120 contains bits that describe the event that occurred (see below).
123 is a unique integer that connects related events.
124 Currently this is used only for rename events, and
125 allows the resulting pair of
129 events to be connected by the application.
130 For all other event types,
136 field is present only when an event is returned
137 for a file inside a watched directory;
138 it identifies the file pathname relative to the watched directory.
139 This pathname is null-terminated,
140 and may include further null bytes (\(aq\\0\(aq) to align subsequent reads to a
141 suitable address boundary.
145 field counts all of the bytes in
147 including the null bytes;
151 .IR "sizeof(struct inotify_event)+len" .
153 The behavior when the buffer given to
155 is too small to return information about the next event depends
156 on the kernel version: in kernels before 2.6.21,
158 returns 0; since kernel 2.6.21,
162 Specifying a buffer of size
164 sizeof(struct inotify_event) + NAME_MAX + 1
166 will be sufficient to read at least one event.
169 .BR inotify_add_watch (2)
175 structure returned when
177 an inotify file descriptor are both bit masks identifying
179 The following bits can be specified in
182 .BR inotify_add_watch (2)
183 and may be returned in the
190 File was accessed (e.g.,
195 Metadata changed\(emfor example, permissions (e.g.,
201 link count (since Linux 2.6.25; e.g.,
206 and user/group ID (e.g.,
209 .BR IN_CLOSE_WRITE " (*)"
210 File opened for writing was closed.
212 .BR IN_CLOSE_NOWRITE " (*)"
213 File not opened for writing was closed.
216 File/directory created in watched directory (e.g.,
223 on a UNIX domain socket).
226 File/directory deleted from watched directory.
229 Watched file/directory was itself deleted.
230 (This event also occurs if an object is moved to another filesystem,
233 in effect copies the file to the other filesystem and
234 then deletes it from the original filesystem.)
237 event will subsequently be generated for the watch descriptor.
240 File was modified (e.g.,
245 Watched file/directory was itself moved.
247 .BR IN_MOVED_FROM " (*)"
248 Generated for the directory containing the old filename
249 when a file is renamed.
251 .BR IN_MOVED_TO " (*)"
252 Generated for the directory containing the new filename
253 when a file is renamed.
259 When monitoring a directory,
260 the events marked with an asterisk (*) above can occur for
261 files in the directory, in which case the
263 field in the returned
265 structure identifies the name of the file within the directory.
269 macro is defined as a bit mask of all of the above events.
270 This macro can be used as the
272 argument when calling
273 .BR inotify_add_watch (2).
275 Two additional convenience macros are defined:
280 .BR "IN_MOVED_FROM | IN_MOVED_TO" .
284 .BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" .
287 The following further bits can be specified in
290 .BR inotify_add_watch (2):
293 .BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
296 if it is a symbolic link.
298 .BR IN_EXCL_UNLINK " (since Linux 2.6.36)"
299 .\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
300 By default, when watching events on the children of a directory,
301 events are generated for children even after they have been unlinked
303 This can result in large numbers of uninteresting events for
304 some applications (e.g., if watching
306 in which many applications create temporary files whose
307 names are immediately unlinked).
310 changes the default behavior,
311 so that events are not generated for children after
312 they have been unlinked from the watched directory.
315 Add (OR) events to watch mask for this pathname if
316 it already exists (instead of replacing mask).
321 for one event, then remove from
324 .BR IN_ONLYDIR " (since Linux 2.6.15)"
327 if it is a directory.
330 The following bits may be set in the
337 Watch was removed explicitly
338 .RB ( inotify_rm_watch (2))
339 or automatically (file was deleted, or filesystem was unmounted).
343 Subject of this event is a directory.
346 Event queue overflowed
348 is \-1 for this event).
351 Filesystem containing watched object was unmounted.
354 event will subsequently be generated for the watch descriptor.
357 Suppose an application is watching the directory
362 The examples below show some events that will be generated
363 for these two objects.
366 fd = open("dir/myfile", O_RDWR);
374 read(fd, buf, count);
382 write(fd, buf, count);
407 Suppose an application is watching the directories
413 The following examples show some events that may be generated.
416 link("dir/myfile", "dir2/new");
426 rename("dir1/myfile", "dir2/myfile");
443 events will have the same
452 are (the only) links to the same file, and an application is watching
458 Executing the following calls in the order given below will generate
459 the following events:
467 (because its link count changes)
487 Suppose an application is watching the directory
489 and (the empty) directory
491 The following examples show some events that may be generated.
494 mkdir("dir/new", mode);
496 .B "IN_CREATE | IN_ISDIR"
508 .B "IN_DELETE | IN_ISDIR"
513 The following interfaces can be used to limit the amount of
514 kernel memory consumed by inotify:
516 .I /proc/sys/fs/inotify/max_queued_events
517 The value in this file is used when an application calls
519 to set an upper limit on the number of events that can be
520 queued to the corresponding inotify instance.
521 Events in excess of this limit are dropped, but an
523 event is always generated.
525 .I /proc/sys/fs/inotify/max_user_instances
526 This specifies an upper limit on the number of inotify instances
527 that can be created per real user ID.
529 .I /proc/sys/fs/inotify/max_user_watches
530 This specifies an upper limit on the number of watches
531 that can be created per real user ID.
533 Inotify was merged into the 2.6.13 Linux kernel.
534 The required library interfaces were added to glibc in version 2.4.
535 .RB ( IN_DONT_FOLLOW ,
539 were added in glibc version 2.5.)
541 The inotify API is Linux-specific.
543 Inotify file descriptors can be monitored using
548 When an event is available, the file descriptor indicates as readable.
551 signal-driven I/O notification is available for inotify file descriptors;
552 see the discussion of
564 structure (described in
566 that is passed to the signal handler has the following fields set:
568 is set to the inotify file descriptor number;
570 is set to the signal number;
579 If successive output inotify events produced on the
580 inotify file descriptor are identical (same
586 then they are coalesced into a single event if the
587 older event has not yet been read (but see BUGS).
588 This reduces the amount of kernel memory required for the event queue,
589 but also means that an application can't use inotify to reliably count
592 The events returned by reading from an inotify file descriptor
593 form an ordered queue.
594 Thus, for example, it is guaranteed that when renaming from
595 one directory to another, events will be produced in the
596 correct order on the inotify file descriptor.
601 returns the number of bytes available to read from an
602 inotify file descriptor.
603 .SS Limitations and caveats
604 The inotify API provides no information about the user or process that
605 triggered the inotify event.
606 In particular, there is no easy
607 way for a process that is monitoring events via inotify
608 to distinguish events that it triggers
609 itself from those that are triggered by other processes.
611 The inotify API identifies affected files by filename.
612 However, by the time an application processes an inotify event,
613 the filename may already have been deleted or renamed.
615 The inotify API identifies events via watch descriptors.
616 It is the application's responsibility to cache a mapping
617 (if one is needed) between watch descriptors and pathnames.
618 Be aware that directory renamings may affect multiple cached pathnames.
620 Inotify monitoring of directories is not recursive:
621 to monitor subdirectories under a directory,
622 additional watches must be created.
623 This can take a significant amount time for large directory trees.
625 If monitoring an entire directory subtree,
626 and a new subdirectory is created in that tree or an existing directory
627 is renamed into that tree,
628 be aware that by the time you create a watch for the new subdirectory,
629 new files (and subdirectories) may already exist inside the subdirectory.
630 Therefore, you may want to scan the contents of the subdirectory
631 immediately after adding the watch (and, if desired,
632 recursively add watches for any subdirectories that it contains).
634 Note that the event queue can overflow.
635 In this case, events are lost.
636 Robust applications should handle the possibility of
637 lost events gracefully.
638 For example, it may be necessary to rebuild part or all of
639 the application cache.
640 (One simple, but possibly expensive,
641 approach is to close the inotify file descriptor, empty the cache,
642 create a new inotify file descriptor,
643 and then re-create watches and cache entries
644 for the objects to be monitored.)
645 .SS Dealing with rename() events
650 events that are generated by
652 are usually available as consecutive events when reading
653 from the inotify file descriptor.
654 However, this is not guaranteed.
655 If multiple processes are triggering events for monitored objects,
656 then (on rare occasions) an arbitrary number of
657 other events may appear between the
667 event pair generated by
669 is thus inherently racy.
670 (Don't forget that if an object is renamed outside of a monitored directory,
671 there may not even be an
674 Heuristic approaches (e.g., assume the events are always consecutive)
675 can be used to ensure a match in most cases,
676 but will inevitably miss some cases,
677 causing the application to perceive the
681 events as being unrelated.
682 If watch descriptors are destroyed and re-created as a result,
683 then those watch descriptors will be inconsistent with
684 the watch descriptors in any pending events.
685 (Rebuilding the cache and re-creating the inotify file descriptor may
686 be useful to deal with this scenario.)
688 Applications should also allow for the possibility that the
690 event was the last event that could fit in the buffer
691 returned by the current call to
695 event might be fetched only on the next
698 .\" FIXME kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
699 .\" implies that unmount events were buggy 2.6.11 to 2.6.36
701 In kernels before 2.6.16, the
706 As originally designed and implemented, the
708 flag did not cause an
710 event to be generated when the watch was dropped after one event.
711 However, as an unintended effect of other changes,
712 since Linux 2.6.36, an
714 event is generated in this case.
716 Before kernel 2.6.25,
717 .\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2
718 the kernel code that was intended to coalesce successive identical events
719 (i.e., the two most recent events could potentially be coalesced
720 if the older had not yet been read)
721 instead checked if the most recent event could be coalesced with the
726 .BR inotifywatch (1),
727 .BR inotify_add_watch (2),
728 .BR inotify_init (2),
729 .BR inotify_init1 (2),
730 .BR inotify_rm_watch (2),
734 .IR Documentation/filesystems/inotify.txt
735 in the Linux kernel source tree