]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/inotify.7
ftok.3: grfix
[thirdparty/man-pages.git] / man7 / inotify.7
CommitLineData
4d2b74dd
MK
1'\" t
2.\" Hey Emacs! This file is -*- nroff -*- source.
3.\"
c11b1abf 4.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4d2b74dd
MK
5.\"
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.
25.\"
c3270a15 26.TH INOTIFY 7 2010-11-12 "Linux" "Linux Programmer's Manual"
4d2b74dd 27.SH NAME
3a20b4ca 28inotify \- monitoring file system events
4d2b74dd
MK
29.SH DESCRIPTION
30The
c13182ef 31.I inotify
4d2b74dd
MK
32API provides a mechanism for monitoring file system events.
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.
123Currently this is only used for rename events, and
124allows the resulting pair of
125.B IN_MOVE_FROM
c13182ef 126and
4d2b74dd
MK
127.B IN_MOVE_TO
128events to be connected by the application.
129
c13182ef 130The
4d2b74dd
MK
131.I name
132field is only present when an event is returned
c13182ef 133for a file inside a watched directory;
4d2b74dd 134it identifies the file pathname relative to the watched directory.
c13182ef 135This pathname is null-terminated,
ad31978e 136and may include further null bytes to align subsequent reads to a
4d2b74dd
MK
137suitable address boundary.
138
139The
140.I len
c13182ef 141field counts all of the bytes in
4d2b74dd 142.IR name ,
c13182ef 143including the null bytes;
4d2b74dd
MK
144the length of each
145.I inotify_event
146structure is thus
147.IR "sizeof(inotify_event)+len" .
c7e3ee6f 148
988db661 149The behavior when the buffer given to
c7e3ee6f 150.BR read (2)
988db661 151is too small to return information about the next event depends
c7e3ee6f
MK
152on the kernel version: in kernels before 2.6.21,
153.BR read (2)
154returns 0; since kernel 2.6.21,
155.BR read (2)
156fails with the error
157.BR EINVAL .
4d2b74dd 158.SS inotify events
c13182ef 159The
4d2b74dd
MK
160.BR inotify_add_watch (2)
161.I mask
c13182ef 162argument and the
4d2b74dd
MK
163.I mask
164field of the
165.I inotify_event
166structure returned when
167.BR read (2)ing
168an inotify file descriptor are both bit masks identifying
169inotify events.
170The following bits can be specified in
171.I mask
172when calling
63f6a20a 173.BR inotify_add_watch (2)
c13182ef 174and may be returned in the
4d2b74dd
MK
175.I mask
176field returned by
63f6a20a 177.BR read (2):
3f174f7d
MK
178.RS 4
179.sp
180.PD 0
181.TP 18
182.B IN_ACCESS
183File was accessed (read) (*).
184.TP
185.B IN_ATTRIB
3a5c365a
MK
186Metadata changed, e.g., permissions, timestamps, extended attributes,
187link count (since Linux 2.6.25), UID, GID, etc. (*).
3f174f7d
MK
188.TP
189.B IN_CLOSE_WRITE
190File opened for writing was closed (*).
191.TP
192.B IN_CLOSE_NOWRITE
193File not opened for writing was closed (*).
194.TP
195.B IN_CREATE
196File/directory created in watched directory (*).
197.TP
198.B IN_DELETE
199File/directory deleted from watched directory (*).
200.TP
201.B IN_DELETE_SELF
202Watched file/directory was itself deleted.
203.TP
204.B IN_MODIFY
205File was modified (*).
206.TP
207.B IN_MOVE_SELF
208Watched file/directory was itself moved.
209.TP
210.B IN_MOVED_FROM
211File moved out of watched directory (*).
212.TP
213.B IN_MOVED_TO
214File moved into watched directory (*).
215.TP
216.B IN_OPEN
217File was opened (*).
218.PD
219.RE
4d2b74dd 220.PP
c13182ef
MK
221When monitoring a directory,
222the events marked with an asterisk (*) above can occur for
4d2b74dd
MK
223files in the directory, in which case the
224.I name
225field in the returned
226.I inotify_event
227structure identifies the name of the file within the directory.
228.PP
229The
230.B IN_ALL_EVENTS
231macro is defined as a bit mask of all of the above events.
232This macro can be used as the
233.I mask
234argument when calling
63f6a20a 235.BR inotify_add_watch (2).
4d2b74dd
MK
236
237Two additional convenience macros are
238.BR IN_MOVE ,
239which equates to
240IN_MOVED_FROM|IN_MOVED_TO,
241and
fe252985 242.BR IN_CLOSE ,
4d2b74dd
MK
243which equates to
244IN_CLOSE_WRITE|IN_CLOSE_NOWRITE.
245.PP
c13182ef 246The following further bits can be specified in
4d2b74dd
MK
247.I mask
248when calling
63f6a20a 249.BR inotify_add_watch (2):
3f174f7d
MK
250.RS 4
251.sp
252.PD 0
253.TP 18
31daf529
MK
254.BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
255Don't dereference \fIpathname\fP if it is a symbolic link.
dda869a4 256.TP
b3ad7609
MK
257.B IN_EXCL_UNLINK " (since Linux 2.6.36)"
258.\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
259By default, when watching events on the children of a directory,
260events are generated for children even after they have been unlinked
261from the directory.
262This can result in large numbers of uninteresting events for
263some applications (e.g., if watching
264.IR /tmp ,
265in which many applications create temporary files whose
266names are immediately unlinked).
267Specifying
268.B IN_EXCL_UNLINK
269changes the default behavior,
270so that events are not generated for children after
271they have been unlinked from the watched directory.
272.TP
dda869a4
MK
273.B IN_MASK_ADD
274Add (OR) events to watch mask for this pathname if
275it already exists (instead of replacing mask).
276.TP
277.B IN_ONESHOT
278Monitor \fIpathname\fP for one event, then remove from
279watch list.
280.TP
31daf529
MK
281.BR IN_ONLYDIR " (since Linux 2.6.15)"
282Only watch \fIpathname\fP if it is a directory.
3f174f7d
MK
283.PD
284.RE
4d2b74dd
MK
285.PP
286The following bits may be set in the
287.I mask
288field returned by
63f6a20a 289.BR read (2):
3f174f7d
MK
290.RS 4
291.sp
292.PD 0
293.TP 18
dda869a4
MK
294.B IN_IGNORED
295Watch was removed explicitly (\fBinotify_rm_watch\fP(2))
296or automatically (file was deleted, or file system was unmounted).
297.TP
298.B IN_ISDIR
299Subject of this event is a directory.
300.TP
301.B IN_Q_OVERFLOW
302Event queue overflowed (\fIwd\fP is \-1 for this event).
303.TP
304.B IN_UNMOUNT
305File system containing watched object was unmounted.
3f174f7d
MK
306.PD
307.RE
4d2b74dd 308.SS /proc interfaces
c13182ef 309The following interfaces can be used to limit the amount of
4d2b74dd
MK
310kernel memory consumed by inotify:
311.TP
0daa9e92 312.I /proc/sys/fs/inotify/max_queued_events
4d2b74dd
MK
313The value in this file is used when an application calls
314.BR inotify_init (2)
c13182ef 315to set an upper limit on the number of events that can be
4d2b74dd
MK
316queued to the corresponding inotify instance.
317Events in excess of this limit are dropped, but an
318.B IN_Q_OVERFLOW
319event is always generated.
320.TP
0daa9e92 321.I /proc/sys/fs/inotify/max_user_instances
c13182ef 322This specifies an upper limit on the number of inotify instances
4d2b74dd
MK
323that can be created per real user ID.
324.TP
0daa9e92 325.I /proc/sys/fs/inotify/max_user_watches
31546b46
VN
326This specifies an upper limit on the number of watches
327that can be created per real user ID.
2b2581ee
MK
328.SH "VERSIONS"
329Inotify was merged into the 2.6.13 Linux kernel.
330The required library interfaces were added to glibc in version 2.4.
331.RB ( IN_DONT_FOLLOW ,
332.BR IN_MASK_ADD ,
333and
334.B IN_ONLYDIR
335were only added in version 2.5.)
336.SH "CONFORMING TO"
8382f16d 337The inotify API is Linux-specific.
4d2b74dd
MK
338.SH "NOTES"
339Inotify file descriptors can be monitored using
340.BR select (2),
341.BR poll (2),
c13182ef 342and
2315114c 343.BR epoll (7).
0000daa5
MK
344When an event is available, the file descriptor indicates as readable.
345
346Since Linux 2.6.25,
347signal-driven I/O notification is available for inotify file descriptors;
348see the discussion of
349.B F_SETFL
350(for setting the
351.B O_ASYNC
352flag),
353.BR F_SETOWN ,
354and
355.B F_SETSIG
356in
357.BR fcntl (2).
358The
359.I siginfo_t
360structure (described in
361.BR sigaction (2))
362that is passed to the signal handler has the following fields set:
363.IR si_fd
364is set to the inotify file descriptor number;
365.IR si_signo
366is set to the signal number;
367.IR si_code
368is set to
369.BR POLL_IN ;
370and
371.B POLLIN
372is set in
373.IR si_band .
4d2b74dd 374
c13182ef
MK
375If successive output inotify events produced on the
376inotify file descriptor are identical (same
377.IR wd ,
378.IR mask ,
4d2b74dd
MK
379.IR cookie ,
380and
381.IR name )
6f0ab035
MK
382then they are coalesced into a single event if the
383older event has not yet been read (but see BUGS).
4d2b74dd 384
c13182ef
MK
385The events returned by reading from an inotify file descriptor
386form an ordered queue.
387Thus, for example, it is guaranteed that when renaming from
388one directory to another, events will be produced in the
4d2b74dd
MK
389correct order on the inotify file descriptor.
390
391The
392.B FIONREAD
63f6a20a 393.BR ioctl (2)
c13182ef 394returns the number of bytes available to read from an
4d2b74dd 395inotify file descriptor.
613836aa 396.SS Limitations and caveats
4d2b74dd
MK
397Inotify monitoring of directories is not recursive:
398to monitor subdirectories under a directory,
399additional watches must be created.
613836aa
MK
400This can take a significant amount time for large directory trees.
401
4d2ddb4e
MK
402The inotify API provides no information about the user or process that
403triggered the inotify event.
404
613836aa
MK
405Note that the event queue can overflow.
406In this case, events are lost.
09fa72fa 407Robust applications should handle the possibility of
613836aa
MK
408lost events gracefully.
409
410The inotify API identifies affected files by filename.
411However, by the time an application processes an inotify event,
412the filename may already have been deleted or renamed.
413
414If monitoring an entire directory subtree,
415and a new subdirectory is created in that tree,
416be aware that by the time you create a watch for the new subdirectory,
417new files may already have been created in the subdirectory.
418Therefore, you may want to scan the contents of the subdirectory
419immediately after adding the watch.
ed7b0235
MK
420.SH "BUGS"
421In kernels before 2.6.16, the
422.B IN_ONESHOT
c13182ef 423.I mask
ed7b0235 424flag does not work.
6f0ab035
MK
425
426Before kernel 2.6.25,
9ed6b517 427the kernel code that was intended to coalesce successive identical events
6f0ab035
MK
428(i.e., the two most recent events could potentially be coalesced
429if the older had not yet been read)
430instead checked if the most recent event could be coalesced with the
431.I oldest
432unread event.
4d2b74dd
MK
433.SH "SEE ALSO"
434.BR inotify_add_watch (2),
435.BR inotify_init (2),
43bb5faf 436.BR inotify_init1 (2),
4d2b74dd
MK
437.BR inotify_rm_watch (2),
438.BR read (2),
439.BR stat (2),
440.IR Documentation/filesystems/inotify.txt .