OSDN Git Service

f36204a86b090b942f5f4880fb289e292b5f6524
[linuxjm/LDP_man-pages.git] / original / man7 / inotify.7
1 '\" t
2 .\" Copyright (C) 2006, 2014 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-05-08 "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 .IP * 3
40 .BR inotify_init (2)
41 creates an inotify instance and returns a file descriptor
42 referring to the inotify instance.
43 The more recent
44 .BR inotify_init1 (2)
45 is like
46 .BR inotify_init (2),
47 but has a
48 .IR flags
49 argument that provides access to some extra functionality.
50 .IP *
51 .BR inotify_add_watch (2)
52 manipulates the "watch list" associated with an inotify instance.
53 Each item ("watch") in the watch list specifies the pathname of
54 a file or directory,
55 along with some set of events that the kernel should monitor for the
56 file referred to by that pathname.
57 .BR inotify_add_watch (2)
58 either creates a new watch item, or modifies an existing watch.
59 Each watch has a unique "watch descriptor", an integer
60 returned by
61 .BR inotify_add_watch (2)
62 when the watch is created.
63 .IP *
64 When events occur for monitored files and directories,
65 those events are made available to the application as structured data that
66 can be read from the inotify file descriptor using
67 .BR read (2)
68 (see below).
69 .IP *
70 .BR inotify_rm_watch (2)
71 removes an item from an inotify watch list.
72 .IP *
73 When all file descriptors referring to an inotify
74 instance have been closed (using
75 .BR close (2)),
76 the underlying object and its resources are
77 freed for reuse by the kernel;
78 all associated watches are automatically freed.
79
80 With careful programming,
81 an application can use inotify to efficiently monitor and cache
82 the state of a set of filesystem objects.
83 However, robust applications should allow for the fact that bugs
84 in the monitoring logic or races of the kind described below
85 may leave the cache inconsistent with the filesystem state.
86 It is probably wise to to do some consistency checking,
87 and rebuild the cache when inconsistencies are detected.
88 .SS Reading events from an inotify file descriptor
89 To determine what events have occurred, an application
90 .BR read (2)s
91 from the inotify file descriptor.
92 If no events have so far occurred, then,
93 assuming a blocking file descriptor,
94 .BR read (2)
95 will block until at least one event occurs
96 (unless interrupted by a signal,
97 in which case the call fails with the error
98 .BR EINTR ;
99 see
100 .BR signal (7)).
101
102 Each successful
103 .BR read (2)
104 returns a buffer containing one or more of the following structures:
105 .in +4n
106 .nf
107
108 struct inotify_event {
109     int      wd;       /* Watch descriptor */
110 .\" FIXME . The type of the 'wd' field should probably be "int32_t".
111 .\" I submitted a patch to fix this.  See the LKML thread
112 .\" "[patch] Fix type errors in inotify interfaces", 18 Nov 2008
113 .\" Glibc bug filed: http://sources.redhat.com/bugzilla/show_bug.cgi?id=7040
114     uint32_t mask;     /* Mask of events */
115     uint32_t cookie;   /* Unique cookie associating related
116                           events (for rename(2)) */
117     uint32_t len;      /* Size of \fIname\fP field */
118     char     name[];   /* Optional null-terminated name */
119 };
120 .fi
121 .in
122
123 .I wd
124 identifies the watch for which this event occurs.
125 It is one of the watch descriptors returned by a previous call to
126 .BR inotify_add_watch (2).
127
128 .I mask
129 contains bits that describe the event that occurred (see below).
130
131 .I cookie
132 is a unique integer that connects related events.
133 Currently this is used only for rename events, and
134 allows the resulting pair of
135 .B IN_MOVED_FROM
136 and
137 .B IN_MOVED_TO
138 events to be connected by the application.
139 For all other event types,
140 .I cookie
141 is set to 0.
142
143 The
144 .I name
145 field is present only when an event is returned
146 for a file inside a watched directory;
147 it identifies the file pathname relative to the watched directory.
148 This pathname is null-terminated,
149 and may include further null bytes (\(aq\\0\(aq) to align subsequent reads to a
150 suitable address boundary.
151
152 The
153 .I len
154 field counts all of the bytes in
155 .IR name ,
156 including the null bytes;
157 the length of each
158 .I inotify_event
159 structure is thus
160 .IR "sizeof(struct inotify_event)+len" .
161
162 The behavior when the buffer given to
163 .BR read (2)
164 is too small to return information about the next event depends
165 on the kernel version: in kernels before 2.6.21,
166 .BR read (2)
167 returns 0; since kernel 2.6.21,
168 .BR read (2)
169 fails with the error
170 .BR EINVAL .
171 Specifying a buffer of size
172
173     sizeof(struct inotify_event) + NAME_MAX + 1
174
175 will be sufficient to read at least one event.
176 .SS inotify events
177 The
178 .BR inotify_add_watch (2)
179 .I mask
180 argument and the
181 .I mask
182 field of the
183 .I inotify_event
184 structure returned when
185 .BR read (2)ing
186 an inotify file descriptor are both bit masks identifying
187 inotify events.
188 The following bits can be specified in
189 .I mask
190 when calling
191 .BR inotify_add_watch (2)
192 and may be returned in the
193 .I mask
194 field returned by
195 .BR read (2):
196 .RS 4
197 .TP
198 .BR IN_ACCESS " (*)"
199 File was accessed (e.g.,
200 .BR read (2),
201 .BR execve (2)).
202 .TP
203 .BR IN_ATTRIB " (*)"
204 Metadata changed\(emfor example, permissions (e.g.,
205 .BR chmod (2)),
206 timestamps (e.g.,
207 .BR utimensat (2)),
208 extended attributes
209 .RB ( setxattr (2)),
210 link count (since Linux 2.6.25; e.g.,
211 for the target of
212 .BR link (2)
213 and for
214 .BR unlink (2)),
215 and user/group ID (e.g.,
216 .BR chown (2)).
217 .TP
218 .BR IN_CLOSE_WRITE " (*)"
219 File opened for writing was closed.
220 .TP
221 .BR IN_CLOSE_NOWRITE " (*)"
222 File not opened for writing was closed.
223 .TP
224 .BR IN_CREATE " (*)"
225 File/directory created in watched directory (e.g.,
226 .BR open (2)
227 .BR O_CREAT ,
228 .BR mkdir (2),
229 .BR link (2),
230 .BR symlink (2),
231 .BR bind (2)
232 on a UNIX domain socket).
233 .TP
234 .BR IN_DELETE " (*)"
235 File/directory deleted from watched directory.
236 .TP
237 .B IN_DELETE_SELF
238 Watched file/directory was itself deleted.
239 (This event also occurs if an object is moved to another filesystem,
240 since
241 .BR mv (1)
242 in effect copies the file to the other filesystem and
243 then deletes it from the original filesystem.)
244 In addition, an
245 .B IN_IGNORED
246 event will subsequently be generated for the watch descriptor.
247 .TP
248 .BR IN_MODIFY " (*)"
249 File was modified (e.g.,
250 .BR write (2),
251 .BR truncate (2)).
252 .TP
253 .B IN_MOVE_SELF
254 Watched file/directory was itself moved.
255 .TP
256 .BR IN_MOVED_FROM " (*)"
257 Generated for the directory containing the old filename
258 when a file is renamed.
259 .TP
260 .BR IN_MOVED_TO " (*)"
261 Generated for the directory containing the new filename
262 when a file is renamed.
263 .TP
264 .BR IN_OPEN " (*)"
265 File was opened.
266 .RE
267 .PP
268 When monitoring a directory,
269 the events marked with an asterisk (*) above can occur for
270 files in the directory, in which case the
271 .I name
272 field in the returned
273 .I inotify_event
274 structure identifies the name of the file within the directory.
275 .PP
276 The
277 .B IN_ALL_EVENTS
278 macro is defined as a bit mask of all of the above events.
279 This macro can be used as the
280 .I mask
281 argument when calling
282 .BR inotify_add_watch (2).
283
284 Two additional convenience macros are defined:
285 .RS 4
286 .TP
287 .BR IN_MOVE
288 Equates to
289 .BR "IN_MOVED_FROM | IN_MOVED_TO" .
290 .TP
291 .BR IN_CLOSE
292 Equates to
293 .BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" .
294 .RE
295 .PP
296 The following further bits can be specified in
297 .I mask
298 when calling
299 .BR inotify_add_watch (2):
300 .RS 4
301 .TP
302 .BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
303 Don't dereference
304 .I pathname
305 if it is a symbolic link.
306 .TP
307 .BR IN_EXCL_UNLINK " (since Linux 2.6.36)"
308 .\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
309 By default, when watching events on the children of a directory,
310 events are generated for children even after they have been unlinked
311 from the directory.
312 This can result in large numbers of uninteresting events for
313 some applications (e.g., if watching
314 .IR /tmp ,
315 in which many applications create temporary files whose
316 names are immediately unlinked).
317 Specifying
318 .B IN_EXCL_UNLINK
319 changes the default behavior,
320 so that events are not generated for children after
321 they have been unlinked from the watched directory.
322 .TP
323 .B IN_MASK_ADD
324 Add (OR) events to watch mask for this pathname if
325 it already exists (instead of replacing mask).
326 .TP
327 .B IN_ONESHOT
328 Monitor
329 .I pathname
330 for one event, then remove from
331 watch list.
332 .TP
333 .BR IN_ONLYDIR " (since Linux 2.6.15)"
334 Only watch
335 .I pathname
336 if it is a directory.
337 .RE
338 .PP
339 The following bits may be set in the
340 .I mask
341 field returned by
342 .BR read (2):
343 .RS 4
344 .TP
345 .B IN_IGNORED
346 Watch was removed explicitly
347 .RB ( inotify_rm_watch (2))
348 or automatically (file was deleted, or filesystem was unmounted).
349 See also BUGS.
350 .TP
351 .B IN_ISDIR
352 Subject of this event is a directory.
353 .TP
354 .B IN_Q_OVERFLOW
355 Event queue overflowed
356 .RI ( wd
357 is \-1 for this event).
358 .TP
359 .B IN_UNMOUNT
360 Filesystem containing watched object was unmounted.
361 In addition, an
362 .B IN_IGNORED
363 event will subsequently be generated for the watch descriptor.
364 .RE
365 .SS Examples
366 Suppose an application is watching the directory
367 .I dir
368 and the file
369 .IR dir/myfile
370 for all events.
371 The examples below show some events that will be generated
372 for these two objects.
373 .RS 4
374 .TP
375 fd = open("dir/myfile", O_RDWR);
376 Generates
377 .B IN_OPEN
378 events for both
379 .I dir
380 and
381 .IR dir/myfile .
382 .TP
383 read(fd, buf, count);
384 Generates
385 .B IN_ACCESS
386 events for both
387 .I dir
388 and
389 .IR dir/myfile .
390 .TP
391 write(fd, buf, count);
392 Generates
393 .B IN_MODIFY
394 events for both
395 .I dir
396 and
397 .IR dir/myfile .
398 .TP
399 fchmod(fd, mode);
400 Generates
401 .B IN_ATTRIB
402 events for both
403 .I dir
404 and
405 .IR dir/myfile .
406 .TP
407 close(fd);
408 Generates
409 .B IN_CLOSE_WRITE
410 events for both
411 .I dir
412 and
413 .IR dir/myfile .
414 .RE
415 .PP
416 Suppose an application is watching the directories
417 .I dir1
418 and
419 .IR dir2 ,
420 and the file
421 .IR dir1/myfile .
422 The following examples show some events that may be generated.
423 .RS 4
424 .TP
425 link("dir1/myfile", "dir2/new");
426 Generates an
427 .B IN_ATTRIB
428 event for
429 .IR myfile
430 and an
431 .B IN_CREATE
432 event for
433 .IR dir2 .
434 .TP
435 rename("dir1/myfile", "dir2/myfile");
436 Generates an
437 .B IN_MOVED_FROM
438 event for
439 .IR dir1 ,
440 an
441 .B IN_MOVED_TO
442 event for
443 .IR dir2 ,
444 and an
445 .B IN_MOVE_SELF
446 event for
447 .IR myfile .
448 The
449 .B IN_MOVED_FROM
450 and
451 .B IN_MOVED_TO
452 events will have the same
453 .I cookie
454 value.
455 .RE
456 .PP
457 Suppose that
458 .IR dir1/xx
459 and
460 .IR dir2/yy
461 are (the only) links to the same file, and an application is watching
462 .IR dir1 ,
463 .IR dir2 ,
464 .IR dir1/xx ,
465 and
466 .IR dir2/yy .
467 Executing the following calls in the order given below will generate
468 the following events:
469 .RS 4
470 .TP
471 unlink("dir2/yy");
472 Generates an
473 .BR IN_ATTRIB
474 event for
475 .IR xx
476 (because its link count changes)
477 and an
478 .B IN_DELETE
479 event for
480 .IR dir2 .
481 .TP
482 unlink("dir1/xx");
483 Generates
484 .BR IN_ATTRIB ,
485 .BR IN_DELETE_SELF ,
486 and
487 .BR IN_IGNORED
488 events for
489 .IR xx ,
490 and an
491 .BR IN_DELETE
492 event for
493 .IR dir1 .
494 .RE
495 .PP
496 Suppose an application is watching the directory
497 .IR dir
498 and (the empty) directory
499 .IR dir/subdir .
500 The following examples show some events that may be generated.
501 .RS 4
502 .TP
503 mkdir("dir/new", mode);
504 Generates an
505 .B "IN_CREATE | IN_ISDIR"
506 event for
507 .IR dir .
508 .TP
509 rmdir("dir/subdir");
510 Generates
511 .B IN_DELETE_SELF
512 and
513 .B IN_IGNORED
514 events for
515 .IR subdir ,
516 and an
517 .B "IN_DELETE | IN_ISDIR"
518 event for
519 .IR dir .
520 .RE
521 .SS /proc interfaces
522 The following interfaces can be used to limit the amount of
523 kernel memory consumed by inotify:
524 .TP
525 .I /proc/sys/fs/inotify/max_queued_events
526 The value in this file is used when an application calls
527 .BR inotify_init (2)
528 to set an upper limit on the number of events that can be
529 queued to the corresponding inotify instance.
530 Events in excess of this limit are dropped, but an
531 .B IN_Q_OVERFLOW
532 event is always generated.
533 .TP
534 .I /proc/sys/fs/inotify/max_user_instances
535 This specifies an upper limit on the number of inotify instances
536 that can be created per real user ID.
537 .TP
538 .I /proc/sys/fs/inotify/max_user_watches
539 This specifies an upper limit on the number of watches
540 that can be created per real user ID.
541 .SH VERSIONS
542 Inotify was merged into the 2.6.13 Linux kernel.
543 The required library interfaces were added to glibc in version 2.4.
544 .RB ( IN_DONT_FOLLOW ,
545 .BR IN_MASK_ADD ,
546 and
547 .B IN_ONLYDIR
548 were added in glibc version 2.5.)
549 .SH CONFORMING TO
550 The inotify API is Linux-specific.
551 .SH NOTES
552 Inotify file descriptors can be monitored using
553 .BR select (2),
554 .BR poll (2),
555 and
556 .BR epoll (7).
557 When an event is available, the file descriptor indicates as readable.
558
559 Since Linux 2.6.25,
560 signal-driven I/O notification is available for inotify file descriptors;
561 see the discussion of
562 .B F_SETFL
563 (for setting the
564 .B O_ASYNC
565 flag),
566 .BR F_SETOWN ,
567 and
568 .B F_SETSIG
569 in
570 .BR fcntl (2).
571 The
572 .I siginfo_t
573 structure (described in
574 .BR sigaction (2))
575 that is passed to the signal handler has the following fields set:
576 .IR si_fd
577 is set to the inotify file descriptor number;
578 .IR si_signo
579 is set to the signal number;
580 .IR si_code
581 is set to
582 .BR POLL_IN ;
583 and
584 .B POLLIN
585 is set in
586 .IR si_band .
587
588 If successive output inotify events produced on the
589 inotify file descriptor are identical (same
590 .IR wd ,
591 .IR mask ,
592 .IR cookie ,
593 and
594 .IR name ),
595 then they are coalesced into a single event if the
596 older event has not yet been read (but see BUGS).
597 This reduces the amount of kernel memory required for the event queue,
598 but also means that an application can't use inotify to reliably count
599 file events.
600
601 The events returned by reading from an inotify file descriptor
602 form an ordered queue.
603 Thus, for example, it is guaranteed that when renaming from
604 one directory to another, events will be produced in the
605 correct order on the inotify file descriptor.
606
607 The
608 .B FIONREAD
609 .BR ioctl (2)
610 returns the number of bytes available to read from an
611 inotify file descriptor.
612 .SS Limitations and caveats
613 The inotify API provides no information about the user or process that
614 triggered the inotify event.
615 In particular, there is no easy
616 way for a process that is monitoring events via inotify
617 to distinguish events that it triggers
618 itself from those that are triggered by other processes.
619
620 Inotify reports only events that a user-space program triggers through
621 the filesystem API.
622 As a result, it does not catch remote events that occur
623 on network filesystems.
624 (Applications must fall back to polling the filesystem
625 to catch such events.)
626 Furthermore, various pseudo-filesystems such as
627 .IR /proc ,
628 .IR /sys ,
629 and
630 .IR /dev/pts
631 are not monitorable with inotify.
632
633 The inotify API does not report file accesses and modifications that
634 may occur because of
635 .BR mmap (2)
636 and
637 .BR msync (2).
638
639 The inotify API identifies affected files by filename.
640 However, by the time an application processes an inotify event,
641 the filename may already have been deleted or renamed.
642
643 The inotify API identifies events via watch descriptors.
644 It is the application's responsibility to cache a mapping
645 (if one is needed) between watch descriptors and pathnames.
646 Be aware that directory renamings may affect multiple cached pathnames.
647
648 Inotify monitoring of directories is not recursive:
649 to monitor subdirectories under a directory,
650 additional watches must be created.
651 This can take a significant amount time for large directory trees.
652
653 If monitoring an entire directory subtree,
654 and a new subdirectory is created in that tree or an existing directory
655 is renamed into that tree,
656 be aware that by the time you create a watch for the new subdirectory,
657 new files (and subdirectories) may already exist inside the subdirectory.
658 Therefore, you may want to scan the contents of the subdirectory
659 immediately after adding the watch (and, if desired,
660 recursively add watches for any subdirectories that it contains).
661
662 Note that the event queue can overflow.
663 In this case, events are lost.
664 Robust applications should handle the possibility of
665 lost events gracefully.
666 For example, it may be necessary to rebuild part or all of
667 the application cache.
668 (One simple, but possibly expensive,
669 approach is to close the inotify file descriptor, empty the cache,
670 create a new inotify file descriptor,
671 and then re-create watches and cache entries
672 for the objects to be monitored.)
673 .SS Dealing with rename() events
674 As noted above, the
675 .B IN_MOVED_FROM
676 and
677 .B IN_MOVED_TO
678 event pair that is generated by
679 .BR rename (2)
680 can be matched up via their shared cookie value.
681 However, the task of matching has some challenges.
682
683 These two events are usually consecutive in the event stream available
684 when reading from the inotify file descriptor.
685 However, this is not guaranteed.
686 If multiple processes are triggering events for monitored objects,
687 then (on rare occasions) an arbitrary number of
688 other events may appear between the
689 .B IN_MOVED_FROM
690 and
691 .B IN_MOVED_TO
692 events.
693
694 Matching up the
695 .B IN_MOVED_FROM
696 and
697 .B IN_MOVED_TO
698 event pair generated by
699 .BR rename (2)
700 is thus inherently racy.
701 (Don't forget that if an object is renamed outside of a monitored directory,
702 there may not even be an
703 .BR IN_MOVED_TO
704 event.)
705 Heuristic approaches (e.g., assume the events are always consecutive)
706 can be used to ensure a match in most cases,
707 but will inevitably miss some cases,
708 causing the application to perceive the
709 .B IN_MOVED_FROM
710 and
711 .B IN_MOVED_TO
712 events as being unrelated.
713 If watch descriptors are destroyed and re-created as a result,
714 then those watch descriptors will be inconsistent with
715 the watch descriptors in any pending events.
716 (Re-creating the inotify file descriptor and rebuilding the cache may
717 be useful to deal with this scenario.)
718
719 Applications should also allow for the possibility that the
720 .B IN_MOVED_FROM
721 event was the last event that could fit in the buffer
722 returned by the current call to
723 .BR read (2),
724 and the accompanying
725 .B IN_MOVED_TO
726 event might be fetched only on the next
727 .BR read (2).
728 .SH BUGS
729 .\" FIXME kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
730 .\" implies that unmount events were buggy 2.6.11 to 2.6.36
731 .\"
732 In kernels before 2.6.16, the
733 .B IN_ONESHOT
734 .I mask
735 flag does not work.
736
737 As originally designed and implemented, the
738 .B IN_ONESHOT
739 flag did not cause an
740 .B IN_IGNORED
741 event to be generated when the watch was dropped after one event.
742 However, as an unintended effect of other changes,
743 since Linux 2.6.36, an
744 .B IN_IGNORED
745 event is generated in this case.
746
747 Before kernel 2.6.25,
748 .\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2
749 the kernel code that was intended to coalesce successive identical events
750 (i.e., the two most recent events could potentially be coalesced
751 if the older had not yet been read)
752 instead checked if the most recent event could be coalesced with the
753 .I oldest
754 unread event.
755 .SH SEE ALSO
756 .BR inotifywait (1),
757 .BR inotifywatch (1),
758 .BR inotify_add_watch (2),
759 .BR inotify_init (2),
760 .BR inotify_init1 (2),
761 .BR inotify_rm_watch (2),
762 .BR read (2),
763 .BR stat (2),
764 .BR fanotify (7)
765
766 .IR Documentation/filesystems/inotify.txt
767 in the Linux kernel source tree
768 .SH COLOPHON
769 This page is part of release 3.67 of the Linux
770 .I man-pages
771 project.
772 A description of the project,
773 information about reporting bugs,
774 and the latest version of this page,
775 can be found at
776 \%http://www.kernel.org/doc/man\-pages/.