]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/inotify.7
Minor wording chnages.
[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
c13182ef
MK
18.\" the use of the information contained herein.
19.\"
4d2b74dd
MK
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
c7e3ee6f 23.TH INOTIFY 7 2007-06-03 "Linux" "Linux Programmer's Manual"
4d2b74dd 24.SH NAME
3a20b4ca 25inotify \- monitoring file system events
4d2b74dd
MK
26.SH DESCRIPTION
27The
c13182ef 28.I inotify
4d2b74dd
MK
29API provides a mechanism for monitoring file system events.
30Inotify can be used to monitor individual files,
31or to monitor directories.
32When a directory is monitored, inotify will return events
33for the directory itself, and for files inside the directory.
34
c13182ef 35The following system calls are used with this API:
63f6a20a
MK
36.BR inotify_init (2),
37.BR inotify_add_watch (2),
38.BR inotify_rm_watch (2),
39.BR read (2),
c13182ef 40and
63f6a20a 41.BR close (2).
4d2b74dd
MK
42
43.BR inotify_init (2)
c13182ef 44creates an inotify instance and returns a file descriptor
a2cc46ca 45referring to the inotify instance.
4d2b74dd
MK
46
47.BR inotify_add_watch (2)
a2cc46ca 48manipulates the "watch list" associated with an inotify instance.
3a065ac0 49Each item ("watch") in the watch list specifies the pathname of
c13182ef 50a file or directory,
4d2b74dd
MK
51along with some set of events that the kernel should monitor for the
52file referred to by that pathname.
63f6a20a 53.BR inotify_add_watch (2)
4d2b74dd
MK
54either creates a new watch item, or modifies an existing watch.
55Each watch has a unique "watch descriptor", an integer
56returned by
63f6a20a 57.BR inotify_add_watch (2)
4d2b74dd
MK
58when the watch is created.
59
60.BR inotify_rm_watch (2)
61removes an item from an inotify watch list.
62
c13182ef 63When all file descriptors referring to an inotify
a2cc46ca 64instance have been closed,
c13182ef 65the underlying object and its resources are
a2cc46ca 66freed for re-use by the kernel;
4d2b74dd
MK
67all associated watches are automatically freed.
68
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)
4d2b74dd
MK
75will block until at least one event occurs.
76
77Each successful
63f6a20a 78.BR read (2)
4d2b74dd 79returns a buffer containing one or more of the following structures:
a08ea57c 80.in +4n
4d2b74dd
MK
81.nf
82
83struct inotify_event {
84 int wd; /* Watch descriptor */
85 uint32_t mask; /* Mask of events */
c13182ef 86 uint32_t cookie; /* Unique cookie associating related
4d2b74dd
MK
87 events (for rename(2)) */
88 uint32_t len; /* Size of 'name' field */
89 char name[]; /* Optional null-terminated name */
90};
91.fi
a08ea57c 92.in
4d2b74dd
MK
93
94.I wd
95identifies the watch for which this event occurs.
c13182ef 96It is one of the watch descriptors returned by a previous call to
63f6a20a 97.BR inotify_add_watch (2).
4d2b74dd
MK
98
99.I mask
100contains bits that describe the event that occurred (see below).
101
102.I cookie
103is a unique integer that connects related events.
104Currently this is only used for rename events, and
105allows the resulting pair of
106.B IN_MOVE_FROM
c13182ef 107and
4d2b74dd
MK
108.B IN_MOVE_TO
109events to be connected by the application.
110
c13182ef 111The
4d2b74dd
MK
112.I name
113field is only present when an event is returned
c13182ef 114for a file inside a watched directory;
4d2b74dd 115it identifies the file pathname relative to the watched directory.
c13182ef 116This pathname is null-terminated,
ad31978e 117and may include further null bytes to align subsequent reads to a
4d2b74dd
MK
118suitable address boundary.
119
120The
121.I len
c13182ef 122field counts all of the bytes in
4d2b74dd 123.IR name ,
c13182ef 124including the null bytes;
4d2b74dd
MK
125the length of each
126.I inotify_event
127structure is thus
128.IR "sizeof(inotify_event)+len" .
c7e3ee6f 129
988db661 130The behavior when the buffer given to
c7e3ee6f 131.BR read (2)
988db661 132is too small to return information about the next event depends
c7e3ee6f
MK
133on the kernel version: in kernels before 2.6.21,
134.BR read (2)
135returns 0; since kernel 2.6.21,
136.BR read (2)
137fails with the error
138.BR EINVAL .
4d2b74dd 139.SS inotify events
c13182ef 140The
4d2b74dd
MK
141.BR inotify_add_watch (2)
142.I mask
c13182ef 143argument and the
4d2b74dd
MK
144.I mask
145field of the
146.I inotify_event
147structure returned when
148.BR read (2)ing
149an inotify file descriptor are both bit masks identifying
150inotify events.
151The following bits can be specified in
152.I mask
153when calling
63f6a20a 154.BR inotify_add_watch (2)
c13182ef 155and may be returned in the
4d2b74dd
MK
156.I mask
157field returned by
63f6a20a 158.BR read (2):
4d2b74dd 159.TS
4d2b74dd 160lB l.
4d2b74dd 161IN_ACCESS File was accessed (read) (*)
c13182ef 162IN_ATTRIB Metadata changed (permissions, timestamps,
4d2b74dd
MK
163 extended attributes, etc.) (*)
164IN_CLOSE_WRITE File opened for writing was closed (*)
165IN_CLOSE_NOWRITE File not opened for writing was closed (*)
166IN_CREATE File/directory created in watched directory (*)
167IN_DELETE File/directory deleted from watched directory (*)
168IN_DELETE_SELF Watched file/directory was itself deleted
169IN_MODIFY File was modified (*)
170IN_MOVE_SELF Watched file/directory was itself moved
171IN_MOVED_FROM File moved out of watched directory (*)
172IN_MOVED_TO File moved into watched directory (*)
173IN_OPEN File was opened (*)
174.TE
4d2b74dd 175.PP
c13182ef
MK
176When monitoring a directory,
177the events marked with an asterisk (*) above can occur for
4d2b74dd
MK
178files in the directory, in which case the
179.I name
180field in the returned
181.I inotify_event
182structure identifies the name of the file within the directory.
183.PP
184The
185.B IN_ALL_EVENTS
186macro is defined as a bit mask of all of the above events.
187This macro can be used as the
188.I mask
189argument when calling
63f6a20a 190.BR inotify_add_watch (2).
4d2b74dd
MK
191
192Two additional convenience macros are
193.BR IN_MOVE ,
194which equates to
195IN_MOVED_FROM|IN_MOVED_TO,
196and
0daa9e92 197.B IN_CLOSE
4d2b74dd
MK
198which equates to
199IN_CLOSE_WRITE|IN_CLOSE_NOWRITE.
200.PP
c13182ef 201The following further bits can be specified in
4d2b74dd
MK
202.I mask
203when calling
63f6a20a 204.BR inotify_add_watch (2):
4d2b74dd 205.TS
4d2b74dd 206lB l.
c13182ef 207IN_DONT_FOLLOW Don't dereference \fIpathname\fP if it is a symbolic link
25d63cde 208 (Since Linux 2.6.15)
b41beb55 209IN_MASK_ADD Add (OR) events to watch mask for this pathname if
4d2b74dd 210 it already exists (instead of replacing mask)
c13182ef 211IN_ONESHOT Monitor \fIpathname\fP for one event, then remove from
4d2b74dd 212 watch list
b41beb55 213IN_ONLYDIR Only watch \fIpathname\fP if it is a directory
25d63cde 214 (Since Linux 2.6.15)
4d2b74dd 215.TE
4d2b74dd
MK
216.PP
217The following bits may be set in the
218.I mask
219field returned by
63f6a20a 220.BR read (2):
4d2b74dd 221.TS
4d2b74dd 222lB l.
63f6a20a 223IN_IGNORED Watch was removed explicitly (\fBinotify_rm_watch\fP(2))
4d2b74dd
MK
224 or automatically (file was deleted, or
225 file system was unmounted)
226IN_ISDIR Subject of this event is a directory
227IN_Q_OVERFLOW Event queue overflowed (\fIwd\fP is \-1 for this event)
228IN_UNMOUNT File system containing watched object was unmounted
229.TE
4d2b74dd 230.SS /proc interfaces
c13182ef 231The following interfaces can be used to limit the amount of
4d2b74dd
MK
232kernel memory consumed by inotify:
233.TP
0daa9e92 234.I /proc/sys/fs/inotify/max_queued_events
4d2b74dd
MK
235The value in this file is used when an application calls
236.BR inotify_init (2)
c13182ef 237to set an upper limit on the number of events that can be
4d2b74dd
MK
238queued to the corresponding inotify instance.
239Events in excess of this limit are dropped, but an
240.B IN_Q_OVERFLOW
241event is always generated.
242.TP
0daa9e92 243.I /proc/sys/fs/inotify/max_user_instances
c13182ef 244This specifies an upper limit on the number of inotify instances
4d2b74dd
MK
245that can be created per real user ID.
246.TP
0daa9e92 247.I /proc/sys/fs/inotify/max_user_watches
c13182ef 248This specifies a limit on the number of watches that can be associated
4d2b74dd 249with each inotify instance.
2b2581ee
MK
250.SH "VERSIONS"
251Inotify was merged into the 2.6.13 Linux kernel.
252The required library interfaces were added to glibc in version 2.4.
253.RB ( IN_DONT_FOLLOW ,
254.BR IN_MASK_ADD ,
255and
256.B IN_ONLYDIR
257were only added in version 2.5.)
258.SH "CONFORMING TO"
8382f16d 259The inotify API is Linux-specific.
4d2b74dd
MK
260.SH "NOTES"
261Inotify file descriptors can be monitored using
262.BR select (2),
263.BR poll (2),
c13182ef 264and
2315114c 265.BR epoll (7).
4d2b74dd 266
c13182ef
MK
267If successive output inotify events produced on the
268inotify file descriptor are identical (same
269.IR wd ,
270.IR mask ,
4d2b74dd
MK
271.IR cookie ,
272and
273.IR name )
274then they are coalesced into a single event.
275
c13182ef
MK
276The events returned by reading from an inotify file descriptor
277form an ordered queue.
278Thus, for example, it is guaranteed that when renaming from
279one directory to another, events will be produced in the
4d2b74dd
MK
280correct order on the inotify file descriptor.
281
282The
283.B FIONREAD
63f6a20a 284.BR ioctl (2)
c13182ef 285returns the number of bytes available to read from an
4d2b74dd
MK
286inotify file descriptor.
287
288Inotify monitoring of directories is not recursive:
289to monitor subdirectories under a directory,
290additional watches must be created.
ed7b0235
MK
291.SH "BUGS"
292In kernels before 2.6.16, the
293.B IN_ONESHOT
c13182ef 294.I mask
ed7b0235 295flag does not work.
4d2b74dd
MK
296.SH "SEE ALSO"
297.BR inotify_add_watch (2),
298.BR inotify_init (2),
299.BR inotify_rm_watch (2),
300.BR read (2),
301.BR stat (2),
302.IR Documentation/filesystems/inotify.txt .