2 .\" Copyright (C) 2006 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-03-28 "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 .BR inotify_init1 (2)),
42 .BR inotify_add_watch (2),
43 .BR inotify_rm_watch (2),
49 creates an inotify instance and returns a file descriptor
50 referring to the inotify instance.
55 but provides some extra functionality.
57 .BR inotify_add_watch (2)
58 manipulates the "watch list" associated with an inotify instance.
59 Each item ("watch") in the watch list specifies the pathname of
61 along with some set of events that the kernel should monitor for the
62 file referred to by that pathname.
63 .BR inotify_add_watch (2)
64 either creates a new watch item, or modifies an existing watch.
65 Each watch has a unique "watch descriptor", an integer
67 .BR inotify_add_watch (2)
68 when the watch is created.
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,
75 the underlying object and its resources are
76 freed for reuse by the kernel;
77 all associated watches are automatically freed.
79 To determine what events have occurred, an application
81 from the inotify file descriptor.
82 If no events have so far occurred, then,
83 assuming a blocking file descriptor,
85 will block until at least one event occurs
86 (unless interrupted by a signal,
87 in which case the call fails with the error
94 returns a buffer containing one or more of the following structures:
98 struct inotify_event {
99 int wd; /* Watch descriptor */
100 .\" FIXME . The type of the 'wd' field should probably be "int32_t".
101 .\" I submitted a patch to fix this. See the LKML thread
102 .\" "[patch] Fix type errors in inotify interfaces", 18 Nov 2008
103 .\" Glibc bug filed: http://sources.redhat.com/bugzilla/show_bug.cgi?id=7040
104 uint32_t mask; /* Mask of events */
105 uint32_t cookie; /* Unique cookie associating related
106 events (for rename(2)) */
107 uint32_t len; /* Size of \fIname\fP field */
108 char name[]; /* Optional null-terminated name */
114 identifies the watch for which this event occurs.
115 It is one of the watch descriptors returned by a previous call to
116 .BR inotify_add_watch (2).
119 contains bits that describe the event that occurred (see below).
122 is a unique integer that connects related events.
123 Currently this is used only for rename events, and
124 allows the resulting pair of
128 events to be connected by the application.
129 For all other event types,
135 field is present only when an event is returned
136 for a file inside a watched directory;
137 it identifies the file pathname relative to the watched directory.
138 This pathname is null-terminated,
139 and may include further null bytes (\(aq\\0\(aq) to align subsequent reads to a
140 suitable address boundary.
144 field counts all of the bytes in
146 including the null bytes;
150 .IR "sizeof(struct inotify_event)+len" .
152 The behavior when the buffer given to
154 is too small to return information about the next event depends
155 on the kernel version: in kernels before 2.6.21,
157 returns 0; since kernel 2.6.21,
161 Specifying a buffer of size
163 sizeof(struct inotify_event) + NAME_MAX + 1
165 will be sufficient to read at least one event.
168 .BR inotify_add_watch (2)
174 structure returned when
176 an inotify file descriptor are both bit masks identifying
178 The following bits can be specified in
181 .BR inotify_add_watch (2)
182 and may be returned in the
189 File was accessed (e.g.,
194 Metadata changed\(emfor example, permissions, timestamps, extended attributes,
195 link count (since Linux 2.6.25), user ID, or group ID.
197 .BR IN_CLOSE_WRITE " (*)"
198 File opened for writing was closed.
200 .BR IN_CLOSE_NOWRITE " (*)"
201 File not opened for writing was closed.
204 File/directory created in watched directory.
207 File/directory deleted from watched directory.
210 Watched file/directory was itself deleted.
211 (This event also occurs if an object is moved to another filesystem,
214 in effect copies the file to the other filesystem and
215 then deletes it from the original filesystem.)
218 File was modified (e.g.,
223 Watched file/directory was itself moved.
225 .BR IN_MOVED_FROM " (*)"
226 Generated for the directory containing the old filename
227 when a file is renamed.
229 .BR IN_MOVED_TO " (*)"
230 Generated for the directory containing the new filename
231 when a file is renamed.
237 When monitoring a directory,
238 the events marked with an asterisk (*) above can occur for
239 files in the directory, in which case the
241 field in the returned
243 structure identifies the name of the file within the directory.
247 macro is defined as a bit mask of all of the above events.
248 This macro can be used as the
250 argument when calling
251 .BR inotify_add_watch (2).
253 Two additional convenience macros are defined:
258 .BR "IN_MOVED_FROM | IN_MOVED_TO" .
262 .BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" .
265 The following further bits can be specified in
268 .BR inotify_add_watch (2):
271 .BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
274 if it is a symbolic link.
276 .BR IN_EXCL_UNLINK " (since Linux 2.6.36)"
277 .\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
278 By default, when watching events on the children of a directory,
279 events are generated for children even after they have been unlinked
281 This can result in large numbers of uninteresting events for
282 some applications (e.g., if watching
284 in which many applications create temporary files whose
285 names are immediately unlinked).
288 changes the default behavior,
289 so that events are not generated for children after
290 they have been unlinked from the watched directory.
293 Add (OR) events to watch mask for this pathname if
294 it already exists (instead of replacing mask).
299 for one event, then remove from
302 .BR IN_ONLYDIR " (since Linux 2.6.15)"
305 if it is a directory.
308 The following bits may be set in the
315 Watch was removed explicitly
316 .RB ( inotify_rm_watch (2))
317 or automatically (file was deleted, or filesystem was unmounted).
321 Subject of this event is a directory.
324 Event queue overflowed
326 is \-1 for this event).
329 Filesystem containing watched object was unmounted.
332 The following interfaces can be used to limit the amount of
333 kernel memory consumed by inotify:
335 .I /proc/sys/fs/inotify/max_queued_events
336 The value in this file is used when an application calls
338 to set an upper limit on the number of events that can be
339 queued to the corresponding inotify instance.
340 Events in excess of this limit are dropped, but an
342 event is always generated.
344 .I /proc/sys/fs/inotify/max_user_instances
345 This specifies an upper limit on the number of inotify instances
346 that can be created per real user ID.
348 .I /proc/sys/fs/inotify/max_user_watches
349 This specifies an upper limit on the number of watches
350 that can be created per real user ID.
352 Inotify was merged into the 2.6.13 Linux kernel.
353 The required library interfaces were added to glibc in version 2.4.
354 .RB ( IN_DONT_FOLLOW ,
358 were added in glibc version 2.5.)
360 The inotify API is Linux-specific.
362 Inotify file descriptors can be monitored using
367 When an event is available, the file descriptor indicates as readable.
370 signal-driven I/O notification is available for inotify file descriptors;
371 see the discussion of
383 structure (described in
385 that is passed to the signal handler has the following fields set:
387 is set to the inotify file descriptor number;
389 is set to the signal number;
398 If successive output inotify events produced on the
399 inotify file descriptor are identical (same
405 then they are coalesced into a single event if the
406 older event has not yet been read (but see BUGS).
407 This reduces the amount of kernel memory required for the event queue,
408 but also means that an application can't use inotify to reliably count
411 The events returned by reading from an inotify file descriptor
412 form an ordered queue.
413 Thus, for example, it is guaranteed that when renaming from
414 one directory to another, events will be produced in the
415 correct order on the inotify file descriptor.
420 returns the number of bytes available to read from an
421 inotify file descriptor.
422 .SS Limitations and caveats
423 Inotify monitoring of directories is not recursive:
424 to monitor subdirectories under a directory,
425 additional watches must be created.
426 This can take a significant amount time for large directory trees.
428 The inotify API provides no information about the user or process that
429 triggered the inotify event.
430 In particular, there is no easy
431 way for a process that is monitoring events via inotify
432 to distinguish events that it triggers
433 itself from those that are triggered by other processes.
435 Note that the event queue can overflow.
436 In this case, events are lost.
437 Robust applications should handle the possibility of
438 lost events gracefully.
440 The inotify API identifies affected files by filename.
441 However, by the time an application processes an inotify event,
442 the filename may already have been deleted or renamed.
444 If monitoring an entire directory subtree,
445 and a new subdirectory is created in that tree or an existing directory
446 is renamed into that tree,
447 be aware that by the time you create a watch for the new subdirectory,
448 new files (and subdirectories) may already exist inside the subdirectory.
449 Therefore, you may want to scan the contents of the subdirectory
450 immediately after adding the watch (and, if desired,
451 recursively add watches for any subdirectories that it contains).
453 The inotify applications identifies events via watch descriptors.
454 It is the application's responsibility to cache a mapping
455 (if one is needed) between watch descriptors and pathnames.
456 Be aware that directory renamings may affect multiple cached pathnames.
458 .\" FIXME kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
459 .\" implies that unmount events were buggy 2.6.11 to 2.6.36
461 In kernels before 2.6.16, the
466 As originally designed and implemented, the
468 flag did not cause an
470 event to be generated when the watch was dropped after one event.
471 However, as an unintended effect of other changes,
472 since Linux 2.6.36, an
474 event is generated in this case.
476 Before kernel 2.6.25,
477 .\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2
478 the kernel code that was intended to coalesce successive identical events
479 (i.e., the two most recent events could potentially be coalesced
480 if the older had not yet been read)
481 instead checked if the most recent event could be coalesced with the
486 .BR inotifywatch (1),
487 .BR inotify_add_watch (2),
488 .BR inotify_init (2),
489 .BR inotify_init1 (2),
490 .BR inotify_rm_watch (2),
494 .IR Documentation/filesystems/inotify.txt
495 in the Linux kernel source tree