]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/inotify.7
tcp.7: Document /proc/sys/net/ipv4/tcp_autocorking
[thirdparty/man-pages.git] / man7 / inotify.7
CommitLineData
4d2b74dd 1'\" t
c11b1abf 2.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4d2b74dd 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
4d2b74dd
MK
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.
8.\"
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.
c13182ef 13.\"
4d2b74dd
MK
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
10d76543
MK
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
20.\" professionally.
c13182ef 21.\"
4d2b74dd
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
4d2b74dd 25.\"
7b5151b7 26.TH INOTIFY 7 2014-03-28 "Linux" "Linux Programmer's Manual"
4d2b74dd 27.SH NAME
9ee4a2b6 28inotify \- monitoring filesystem events
4d2b74dd
MK
29.SH DESCRIPTION
30The
c13182ef 31.I inotify
9ee4a2b6 32API provides a mechanism for monitoring filesystem events.
4d2b74dd
MK
33Inotify can be used to monitor individual files,
34or to monitor directories.
35When a directory is monitored, inotify will return events
36for the directory itself, and for files inside the directory.
37
c13182ef 38The following system calls are used with this API:
43bb5faf 39.BR inotify_init (2)
c5571b61 40(or
43bb5faf 41.BR inotify_init1 (2)),
63f6a20a
MK
42.BR inotify_add_watch (2),
43.BR inotify_rm_watch (2),
44.BR read (2),
c13182ef 45and
63f6a20a 46.BR close (2).
4d2b74dd
MK
47
48.BR inotify_init (2)
c13182ef 49creates an inotify instance and returns a file descriptor
a2cc46ca 50referring to the inotify instance.
43bb5faf
MK
51The more recent
52.BR inotify_init1 (2)
53is like
54.BR inotify_init (2),
55but provides some extra functionality.
4d2b74dd
MK
56
57.BR inotify_add_watch (2)
a2cc46ca 58manipulates the "watch list" associated with an inotify instance.
3a065ac0 59Each item ("watch") in the watch list specifies the pathname of
c13182ef 60a file or directory,
4d2b74dd
MK
61along with some set of events that the kernel should monitor for the
62file referred to by that pathname.
63f6a20a 63.BR inotify_add_watch (2)
4d2b74dd
MK
64either creates a new watch item, or modifies an existing watch.
65Each watch has a unique "watch descriptor", an integer
66returned by
63f6a20a 67.BR inotify_add_watch (2)
4d2b74dd
MK
68when the watch is created.
69
70.BR inotify_rm_watch (2)
71removes an item from an inotify watch list.
72
c13182ef 73When all file descriptors referring to an inotify
a2cc46ca 74instance have been closed,
c13182ef 75the underlying object and its resources are
3b777aff 76freed for reuse by the kernel;
4d2b74dd
MK
77all associated watches are automatically freed.
78
79To determine what events have occurred, an application
80.BR read (2)s
81from the inotify file descriptor.
c13182ef 82If no events have so far occurred, then,
11da88fb 83assuming a blocking file descriptor,
63f6a20a 84.BR read (2)
01538d0d
MK
85will block until at least one event occurs
86(unless interrupted by a signal,
87in which case the call fails with the error
88.BR EINTR ;
89see
90.BR signal (7)).
4d2b74dd
MK
91
92Each successful
63f6a20a 93.BR read (2)
4d2b74dd 94returns a buffer containing one or more of the following structures:
a08ea57c 95.in +4n
4d2b74dd
MK
96.nf
97
98struct inotify_event {
99 int wd; /* Watch descriptor */
24bbe02c
MK
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
6e6231c1 103.\" Glibc bug filed: http://sources.redhat.com/bugzilla/show_bug.cgi?id=7040
4d2b74dd 104 uint32_t mask; /* Mask of events */
c13182ef 105 uint32_t cookie; /* Unique cookie associating related
4d2b74dd 106 events (for rename(2)) */
84c517a4 107 uint32_t len; /* Size of \fIname\fP field */
4d2b74dd
MK
108 char name[]; /* Optional null-terminated name */
109};
110.fi
a08ea57c 111.in
4d2b74dd
MK
112
113.I wd
114identifies the watch for which this event occurs.
c13182ef 115It is one of the watch descriptors returned by a previous call to
63f6a20a 116.BR inotify_add_watch (2).
4d2b74dd
MK
117
118.I mask
119contains bits that describe the event that occurred (see below).
120
121.I cookie
122is a unique integer that connects related events.
33a0ccb2 123Currently this is used only for rename events, and
4d2b74dd 124allows the resulting pair of
bc636d8a 125.B IN_MOVED_FROM
c13182ef 126and
bc636d8a 127.B IN_MOVED_TO
4d2b74dd 128events to be connected by the application.
591b7a5f
MK
129For all other event types,
130.I cookie
131is set to 0.
4d2b74dd 132
c13182ef 133The
4d2b74dd 134.I name
33a0ccb2 135field is present only when an event is returned
c13182ef 136for a file inside a watched directory;
4d2b74dd 137it identifies the file pathname relative to the watched directory.
c13182ef 138This pathname is null-terminated,
1aff5804 139and may include further null bytes (\(aq\\0\(aq) to align subsequent reads to a
4d2b74dd
MK
140suitable address boundary.
141
142The
143.I len
c13182ef 144field counts all of the bytes in
4d2b74dd 145.IR name ,
c13182ef 146including the null bytes;
4d2b74dd
MK
147the length of each
148.I inotify_event
149structure is thus
655684a9 150.IR "sizeof(struct inotify_event)+len" .
c7e3ee6f 151
988db661 152The behavior when the buffer given to
c7e3ee6f 153.BR read (2)
988db661 154is too small to return information about the next event depends
c7e3ee6f
MK
155on the kernel version: in kernels before 2.6.21,
156.BR read (2)
157returns 0; since kernel 2.6.21,
158.BR read (2)
159fails with the error
160.BR EINVAL .
4ba272b3
MK
161Specifying a buffer of size
162
163 sizeof(struct inotify_event) + NAME_MAX + 1
164
165will be sufficient to read at least one event.
4d2b74dd 166.SS inotify events
c13182ef 167The
4d2b74dd
MK
168.BR inotify_add_watch (2)
169.I mask
c13182ef 170argument and the
4d2b74dd
MK
171.I mask
172field of the
173.I inotify_event
174structure returned when
175.BR read (2)ing
176an inotify file descriptor are both bit masks identifying
177inotify events.
178The following bits can be specified in
179.I mask
180when calling
63f6a20a 181.BR inotify_add_watch (2)
c13182ef 182and may be returned in the
4d2b74dd
MK
183.I mask
184field returned by
63f6a20a 185.BR read (2):
64aa9bcb 186.RS 4
c577b95c 187.TP
3f174f7d 188.B IN_ACCESS
70f70c9d
MK
189File was accessed (e.g.,
190.BR read (2),
f20fc092
MK
191.BR execve (2))
192(*).
3f174f7d
MK
193.TP
194.B IN_ATTRIB
10850212 195Metadata changed\(emfor example, permissions, timestamps, extended attributes,
f08cdf85 196link count (since Linux 2.6.25), UID, or GID (*).
3f174f7d
MK
197.TP
198.B IN_CLOSE_WRITE
199File opened for writing was closed (*).
200.TP
201.B IN_CLOSE_NOWRITE
202File not opened for writing was closed (*).
203.TP
204.B IN_CREATE
205File/directory created in watched directory (*).
206.TP
207.B IN_DELETE
208File/directory deleted from watched directory (*).
209.TP
210.B IN_DELETE_SELF
211Watched file/directory was itself deleted.
4a1e4cca
MK
212(This event also occurs if an object is moved to another filesystem,
213since
214.BR mv (1)
215in effect copies the file to the other filesystem and
216then deletes it from original filesystem.)
3f174f7d
MK
217.TP
218.B IN_MODIFY
219File was modified (*).
220.TP
221.B IN_MOVE_SELF
222Watched file/directory was itself moved.
223.TP
224.B IN_MOVED_FROM
107375cf
MK
225Generated for the directory containing the old filename
226when a file is renamed (*).
3f174f7d
MK
227.TP
228.B IN_MOVED_TO
107375cf
MK
229Generated for the directory containing the new filename
230when a file is renamed (*).
3f174f7d
MK
231.TP
232.B IN_OPEN
233File was opened (*).
64aa9bcb 234.RE
4d2b74dd 235.PP
c13182ef
MK
236When monitoring a directory,
237the events marked with an asterisk (*) above can occur for
4d2b74dd
MK
238files in the directory, in which case the
239.I name
240field in the returned
241.I inotify_event
242structure identifies the name of the file within the directory.
243.PP
244The
245.B IN_ALL_EVENTS
246macro is defined as a bit mask of all of the above events.
247This macro can be used as the
248.I mask
249argument when calling
63f6a20a 250.BR inotify_add_watch (2).
4d2b74dd 251
dede00fe 252Two additional convenience macros are defined:
64aa9bcb 253.RS 4
dede00fe
MK
254.TP
255.BR IN_MOVE
256Equates to
257.BR "IN_MOVED_FROM | IN_MOVED_TO" .
258.TP
259.BR IN_CLOSE
260Equates to
261.BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" .
64aa9bcb 262.RE
4d2b74dd 263.PP
c13182ef 264The following further bits can be specified in
4d2b74dd
MK
265.I mask
266when calling
63f6a20a 267.BR inotify_add_watch (2):
64aa9bcb 268.RS 4
c577b95c 269.TP
31daf529 270.BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
aeb9b6a6
MK
271Don't dereference
272.I pathname
273if it is a symbolic link.
dda869a4 274.TP
0ff2cc88 275.BR IN_EXCL_UNLINK " (since Linux 2.6.36)"
b3ad7609
MK
276.\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
277By default, when watching events on the children of a directory,
278events are generated for children even after they have been unlinked
279from the directory.
280This can result in large numbers of uninteresting events for
281some applications (e.g., if watching
282.IR /tmp ,
283in which many applications create temporary files whose
284names are immediately unlinked).
285Specifying
286.B IN_EXCL_UNLINK
287changes the default behavior,
288so that events are not generated for children after
289they have been unlinked from the watched directory.
290.TP
dda869a4
MK
291.B IN_MASK_ADD
292Add (OR) events to watch mask for this pathname if
293it already exists (instead of replacing mask).
294.TP
295.B IN_ONESHOT
aeb9b6a6
MK
296Monitor
297.I pathname
298for one event, then remove from
dda869a4
MK
299watch list.
300.TP
31daf529 301.BR IN_ONLYDIR " (since Linux 2.6.15)"
aeb9b6a6
MK
302Only watch
303.I pathname
304if it is a directory.
64aa9bcb 305.RE
4d2b74dd
MK
306.PP
307The following bits may be set in the
308.I mask
309field returned by
63f6a20a 310.BR read (2):
64aa9bcb 311.RS 4
c577b95c 312.TP
dda869a4 313.B IN_IGNORED
aeb9b6a6
MK
314Watch was removed explicitly
315.RB ( inotify_rm_watch (2))
9ee4a2b6 316or automatically (file was deleted, or filesystem was unmounted).
7b5151b7 317See also BUGS.
dda869a4
MK
318.TP
319.B IN_ISDIR
320Subject of this event is a directory.
321.TP
322.B IN_Q_OVERFLOW
aeb9b6a6
MK
323Event queue overflowed
324.RI ( wd
325is \-1 for this event).
dda869a4
MK
326.TP
327.B IN_UNMOUNT
9ee4a2b6 328Filesystem containing watched object was unmounted.
64aa9bcb 329.RE
4d2b74dd 330.SS /proc interfaces
c13182ef 331The following interfaces can be used to limit the amount of
4d2b74dd
MK
332kernel memory consumed by inotify:
333.TP
0daa9e92 334.I /proc/sys/fs/inotify/max_queued_events
4d2b74dd
MK
335The value in this file is used when an application calls
336.BR inotify_init (2)
c13182ef 337to set an upper limit on the number of events that can be
4d2b74dd
MK
338queued to the corresponding inotify instance.
339Events in excess of this limit are dropped, but an
340.B IN_Q_OVERFLOW
341event is always generated.
342.TP
0daa9e92 343.I /proc/sys/fs/inotify/max_user_instances
c13182ef 344This specifies an upper limit on the number of inotify instances
4d2b74dd
MK
345that can be created per real user ID.
346.TP
0daa9e92 347.I /proc/sys/fs/inotify/max_user_watches
31546b46
VN
348This specifies an upper limit on the number of watches
349that can be created per real user ID.
47297adb 350.SH VERSIONS
2b2581ee
MK
351Inotify was merged into the 2.6.13 Linux kernel.
352The required library interfaces were added to glibc in version 2.4.
353.RB ( IN_DONT_FOLLOW ,
354.BR IN_MASK_ADD ,
355and
356.B IN_ONLYDIR
64aa9bcb 357were added in glibc version 2.5.)
47297adb 358.SH CONFORMING TO
8382f16d 359The inotify API is Linux-specific.
47297adb 360.SH NOTES
4d2b74dd
MK
361Inotify file descriptors can be monitored using
362.BR select (2),
363.BR poll (2),
c13182ef 364and
2315114c 365.BR epoll (7).
0000daa5
MK
366When an event is available, the file descriptor indicates as readable.
367
368Since Linux 2.6.25,
369signal-driven I/O notification is available for inotify file descriptors;
370see the discussion of
371.B F_SETFL
372(for setting the
373.B O_ASYNC
374flag),
375.BR F_SETOWN ,
376and
377.B F_SETSIG
378in
379.BR fcntl (2).
380The
381.I siginfo_t
382structure (described in
383.BR sigaction (2))
384that is passed to the signal handler has the following fields set:
385.IR si_fd
386is set to the inotify file descriptor number;
387.IR si_signo
388is set to the signal number;
389.IR si_code
390is set to
391.BR POLL_IN ;
392and
393.B POLLIN
394is set in
395.IR si_band .
4d2b74dd 396
c13182ef
MK
397If successive output inotify events produced on the
398inotify file descriptor are identical (same
399.IR wd ,
400.IR mask ,
4d2b74dd
MK
401.IR cookie ,
402and
3f3698d8 403.IR name ),
6f0ab035
MK
404then they are coalesced into a single event if the
405older event has not yet been read (but see BUGS).
8856aab8
MK
406This reduces the amount of kernel memory required for the event queue,
407but also means that an application can't use inotify to reliably count
408file events.
4d2b74dd 409
c13182ef
MK
410The events returned by reading from an inotify file descriptor
411form an ordered queue.
412Thus, for example, it is guaranteed that when renaming from
413one directory to another, events will be produced in the
4d2b74dd
MK
414correct order on the inotify file descriptor.
415
416The
417.B FIONREAD
63f6a20a 418.BR ioctl (2)
c13182ef 419returns the number of bytes available to read from an
4d2b74dd 420inotify file descriptor.
613836aa 421.SS Limitations and caveats
4d2b74dd
MK
422Inotify monitoring of directories is not recursive:
423to monitor subdirectories under a directory,
424additional watches must be created.
613836aa
MK
425This can take a significant amount time for large directory trees.
426
4d2ddb4e
MK
427The inotify API provides no information about the user or process that
428triggered the inotify event.
99e603bd
MK
429In particular, there is no easy
430way for a process that is monitoring events via inotify
431to distinguish events that it triggers
432itself from those that are triggered by other processes.
4d2ddb4e 433
613836aa
MK
434Note that the event queue can overflow.
435In this case, events are lost.
09fa72fa 436Robust applications should handle the possibility of
613836aa
MK
437lost events gracefully.
438
439The inotify API identifies affected files by filename.
440However, by the time an application processes an inotify event,
441the filename may already have been deleted or renamed.
442
443If monitoring an entire directory subtree,
031de152
MK
444and a new subdirectory is created in that tree or an existing directory
445is renamed into that tree,
613836aa 446be aware that by the time you create a watch for the new subdirectory,
031de152 447new files (and subdirectories) may already exist inside the subdirectory.
613836aa 448Therefore, you may want to scan the contents of the subdirectory
031de152
MK
449immediately after adding the watch (and, if desired,
450recursively add watches for any subdirectories that it contains).
2f482041
MK
451
452The inotify applications identifies events via watch descriptors.
453It is the application's responsibility to cache a mapping
454(if one is needed) between watch descriptors and pathnames.
455Be aware that directory renamings may affect multiple cached pathnames.
47297adb 456.SH BUGS
a15ead5e
MK
457.\" FIXME kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
458.\" implies that unmount events were buggy 2.6.11 to 2.6.36
459.\"
ed7b0235
MK
460In kernels before 2.6.16, the
461.B IN_ONESHOT
c13182ef 462.I mask
ed7b0235 463flag does not work.
6f0ab035 464
7b5151b7
MK
465As originally designed and implemented, the
466.B IN_ONESHOT
467flag did not cause an
468.B IN_IGNORED
469event to be generated when the watch was dropped after one event.
470However, as an unintended effect of other changes,
471since Linux 2.6.36, an
472.B IN_IGNORED
473event is generated in this case.
474
6f0ab035 475Before kernel 2.6.25,
22129aa9 476.\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2
9ed6b517 477the kernel code that was intended to coalesce successive identical events
6f0ab035
MK
478(i.e., the two most recent events could potentially be coalesced
479if the older had not yet been read)
480instead checked if the most recent event could be coalesced with the
481.I oldest
482unread event.
47297adb 483.SH SEE ALSO
f0afb16a
MK
484.BR inotifywait (1),
485.BR inotifywatch (1),
4d2b74dd
MK
486.BR inotify_add_watch (2),
487.BR inotify_init (2),
43bb5faf 488.BR inotify_init1 (2),
4d2b74dd
MK
489.BR inotify_rm_watch (2),
490.BR read (2),
173fe7e7
DP
491.BR stat (2)
492
493.IR Documentation/filesystems/inotify.txt
494in the Linux kernel source tree