OSDN Git Service

52cd981f4a0594a97e07379d8bd8464f44bfc34b
[linuxjm/LDP_man-pages.git] / original / man7 / inotify.7
1 '\" t
2 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
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.
13 .\"
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
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.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH INOTIFY 7 2013-06-21 "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 used only for rename events, and
124 allows the resulting pair of
125 .B IN_MOVED_FROM
126 and
127 .B IN_MOVED_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 present only 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 (\(aq\\0\(aq) 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(struct 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 Specifying a buffer of size
162
163     sizeof(struct inotify_event) + NAME_MAX + 1
164
165 will be sufficient to read at least one event.
166 .SS inotify events
167 The
168 .BR inotify_add_watch (2)
169 .I mask
170 argument and the
171 .I mask
172 field of the
173 .I inotify_event
174 structure returned when
175 .BR read (2)ing
176 an inotify file descriptor are both bit masks identifying
177 inotify events.
178 The following bits can be specified in
179 .I mask
180 when calling
181 .BR inotify_add_watch (2)
182 and may be returned in the
183 .I mask
184 field returned by
185 .BR read (2):
186 .RS 4
187 .sp
188 .PD 0
189 .TP 18
190 .B IN_ACCESS
191 File was accessed (read) (*).
192 .TP
193 .B IN_ATTRIB
194 Metadata changed, e.g., permissions, timestamps, extended attributes,
195 link count (since Linux 2.6.25), UID, GID, etc. (*).
196 .TP
197 .B IN_CLOSE_WRITE
198 File opened for writing was closed (*).
199 .TP
200 .B IN_CLOSE_NOWRITE
201 File not opened for writing was closed (*).
202 .TP
203 .B IN_CREATE
204 File/directory created in watched directory (*).
205 .TP
206 .B IN_DELETE
207 File/directory deleted from watched directory (*).
208 .TP
209 .B IN_DELETE_SELF
210 Watched file/directory was itself deleted.
211 .TP
212 .B IN_MODIFY
213 File was modified (*).
214 .TP
215 .B IN_MOVE_SELF
216 Watched file/directory was itself moved.
217 .TP
218 .B IN_MOVED_FROM
219 File moved out of watched directory (*).
220 .TP
221 .B IN_MOVED_TO
222 File moved into watched directory (*).
223 .TP
224 .B IN_OPEN
225 File was opened (*).
226 .PD
227 .RE
228 .PP
229 When monitoring a directory,
230 the events marked with an asterisk (*) above can occur for
231 files in the directory, in which case the
232 .I name
233 field in the returned
234 .I inotify_event
235 structure identifies the name of the file within the directory.
236 .PP
237 The
238 .B IN_ALL_EVENTS
239 macro is defined as a bit mask of all of the above events.
240 This macro can be used as the
241 .I mask
242 argument when calling
243 .BR inotify_add_watch (2).
244
245 Two additional convenience macros are
246 .BR IN_MOVE ,
247 which equates to
248 IN_MOVED_FROM|IN_MOVED_TO,
249 and
250 .BR IN_CLOSE ,
251 which equates to
252 IN_CLOSE_WRITE|IN_CLOSE_NOWRITE.
253 .PP
254 The following further bits can be specified in
255 .I mask
256 when calling
257 .BR inotify_add_watch (2):
258 .RS 4
259 .sp
260 .PD 0
261 .TP 18
262 .BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
263 Don't dereference
264 .I pathname
265 if it is a symbolic link.
266 .TP
267 .BR IN_EXCL_UNLINK " (since Linux 2.6.36)"
268 .\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
269 By default, when watching events on the children of a directory,
270 events are generated for children even after they have been unlinked
271 from the directory.
272 This can result in large numbers of uninteresting events for
273 some applications (e.g., if watching
274 .IR /tmp ,
275 in which many applications create temporary files whose
276 names are immediately unlinked).
277 Specifying
278 .B IN_EXCL_UNLINK
279 changes the default behavior,
280 so that events are not generated for children after
281 they have been unlinked from the watched directory.
282 .TP
283 .B IN_MASK_ADD
284 Add (OR) events to watch mask for this pathname if
285 it already exists (instead of replacing mask).
286 .TP
287 .B IN_ONESHOT
288 Monitor
289 .I pathname
290 for one event, then remove from
291 watch list.
292 .TP
293 .BR IN_ONLYDIR " (since Linux 2.6.15)"
294 Only watch
295 .I pathname
296 if it is a directory.
297 .PD
298 .RE
299 .PP
300 The following bits may be set in the
301 .I mask
302 field returned by
303 .BR read (2):
304 .RS 4
305 .sp
306 .PD 0
307 .TP 18
308 .B IN_IGNORED
309 Watch was removed explicitly
310 .RB ( inotify_rm_watch (2))
311 or automatically (file was deleted, or file system was unmounted).
312 .TP
313 .B IN_ISDIR
314 Subject of this event is a directory.
315 .TP
316 .B IN_Q_OVERFLOW
317 Event queue overflowed
318 .RI ( wd
319 is \-1 for this event).
320 .TP
321 .B IN_UNMOUNT
322 File system containing watched object was unmounted.
323 .PD
324 .RE
325 .SS /proc interfaces
326 The following interfaces can be used to limit the amount of
327 kernel memory consumed by inotify:
328 .TP
329 .I /proc/sys/fs/inotify/max_queued_events
330 The value in this file is used when an application calls
331 .BR inotify_init (2)
332 to set an upper limit on the number of events that can be
333 queued to the corresponding inotify instance.
334 Events in excess of this limit are dropped, but an
335 .B IN_Q_OVERFLOW
336 event is always generated.
337 .TP
338 .I /proc/sys/fs/inotify/max_user_instances
339 This specifies an upper limit on the number of inotify instances
340 that can be created per real user ID.
341 .TP
342 .I /proc/sys/fs/inotify/max_user_watches
343 This specifies an upper limit on the number of watches
344 that can be created per real user ID.
345 .SH VERSIONS
346 Inotify was merged into the 2.6.13 Linux kernel.
347 The required library interfaces were added to glibc in version 2.4.
348 .RB ( IN_DONT_FOLLOW ,
349 .BR IN_MASK_ADD ,
350 and
351 .B IN_ONLYDIR
352 were added in version 2.5.)
353 .SH CONFORMING TO
354 The inotify API is Linux-specific.
355 .SH NOTES
356 Inotify file descriptors can be monitored using
357 .BR select (2),
358 .BR poll (2),
359 and
360 .BR epoll (7).
361 When an event is available, the file descriptor indicates as readable.
362
363 Since Linux 2.6.25,
364 signal-driven I/O notification is available for inotify file descriptors;
365 see the discussion of
366 .B F_SETFL
367 (for setting the
368 .B O_ASYNC
369 flag),
370 .BR F_SETOWN ,
371 and
372 .B F_SETSIG
373 in
374 .BR fcntl (2).
375 The
376 .I siginfo_t
377 structure (described in
378 .BR sigaction (2))
379 that is passed to the signal handler has the following fields set:
380 .IR si_fd
381 is set to the inotify file descriptor number;
382 .IR si_signo
383 is set to the signal number;
384 .IR si_code
385 is set to
386 .BR POLL_IN ;
387 and
388 .B POLLIN
389 is set in
390 .IR si_band .
391
392 If successive output inotify events produced on the
393 inotify file descriptor are identical (same
394 .IR wd ,
395 .IR mask ,
396 .IR cookie ,
397 and
398 .IR name )
399 then they are coalesced into a single event if the
400 older event has not yet been read (but see BUGS).
401
402 The events returned by reading from an inotify file descriptor
403 form an ordered queue.
404 Thus, for example, it is guaranteed that when renaming from
405 one directory to another, events will be produced in the
406 correct order on the inotify file descriptor.
407
408 The
409 .B FIONREAD
410 .BR ioctl (2)
411 returns the number of bytes available to read from an
412 inotify file descriptor.
413 .SS Limitations and caveats
414 Inotify monitoring of directories is not recursive:
415 to monitor subdirectories under a directory,
416 additional watches must be created.
417 This can take a significant amount time for large directory trees.
418
419 The inotify API provides no information about the user or process that
420 triggered the inotify event.
421 In particular, there is no easy
422 way for a process that is monitoring events via inotify
423 to distinguish events that it triggers
424 itself from those that are triggered by other processes.
425
426 Note that the event queue can overflow.
427 In this case, events are lost.
428 Robust applications should handle the possibility of
429 lost events gracefully.
430
431 The inotify API identifies affected files by filename.
432 However, by the time an application processes an inotify event,
433 the filename may already have been deleted or renamed.
434
435 If monitoring an entire directory subtree,
436 and a new subdirectory is created in that tree,
437 be aware that by the time you create a watch for the new subdirectory,
438 new files may already have been created in the subdirectory.
439 Therefore, you may want to scan the contents of the subdirectory
440 immediately after adding the watch.
441 .SH BUGS
442 In kernels before 2.6.16, the
443 .B IN_ONESHOT
444 .I mask
445 flag does not work.
446
447 Before kernel 2.6.25,
448 the kernel code that was intended to coalesce successive identical events
449 (i.e., the two most recent events could potentially be coalesced
450 if the older had not yet been read)
451 instead checked if the most recent event could be coalesced with the
452 .I oldest
453 unread event.
454 .SH SEE ALSO
455 .BR inotify_add_watch (2),
456 .BR inotify_init (2),
457 .BR inotify_init1 (2),
458 .BR inotify_rm_watch (2),
459 .BR read (2),
460 .BR stat (2)
461
462 .IR Documentation/filesystems/inotify.txt
463 in the Linux kernel source tree