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