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