1INOTIFY(7) Linux Programmer's Manual INOTIFY(7)
2
3
4
6 inotify - monitoring file system events
7
9 The inotify API provides a mechanism for monitoring file system events.
10 Inotify can be used to monitor individual files, or to monitor directo‐
11 ries. When a directory is monitored, inotify will return events for
12 the directory itself, and for files inside the directory.
13
14 The following system calls are used with this API: inotify_init(), ino‐
15 tify_add_watch(), inotify_rm_watch(), read(), and close().
16
17 inotify_init(2) creates an inotify instance and returns a file descrip‐
18 tor referring to the inotify instance.
19
20 inotify_add_watch(2) manipulates the "watch list" associated with an
21 inotify instance. Each item ("watch") in the watch list specifies the
22 pathname of a file or directory, along with some set of events that the
23 kernel should monitor for the file referred to by that pathname. ino‐
24 tify_add_watch() either creates a new watch item, or modifies an exist‐
25 ing watch. Each watch has a unique "watch descriptor", an integer
26 returned by inotify_add_watch() when the watch is created.
27
28 inotify_rm_watch(2) removes an item from an inotify watch list.
29
30 When all file descriptors referring to an inotify instance have been
31 closed, the underlying object and its resources are freed for re-use by
32 the kernel; all associated watches are automatically freed.
33
34 To determine what events have occurred, an application read(2)s from
35 the inotify file descriptor. If no events have so far occurred, then,
36 assuming a blocking file descriptor, read() will block until at least
37 one event occurs.
38
39 Each successful read() returns a buffer containing one or more of the
40 following structures:
41
42 struct inotify_event {
43 int wd; /* Watch descriptor */
44 uint32_t mask; /* Mask of events */
45 uint32_t cookie; /* Unique cookie associating related
46 events (for rename(2)) */
47 uint32_t len; /* Size of 'name' field */
48 char name[]; /* Optional null-terminated name */
49 };
50
51 wd identifies the watch for which this event occurs. It is one of the
52 watch descriptors returned by a previous call to inotify_add_watch().
53
54 mask contains bits that describe the event that occurred (see below).
55
56 cookie is a unique integer that connects related events. Currently
57 this is only used for rename events, and allows the resulting pair of
58 IN_MOVE_FROM and IN_MOVE_TO events to be connected by the application.
59
60 The name field is only present when an event is returned for a file
61 inside a watched directory; it identifies the file pathname relative to
62 the watched directory. This pathname is null-terminated, and may
63 include further null bytes to align subsequent reads to a suitable
64 address boundary.
65
66 The len field counts all of the bytes in name, including the null
67 bytes; the length of each inotify_event structure is thus sizeof(ino‐
68 tify_event)+len.
69
70 inotify events
71 The inotify_add_watch(2) mask argument and the mask field of the ino‐
72 tify_event structure returned when read(2)ing an inotify file descrip‐
73 tor are both bit masks identifying inotify events. The following bits
74 can be specified in mask when calling inotify_add_watch() and may be
75 returned in the mask field returned by read():
76
77 Bit Description
78 IN_ACCESS File was accessed (read) (*)
79 IN_ATTRIB Metadata changed (permissions, timestamps,
80 extended attributes, etc.) (*)
81 IN_CLOSE_WRITE File opened for writing was closed (*)
82 IN_CLOSE_NOWRITE File not opened for writing was closed (*)
83 IN_CREATE File/directory created in watched directory (*)
84 IN_DELETE File/directory deleted from watched directory (*)
85 IN_DELETE_SELF Watched file/directory was itself deleted
86 IN_MODIFY File was modified (*)
87 IN_MOVE_SELF Watched file/directory was itself moved
88 IN_MOVED_FROM File moved out of watched directory (*)
89 IN_MOVED_TO File moved into watched directory (*)
90 IN_OPEN File was opened (*)
91
92 When monitoring a directory, the events marked with an asterisk (*)
93 above can occur for files in the directory, in which case the name
94 field in the returned inotify_event structure identifies the name of
95 the file within the directory.
96
97 The IN_ALL_EVENTS macro is defined as a bit mask of all of the above
98 events. This macro can be used as the mask argument when calling ino‐
99 tify_add_watch().
100
101 Two additional convenience macros are IN_MOVE, which equates to
102 IN_MOVED_FROM|IN_MOVED_TO, and IN_CLOSE which equates to
103 IN_CLOSE_WRITE|IN_CLOSE_NOWRITE.
104
105 The following further bits can be specified in mask when calling ino‐
106 tify_add_watch():
107
108 Bit Description
109 IN_DONT_FOLLOW Don't dereference pathname if it is a symbolic link
110 IN_MASK_ADD Add (OR) events to watch mask for this pathname if
111 it already exists (instead of replacing mask)
112 IN_ONESHOT Monitor pathname for one event, then remove from
113 watch list
114 IN_ONLYDIR Only watch pathname if it is a directory
115
116 The following bits may be set in the mask field returned by read():
117
118 Bit Description
119 IN_IGNORED Watch was removed explicitly (inotify_rm_watch())
120 or automatically (file was deleted, or
121 file system was unmounted)
122 IN_ISDIR Subject of this event is a directory
123 IN_Q_OVERFLOW Event queue overflowed (wd is -1 for this event)
124 IN_UNMOUNT File system containing watched object was unmounted
125
126 /proc interfaces
127 The following interfaces can be used to limit the amount of kernel mem‐
128 ory consumed by inotify:
129
130 /proc/sys/fs/inotify/max_queued_events
131 The value in this file is used when an application calls ino‐
132 tify_init(2) to set an upper limit on the number of events that
133 can be queued to the corresponding inotify instance. Events in
134 excess of this limit are dropped, but an IN_Q_OVERFLOW event is
135 always generated.
136
137 /proc/sys/fs/inotify/max_user_instances
138 This specifies an upper limit on the number of inotify instances
139 that can be created per real user ID.
140
141 /proc/sys/fs/inotify/max_user_watches
142 This specifies a limit on the number of watches that can be
143 associated with each inotify instance.
144
146 Inotify file descriptors can be monitored using select(2), poll(2), and
147 epoll(7).
148
149 If successive output inotify events produced on the inotify file
150 descriptor are identical (same wd, mask, cookie, and name) then they
151 are coalesced into a single event.
152
153 The events returned by reading from an inotify file descriptor form an
154 ordered queue. Thus, for example, it is guaranteed that when renaming
155 from one directory to another, events will be produced in the correct
156 order on the inotify file descriptor.
157
158 The FIONREAD ioctl() returns the number of bytes available to read from
159 an inotify file descriptor.
160
161 Inotify monitoring of directories is not recursive: to monitor subdi‐
162 rectories under a directory, additional watches must be created.
163
165 Inotify was merged into the 2.6.13 Linux kernel. The required library
166 interfaces were added to glibc in version 2.4.
167
169 In kernels before 2.6.16, the IN_ONESHOT mask flag does not work.
170
171 As at glibc 2.4, the definitions for IN_DONT_FOLLOW, IN_MASK_ADD, and
172 IN_ONLYDIR are missing from <sys/inotify.h>.
173
175 The inotify API is Linux specific.
176
178 inotify_add_watch(2), inotify_init(2), inotify_rm_watch(2), read(2),
179 stat(2), Documentation/filesystems/inotify.txt.
180
181
182
183Linux 2.6.15 2006-02-07 INOTIFY(7)