]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/inotify.7
inotify.7: Add examples of syscalls that trigger IN_CREAT
[thirdparty/man-pages.git] / 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 2014-03-28 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 inotify \- monitoring filesystem events
29 .SH DESCRIPTION
30 The
31 .I inotify
32 API provides a mechanism for monitoring filesystem 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 .TP
188 .BR IN_ACCESS " (*)"
189 File was accessed (e.g.,
190 .BR read (2),
191 .BR execve (2)).
192 .TP
193 .BR IN_ATTRIB " (*)"
194 Metadata changed\(emfor example, permissions (e.g.,
195 .BR chmod (2)),
196 timestamps (e.g.,
197 .BR utimensat (2)),
198 extended attributes
199 .RB ( setxattr (2)),
200 link count (since Linux 2.6.25; e.g.,
201 for the target of
202 .BR link (2)
203 and for
204 .BR unlink (2)),
205 and user/group ID (e.g.,
206 .BR chown (2)).
207 .TP
208 .BR IN_CLOSE_WRITE " (*)"
209 File opened for writing was closed.
210 .TP
211 .BR IN_CLOSE_NOWRITE " (*)"
212 File not opened for writing was closed.
213 .TP
214 .BR IN_CREATE " (*)"
215 File/directory created in watched directory (e.g.,
216 .BR open (2)
217 .BR O_CREAT ,
218 .BR mkdir (2),
219 .BR link (2),
220 .BR bind (2)
221 on a UNIX domain socket).
222 .TP
223 .BR IN_DELETE " (*)"
224 File/directory deleted from watched directory.
225 .TP
226 .B IN_DELETE_SELF
227 Watched file/directory was itself deleted.
228 (This event also occurs if an object is moved to another filesystem,
229 since
230 .BR mv (1)
231 in effect copies the file to the other filesystem and
232 then deletes it from the original filesystem.)
233 .TP
234 .BR IN_MODIFY " (*)"
235 File was modified (e.g.,
236 .BR write (2),
237 .BR truncate (2)).
238 .TP
239 .B IN_MOVE_SELF
240 Watched file/directory was itself moved.
241 .TP
242 .BR IN_MOVED_FROM " (*)"
243 Generated for the directory containing the old filename
244 when a file is renamed.
245 .TP
246 .BR IN_MOVED_TO " (*)"
247 Generated for the directory containing the new filename
248 when a file is renamed.
249 .TP
250 .BR IN_OPEN " (*)"
251 File was opened.
252 .RE
253 .PP
254 When monitoring a directory,
255 the events marked with an asterisk (*) above can occur for
256 files in the directory, in which case the
257 .I name
258 field in the returned
259 .I inotify_event
260 structure identifies the name of the file within the directory.
261 .PP
262 The
263 .B IN_ALL_EVENTS
264 macro is defined as a bit mask of all of the above events.
265 This macro can be used as the
266 .I mask
267 argument when calling
268 .BR inotify_add_watch (2).
269
270 Two additional convenience macros are defined:
271 .RS 4
272 .TP
273 .BR IN_MOVE
274 Equates to
275 .BR "IN_MOVED_FROM | IN_MOVED_TO" .
276 .TP
277 .BR IN_CLOSE
278 Equates to
279 .BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" .
280 .RE
281 .PP
282 The following further bits can be specified in
283 .I mask
284 when calling
285 .BR inotify_add_watch (2):
286 .RS 4
287 .TP
288 .BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
289 Don't dereference
290 .I pathname
291 if it is a symbolic link.
292 .TP
293 .BR IN_EXCL_UNLINK " (since Linux 2.6.36)"
294 .\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
295 By default, when watching events on the children of a directory,
296 events are generated for children even after they have been unlinked
297 from the directory.
298 This can result in large numbers of uninteresting events for
299 some applications (e.g., if watching
300 .IR /tmp ,
301 in which many applications create temporary files whose
302 names are immediately unlinked).
303 Specifying
304 .B IN_EXCL_UNLINK
305 changes the default behavior,
306 so that events are not generated for children after
307 they have been unlinked from the watched directory.
308 .TP
309 .B IN_MASK_ADD
310 Add (OR) events to watch mask for this pathname if
311 it already exists (instead of replacing mask).
312 .TP
313 .B IN_ONESHOT
314 Monitor
315 .I pathname
316 for one event, then remove from
317 watch list.
318 .TP
319 .BR IN_ONLYDIR " (since Linux 2.6.15)"
320 Only watch
321 .I pathname
322 if it is a directory.
323 .RE
324 .PP
325 The following bits may be set in the
326 .I mask
327 field returned by
328 .BR read (2):
329 .RS 4
330 .TP
331 .B IN_IGNORED
332 Watch was removed explicitly
333 .RB ( inotify_rm_watch (2))
334 or automatically (file was deleted, or filesystem was unmounted).
335 See also BUGS.
336 .TP
337 .B IN_ISDIR
338 Subject of this event is a directory.
339 .TP
340 .B IN_Q_OVERFLOW
341 Event queue overflowed
342 .RI ( wd
343 is \-1 for this event).
344 .TP
345 .B IN_UNMOUNT
346 Filesystem containing watched object was unmounted.
347 .RE
348 .SS /proc interfaces
349 The following interfaces can be used to limit the amount of
350 kernel memory consumed by inotify:
351 .TP
352 .I /proc/sys/fs/inotify/max_queued_events
353 The value in this file is used when an application calls
354 .BR inotify_init (2)
355 to set an upper limit on the number of events that can be
356 queued to the corresponding inotify instance.
357 Events in excess of this limit are dropped, but an
358 .B IN_Q_OVERFLOW
359 event is always generated.
360 .TP
361 .I /proc/sys/fs/inotify/max_user_instances
362 This specifies an upper limit on the number of inotify instances
363 that can be created per real user ID.
364 .TP
365 .I /proc/sys/fs/inotify/max_user_watches
366 This specifies an upper limit on the number of watches
367 that can be created per real user ID.
368 .SH VERSIONS
369 Inotify was merged into the 2.6.13 Linux kernel.
370 The required library interfaces were added to glibc in version 2.4.
371 .RB ( IN_DONT_FOLLOW ,
372 .BR IN_MASK_ADD ,
373 and
374 .B IN_ONLYDIR
375 were added in glibc version 2.5.)
376 .SH CONFORMING TO
377 The inotify API is Linux-specific.
378 .SH NOTES
379 Inotify file descriptors can be monitored using
380 .BR select (2),
381 .BR poll (2),
382 and
383 .BR epoll (7).
384 When an event is available, the file descriptor indicates as readable.
385
386 Since Linux 2.6.25,
387 signal-driven I/O notification is available for inotify file descriptors;
388 see the discussion of
389 .B F_SETFL
390 (for setting the
391 .B O_ASYNC
392 flag),
393 .BR F_SETOWN ,
394 and
395 .B F_SETSIG
396 in
397 .BR fcntl (2).
398 The
399 .I siginfo_t
400 structure (described in
401 .BR sigaction (2))
402 that is passed to the signal handler has the following fields set:
403 .IR si_fd
404 is set to the inotify file descriptor number;
405 .IR si_signo
406 is set to the signal number;
407 .IR si_code
408 is set to
409 .BR POLL_IN ;
410 and
411 .B POLLIN
412 is set in
413 .IR si_band .
414
415 If successive output inotify events produced on the
416 inotify file descriptor are identical (same
417 .IR wd ,
418 .IR mask ,
419 .IR cookie ,
420 and
421 .IR name ),
422 then they are coalesced into a single event if the
423 older event has not yet been read (but see BUGS).
424 This reduces the amount of kernel memory required for the event queue,
425 but also means that an application can't use inotify to reliably count
426 file events.
427
428 The events returned by reading from an inotify file descriptor
429 form an ordered queue.
430 Thus, for example, it is guaranteed that when renaming from
431 one directory to another, events will be produced in the
432 correct order on the inotify file descriptor.
433
434 The
435 .B FIONREAD
436 .BR ioctl (2)
437 returns the number of bytes available to read from an
438 inotify file descriptor.
439 .SS Limitations and caveats
440 Inotify monitoring of directories is not recursive:
441 to monitor subdirectories under a directory,
442 additional watches must be created.
443 This can take a significant amount time for large directory trees.
444
445 The inotify API provides no information about the user or process that
446 triggered the inotify event.
447 In particular, there is no easy
448 way for a process that is monitoring events via inotify
449 to distinguish events that it triggers
450 itself from those that are triggered by other processes.
451
452 Note that the event queue can overflow.
453 In this case, events are lost.
454 Robust applications should handle the possibility of
455 lost events gracefully.
456
457 The inotify API identifies affected files by filename.
458 However, by the time an application processes an inotify event,
459 the filename may already have been deleted or renamed.
460
461 If monitoring an entire directory subtree,
462 and a new subdirectory is created in that tree or an existing directory
463 is renamed into that tree,
464 be aware that by the time you create a watch for the new subdirectory,
465 new files (and subdirectories) may already exist inside the subdirectory.
466 Therefore, you may want to scan the contents of the subdirectory
467 immediately after adding the watch (and, if desired,
468 recursively add watches for any subdirectories that it contains).
469
470 The inotify applications identifies events via watch descriptors.
471 It is the application's responsibility to cache a mapping
472 (if one is needed) between watch descriptors and pathnames.
473 Be aware that directory renamings may affect multiple cached pathnames.
474 .SH BUGS
475 .\" FIXME kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
476 .\" implies that unmount events were buggy 2.6.11 to 2.6.36
477 .\"
478 In kernels before 2.6.16, the
479 .B IN_ONESHOT
480 .I mask
481 flag does not work.
482
483 As originally designed and implemented, the
484 .B IN_ONESHOT
485 flag did not cause an
486 .B IN_IGNORED
487 event to be generated when the watch was dropped after one event.
488 However, as an unintended effect of other changes,
489 since Linux 2.6.36, an
490 .B IN_IGNORED
491 event is generated in this case.
492
493 Before kernel 2.6.25,
494 .\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2
495 the kernel code that was intended to coalesce successive identical events
496 (i.e., the two most recent events could potentially be coalesced
497 if the older had not yet been read)
498 instead checked if the most recent event could be coalesced with the
499 .I oldest
500 unread event.
501 .SH SEE ALSO
502 .BR inotifywait (1),
503 .BR inotifywatch (1),
504 .BR inotify_add_watch (2),
505 .BR inotify_init (2),
506 .BR inotify_init1 (2),
507 .BR inotify_rm_watch (2),
508 .BR read (2),
509 .BR stat (2)
510
511 .IR Documentation/filesystems/inotify.txt
512 in the Linux kernel source tree