]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/inotify.7
man*/: srcfix (Use .P instead of .PP or .LP)
[thirdparty/man-pages.git] / man7 / inotify.7
1 .\" Copyright (C) 2006, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" Copyright (C) 2014 Heinrich Schuchardt <xypron.glpk@gmx.de>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH inotify 7 (date) "Linux man-pages (unreleased)"
7 .SH NAME
8 inotify \- monitoring filesystem events
9 .SH DESCRIPTION
10 The
11 .I inotify
12 API provides a mechanism for monitoring filesystem events.
13 Inotify can be used to monitor individual files,
14 or to monitor directories.
15 When a directory is monitored, inotify will return events
16 for the directory itself, and for files inside the directory.
17 .P
18 The following system calls are used with this API:
19 .IP \[bu] 3
20 .BR inotify_init (2)
21 creates an inotify instance and returns a file descriptor
22 referring to the inotify instance.
23 The more recent
24 .BR inotify_init1 (2)
25 is like
26 .BR inotify_init (2),
27 but has a
28 .I flags
29 argument that provides access to some extra functionality.
30 .IP \[bu]
31 .BR inotify_add_watch (2)
32 manipulates the "watch list" associated with an inotify instance.
33 Each item ("watch") in the watch list specifies the pathname of
34 a file or directory,
35 along with some set of events that the kernel should monitor for the
36 file referred to by that pathname.
37 .BR inotify_add_watch (2)
38 either creates a new watch item, or modifies an existing watch.
39 Each watch has a unique "watch descriptor", an integer
40 returned by
41 .BR inotify_add_watch (2)
42 when the watch is created.
43 .IP \[bu]
44 When events occur for monitored files and directories,
45 those events are made available to the application as structured data that
46 can be read from the inotify file descriptor using
47 .BR read (2)
48 (see below).
49 .IP \[bu]
50 .BR inotify_rm_watch (2)
51 removes an item from an inotify watch list.
52 .IP \[bu]
53 When all file descriptors referring to an inotify
54 instance have been closed (using
55 .BR close (2)),
56 the underlying object and its resources are
57 freed for reuse by the kernel;
58 all associated watches are automatically freed.
59 .P
60 With careful programming,
61 an application can use inotify to efficiently monitor and cache
62 the state of a set of filesystem objects.
63 However, robust applications should allow for the fact that bugs
64 in the monitoring logic or races of the kind described below
65 may leave the cache inconsistent with the filesystem state.
66 It is probably wise to do some consistency checking,
67 and rebuild the cache when inconsistencies are detected.
68 .SS Reading events from an inotify file descriptor
69 To determine what events have occurred, an application
70 .BR read (2)s
71 from the inotify file descriptor.
72 If no events have so far occurred, then,
73 assuming a blocking file descriptor,
74 .BR read (2)
75 will block until at least one event occurs
76 (unless interrupted by a signal,
77 in which case the call fails with the error
78 .BR EINTR ;
79 see
80 .BR signal (7)).
81 .P
82 Each successful
83 .BR read (2)
84 returns a buffer containing one or more of the following structures:
85 .P
86 .in +4n
87 .EX
88 struct inotify_event {
89 int wd; /* Watch descriptor */
90 .\" FIXME . The type of the 'wd' field should probably be "int32_t".
91 .\" I submitted a patch to fix this. See the LKML thread
92 .\" "[patch] Fix type errors in inotify interfaces", 18 Nov 2008
93 .\" glibc bug filed: https://www.sourceware.org/bugzilla/show_bug.cgi?id=7040
94 uint32_t mask; /* Mask describing event */
95 uint32_t cookie; /* Unique cookie associating related
96 events (for rename(2)) */
97 uint32_t len; /* Size of \fIname\fP field */
98 char name[]; /* Optional null\-terminated name */
99 };
100 .EE
101 .in
102 .P
103 .I wd
104 identifies the watch for which this event occurs.
105 It is one of the watch descriptors returned by a previous call to
106 .BR inotify_add_watch (2).
107 .P
108 .I mask
109 contains bits that describe the event that occurred (see below).
110 .P
111 .I cookie
112 is a unique integer that connects related events.
113 Currently, this is used only for rename events, and
114 allows the resulting pair of
115 .B IN_MOVED_FROM
116 and
117 .B IN_MOVED_TO
118 events to be connected by the application.
119 For all other event types,
120 .I cookie
121 is set to 0.
122 .P
123 The
124 .I name
125 field is present only when an event is returned
126 for a file inside a watched directory;
127 it identifies the filename within the watched directory.
128 This filename is null-terminated,
129 and may include further null bytes (\[aq]\e0\[aq])
130 to align subsequent reads to a suitable address boundary.
131 .P
132 The
133 .I len
134 field counts all of the bytes in
135 .IR name ,
136 including the null bytes;
137 the length of each
138 .I inotify_event
139 structure is thus
140 .IR "sizeof(struct inotify_event)+len" .
141 .P
142 The behavior when the buffer given to
143 .BR read (2)
144 is too small to return information about the next event depends
145 on the kernel version: before Linux 2.6.21,
146 .BR read (2)
147 returns 0; since Linux 2.6.21,
148 .BR read (2)
149 fails with the error
150 .BR EINVAL .
151 Specifying a buffer of size
152 .P
153 .in +4n
154 .EX
155 sizeof(struct inotify_event) + NAME_MAX + 1
156 .EE
157 .in
158 .P
159 will be sufficient to read at least one event.
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 .TP
182 .BR IN_ACCESS " (+)"
183 File was accessed (e.g.,
184 .BR read (2),
185 .BR execve (2)).
186 .TP
187 .BR IN_ATTRIB " (*)"
188 Metadata changed\[em]for example, permissions (e.g.,
189 .BR chmod (2)),
190 timestamps (e.g.,
191 .BR utimensat (2)),
192 extended attributes
193 .RB ( setxattr (2)),
194 link count (since Linux 2.6.25; e.g.,
195 .\" FIXME .
196 .\" Events do not occur for link count changes on a file inside a monitored
197 .\" directory. This differs from other metadata changes for files inside
198 .\" a monitored directory.
199 for the target of
200 .BR link (2)
201 and for
202 .BR unlink (2)),
203 and user/group ID (e.g.,
204 .BR chown (2)).
205 .TP
206 .BR IN_CLOSE_WRITE " (+)"
207 File opened for writing was closed.
208 .TP
209 .BR IN_CLOSE_NOWRITE " (*)"
210 File or directory not opened for writing was closed.
211 .TP
212 .BR IN_CREATE " (+)"
213 File/directory created in watched directory (e.g.,
214 .BR open (2)
215 .BR O_CREAT ,
216 .BR mkdir (2),
217 .BR link (2),
218 .BR symlink (2),
219 .BR bind (2)
220 on a UNIX domain socket).
221 .TP
222 .BR IN_DELETE " (+)"
223 File/directory deleted from watched directory.
224 .TP
225 .B IN_DELETE_SELF
226 Watched file/directory was itself deleted.
227 (This event also occurs if an object is moved to another filesystem,
228 since
229 .BR mv (1)
230 in effect copies the file to the other filesystem and
231 then deletes it from the original filesystem.)
232 In addition, an
233 .B IN_IGNORED
234 event will subsequently be generated for the watch descriptor.
235 .TP
236 .BR IN_MODIFY " (+)"
237 File was modified (e.g.,
238 .BR write (2),
239 .BR truncate (2)).
240 .TP
241 .B IN_MOVE_SELF
242 Watched file/directory was itself moved.
243 .TP
244 .BR IN_MOVED_FROM " (+)"
245 Generated for the directory containing the old filename
246 when a file is renamed.
247 .TP
248 .BR IN_MOVED_TO " (+)"
249 Generated for the directory containing the new filename
250 when a file is renamed.
251 .TP
252 .BR IN_OPEN " (*)"
253 File or directory was opened.
254 .RE
255 .P
256 Inotify monitoring is inode-based: when monitoring a file
257 (but not when monitoring the directory containing a file),
258 an event can be generated for activity on any link to the file
259 (in the same or a different directory).
260 .P
261 When monitoring a directory:
262 .IP \[bu] 3
263 the events marked above with an asterisk (*) can occur both
264 for the directory itself and for objects inside the directory; and
265 .IP \[bu]
266 the events marked with a plus sign (+) occur only for objects
267 inside the directory (not for the directory itself).
268 .P
269 .IR Note :
270 when monitoring a directory,
271 events are not generated for the files inside the directory
272 when the events are performed via a pathname (i.e., a link)
273 that lies outside the monitored directory.
274 .P
275 When events are generated for objects inside a watched directory, the
276 .I name
277 field in the returned
278 .I inotify_event
279 structure identifies the name of the file within the directory.
280 .P
281 The
282 .B IN_ALL_EVENTS
283 macro is defined as a bit mask of all of the above events.
284 This macro can be used as the
285 .I mask
286 argument when calling
287 .BR inotify_add_watch (2).
288 .P
289 Two additional convenience macros are defined:
290 .RS 4
291 .TP
292 .B IN_MOVE
293 Equates to
294 .BR "IN_MOVED_FROM | IN_MOVED_TO" .
295 .TP
296 .B IN_CLOSE
297 Equates to
298 .BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" .
299 .RE
300 .P
301 The following further bits can be specified in
302 .I mask
303 when calling
304 .BR inotify_add_watch (2):
305 .RS 4
306 .TP
307 .BR IN_DONT_FOLLOW " (since Linux 2.6.15)"
308 Don't dereference
309 .I pathname
310 if it is a symbolic link.
311 .TP
312 .BR IN_EXCL_UNLINK " (since Linux 2.6.36)"
313 .\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6
314 By default, when watching events on the children of a directory,
315 events are generated for children even after they have been unlinked
316 from the directory.
317 This can result in large numbers of uninteresting events for
318 some applications (e.g., if watching
319 .IR /tmp ,
320 in which many applications create temporary files whose
321 names are immediately unlinked).
322 Specifying
323 .B IN_EXCL_UNLINK
324 changes the default behavior,
325 so that events are not generated for children after
326 they have been unlinked from the watched directory.
327 .TP
328 .B IN_MASK_ADD
329 If a watch instance already exists for the filesystem object corresponding to
330 .IR pathname ,
331 add (OR) the events in
332 .I mask
333 to the watch mask (instead of replacing the mask);
334 the error
335 .B EINVAL
336 results if
337 .B IN_MASK_CREATE
338 is also specified.
339 .TP
340 .B IN_ONESHOT
341 Monitor the filesystem object corresponding to
342 .I pathname
343 for one event, then remove from
344 watch list.
345 .TP
346 .BR IN_ONLYDIR " (since Linux 2.6.15)"
347 Watch
348 .I pathname
349 only if it is a directory;
350 the error
351 .B ENOTDIR
352 results if
353 .I pathname
354 is not a directory.
355 Using this flag provides an application with a race-free way of
356 ensuring that the monitored object is a directory.
357 .TP
358 .BR IN_MASK_CREATE " (since Linux 4.18)"
359 Watch
360 .I pathname
361 only if it does not already have a watch associated with it;
362 the error
363 .B EEXIST
364 results if
365 .I pathname
366 is already being watched.
367 .IP
368 Using this flag provides an application with a way of ensuring
369 that new watches do not modify existing ones.
370 This is useful because multiple paths may refer to the same inode,
371 and multiple calls to
372 .BR inotify_add_watch (2)
373 without this flag may clobber existing watch masks.
374 .RE
375 .P
376 The following bits may be set in the
377 .I mask
378 field returned by
379 .BR read (2):
380 .RS 4
381 .TP
382 .B IN_IGNORED
383 Watch was removed explicitly
384 .RB ( inotify_rm_watch (2))
385 or automatically (file was deleted, or filesystem was unmounted).
386 See also BUGS.
387 .TP
388 .B IN_ISDIR
389 Subject of this event is a directory.
390 .TP
391 .B IN_Q_OVERFLOW
392 Event queue overflowed
393 .RI ( wd
394 is \-1 for this event).
395 .TP
396 .B IN_UNMOUNT
397 Filesystem containing watched object was unmounted.
398 In addition, an
399 .B IN_IGNORED
400 event will subsequently be generated for the watch descriptor.
401 .RE
402 .SS Examples
403 Suppose an application is watching the directory
404 .I dir
405 and the file
406 .I dir/myfile
407 for all events.
408 The examples below show some events that will be generated
409 for these two objects.
410 .RS 4
411 .TP
412 fd = open("dir/myfile", O_RDWR);
413 Generates
414 .B IN_OPEN
415 events for both
416 .I dir
417 and
418 .IR dir/myfile .
419 .TP
420 read(fd, buf, count);
421 Generates
422 .B IN_ACCESS
423 events for both
424 .I dir
425 and
426 .IR dir/myfile .
427 .TP
428 write(fd, buf, count);
429 Generates
430 .B IN_MODIFY
431 events for both
432 .I dir
433 and
434 .IR dir/myfile .
435 .TP
436 fchmod(fd, mode);
437 Generates
438 .B IN_ATTRIB
439 events for both
440 .I dir
441 and
442 .IR dir/myfile .
443 .TP
444 close(fd);
445 Generates
446 .B IN_CLOSE_WRITE
447 events for both
448 .I dir
449 and
450 .IR dir/myfile .
451 .RE
452 .P
453 Suppose an application is watching the directories
454 .I dir1
455 and
456 .IR dir2 ,
457 and the file
458 .IR dir1/myfile .
459 The following examples show some events that may be generated.
460 .RS 4
461 .TP
462 link("dir1/myfile", "dir2/new");
463 Generates an
464 .B IN_ATTRIB
465 event for
466 .I myfile
467 and an
468 .B IN_CREATE
469 event for
470 .IR dir2 .
471 .TP
472 rename("dir1/myfile", "dir2/myfile");
473 Generates an
474 .B IN_MOVED_FROM
475 event for
476 .IR dir1 ,
477 an
478 .B IN_MOVED_TO
479 event for
480 .IR dir2 ,
481 and an
482 .B IN_MOVE_SELF
483 event for
484 .IR myfile .
485 The
486 .B IN_MOVED_FROM
487 and
488 .B IN_MOVED_TO
489 events will have the same
490 .I cookie
491 value.
492 .RE
493 .P
494 Suppose that
495 .I dir1/xx
496 and
497 .I dir2/yy
498 are (the only) links to the same file, and an application is watching
499 .IR dir1 ,
500 .IR dir2 ,
501 .IR dir1/xx ,
502 and
503 .IR dir2/yy .
504 Executing the following calls in the order given below will generate
505 the following events:
506 .RS 4
507 .TP
508 unlink("dir2/yy");
509 Generates an
510 .B IN_ATTRIB
511 event for
512 .I xx
513 (because its link count changes)
514 and an
515 .B IN_DELETE
516 event for
517 .IR dir2 .
518 .TP
519 unlink("dir1/xx");
520 Generates
521 .BR IN_ATTRIB ,
522 .BR IN_DELETE_SELF ,
523 and
524 .B IN_IGNORED
525 events for
526 .IR xx ,
527 and an
528 .B IN_DELETE
529 event for
530 .IR dir1 .
531 .RE
532 .P
533 Suppose an application is watching the directory
534 .I dir
535 and (the empty) directory
536 .IR dir/subdir .
537 The following examples show some events that may be generated.
538 .RS 4
539 .TP
540 mkdir("dir/new", mode);
541 Generates an
542 .B "IN_CREATE | IN_ISDIR"
543 event for
544 .IR dir .
545 .TP
546 rmdir("dir/subdir");
547 Generates
548 .B IN_DELETE_SELF
549 and
550 .B IN_IGNORED
551 events for
552 .IR subdir ,
553 and an
554 .B "IN_DELETE | IN_ISDIR"
555 event for
556 .IR dir .
557 .RE
558 .SS /proc interfaces
559 The following interfaces can be used to limit the amount of
560 kernel memory consumed by inotify:
561 .TP
562 .I /proc/sys/fs/inotify/max_queued_events
563 The value in this file is used when an application calls
564 .BR inotify_init (2)
565 to set an upper limit on the number of events that can be
566 queued to the corresponding inotify instance.
567 Events in excess of this limit are dropped, but an
568 .B IN_Q_OVERFLOW
569 event is always generated.
570 .TP
571 .I /proc/sys/fs/inotify/max_user_instances
572 This specifies an upper limit on the number of inotify instances
573 that can be created per real user ID.
574 .TP
575 .I /proc/sys/fs/inotify/max_user_watches
576 This specifies an upper limit on the number of watches
577 that can be created per real user ID.
578 .SH STANDARDS
579 Linux.
580 .SH HISTORY
581 Inotify was merged into Linux 2.6.13.
582 The required library interfaces were added in glibc 2.4.
583 .RB ( IN_DONT_FOLLOW ,
584 .BR IN_MASK_ADD ,
585 and
586 .B IN_ONLYDIR
587 were added in glibc 2.5.)
588 .SH NOTES
589 Inotify file descriptors can be monitored using
590 .BR select (2),
591 .BR poll (2),
592 and
593 .BR epoll (7).
594 When an event is available, the file descriptor indicates as readable.
595 .P
596 Since Linux 2.6.25,
597 signal-driven I/O notification is available for inotify file descriptors;
598 see the discussion of
599 .B F_SETFL
600 (for setting the
601 .B O_ASYNC
602 flag),
603 .BR F_SETOWN ,
604 and
605 .B F_SETSIG
606 in
607 .BR fcntl (2).
608 The
609 .I siginfo_t
610 structure (described in
611 .BR sigaction (2))
612 that is passed to the signal handler has the following fields set:
613 .I si_fd
614 is set to the inotify file descriptor number;
615 .I si_signo
616 is set to the signal number;
617 .I si_code
618 is set to
619 .BR POLL_IN ;
620 and
621 .B POLLIN
622 is set in
623 .IR si_band .
624 .P
625 If successive output inotify events produced on the
626 inotify file descriptor are identical (same
627 .IR wd ,
628 .IR mask ,
629 .IR cookie ,
630 and
631 .IR name ),
632 then they are coalesced into a single event if the
633 older event has not yet been read (but see BUGS).
634 This reduces the amount of kernel memory required for the event queue,
635 but also means that an application can't use inotify to reliably count
636 file events.
637 .P
638 The events returned by reading from an inotify file descriptor
639 form an ordered queue.
640 Thus, for example, it is guaranteed that when renaming from
641 one directory to another, events will be produced in the
642 correct order on the inotify file descriptor.
643 .P
644 The set of watch descriptors that is being monitored via
645 an inotify file descriptor can be viewed via the entry for
646 the inotify file descriptor in the process's
647 .IR /proc/ pid /fdinfo
648 directory.
649 See
650 .BR proc (5)
651 for further details.
652 The
653 .B FIONREAD
654 .BR ioctl (2)
655 returns the number of bytes available to read from an
656 inotify file descriptor.
657 .SS Limitations and caveats
658 The inotify API provides no information about the user or process that
659 triggered the inotify event.
660 In particular, there is no easy
661 way for a process that is monitoring events via inotify
662 to distinguish events that it triggers
663 itself from those that are triggered by other processes.
664 .P
665 Inotify reports only events that a user-space program triggers through
666 the filesystem API.
667 As a result, it does not catch remote events that occur
668 on network filesystems.
669 (Applications must fall back to polling the filesystem
670 to catch such events.)
671 Furthermore, various pseudo-filesystems such as
672 .IR /proc ,
673 .IR /sys ,
674 and
675 .I /dev/pts
676 are not monitorable with inotify.
677 .P
678 The inotify API does not report file accesses and modifications that
679 may occur because of
680 .BR mmap (2),
681 .BR msync (2),
682 and
683 .BR munmap (2).
684 .P
685 The inotify API identifies affected files by filename.
686 However, by the time an application processes an inotify event,
687 the filename may already have been deleted or renamed.
688 .P
689 The inotify API identifies events via watch descriptors.
690 It is the application's responsibility to cache a mapping
691 (if one is needed) between watch descriptors and pathnames.
692 Be aware that directory renamings may affect multiple cached pathnames.
693 .P
694 Inotify monitoring of directories is not recursive:
695 to monitor subdirectories under a directory,
696 additional watches must be created.
697 This can take a significant amount time for large directory trees.
698 .P
699 If monitoring an entire directory subtree,
700 and a new subdirectory is created in that tree or an existing directory
701 is renamed into that tree,
702 be aware that by the time you create a watch for the new subdirectory,
703 new files (and subdirectories) may already exist inside the subdirectory.
704 Therefore, you may want to scan the contents of the subdirectory
705 immediately after adding the watch (and, if desired,
706 recursively add watches for any subdirectories that it contains).
707 .P
708 Note that the event queue can overflow.
709 In this case, events are lost.
710 Robust applications should handle the possibility of
711 lost events gracefully.
712 For example, it may be necessary to rebuild part or all of
713 the application cache.
714 (One simple, but possibly expensive,
715 approach is to close the inotify file descriptor, empty the cache,
716 create a new inotify file descriptor,
717 and then re-create watches and cache entries
718 for the objects to be monitored.)
719 .P
720 If a filesystem is mounted on top of a monitored directory,
721 no event is generated, and no events are generated
722 for objects immediately under the new mount point.
723 If the filesystem is subsequently unmounted,
724 events will subsequently be generated for the directory and
725 the objects it contains.
726 .\"
727 .SS Dealing with rename() events
728 As noted above, the
729 .B IN_MOVED_FROM
730 and
731 .B IN_MOVED_TO
732 event pair that is generated by
733 .BR rename (2)
734 can be matched up via their shared cookie value.
735 However, the task of matching has some challenges.
736 .P
737 These two events are usually consecutive in the event stream available
738 when reading from the inotify file descriptor.
739 However, this is not guaranteed.
740 If multiple processes are triggering events for monitored objects,
741 then (on rare occasions) an arbitrary number of
742 other events may appear between the
743 .B IN_MOVED_FROM
744 and
745 .B IN_MOVED_TO
746 events.
747 Furthermore, it is not guaranteed that the event pair is atomically
748 inserted into the queue: there may be a brief interval where the
749 .B IN_MOVED_FROM
750 has appeared, but the
751 .B IN_MOVED_TO
752 has not.
753 .P
754 Matching up the
755 .B IN_MOVED_FROM
756 and
757 .B IN_MOVED_TO
758 event pair generated by
759 .BR rename (2)
760 is thus inherently racy.
761 (Don't forget that if an object is renamed outside of a monitored directory,
762 there may not even be an
763 .B IN_MOVED_TO
764 event.)
765 Heuristic approaches (e.g., assume the events are always consecutive)
766 can be used to ensure a match in most cases,
767 but will inevitably miss some cases,
768 causing the application to perceive the
769 .B IN_MOVED_FROM
770 and
771 .B IN_MOVED_TO
772 events as being unrelated.
773 If watch descriptors are destroyed and re-created as a result,
774 then those watch descriptors will be inconsistent with
775 the watch descriptors in any pending events.
776 (Re-creating the inotify file descriptor and rebuilding the cache may
777 be useful to deal with this scenario.)
778 .P
779 Applications should also allow for the possibility that the
780 .B IN_MOVED_FROM
781 event was the last event that could fit in the buffer
782 returned by the current call to
783 .BR read (2),
784 and the accompanying
785 .B IN_MOVED_TO
786 event might be fetched only on the next
787 .BR read (2),
788 which should be done with a (small) timeout to allow for the fact that
789 insertion of the
790 .BR IN_MOVED_FROM + IN_MOVED_TO
791 event pair is not atomic,
792 and also the possibility that there may not be any
793 .B IN_MOVED_TO
794 event.
795 .SH BUGS
796 Before Linux 3.19,
797 .BR fallocate (2)
798 did not create any inotify events.
799 Since Linux 3.19,
800 .\" commit 820c12d5d6c0890bc93dd63893924a13041fdc35
801 calls to
802 .BR fallocate (2)
803 generate
804 .B IN_MODIFY
805 events.
806 .P
807 .\" FIXME . kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321
808 .\" implies that unmount events were buggy since Linux 2.6.11 to Linux 2.6.36
809 .\"
810 Before Linux 2.6.16, the
811 .B IN_ONESHOT
812 .I mask
813 flag does not work.
814 .P
815 As originally designed and implemented, the
816 .B IN_ONESHOT
817 flag did not cause an
818 .B IN_IGNORED
819 event to be generated when the watch was dropped after one event.
820 However, as an unintended effect of other changes,
821 since Linux 2.6.36, an
822 .B IN_IGNORED
823 event is generated in this case.
824 .P
825 Before Linux 2.6.25,
826 .\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2
827 the kernel code that was intended to coalesce successive identical events
828 (i.e., the two most recent events could potentially be coalesced
829 if the older had not yet been read)
830 instead checked if the most recent event could be coalesced with the
831 .I oldest
832 unread event.
833 .P
834 When a watch descriptor is removed by calling
835 .BR inotify_rm_watch (2)
836 (or because a watch file is deleted or the filesystem
837 that contains it is unmounted),
838 any pending unread events for that watch descriptor remain available to read.
839 As watch descriptors are subsequently allocated with
840 .BR inotify_add_watch (2),
841 the kernel cycles through the range of possible watch descriptors (1 to
842 .BR INT_MAX )
843 incrementally.
844 When allocating a free watch descriptor, no check is made to see whether that
845 watch descriptor number has any pending unread events in the inotify queue.
846 Thus, it can happen that a watch descriptor is reallocated even
847 when pending unread events exist for a previous incarnation of
848 that watch descriptor number, with the result that the application
849 might then read those events and interpret them as belonging to
850 the file associated with the newly recycled watch descriptor.
851 In practice, the likelihood of hitting this bug may be extremely low,
852 since it requires that an application cycle through
853 .B INT_MAX
854 watch descriptors,
855 release a watch descriptor while leaving unread events for that
856 watch descriptor in the queue,
857 and then recycle that watch descriptor.
858 For this reason, and because there have been no reports
859 of the bug occurring in real-world applications,
860 as of Linux 3.15,
861 .\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=77111
862 no kernel changes have yet been made to eliminate this possible bug.
863 .SH EXAMPLES
864 The following program demonstrates the usage of the inotify API.
865 It marks the directories passed as a command-line arguments
866 and waits for events of type
867 .BR IN_OPEN ,
868 .BR IN_CLOSE_NOWRITE ,
869 and
870 .BR IN_CLOSE_WRITE .
871 .P
872 The following output was recorded while editing the file
873 .I /home/user/temp/foo
874 and listing directory
875 .IR /tmp .
876 Before the file and the directory were opened,
877 .B IN_OPEN
878 events occurred.
879 After the file was closed, an
880 .B IN_CLOSE_WRITE
881 event occurred.
882 After the directory was closed, an
883 .B IN_CLOSE_NOWRITE
884 event occurred.
885 Execution of the program ended when the user pressed the ENTER key.
886 .SS Example output
887 .in +4n
888 .EX
889 $ \fB./a.out /tmp /home/user/temp\fP
890 Press enter key to terminate.
891 Listening for events.
892 IN_OPEN: /home/user/temp/foo [file]
893 IN_CLOSE_WRITE: /home/user/temp/foo [file]
894 IN_OPEN: /tmp/ [directory]
895 IN_CLOSE_NOWRITE: /tmp/ [directory]
896 \&
897 Listening for events stopped.
898 .EE
899 .in
900 .SS Program source
901 \&
902 .EX
903 #include <errno.h>
904 #include <poll.h>
905 #include <stdio.h>
906 #include <stdlib.h>
907 #include <sys/inotify.h>
908 #include <unistd.h>
909 #include <string.h>
910 \&
911 /* Read all available inotify events from the file descriptor \[aq]fd\[aq].
912 wd is the table of watch descriptors for the directories in argv.
913 argc is the length of wd and argv.
914 argv is the list of watched directories.
915 Entry 0 of wd and argv is unused. */
916 \&
917 static void
918 handle_events(int fd, int *wd, int argc, char* argv[])
919 {
920 /* Some systems cannot read integer variables if they are not
921 properly aligned. On other systems, incorrect alignment may
922 decrease performance. Hence, the buffer used for reading from
923 the inotify file descriptor should have the same alignment as
924 struct inotify_event. */
925 \&
926 char buf[4096]
927 __attribute__ ((aligned(__alignof__(struct inotify_event))));
928 const struct inotify_event *event;
929 ssize_t len;
930 \&
931 /* Loop while events can be read from inotify file descriptor. */
932 \&
933 for (;;) {
934 \&
935 /* Read some events. */
936 \&
937 len = read(fd, buf, sizeof(buf));
938 if (len == \-1 && errno != EAGAIN) {
939 perror("read");
940 exit(EXIT_FAILURE);
941 }
942 \&
943 /* If the nonblocking read() found no events to read, then
944 it returns \-1 with errno set to EAGAIN. In that case,
945 we exit the loop. */
946 \&
947 if (len <= 0)
948 break;
949 \&
950 /* Loop over all events in the buffer. */
951 \&
952 for (char *ptr = buf; ptr < buf + len;
953 ptr += sizeof(struct inotify_event) + event\->len) {
954 \&
955 event = (const struct inotify_event *) ptr;
956 \&
957 /* Print event type. */
958 \&
959 if (event\->mask & IN_OPEN)
960 printf("IN_OPEN: ");
961 if (event\->mask & IN_CLOSE_NOWRITE)
962 printf("IN_CLOSE_NOWRITE: ");
963 if (event\->mask & IN_CLOSE_WRITE)
964 printf("IN_CLOSE_WRITE: ");
965 \&
966 /* Print the name of the watched directory. */
967 \&
968 for (size_t i = 1; i < argc; ++i) {
969 if (wd[i] == event\->wd) {
970 printf("%s/", argv[i]);
971 break;
972 }
973 }
974 \&
975 /* Print the name of the file. */
976 \&
977 if (event\->len)
978 printf("%s", event\->name);
979 \&
980 /* Print type of filesystem object. */
981 \&
982 if (event\->mask & IN_ISDIR)
983 printf(" [directory]\en");
984 else
985 printf(" [file]\en");
986 }
987 }
988 }
989 \&
990 int
991 main(int argc, char* argv[])
992 {
993 char buf;
994 int fd, i, poll_num;
995 int *wd;
996 nfds_t nfds;
997 struct pollfd fds[2];
998 \&
999 if (argc < 2) {
1000 printf("Usage: %s PATH [PATH ...]\en", argv[0]);
1001 exit(EXIT_FAILURE);
1002 }
1003 \&
1004 printf("Press ENTER key to terminate.\en");
1005 \&
1006 /* Create the file descriptor for accessing the inotify API. */
1007 \&
1008 fd = inotify_init1(IN_NONBLOCK);
1009 if (fd == \-1) {
1010 perror("inotify_init1");
1011 exit(EXIT_FAILURE);
1012 }
1013 \&
1014 /* Allocate memory for watch descriptors. */
1015 \&
1016 wd = calloc(argc, sizeof(int));
1017 if (wd == NULL) {
1018 perror("calloc");
1019 exit(EXIT_FAILURE);
1020 }
1021 \&
1022 /* Mark directories for events
1023 \- file was opened
1024 \- file was closed */
1025 \&
1026 for (i = 1; i < argc; i++) {
1027 wd[i] = inotify_add_watch(fd, argv[i],
1028 IN_OPEN | IN_CLOSE);
1029 if (wd[i] == \-1) {
1030 fprintf(stderr, "Cannot watch \[aq]%s\[aq]: %s\en",
1031 argv[i], strerror(errno));
1032 exit(EXIT_FAILURE);
1033 }
1034 }
1035 \&
1036 /* Prepare for polling. */
1037 \&
1038 nfds = 2;
1039 \&
1040 fds[0].fd = STDIN_FILENO; /* Console input */
1041 fds[0].events = POLLIN;
1042 \&
1043 fds[1].fd = fd; /* Inotify input */
1044 fds[1].events = POLLIN;
1045 \&
1046 /* Wait for events and/or terminal input. */
1047 \&
1048 printf("Listening for events.\en");
1049 while (1) {
1050 poll_num = poll(fds, nfds, \-1);
1051 if (poll_num == \-1) {
1052 if (errno == EINTR)
1053 continue;
1054 perror("poll");
1055 exit(EXIT_FAILURE);
1056 }
1057 \&
1058 if (poll_num > 0) {
1059 \&
1060 if (fds[0].revents & POLLIN) {
1061 \&
1062 /* Console input is available. Empty stdin and quit. */
1063 \&
1064 while (read(STDIN_FILENO, &buf, 1) > 0 && buf != \[aq]\en\[aq])
1065 continue;
1066 break;
1067 }
1068 \&
1069 if (fds[1].revents & POLLIN) {
1070 \&
1071 /* Inotify events are available. */
1072 \&
1073 handle_events(fd, wd, argc, argv);
1074 }
1075 }
1076 }
1077 \&
1078 printf("Listening for events stopped.\en");
1079 \&
1080 /* Close inotify file descriptor. */
1081 \&
1082 close(fd);
1083 \&
1084 free(wd);
1085 exit(EXIT_SUCCESS);
1086 }
1087 .EE
1088 .SH SEE ALSO
1089 .BR inotifywait (1),
1090 .BR inotifywatch (1),
1091 .BR inotify_add_watch (2),
1092 .BR inotify_init (2),
1093 .BR inotify_init1 (2),
1094 .BR inotify_rm_watch (2),
1095 .BR read (2),
1096 .BR stat (2),
1097 .BR fanotify (7)
1098 .P
1099 .I Documentation/filesystems/inotify.txt
1100 in the Linux kernel source tree