]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/inotify.7
man*/, man.ignore.grep: srcfix; warn about blank lines
[thirdparty/man-pages.git] / man7 / inotify.7
CommitLineData
415f63be 1.\" Copyright (C) 2006, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
ecd96f7c 2.\" Copyright (C) 2014 Heinrich Schuchardt <xypron.glpk@gmx.de>
4d2b74dd 3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
4d2b74dd 5.\"
4c1c5274 6.TH inotify 7 (date) "Linux man-pages (unreleased)"
4d2b74dd 7.SH NAME
9ee4a2b6 8inotify \- monitoring filesystem events
4d2b74dd
MK
9.SH DESCRIPTION
10The
c13182ef 11.I inotify
9ee4a2b6 12API provides a mechanism for monitoring filesystem events.
4d2b74dd
MK
13Inotify can be used to monitor individual files,
14or to monitor directories.
15When a directory is monitored, inotify will return events
16for the directory itself, and for files inside the directory.
a721e8b2 17.PP
c13182ef 18The following system calls are used with this API:
cdede5cd 19.IP \[bu] 3
4d2b74dd 20.BR inotify_init (2)
c13182ef 21creates an inotify instance and returns a file descriptor
a2cc46ca 22referring to the inotify instance.
43bb5faf
MK
23The more recent
24.BR inotify_init1 (2)
25is like
26.BR inotify_init (2),
356911f6 27but has a
1ae6b2c7 28.I flags
356911f6 29argument that provides access to some extra functionality.
cdede5cd 30.IP \[bu]
4d2b74dd 31.BR inotify_add_watch (2)
a2cc46ca 32manipulates the "watch list" associated with an inotify instance.
3a065ac0 33Each item ("watch") in the watch list specifies the pathname of
c13182ef 34a file or directory,
4d2b74dd
MK
35along with some set of events that the kernel should monitor for the
36file referred to by that pathname.
63f6a20a 37.BR inotify_add_watch (2)
4d2b74dd
MK
38either creates a new watch item, or modifies an existing watch.
39Each watch has a unique "watch descriptor", an integer
40returned by
63f6a20a 41.BR inotify_add_watch (2)
4d2b74dd 42when the watch is created.
cdede5cd 43.IP \[bu]
356911f6
MK
44When events occur for monitored files and directories,
45those events are made available to the application as structured data that
46can be read from the inotify file descriptor using
47.BR read (2)
48(see below).
cdede5cd 49.IP \[bu]
4d2b74dd
MK
50.BR inotify_rm_watch (2)
51removes an item from an inotify watch list.
cdede5cd 52.IP \[bu]
c13182ef 53When all file descriptors referring to an inotify
356911f6
MK
54instance have been closed (using
55.BR close (2)),
c13182ef 56the underlying object and its resources are
3b777aff 57freed for reuse by the kernel;
4d2b74dd 58all associated watches are automatically freed.
f070e954 59.PP
6b1cc2c9
MK
60With careful programming,
61an application can use inotify to efficiently monitor and cache
62the state of a set of filesystem objects.
63However, robust applications should allow for the fact that bugs
64in the monitoring logic or races of the kind described below
65may leave the cache inconsistent with the filesystem state.
e3a0ac0e 66It is probably wise to do some consistency checking,
6b1cc2c9 67and rebuild the cache when inconsistencies are detected.
ff6e2397 68.SS Reading events from an inotify file descriptor
4d2b74dd
MK
69To determine what events have occurred, an application
70.BR read (2)s
71from the inotify file descriptor.
c13182ef 72If no events have so far occurred, then,
11da88fb 73assuming a blocking file descriptor,
63f6a20a 74.BR read (2)
01538d0d
MK
75will block until at least one event occurs
76(unless interrupted by a signal,
77in which case the call fails with the error
78.BR EINTR ;
79see
80.BR signal (7)).
a721e8b2 81.PP
4d2b74dd 82Each successful
63f6a20a 83.BR read (2)
4d2b74dd 84returns a buffer containing one or more of the following structures:
e646a1ba 85.PP
a08ea57c 86.in +4n
e646a1ba 87.EX
4d2b74dd
MK
88struct inotify_event {
89 int wd; /* Watch descriptor */
24bbe02c
MK
90.\" FIXME . The type of the 'wd' field should probably be "int32_t".
91.\" I submitted a patch to fix this. See the LKML thread
92.\" "[patch] Fix type errors in inotify interfaces", 18 Nov 2008
fd00f831 93.\" glibc bug filed: https://www.sourceware.org/bugzilla/show_bug.cgi?id=7040
425ad621 94 uint32_t mask; /* Mask describing event */
c13182ef 95 uint32_t cookie; /* Unique cookie associating related
4d2b74dd 96 events (for rename(2)) */
84c517a4 97 uint32_t len; /* Size of \fIname\fP field */
d064d41a 98 char name[]; /* Optional null\-terminated name */
4d2b74dd 99};
b8302363 100.EE
a08ea57c 101.in
a721e8b2 102.PP
4d2b74dd
MK
103.I wd
104identifies the watch for which this event occurs.
c13182ef 105It is one of the watch descriptors returned by a previous call to
63f6a20a 106.BR inotify_add_watch (2).
a721e8b2 107.PP
4d2b74dd
MK
108.I mask
109contains bits that describe the event that occurred (see below).
a721e8b2 110.PP
4d2b74dd
MK
111.I cookie
112is a unique integer that connects related events.
34d4e61d 113Currently, this is used only for rename events, and
4d2b74dd 114allows the resulting pair of
bc636d8a 115.B IN_MOVED_FROM
c13182ef 116and
bc636d8a 117.B IN_MOVED_TO
4d2b74dd 118events to be connected by the application.
591b7a5f
MK
119For all other event types,
120.I cookie
121is set to 0.
a721e8b2 122.PP
c13182ef 123The
4d2b74dd 124.I name
33a0ccb2 125field is present only when an event is returned
c13182ef 126for a file inside a watched directory;
d1203f45 127it identifies the filename within the watched directory.
454ab872 128This filename is null-terminated,
d6c1998e
AC
129and may include further null bytes (\[aq]\e0\[aq])
130to align subsequent reads to a suitable address boundary.
a721e8b2 131.PP
4d2b74dd
MK
132The
133.I len
c13182ef 134field counts all of the bytes in
4d2b74dd 135.IR name ,
c13182ef 136including the null bytes;
4d2b74dd
MK
137the length of each
138.I inotify_event
139structure is thus
655684a9 140.IR "sizeof(struct inotify_event)+len" .
a721e8b2 141.PP
988db661 142The behavior when the buffer given to
c7e3ee6f 143.BR read (2)
988db661 144is too small to return information about the next event depends
b324e17d 145on the kernel version: before Linux 2.6.21,
c7e3ee6f 146.BR read (2)
b324e17d 147returns 0; since Linux 2.6.21,
c7e3ee6f
MK
148.BR read (2)
149fails with the error
150.BR EINVAL .
4ba272b3 151Specifying a buffer of size
a721e8b2 152.PP
1ae6b2c7
AC
153.in +4n
154.EX
155sizeof(struct inotify_event) + NAME_MAX + 1
156.EE
157.in
a721e8b2 158.PP
4ba272b3 159will be sufficient to read at least one event.
4d2b74dd 160.SS inotify events
c13182ef 161The
4d2b74dd
MK
162.BR inotify_add_watch (2)
163.I mask
c13182ef 164argument and the
4d2b74dd
MK
165.I mask
166field of the
167.I inotify_event
168structure returned when
169.BR read (2)ing
170an inotify file descriptor are both bit masks identifying
171inotify events.
172The following bits can be specified in
173.I mask
174when calling
63f6a20a 175.BR inotify_add_watch (2)
c13182ef 176and may be returned in the
4d2b74dd
MK
177.I mask
178field returned by
63f6a20a 179.BR read (2):
64aa9bcb 180.RS 4
c577b95c 181.TP
f279ad49 182.BR IN_ACCESS " (+)"
70f70c9d
MK
183File was accessed (e.g.,
184.BR read (2),
f23fc716 185.BR execve (2)).
3f174f7d 186.TP
f23fc716 187.BR IN_ATTRIB " (*)"
36546c38 188Metadata changed\[em]for example, permissions (e.g.,
b0e1ac43
MK
189.BR chmod (2)),
190timestamps (e.g.,
191.BR utimensat (2)),
192extended attributes
193.RB ( setxattr (2)),
194link count (since Linux 2.6.25; e.g.,
a4439a50
MK
195.\" FIXME .
196.\" Events do not occur for link count changes on a file inside a monitored
197.\" directory. This differs from other metadata changes for files inside
198.\" a monitored directory.
b0e1ac43
MK
199for the target of
200.BR link (2)
201and for
202.BR unlink (2)),
203and user/group ID (e.g.,
204.BR chown (2)).
3f174f7d 205.TP
858bbaaa 206.BR IN_CLOSE_WRITE " (+)"
f23fc716 207File opened for writing was closed.
3f174f7d 208.TP
f23fc716 209.BR IN_CLOSE_NOWRITE " (*)"
7465ffa6 210File or directory not opened for writing was closed.
3f174f7d 211.TP
e95b1911 212.BR IN_CREATE " (+)"
7a64793b
MK
213File/directory created in watched directory (e.g.,
214.BR open (2)
215.BR O_CREAT ,
216.BR mkdir (2),
217.BR link (2),
1a737afd 218.BR symlink (2),
7a64793b
MK
219.BR bind (2)
220on a UNIX domain socket).
3f174f7d 221.TP
e95b1911 222.BR IN_DELETE " (+)"
f23fc716 223File/directory deleted from watched directory.
3f174f7d
MK
224.TP
225.B IN_DELETE_SELF
226Watched file/directory was itself deleted.
4a1e4cca
MK
227(This event also occurs if an object is moved to another filesystem,
228since
229.BR mv (1)
230in effect copies the file to the other filesystem and
49b07b8f 231then deletes it from the original filesystem.)
39f43968
MK
232In addition, an
233.B IN_IGNORED
234event will subsequently be generated for the watch descriptor.
3f174f7d 235.TP
e06fc20c 236.BR IN_MODIFY " (+)"
f54a1255
MK
237File was modified (e.g.,
238.BR write (2),
239.BR truncate (2)).
3f174f7d
MK
240.TP
241.B IN_MOVE_SELF
242Watched file/directory was itself moved.
243.TP
e95b1911 244.BR IN_MOVED_FROM " (+)"
107375cf 245Generated for the directory containing the old filename
f23fc716 246when a file is renamed.
3f174f7d 247.TP
e95b1911 248.BR IN_MOVED_TO " (+)"
107375cf 249Generated for the directory containing the new filename
f23fc716 250when a file is renamed.
3f174f7d 251.TP
f23fc716 252.BR IN_OPEN " (*)"
7465ffa6 253File or directory was opened.
64aa9bcb 254.RE
4d2b74dd 255.PP
7954e552
MK
256Inotify monitoring is inode-based: when monitoring a file
257(but not when monitoring the directory containing a file),
258an event can be generated for activity on any link to the file
259(in the same or a different directory).
a721e8b2 260.PP
e95b1911 261When monitoring a directory:
cdede5cd 262.IP \[bu] 3
e95b1911
MK
263the events marked above with an asterisk (*) can occur both
264for the directory itself and for objects inside the directory; and
cdede5cd 265.IP \[bu]
e95b1911
MK
266the events marked with a plus sign (+) occur only for objects
267inside the directory (not for the directory itself).
268.PP
705bb673
MK
269.IR Note :
270when monitoring a directory,
271events are not generated for the files inside the directory
272when the events are performed via a pathname (i.e., a link)
273that lies outside the monitored directory.
a721e8b2 274.PP
e95b1911 275When events are generated for objects inside a watched directory, the
4d2b74dd
MK
276.I name
277field in the returned
278.I inotify_event
279structure identifies the name of the file within the directory.
280.PP
281The
282.B IN_ALL_EVENTS
283macro is defined as a bit mask of all of the above events.
284This macro can be used as the
285.I mask
286argument when calling
63f6a20a 287.BR inotify_add_watch (2).
a721e8b2 288.PP
dede00fe 289Two additional convenience macros are defined:
64aa9bcb 290.RS 4
dede00fe 291.TP
1ae6b2c7 292.B IN_MOVE
dede00fe
MK
293Equates to
294.BR "IN_MOVED_FROM | IN_MOVED_TO" .
295.TP
1ae6b2c7 296.B IN_CLOSE
dede00fe
MK
297Equates to
298.BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" .
64aa9bcb 299.RE
4d2b74dd 300.PP
c13182ef 301The following further bits can be specified in
4d2b74dd
MK
302.I mask
303when calling
63f6a20a 304.BR inotify_add_watch (2):
64aa9bcb 305.RS 4
c577b95c 306.TP
31daf529 307.BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
aeb9b6a6
MK
308Don't dereference
309.I pathname
310if it is a symbolic link.
dda869a4 311.TP
0ff2cc88 312.BR IN_EXCL_UNLINK " (since Linux 2.6.36)"
b3ad7609
MK
313.\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
314By default, when watching events on the children of a directory,
315events are generated for children even after they have been unlinked
316from the directory.
317This can result in large numbers of uninteresting events for
318some applications (e.g., if watching
319.IR /tmp ,
320in which many applications create temporary files whose
321names are immediately unlinked).
322Specifying
323.B IN_EXCL_UNLINK
324changes the default behavior,
325so that events are not generated for children after
326they have been unlinked from the watched directory.
327.TP
dda869a4 328.B IN_MASK_ADD
7747b84b
MK
329If a watch instance already exists for the filesystem object corresponding to
330.IR pathname ,
331add (OR) the events in
332.I mask
381b7a91
HW
333to the watch mask (instead of replacing the mask);
334the error
335.B EINVAL
336results if
337.B IN_MASK_CREATE
338is also specified.
dda869a4
MK
339.TP
340.B IN_ONESHOT
dcaff23c 341Monitor the filesystem object corresponding to
aeb9b6a6
MK
342.I pathname
343for one event, then remove from
dda869a4
MK
344watch list.
345.TP
31daf529 346.BR IN_ONLYDIR " (since Linux 2.6.15)"
4e9362fe 347Watch
aeb9b6a6 348.I pathname
390795d7
MK
349only if it is a directory;
350the error
351.B ENOTDIR
352results if
353.I pathname
354is not a directory.
18d92a14 355Using this flag provides an application with a race-free way of
9dcdfde4 356ensuring that the monitored object is a directory.
381b7a91 357.TP
859758b6 358.BR IN_MASK_CREATE " (since Linux 4.18)"
381b7a91
HW
359Watch
360.I pathname
361only if it does not already have a watch associated with it;
362the error
363.B EEXIST
364results if
365.I pathname
859758b6 366is already being watched.
5ef4a59d 367.IP
381b7a91 368Using this flag provides an application with a way of ensuring
859758b6 369that new watches do not modify existing ones.
5ef4a59d
MK
370This is useful because multiple paths may refer to the same inode,
371and multiple calls to
859758b6 372.BR inotify_add_watch (2)
381b7a91 373without this flag may clobber existing watch masks.
64aa9bcb 374.RE
4d2b74dd
MK
375.PP
376The following bits may be set in the
377.I mask
378field returned by
63f6a20a 379.BR read (2):
64aa9bcb 380.RS 4
c577b95c 381.TP
dda869a4 382.B IN_IGNORED
aeb9b6a6
MK
383Watch was removed explicitly
384.RB ( inotify_rm_watch (2))
9ee4a2b6 385or automatically (file was deleted, or filesystem was unmounted).
7b5151b7 386See also BUGS.
dda869a4
MK
387.TP
388.B IN_ISDIR
389Subject of this event is a directory.
390.TP
391.B IN_Q_OVERFLOW
aeb9b6a6
MK
392Event queue overflowed
393.RI ( wd
394is \-1 for this event).
dda869a4
MK
395.TP
396.B IN_UNMOUNT
9ee4a2b6 397Filesystem containing watched object was unmounted.
b01c936e
MK
398In addition, an
399.B IN_IGNORED
400event will subsequently be generated for the watch descriptor.
64aa9bcb 401.RE
415f63be
MK
402.SS Examples
403Suppose an application is watching the directory
404.I dir
405and the file
1ae6b2c7 406.I dir/myfile
415f63be
MK
407for all events.
408The examples below show some events that will be generated
409for these two objects.
410.RS 4
411.TP
412fd = open("dir/myfile", O_RDWR);
413Generates
414.B IN_OPEN
415events for both
416.I dir
417and
418.IR dir/myfile .
419.TP
420read(fd, buf, count);
421Generates
422.B IN_ACCESS
423events for both
424.I dir
425and
426.IR dir/myfile .
427.TP
428write(fd, buf, count);
429Generates
430.B IN_MODIFY
431events for both
432.I dir
433and
434.IR dir/myfile .
435.TP
436fchmod(fd, mode);
437Generates
438.B IN_ATTRIB
439events for both
440.I dir
441and
442.IR dir/myfile .
443.TP
444close(fd);
445Generates
446.B IN_CLOSE_WRITE
447events for both
448.I dir
449and
450.IR dir/myfile .
451.RE
452.PP
453Suppose an application is watching the directories
454.I dir1
455and
d2c3d8a8 456.IR dir2 ,
415f63be
MK
457and the file
458.IR dir1/myfile .
459The following examples show some events that may be generated.
460.RS 4
461.TP
d2c3d8a8 462link("dir1/myfile", "dir2/new");
415f63be
MK
463Generates an
464.B IN_ATTRIB
465event for
1ae6b2c7 466.I myfile
415f63be
MK
467and an
468.B IN_CREATE
469event for
470.IR dir2 .
471.TP
472rename("dir1/myfile", "dir2/myfile");
473Generates an
474.B IN_MOVED_FROM
475event for
476.IR dir1 ,
477an
478.B IN_MOVED_TO
479event for
480.IR dir2 ,
481and an
482.B IN_MOVE_SELF
483event for
484.IR myfile .
485The
486.B IN_MOVED_FROM
487and
488.B IN_MOVED_TO
489events will have the same
490.I cookie
491value.
492.RE
493.PP
494Suppose that
1ae6b2c7 495.I dir1/xx
415f63be 496and
1ae6b2c7 497.I dir2/yy
415f63be
MK
498are (the only) links to the same file, and an application is watching
499.IR dir1 ,
500.IR dir2 ,
501.IR dir1/xx ,
502and
503.IR dir2/yy .
504Executing the following calls in the order given below will generate
505the following events:
506.RS 4
507.TP
508unlink("dir2/yy");
d2c3d8a8 509Generates an
1ae6b2c7 510.B IN_ATTRIB
415f63be 511event for
1ae6b2c7 512.I xx
415f63be
MK
513(because its link count changes)
514and an
515.B IN_DELETE
516event for
517.IR dir2 .
518.TP
519unlink("dir1/xx");
520Generates
521.BR IN_ATTRIB ,
522.BR IN_DELETE_SELF ,
523and
1ae6b2c7 524.B IN_IGNORED
415f63be
MK
525events for
526.IR xx ,
527and an
1ae6b2c7 528.B IN_DELETE
d2c3d8a8 529event for
415f63be
MK
530.IR dir1 .
531.RE
532.PP
533Suppose an application is watching the directory
1ae6b2c7 534.I dir
415f63be
MK
535and (the empty) directory
536.IR dir/subdir .
537The following examples show some events that may be generated.
538.RS 4
539.TP
540mkdir("dir/new", mode);
541Generates an
542.B "IN_CREATE | IN_ISDIR"
543event for
544.IR dir .
545.TP
d2c3d8a8 546rmdir("dir/subdir");
415f63be
MK
547Generates
548.B IN_DELETE_SELF
549and
550.B IN_IGNORED
551events for
552.IR subdir ,
553and an
554.B "IN_DELETE | IN_ISDIR"
555event for
556.IR dir .
557.RE
4d2b74dd 558.SS /proc interfaces
c13182ef 559The following interfaces can be used to limit the amount of
4d2b74dd
MK
560kernel memory consumed by inotify:
561.TP
0daa9e92 562.I /proc/sys/fs/inotify/max_queued_events
4d2b74dd
MK
563The value in this file is used when an application calls
564.BR inotify_init (2)
c13182ef 565to set an upper limit on the number of events that can be
4d2b74dd
MK
566queued to the corresponding inotify instance.
567Events in excess of this limit are dropped, but an
568.B IN_Q_OVERFLOW
569event is always generated.
570.TP
0daa9e92 571.I /proc/sys/fs/inotify/max_user_instances
c13182ef 572This specifies an upper limit on the number of inotify instances
4d2b74dd
MK
573that can be created per real user ID.
574.TP
0daa9e92 575.I /proc/sys/fs/inotify/max_user_watches
31546b46
VN
576This specifies an upper limit on the number of watches
577that can be created per real user ID.
4131356c
AC
578.SH STANDARDS
579Linux.
580.SH HISTORY
b324e17d
AC
581Inotify was merged into Linux 2.6.13.
582The required library interfaces were added in glibc 2.4.
2b2581ee
MK
583.RB ( IN_DONT_FOLLOW ,
584.BR IN_MASK_ADD ,
585and
586.B IN_ONLYDIR
b324e17d 587were added in glibc 2.5.)
47297adb 588.SH NOTES
4d2b74dd
MK
589Inotify file descriptors can be monitored using
590.BR select (2),
591.BR poll (2),
c13182ef 592and
2315114c 593.BR epoll (7).
0000daa5 594When an event is available, the file descriptor indicates as readable.
a721e8b2 595.PP
0000daa5
MK
596Since Linux 2.6.25,
597signal-driven I/O notification is available for inotify file descriptors;
598see the discussion of
599.B F_SETFL
600(for setting the
601.B O_ASYNC
602flag),
603.BR F_SETOWN ,
604and
605.B F_SETSIG
606in
607.BR fcntl (2).
608The
609.I siginfo_t
610structure (described in
611.BR sigaction (2))
612that is passed to the signal handler has the following fields set:
1ae6b2c7 613.I si_fd
0000daa5 614is set to the inotify file descriptor number;
1ae6b2c7 615.I si_signo
0000daa5 616is set to the signal number;
1ae6b2c7 617.I si_code
0000daa5
MK
618is set to
619.BR POLL_IN ;
620and
621.B POLLIN
622is set in
623.IR si_band .
a721e8b2 624.PP
c13182ef
MK
625If successive output inotify events produced on the
626inotify file descriptor are identical (same
627.IR wd ,
628.IR mask ,
4d2b74dd
MK
629.IR cookie ,
630and
3f3698d8 631.IR name ),
6f0ab035
MK
632then they are coalesced into a single event if the
633older event has not yet been read (but see BUGS).
8856aab8
MK
634This reduces the amount of kernel memory required for the event queue,
635but also means that an application can't use inotify to reliably count
636file events.
a721e8b2 637.PP
c13182ef
MK
638The events returned by reading from an inotify file descriptor
639form an ordered queue.
640Thus, for example, it is guaranteed that when renaming from
641one directory to another, events will be produced in the
4d2b74dd 642correct order on the inotify file descriptor.
a721e8b2 643.PP
d742465f
MK
644The set of watch descriptors that is being monitored via
645an inotify file descriptor can be viewed via the entry for
646the inotify file descriptor in the process's
1ae6b2c7 647.IR /proc/ pid /fdinfo
d742465f
MK
648directory.
649See
650.BR proc (5)
651for further details.
4d2b74dd
MK
652The
653.B FIONREAD
63f6a20a 654.BR ioctl (2)
c13182ef 655returns the number of bytes available to read from an
4d2b74dd 656inotify file descriptor.
613836aa 657.SS Limitations and caveats
67898fdf
MK
658The inotify API provides no information about the user or process that
659triggered the inotify event.
660In particular, there is no easy
661way for a process that is monitoring events via inotify
662to distinguish events that it triggers
663itself from those that are triggered by other processes.
a721e8b2 664.PP
e226bed7
MK
665Inotify reports only events that a user-space program triggers through
666the filesystem API.
667As a result, it does not catch remote events that occur
668on network filesystems.
669(Applications must fall back to polling the filesystem
670to catch such events.)
fa1d49a6 671Furthermore, various pseudo-filesystems such as
e226bed7
MK
672.IR /proc ,
673.IR /sys ,
674and
1ae6b2c7 675.I /dev/pts
e226bed7 676are not monitorable with inotify.
a721e8b2 677.PP
e449e5f1
MK
678The inotify API does not report file accesses and modifications that
679may occur because of
7a8110f6
HS
680.BR mmap (2),
681.BR msync (2),
e449e5f1 682and
7a8110f6 683.BR munmap (2).
a721e8b2 684.PP
67898fdf
MK
685The inotify API identifies affected files by filename.
686However, by the time an application processes an inotify event,
687the filename may already have been deleted or renamed.
a721e8b2 688.PP
da977073 689The inotify API identifies events via watch descriptors.
264276c6
MK
690It is the application's responsibility to cache a mapping
691(if one is needed) between watch descriptors and pathnames.
692Be aware that directory renamings may affect multiple cached pathnames.
a721e8b2 693.PP
4d2b74dd
MK
694Inotify monitoring of directories is not recursive:
695to monitor subdirectories under a directory,
696additional watches must be created.
613836aa 697This can take a significant amount time for large directory trees.
a721e8b2 698.PP
a79d28b2
MK
699If monitoring an entire directory subtree,
700and a new subdirectory is created in that tree or an existing directory
701is renamed into that tree,
702be aware that by the time you create a watch for the new subdirectory,
703new files (and subdirectories) may already exist inside the subdirectory.
704Therefore, you may want to scan the contents of the subdirectory
705immediately after adding the watch (and, if desired,
706recursively add watches for any subdirectories that it contains).
a721e8b2 707.PP
613836aa
MK
708Note that the event queue can overflow.
709In this case, events are lost.
09fa72fa 710Robust applications should handle the possibility of
613836aa 711lost events gracefully.
94d52c15
MK
712For example, it may be necessary to rebuild part or all of
713the application cache.
714(One simple, but possibly expensive,
715approach is to close the inotify file descriptor, empty the cache,
85e179c5 716create a new inotify file descriptor,
94d52c15
MK
717and then re-create watches and cache entries
718for the objects to be monitored.)
a721e8b2 719.PP
c659c68e
MK
720If a filesystem is mounted on top of a monitored directory,
721no event is generated, and no events are generated
722for objects immediately under the new mount point.
723If the filesystem is subsequently unmounted,
724events will subsequently be generated for the directory and
725the objects it contains.
726.\"
85e179c5 727.SS Dealing with rename() events
fa51f4d9 728As noted above, the
85e179c5
MK
729.B IN_MOVED_FROM
730and
731.B IN_MOVED_TO
fa51f4d9 732event pair that is generated by
85e179c5 733.BR rename (2)
fa51f4d9
MK
734can be matched up via their shared cookie value.
735However, the task of matching has some challenges.
a721e8b2 736.PP
fa51f4d9
MK
737These two events are usually consecutive in the event stream available
738when reading from the inotify file descriptor.
85e179c5
MK
739However, this is not guaranteed.
740If multiple processes are triggering events for monitored objects,
741then (on rare occasions) an arbitrary number of
742other events may appear between the
743.B IN_MOVED_FROM
744and
745.B IN_MOVED_TO
746events.
003798c7
MK
747Furthermore, it is not guaranteed that the event pair is atomically
748inserted into the queue: there may be a brief interval where the
749.B IN_MOVED_FROM
750has appeared, but the
751.B IN_MOVED_TO
752has not.
a721e8b2 753.PP
85e179c5
MK
754Matching up the
755.B IN_MOVED_FROM
756and
757.B IN_MOVED_TO
758event pair generated by
759.BR rename (2)
760is thus inherently racy.
761(Don't forget that if an object is renamed outside of a monitored directory,
762there may not even be an
1ae6b2c7 763.B IN_MOVED_TO
85e179c5
MK
764event.)
765Heuristic approaches (e.g., assume the events are always consecutive)
766can be used to ensure a match in most cases,
767but will inevitably miss some cases,
768causing the application to perceive the
769.B IN_MOVED_FROM
770and
771.B IN_MOVED_TO
772events as being unrelated.
773If watch descriptors are destroyed and re-created as a result,
774then those watch descriptors will be inconsistent with
775the watch descriptors in any pending events.
6f1a4954 776(Re-creating the inotify file descriptor and rebuilding the cache may
85e179c5 777be useful to deal with this scenario.)
a721e8b2 778.PP
85e179c5
MK
779Applications should also allow for the possibility that the
780.B IN_MOVED_FROM
781event was the last event that could fit in the buffer
782returned by the current call to
783.BR read (2),
784and the accompanying
785.B IN_MOVED_TO
786event might be fetched only on the next
3da91159
MK
787.BR read (2),
788which should be done with a (small) timeout to allow for the fact that
789insertion of the
57c230fd 790.BR IN_MOVED_FROM + IN_MOVED_TO
3da91159
MK
791event pair is not atomic,
792and also the possibility that there may not be any
793.B IN_MOVED_TO
794event.
47297adb 795.SH BUGS
f9bfa911 796Before Linux 3.19,
360d3116 797.BR fallocate (2)
f9bfa911
MK
798did not create any inotify events.
799Since Linux 3.19,
800.\" commit 820c12d5d6c0890bc93dd63893924a13041fdc35
801calls to
802.BR fallocate (2)
803generate
804.B IN_MODIFY
805events.
a721e8b2 806.PP
bea08fec 807.\" FIXME . kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
b324e17d 808.\" implies that unmount events were buggy since Linux 2.6.11 to Linux 2.6.36
a15ead5e 809.\"
b324e17d 810Before Linux 2.6.16, the
ed7b0235 811.B IN_ONESHOT
c13182ef 812.I mask
ed7b0235 813flag does not work.
a721e8b2 814.PP
7b5151b7
MK
815As originally designed and implemented, the
816.B IN_ONESHOT
817flag did not cause an
818.B IN_IGNORED
819event to be generated when the watch was dropped after one event.
820However, as an unintended effect of other changes,
821since Linux 2.6.36, an
822.B IN_IGNORED
823event is generated in this case.
a721e8b2 824.PP
b324e17d 825Before Linux 2.6.25,
22129aa9 826.\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2
9ed6b517 827the kernel code that was intended to coalesce successive identical events
6f0ab035
MK
828(i.e., the two most recent events could potentially be coalesced
829if the older had not yet been read)
830instead checked if the most recent event could be coalesced with the
831.I oldest
832unread event.
a721e8b2 833.PP
ec976a53 834When a watch descriptor is removed by calling
e530e367
MK
835.BR inotify_rm_watch (2)
836(or because a watch file is deleted or the filesystem
837that contains it is unmounted),
ec976a53
HS
838any pending unread events for that watch descriptor remain available to read.
839As watch descriptors are subsequently allocated with
840.BR inotify_add_watch (2),
841the kernel cycles through the range of possible watch descriptors (0 to
842.BR INT_MAX )
843incrementally.
844When allocating a free watch descriptor, no check is made to see whether that
845watch descriptor number has any pending unread events in the inotify queue.
846Thus, it can happen that a watch descriptor is reallocated even
847when pending unread events exist for a previous incarnation of
848that watch descriptor number, with the result that the application
cbe0e644 849might then read those events and interpret them as belonging to
ec976a53 850the file associated with the newly recycled watch descriptor.
2bbf2299
MK
851In practice, the likelihood of hitting this bug may be extremely low,
852since it requires that an application cycle through
853.B INT_MAX
854watch descriptors,
855release a watch descriptor while leaving unread events for that
08063d8f 856watch descriptor in the queue,
2bbf2299
MK
857and then recycle that watch descriptor.
858For this reason, and because there have been no reports
859of the bug occurring in real-world applications,
860as of Linux 3.15,
bea08fec 861.\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=77111
2bbf2299 862no kernel changes have yet been made to eliminate this possible bug.
a14af333 863.SH EXAMPLES
ecd96f7c
HS
864The following program demonstrates the usage of the inotify API.
865It marks the directories passed as a command-line arguments
866and waits for events of type
867.BR IN_OPEN ,
d556548b 868.BR IN_CLOSE_NOWRITE ,
ecd96f7c
HS
869and
870.BR IN_CLOSE_WRITE .
871.PP
872The following output was recorded while editing the file
873.I /home/user/temp/foo
874and listing directory
875.IR /tmp .
876Before the file and the directory were opened,
877.B IN_OPEN
878events occurred.
879After the file was closed, an
880.B IN_CLOSE_WRITE
881event occurred.
882After the directory was closed, an
883.B IN_CLOSE_NOWRITE
884event occurred.
885Execution of the program ended when the user pressed the ENTER key.
886.SS Example output
887.in +4n
b8302363 888.EX
2483209a 889$ \fB./a.out /tmp /home/user/temp\fP
ecd96f7c
HS
890Press enter key to terminate.
891Listening for events.
892IN_OPEN: /home/user/temp/foo [file]
893IN_CLOSE_WRITE: /home/user/temp/foo [file]
894IN_OPEN: /tmp/ [directory]
895IN_CLOSE_NOWRITE: /tmp/ [directory]
fe5dba13 896\&
ecd96f7c 897Listening for events stopped.
b8302363 898.EE
ecd96f7c
HS
899.in
900.SS Program source
c7885256 901\&
e7d0bb47 902.EX
ecd96f7c
HS
903#include <errno.h>
904#include <poll.h>
905#include <stdio.h>
906#include <stdlib.h>
907#include <sys/inotify.h>
908#include <unistd.h>
113a073b 909#include <string.h>
fe5dba13 910\&
b957f81f 911/* Read all available inotify events from the file descriptor \[aq]fd\[aq].
ecd96f7c
HS
912 wd is the table of watch descriptors for the directories in argv.
913 argc is the length of wd and argv.
914 argv is the list of watched directories.
915 Entry 0 of wd and argv is unused. */
fe5dba13 916\&
ecd96f7c
HS
917static void
918handle_events(int fd, int *wd, int argc, char* argv[])
919{
2483209a
MK
920 /* Some systems cannot read integer variables if they are not
921 properly aligned. On other systems, incorrect alignment may
922 decrease performance. Hence, the buffer used for reading from
923 the inotify file descriptor should have the same alignment as
ecd96f7c 924 struct inotify_event. */
fe5dba13 925\&
ecd96f7c 926 char buf[4096]
2483209a 927 __attribute__ ((aligned(__alignof__(struct inotify_event))));
ecd96f7c 928 const struct inotify_event *event;
ecd96f7c 929 ssize_t len;
fe5dba13 930\&
ecd96f7c 931 /* Loop while events can be read from inotify file descriptor. */
fe5dba13 932\&
ecd96f7c 933 for (;;) {
fe5dba13 934\&
ecd96f7c 935 /* Read some events. */
fe5dba13 936\&
b5cd2470 937 len = read(fd, buf, sizeof(buf));
ecd96f7c
HS
938 if (len == \-1 && errno != EAGAIN) {
939 perror("read");
940 exit(EXIT_FAILURE);
941 }
fe5dba13 942\&
ecd96f7c
HS
943 /* If the nonblocking read() found no events to read, then
944 it returns \-1 with errno set to EAGAIN. In that case,
945 we exit the loop. */
fe5dba13 946\&
ecd96f7c
HS
947 if (len <= 0)
948 break;
fe5dba13 949\&
c6beb8a1 950 /* Loop over all events in the buffer. */
fe5dba13 951\&
88893a77 952 for (char *ptr = buf; ptr < buf + len;
2483209a 953 ptr += sizeof(struct inotify_event) + event\->len) {
fe5dba13 954\&
ecd96f7c 955 event = (const struct inotify_event *) ptr;
fe5dba13 956\&
c6beb8a1 957 /* Print event type. */
fe5dba13 958\&
ecd96f7c
HS
959 if (event\->mask & IN_OPEN)
960 printf("IN_OPEN: ");
961 if (event\->mask & IN_CLOSE_NOWRITE)
962 printf("IN_CLOSE_NOWRITE: ");
963 if (event\->mask & IN_CLOSE_WRITE)
964 printf("IN_CLOSE_WRITE: ");
fe5dba13 965\&
c6beb8a1 966 /* Print the name of the watched directory. */
fe5dba13 967\&
b42296e4 968 for (size_t i = 1; i < argc; ++i) {
ecd96f7c
HS
969 if (wd[i] == event\->wd) {
970 printf("%s/", argv[i]);
971 break;
972 }
973 }
fe5dba13 974\&
c6beb8a1 975 /* Print the name of the file. */
fe5dba13 976\&
ecd96f7c
HS
977 if (event\->len)
978 printf("%s", event\->name);
fe5dba13 979\&
c6beb8a1 980 /* Print type of filesystem object. */
fe5dba13 981\&
ecd96f7c 982 if (event\->mask & IN_ISDIR)
d1a71985 983 printf(" [directory]\en");
ecd96f7c 984 else
d1a71985 985 printf(" [file]\en");
ecd96f7c
HS
986 }
987 }
988}
fe5dba13 989\&
ecd96f7c
HS
990int
991main(int argc, char* argv[])
992{
993 char buf;
994 int fd, i, poll_num;
995 int *wd;
996 nfds_t nfds;
997 struct pollfd fds[2];
fe5dba13 998\&
ecd96f7c 999 if (argc < 2) {
d1a71985 1000 printf("Usage: %s PATH [PATH ...]\en", argv[0]);
ecd96f7c
HS
1001 exit(EXIT_FAILURE);
1002 }
fe5dba13 1003\&
d1a71985 1004 printf("Press ENTER key to terminate.\en");
fe5dba13 1005\&
c6beb8a1 1006 /* Create the file descriptor for accessing the inotify API. */
fe5dba13 1007\&
ecd96f7c
HS
1008 fd = inotify_init1(IN_NONBLOCK);
1009 if (fd == \-1) {
1010 perror("inotify_init1");
1011 exit(EXIT_FAILURE);
1012 }
fe5dba13 1013\&
c6beb8a1 1014 /* Allocate memory for watch descriptors. */
fe5dba13 1015\&
ecd96f7c
HS
1016 wd = calloc(argc, sizeof(int));
1017 if (wd == NULL) {
1018 perror("calloc");
1019 exit(EXIT_FAILURE);
1020 }
fe5dba13 1021\&
ecd96f7c
HS
1022 /* Mark directories for events
1023 \- file was opened
1024 \- file was closed */
fe5dba13 1025\&
ecd96f7c
HS
1026 for (i = 1; i < argc; i++) {
1027 wd[i] = inotify_add_watch(fd, argv[i],
1028 IN_OPEN | IN_CLOSE);
1029 if (wd[i] == \-1) {
b957f81f 1030 fprintf(stderr, "Cannot watch \[aq]%s\[aq]: %s\en",
5687d86f 1031 argv[i], strerror(errno));
ecd96f7c
HS
1032 exit(EXIT_FAILURE);
1033 }
1034 }
fe5dba13 1035\&
c6beb8a1 1036 /* Prepare for polling. */
fe5dba13 1037\&
ecd96f7c 1038 nfds = 2;
fe5dba13 1039\&
a8114045 1040 fds[0].fd = STDIN_FILENO; /* Console input */
ecd96f7c 1041 fds[0].events = POLLIN;
fe5dba13 1042\&
a8114045 1043 fds[1].fd = fd; /* Inotify input */
ecd96f7c 1044 fds[1].events = POLLIN;
fe5dba13 1045\&
c6beb8a1 1046 /* Wait for events and/or terminal input. */
fe5dba13 1047\&
d1a71985 1048 printf("Listening for events.\en");
ecd96f7c
HS
1049 while (1) {
1050 poll_num = poll(fds, nfds, \-1);
1051 if (poll_num == \-1) {
1052 if (errno == EINTR)
1053 continue;
1054 perror("poll");
1055 exit(EXIT_FAILURE);
1056 }
fe5dba13 1057\&
ecd96f7c 1058 if (poll_num > 0) {
fe5dba13 1059\&
ecd96f7c 1060 if (fds[0].revents & POLLIN) {
fe5dba13 1061\&
c6beb8a1 1062 /* Console input is available. Empty stdin and quit. */
fe5dba13 1063\&
b957f81f 1064 while (read(STDIN_FILENO, &buf, 1) > 0 && buf != \[aq]\en\[aq])
ecd96f7c
HS
1065 continue;
1066 break;
1067 }
fe5dba13 1068\&
ecd96f7c 1069 if (fds[1].revents & POLLIN) {
fe5dba13 1070\&
c6beb8a1 1071 /* Inotify events are available. */
fe5dba13 1072\&
ecd96f7c
HS
1073 handle_events(fd, wd, argc, argv);
1074 }
1075 }
1076 }
fe5dba13 1077\&
d1a71985 1078 printf("Listening for events stopped.\en");
fe5dba13 1079\&
c6beb8a1 1080 /* Close inotify file descriptor. */
fe5dba13 1081\&
ecd96f7c 1082 close(fd);
fe5dba13 1083\&
ecd96f7c 1084 free(wd);
ecd96f7c
HS
1085 exit(EXIT_SUCCESS);
1086}
e7d0bb47 1087.EE
47297adb 1088.SH SEE ALSO
f0afb16a
MK
1089.BR inotifywait (1),
1090.BR inotifywatch (1),
4d2b74dd
MK
1091.BR inotify_add_watch (2),
1092.BR inotify_init (2),
43bb5faf 1093.BR inotify_init1 (2),
4d2b74dd
MK
1094.BR inotify_rm_watch (2),
1095.BR read (2),
f75d27e6
HS
1096.BR stat (2),
1097.BR fanotify (7)
a721e8b2 1098.PP
1ae6b2c7 1099.I Documentation/filesystems/inotify.txt
173fe7e7 1100in the Linux kernel source tree