OSDN Git Service

81276a1c38a49ed0b55f49d27aa613a952458745
[linuxjm/LDP_man-pages.git] / original / man7 / inotify.7
1 '\" t
2 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\"
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
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.
14 .\"
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
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.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .\" FIXME 2.6.36 adds IN_EXCL_UNLINK
27 .\"
28 .TH INOTIFY 7 2010-09-27 "Linux" "Linux Programmer's Manual"
29 .SH NAME
30 inotify \- monitoring file system events
31 .SH DESCRIPTION
32 The
33 .I inotify
34 API provides a mechanism for monitoring file system events.
35 Inotify can be used to monitor individual files,
36 or to monitor directories.
37 When a directory is monitored, inotify will return events
38 for the directory itself, and for files inside the directory.
39
40 The following system calls are used with this API:
41 .BR inotify_init (2)
42 (or
43 .BR inotify_init1 (2)),
44 .BR inotify_add_watch (2),
45 .BR inotify_rm_watch (2),
46 .BR read (2),
47 and
48 .BR close (2).
49
50 .BR inotify_init (2)
51 creates an inotify instance and returns a file descriptor
52 referring to the inotify instance.
53 The more recent
54 .BR inotify_init1 (2)
55 is like
56 .BR inotify_init (2),
57 but provides some extra functionality.
58
59 .BR inotify_add_watch (2)
60 manipulates the "watch list" associated with an inotify instance.
61 Each item ("watch") in the watch list specifies the pathname of
62 a file or directory,
63 along with some set of events that the kernel should monitor for the
64 file referred to by that pathname.
65 .BR inotify_add_watch (2)
66 either creates a new watch item, or modifies an existing watch.
67 Each watch has a unique "watch descriptor", an integer
68 returned by
69 .BR inotify_add_watch (2)
70 when the watch is created.
71
72 .BR inotify_rm_watch (2)
73 removes an item from an inotify watch list.
74
75 When all file descriptors referring to an inotify
76 instance have been closed,
77 the underlying object and its resources are
78 freed for reuse by the kernel;
79 all associated watches are automatically freed.
80
81 To determine what events have occurred, an application
82 .BR read (2)s
83 from the inotify file descriptor.
84 If no events have so far occurred, then,
85 assuming a blocking file descriptor,
86 .BR read (2)
87 will block until at least one event occurs
88 (unless interrupted by a signal,
89 in which case the call fails with the error
90 .BR EINTR ;
91 see
92 .BR signal (7)).
93
94 Each successful
95 .BR read (2)
96 returns a buffer containing one or more of the following structures:
97 .in +4n
98 .nf
99
100 struct inotify_event {
101     int      wd;       /* Watch descriptor */
102 .\" FIXME . The type of the 'wd' field should probably be "int32_t".
103 .\" I submitted a patch to fix this.  See the LKML thread
104 .\" "[patch] Fix type errors in inotify interfaces", 18 Nov 2008
105 .\" Glibc bug filed: http://sources.redhat.com/bugzilla/show_bug.cgi?id=7040
106     uint32_t mask;     /* Mask of events */
107     uint32_t cookie;   /* Unique cookie associating related
108                           events (for rename(2)) */
109     uint32_t len;      /* Size of \fIname\fP field */
110     char     name[];   /* Optional null-terminated name */
111 };
112 .fi
113 .in
114
115 .I wd
116 identifies the watch for which this event occurs.
117 It is one of the watch descriptors returned by a previous call to
118 .BR inotify_add_watch (2).
119
120 .I mask
121 contains bits that describe the event that occurred (see below).
122
123 .I cookie
124 is a unique integer that connects related events.
125 Currently this is only used for rename events, and
126 allows the resulting pair of
127 .B IN_MOVE_FROM
128 and
129 .B IN_MOVE_TO
130 events to be connected by the application.
131
132 The
133 .I name
134 field is only present when an event is returned
135 for a file inside a watched directory;
136 it identifies the file pathname relative to the watched directory.
137 This pathname is null-terminated,
138 and may include further null bytes to align subsequent reads to a
139 suitable address boundary.
140
141 The
142 .I len
143 field counts all of the bytes in
144 .IR name ,
145 including the null bytes;
146 the length of each
147 .I inotify_event
148 structure is thus
149 .IR "sizeof(inotify_event)+len" .
150
151 The behavior when the buffer given to
152 .BR read (2)
153 is too small to return information about the next event depends
154 on the kernel version: in kernels before 2.6.21,
155 .BR read (2)
156 returns 0; since kernel 2.6.21,
157 .BR read (2)
158 fails with the error
159 .BR EINVAL .
160 .SS inotify events
161 The
162 .BR inotify_add_watch (2)
163 .I mask
164 argument and the
165 .I mask
166 field of the
167 .I inotify_event
168 structure returned when
169 .BR read (2)ing
170 an inotify file descriptor are both bit masks identifying
171 inotify events.
172 The following bits can be specified in
173 .I mask
174 when calling
175 .BR inotify_add_watch (2)
176 and may be returned in the
177 .I mask
178 field returned by
179 .BR read (2):
180 .RS 4
181 .sp
182 .PD 0
183 .TP 18
184 .B IN_ACCESS
185 File was accessed (read) (*).
186 .TP
187 .B IN_ATTRIB
188 Metadata changed, e.g., permissions, timestamps, extended attributes,
189 link count (since Linux 2.6.25), UID, GID, etc. (*).
190 .TP
191 .B IN_CLOSE_WRITE
192 File opened for writing was closed (*).
193 .TP
194 .B IN_CLOSE_NOWRITE
195 File not opened for writing was closed (*).
196 .TP
197 .B IN_CREATE
198 File/directory created in watched directory (*).
199 .TP
200 .B IN_DELETE
201 File/directory deleted from watched directory (*).
202 .TP
203 .B IN_DELETE_SELF
204 Watched file/directory was itself deleted.
205 .TP
206 .B IN_MODIFY
207 File was modified (*).
208 .TP
209 .B IN_MOVE_SELF
210 Watched file/directory was itself moved.
211 .TP
212 .B IN_MOVED_FROM
213 File moved out of watched directory (*).
214 .TP
215 .B IN_MOVED_TO
216 File moved into watched directory (*).
217 .TP
218 .B IN_OPEN
219 File was opened (*).
220 .PD
221 .RE
222 .PP
223 When monitoring a directory,
224 the events marked with an asterisk (*) above can occur for
225 files in the directory, in which case the
226 .I name
227 field in the returned
228 .I inotify_event
229 structure identifies the name of the file within the directory.
230 .PP
231 The
232 .B IN_ALL_EVENTS
233 macro is defined as a bit mask of all of the above events.
234 This macro can be used as the
235 .I mask
236 argument when calling
237 .BR inotify_add_watch (2).
238
239 Two additional convenience macros are
240 .BR IN_MOVE ,
241 which equates to
242 IN_MOVED_FROM|IN_MOVED_TO,
243 and
244 .B IN_CLOSE
245 which equates to
246 IN_CLOSE_WRITE|IN_CLOSE_NOWRITE.
247 .PP
248 The following further bits can be specified in
249 .I mask
250 when calling
251 .BR inotify_add_watch (2):
252 .RS 4
253 .sp
254 .PD 0
255 .TP 18
256 .BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
257 Don't dereference \fIpathname\fP if it is a symbolic link.
258 .TP
259 .B IN_MASK_ADD
260 Add (OR) events to watch mask for this pathname if
261 it already exists (instead of replacing mask).
262 .TP
263 .B IN_ONESHOT
264 Monitor \fIpathname\fP for one event, then remove from
265 watch list.
266 .TP
267 .BR IN_ONLYDIR " (since Linux 2.6.15)"
268 Only watch \fIpathname\fP if it is a directory.
269 .PD
270 .RE
271 .PP
272 The following bits may be set in the
273 .I mask
274 field returned by
275 .BR read (2):
276 .RS 4
277 .sp
278 .PD 0
279 .TP 18
280 .B IN_IGNORED
281 Watch was removed explicitly (\fBinotify_rm_watch\fP(2))
282 or automatically (file was deleted, or file system was unmounted).
283 .TP
284 .B IN_ISDIR
285 Subject of this event is a directory.
286 .TP
287 .B IN_Q_OVERFLOW
288 Event queue overflowed (\fIwd\fP is \-1 for this event).
289 .TP
290 .B IN_UNMOUNT
291 File system containing watched object was unmounted.
292 .PD
293 .RE
294 .SS /proc interfaces
295 The following interfaces can be used to limit the amount of
296 kernel memory consumed by inotify:
297 .TP
298 .I /proc/sys/fs/inotify/max_queued_events
299 The value in this file is used when an application calls
300 .BR inotify_init (2)
301 to set an upper limit on the number of events that can be
302 queued to the corresponding inotify instance.
303 Events in excess of this limit are dropped, but an
304 .B IN_Q_OVERFLOW
305 event is always generated.
306 .TP
307 .I /proc/sys/fs/inotify/max_user_instances
308 This specifies an upper limit on the number of inotify instances
309 that can be created per real user ID.
310 .TP
311 .I /proc/sys/fs/inotify/max_user_watches
312 This specifies an upper limit on the number of watches
313 that can be created per real user ID.
314 .SH "VERSIONS"
315 Inotify was merged into the 2.6.13 Linux kernel.
316 The required library interfaces were added to glibc in version 2.4.
317 .RB ( IN_DONT_FOLLOW ,
318 .BR IN_MASK_ADD ,
319 and
320 .B IN_ONLYDIR
321 were only added in version 2.5.)
322 .SH "CONFORMING TO"
323 The inotify API is Linux-specific.
324 .SH "NOTES"
325 Inotify file descriptors can be monitored using
326 .BR select (2),
327 .BR poll (2),
328 and
329 .BR epoll (7).
330 When an event is available, the file descriptor indicates as readable.
331
332 Since Linux 2.6.25,
333 signal-driven I/O notification is available for inotify file descriptors;
334 see the discussion of
335 .B F_SETFL
336 (for setting the
337 .B O_ASYNC
338 flag),
339 .BR F_SETOWN ,
340 and
341 .B F_SETSIG
342 in
343 .BR fcntl (2).
344 The
345 .I siginfo_t
346 structure (described in
347 .BR sigaction (2))
348 that is passed to the signal handler has the following fields set:
349 .IR si_fd
350 is set to the inotify file descriptor number;
351 .IR si_signo
352 is set to the signal number;
353 .IR si_code
354 is set to
355 .BR POLL_IN ;
356 and
357 .B POLLIN
358 is set in
359 .IR si_band .
360
361 If successive output inotify events produced on the
362 inotify file descriptor are identical (same
363 .IR wd ,
364 .IR mask ,
365 .IR cookie ,
366 and
367 .IR name )
368 then they are coalesced into a single event if the
369 older event has not yet been read (but see BUGS).
370
371 The events returned by reading from an inotify file descriptor
372 form an ordered queue.
373 Thus, for example, it is guaranteed that when renaming from
374 one directory to another, events will be produced in the
375 correct order on the inotify file descriptor.
376
377 The
378 .B FIONREAD
379 .BR ioctl (2)
380 returns the number of bytes available to read from an
381 inotify file descriptor.
382 .SS Limitations and caveats
383 Inotify monitoring of directories is not recursive:
384 to monitor subdirectories under a directory,
385 additional watches must be created.
386 This can take a significant amount time for large directory trees.
387
388 The inotify API provides no information about the user or process that
389 triggered the inotify event.
390
391 Note that the event queue can overflow.
392 In this case, events are lost.
393 Robust applications should handle the possibility of
394 lost events gracefully.
395
396 The inotify API identifies affected files by filename.
397 However, by the time an application processes an inotify event,
398 the filename may already have been deleted or renamed.
399
400 If monitoring an entire directory subtree,
401 and a new subdirectory is created in that tree,
402 be aware that by the time you create a watch for the new subdirectory,
403 new files may already have been created in the subdirectory.
404 Therefore, you may want to scan the contents of the subdirectory
405 immediately after adding the watch.
406 .SH "BUGS"
407 In kernels before 2.6.16, the
408 .B IN_ONESHOT
409 .I mask
410 flag does not work.
411
412 Before kernel 2.6.25,
413 the kernel code that was intended to coalesce successive identical events
414 (i.e., the two most recent events could potentially be coalesced
415 if the older had not yet been read)
416 instead checked if the most recent event could be coalesced with the
417 .I oldest
418 unread event.
419 .SH "SEE ALSO"
420 .BR inotify_add_watch (2),
421 .BR inotify_init (2),
422 .BR inotify_init1 (2),
423 .BR inotify_rm_watch (2),
424 .BR read (2),
425 .BR stat (2),
426 .IR Documentation/filesystems/inotify.txt .