OSDN Git Service

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