]>
Commit | Line | Data |
---|---|---|
4d2b74dd | 1 | '\" t |
415f63be | 2 | .\" Copyright (C) 2006, 2014 Michael Kerrisk <mtk.manpages@gmail.com> |
ecd96f7c | 3 | .\" Copyright (C) 2014 Heinrich Schuchardt <xypron.glpk@gmx.de> |
4d2b74dd | 4 | .\" |
93015253 | 5 | .\" %%%LICENSE_START(VERBATIM) |
4d2b74dd MK |
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. | |
c13182ef | 14 | .\" |
4d2b74dd MK |
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 | |
10d76543 MK |
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. | |
c13182ef | 22 | .\" |
4d2b74dd MK |
23 | .\" Formatted or processed versions of this manual, if unaccompanied by |
24 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 25 | .\" %%%LICENSE_END |
4d2b74dd | 26 | .\" |
ecd96f7c | 27 | .TH INOTIFY 7 2014-05-23 "Linux" "Linux Programmer's Manual" |
4d2b74dd | 28 | .SH NAME |
9ee4a2b6 | 29 | inotify \- monitoring filesystem events |
4d2b74dd MK |
30 | .SH DESCRIPTION |
31 | The | |
c13182ef | 32 | .I inotify |
9ee4a2b6 | 33 | API provides a mechanism for monitoring filesystem events. |
4d2b74dd MK |
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 | ||
c13182ef | 39 | The following system calls are used with this API: |
356911f6 | 40 | .IP * 3 |
4d2b74dd | 41 | .BR inotify_init (2) |
c13182ef | 42 | creates an inotify instance and returns a file descriptor |
a2cc46ca | 43 | referring to the inotify instance. |
43bb5faf MK |
44 | The more recent |
45 | .BR inotify_init1 (2) | |
46 | is like | |
47 | .BR inotify_init (2), | |
356911f6 MK |
48 | but has a |
49 | .IR flags | |
50 | argument that provides access to some extra functionality. | |
51 | .IP * | |
4d2b74dd | 52 | .BR inotify_add_watch (2) |
a2cc46ca | 53 | manipulates the "watch list" associated with an inotify instance. |
3a065ac0 | 54 | Each item ("watch") in the watch list specifies the pathname of |
c13182ef | 55 | a file or directory, |
4d2b74dd MK |
56 | along with some set of events that the kernel should monitor for the |
57 | file referred to by that pathname. | |
63f6a20a | 58 | .BR inotify_add_watch (2) |
4d2b74dd MK |
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 | |
63f6a20a | 62 | .BR inotify_add_watch (2) |
4d2b74dd | 63 | when the watch is created. |
356911f6 MK |
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 * | |
4d2b74dd MK |
71 | .BR inotify_rm_watch (2) |
72 | removes an item from an inotify watch list. | |
356911f6 | 73 | .IP * |
c13182ef | 74 | When all file descriptors referring to an inotify |
356911f6 MK |
75 | instance have been closed (using |
76 | .BR close (2)), | |
c13182ef | 77 | the underlying object and its resources are |
3b777aff | 78 | freed for reuse by the kernel; |
4d2b74dd | 79 | all associated watches are automatically freed. |
6b1cc2c9 MK |
80 | |
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 to do some consistency checking, | |
88 | and rebuild the cache when inconsistencies are detected. | |
ff6e2397 | 89 | .SS Reading events from an inotify file descriptor |
4d2b74dd MK |
90 | To determine what events have occurred, an application |
91 | .BR read (2)s | |
92 | from the inotify file descriptor. | |
c13182ef | 93 | If no events have so far occurred, then, |
11da88fb | 94 | assuming a blocking file descriptor, |
63f6a20a | 95 | .BR read (2) |
01538d0d MK |
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)). | |
4d2b74dd MK |
102 | |
103 | Each successful | |
63f6a20a | 104 | .BR read (2) |
4d2b74dd | 105 | returns a buffer containing one or more of the following structures: |
a08ea57c | 106 | .in +4n |
4d2b74dd MK |
107 | .nf |
108 | ||
109 | struct inotify_event { | |
110 | int wd; /* Watch descriptor */ | |
24bbe02c MK |
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 | |
6e6231c1 | 114 | .\" Glibc bug filed: http://sources.redhat.com/bugzilla/show_bug.cgi?id=7040 |
4d2b74dd | 115 | uint32_t mask; /* Mask of events */ |
c13182ef | 116 | uint32_t cookie; /* Unique cookie associating related |
4d2b74dd | 117 | events (for rename(2)) */ |
84c517a4 | 118 | uint32_t len; /* Size of \fIname\fP field */ |
4d2b74dd MK |
119 | char name[]; /* Optional null-terminated name */ |
120 | }; | |
121 | .fi | |
a08ea57c | 122 | .in |
4d2b74dd MK |
123 | |
124 | .I wd | |
125 | identifies the watch for which this event occurs. | |
c13182ef | 126 | It is one of the watch descriptors returned by a previous call to |
63f6a20a | 127 | .BR inotify_add_watch (2). |
4d2b74dd MK |
128 | |
129 | .I mask | |
130 | contains bits that describe the event that occurred (see below). | |
131 | ||
132 | .I cookie | |
133 | is a unique integer that connects related events. | |
33a0ccb2 | 134 | Currently this is used only for rename events, and |
4d2b74dd | 135 | allows the resulting pair of |
bc636d8a | 136 | .B IN_MOVED_FROM |
c13182ef | 137 | and |
bc636d8a | 138 | .B IN_MOVED_TO |
4d2b74dd | 139 | events to be connected by the application. |
591b7a5f MK |
140 | For all other event types, |
141 | .I cookie | |
142 | is set to 0. | |
4d2b74dd | 143 | |
c13182ef | 144 | The |
4d2b74dd | 145 | .I name |
33a0ccb2 | 146 | field is present only when an event is returned |
c13182ef | 147 | for a file inside a watched directory; |
4d2b74dd | 148 | it identifies the file pathname relative to the watched directory. |
c13182ef | 149 | This pathname is null-terminated, |
1aff5804 | 150 | and may include further null bytes (\(aq\\0\(aq) to align subsequent reads to a |
4d2b74dd MK |
151 | suitable address boundary. |
152 | ||
153 | The | |
154 | .I len | |
c13182ef | 155 | field counts all of the bytes in |
4d2b74dd | 156 | .IR name , |
c13182ef | 157 | including the null bytes; |
4d2b74dd MK |
158 | the length of each |
159 | .I inotify_event | |
160 | structure is thus | |
655684a9 | 161 | .IR "sizeof(struct inotify_event)+len" . |
c7e3ee6f | 162 | |
988db661 | 163 | The behavior when the buffer given to |
c7e3ee6f | 164 | .BR read (2) |
988db661 | 165 | is too small to return information about the next event depends |
c7e3ee6f MK |
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 . | |
4ba272b3 MK |
172 | Specifying a buffer of size |
173 | ||
174 | sizeof(struct inotify_event) + NAME_MAX + 1 | |
175 | ||
176 | will be sufficient to read at least one event. | |
4d2b74dd | 177 | .SS inotify events |
c13182ef | 178 | The |
4d2b74dd MK |
179 | .BR inotify_add_watch (2) |
180 | .I mask | |
c13182ef | 181 | argument and the |
4d2b74dd MK |
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 | |
63f6a20a | 192 | .BR inotify_add_watch (2) |
c13182ef | 193 | and may be returned in the |
4d2b74dd MK |
194 | .I mask |
195 | field returned by | |
63f6a20a | 196 | .BR read (2): |
64aa9bcb | 197 | .RS 4 |
c577b95c | 198 | .TP |
f23fc716 | 199 | .BR IN_ACCESS " (*)" |
70f70c9d MK |
200 | File was accessed (e.g., |
201 | .BR read (2), | |
f23fc716 | 202 | .BR execve (2)). |
3f174f7d | 203 | .TP |
f23fc716 | 204 | .BR IN_ATTRIB " (*)" |
b0e1ac43 MK |
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 | for the target of | |
213 | .BR link (2) | |
214 | and for | |
215 | .BR unlink (2)), | |
216 | and user/group ID (e.g., | |
217 | .BR chown (2)). | |
3f174f7d | 218 | .TP |
f23fc716 MK |
219 | .BR IN_CLOSE_WRITE " (*)" |
220 | File opened for writing was closed. | |
3f174f7d | 221 | .TP |
f23fc716 MK |
222 | .BR IN_CLOSE_NOWRITE " (*)" |
223 | File not opened for writing was closed. | |
3f174f7d | 224 | .TP |
f23fc716 | 225 | .BR IN_CREATE " (*)" |
7a64793b MK |
226 | File/directory created in watched directory (e.g., |
227 | .BR open (2) | |
228 | .BR O_CREAT , | |
229 | .BR mkdir (2), | |
230 | .BR link (2), | |
1a737afd | 231 | .BR symlink (2), |
7a64793b MK |
232 | .BR bind (2) |
233 | on a UNIX domain socket). | |
3f174f7d | 234 | .TP |
f23fc716 MK |
235 | .BR IN_DELETE " (*)" |
236 | File/directory deleted from watched directory. | |
3f174f7d MK |
237 | .TP |
238 | .B IN_DELETE_SELF | |
239 | Watched file/directory was itself deleted. | |
4a1e4cca MK |
240 | (This event also occurs if an object is moved to another filesystem, |
241 | since | |
242 | .BR mv (1) | |
243 | in effect copies the file to the other filesystem and | |
49b07b8f | 244 | then deletes it from the original filesystem.) |
39f43968 MK |
245 | In addition, an |
246 | .B IN_IGNORED | |
247 | event will subsequently be generated for the watch descriptor. | |
3f174f7d | 248 | .TP |
f23fc716 | 249 | .BR IN_MODIFY " (*)" |
f54a1255 MK |
250 | File was modified (e.g., |
251 | .BR write (2), | |
252 | .BR truncate (2)). | |
3f174f7d MK |
253 | .TP |
254 | .B IN_MOVE_SELF | |
255 | Watched file/directory was itself moved. | |
256 | .TP | |
f23fc716 | 257 | .BR IN_MOVED_FROM " (*)" |
107375cf | 258 | Generated for the directory containing the old filename |
f23fc716 | 259 | when a file is renamed. |
3f174f7d | 260 | .TP |
f23fc716 | 261 | .BR IN_MOVED_TO " (*)" |
107375cf | 262 | Generated for the directory containing the new filename |
f23fc716 | 263 | when a file is renamed. |
3f174f7d | 264 | .TP |
f23fc716 MK |
265 | .BR IN_OPEN " (*)" |
266 | File was opened. | |
64aa9bcb | 267 | .RE |
4d2b74dd | 268 | .PP |
c13182ef MK |
269 | When monitoring a directory, |
270 | the events marked with an asterisk (*) above can occur for | |
4d2b74dd MK |
271 | files in the directory, in which case the |
272 | .I name | |
273 | field in the returned | |
274 | .I inotify_event | |
275 | structure identifies the name of the file within the directory. | |
276 | .PP | |
277 | The | |
278 | .B IN_ALL_EVENTS | |
279 | macro is defined as a bit mask of all of the above events. | |
280 | This macro can be used as the | |
281 | .I mask | |
282 | argument when calling | |
63f6a20a | 283 | .BR inotify_add_watch (2). |
4d2b74dd | 284 | |
dede00fe | 285 | Two additional convenience macros are defined: |
64aa9bcb | 286 | .RS 4 |
dede00fe MK |
287 | .TP |
288 | .BR IN_MOVE | |
289 | Equates to | |
290 | .BR "IN_MOVED_FROM | IN_MOVED_TO" . | |
291 | .TP | |
292 | .BR IN_CLOSE | |
293 | Equates to | |
294 | .BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" . | |
64aa9bcb | 295 | .RE |
4d2b74dd | 296 | .PP |
c13182ef | 297 | The following further bits can be specified in |
4d2b74dd MK |
298 | .I mask |
299 | when calling | |
63f6a20a | 300 | .BR inotify_add_watch (2): |
64aa9bcb | 301 | .RS 4 |
c577b95c | 302 | .TP |
31daf529 | 303 | .BR IN_DONT_FOLLOW " (since Linux 2.6.15)" |
aeb9b6a6 MK |
304 | Don't dereference |
305 | .I pathname | |
306 | if it is a symbolic link. | |
dda869a4 | 307 | .TP |
0ff2cc88 | 308 | .BR IN_EXCL_UNLINK " (since Linux 2.6.36)" |
b3ad7609 MK |
309 | .\" commit 8c1934c8d70b22ca8333b216aec6c7d09fdbd6a6 |
310 | By default, when watching events on the children of a directory, | |
311 | events are generated for children even after they have been unlinked | |
312 | from the directory. | |
313 | This can result in large numbers of uninteresting events for | |
314 | some applications (e.g., if watching | |
315 | .IR /tmp , | |
316 | in which many applications create temporary files whose | |
317 | names are immediately unlinked). | |
318 | Specifying | |
319 | .B IN_EXCL_UNLINK | |
320 | changes the default behavior, | |
321 | so that events are not generated for children after | |
322 | they have been unlinked from the watched directory. | |
323 | .TP | |
dda869a4 MK |
324 | .B IN_MASK_ADD |
325 | Add (OR) events to watch mask for this pathname if | |
326 | it already exists (instead of replacing mask). | |
327 | .TP | |
328 | .B IN_ONESHOT | |
aeb9b6a6 MK |
329 | Monitor |
330 | .I pathname | |
331 | for one event, then remove from | |
dda869a4 MK |
332 | watch list. |
333 | .TP | |
31daf529 | 334 | .BR IN_ONLYDIR " (since Linux 2.6.15)" |
aeb9b6a6 MK |
335 | Only watch |
336 | .I pathname | |
337 | if it is a directory. | |
64aa9bcb | 338 | .RE |
4d2b74dd MK |
339 | .PP |
340 | The following bits may be set in the | |
341 | .I mask | |
342 | field returned by | |
63f6a20a | 343 | .BR read (2): |
64aa9bcb | 344 | .RS 4 |
c577b95c | 345 | .TP |
dda869a4 | 346 | .B IN_IGNORED |
aeb9b6a6 MK |
347 | Watch was removed explicitly |
348 | .RB ( inotify_rm_watch (2)) | |
9ee4a2b6 | 349 | or automatically (file was deleted, or filesystem was unmounted). |
7b5151b7 | 350 | See also BUGS. |
dda869a4 MK |
351 | .TP |
352 | .B IN_ISDIR | |
353 | Subject of this event is a directory. | |
354 | .TP | |
355 | .B IN_Q_OVERFLOW | |
aeb9b6a6 MK |
356 | Event queue overflowed |
357 | .RI ( wd | |
358 | is \-1 for this event). | |
dda869a4 MK |
359 | .TP |
360 | .B IN_UNMOUNT | |
9ee4a2b6 | 361 | Filesystem containing watched object was unmounted. |
b01c936e MK |
362 | In addition, an |
363 | .B IN_IGNORED | |
364 | event will subsequently be generated for the watch descriptor. | |
64aa9bcb | 365 | .RE |
415f63be MK |
366 | .SS Examples |
367 | Suppose an application is watching the directory | |
368 | .I dir | |
369 | and the file | |
370 | .IR dir/myfile | |
371 | for all events. | |
372 | The examples below show some events that will be generated | |
373 | for these two objects. | |
374 | .RS 4 | |
375 | .TP | |
376 | fd = open("dir/myfile", O_RDWR); | |
377 | Generates | |
378 | .B IN_OPEN | |
379 | events for both | |
380 | .I dir | |
381 | and | |
382 | .IR dir/myfile . | |
383 | .TP | |
384 | read(fd, buf, count); | |
385 | Generates | |
386 | .B IN_ACCESS | |
387 | events for both | |
388 | .I dir | |
389 | and | |
390 | .IR dir/myfile . | |
391 | .TP | |
392 | write(fd, buf, count); | |
393 | Generates | |
394 | .B IN_MODIFY | |
395 | events for both | |
396 | .I dir | |
397 | and | |
398 | .IR dir/myfile . | |
399 | .TP | |
400 | fchmod(fd, mode); | |
401 | Generates | |
402 | .B IN_ATTRIB | |
403 | events for both | |
404 | .I dir | |
405 | and | |
406 | .IR dir/myfile . | |
407 | .TP | |
408 | close(fd); | |
409 | Generates | |
410 | .B IN_CLOSE_WRITE | |
411 | events for both | |
412 | .I dir | |
413 | and | |
414 | .IR dir/myfile . | |
415 | .RE | |
416 | .PP | |
417 | Suppose an application is watching the directories | |
418 | .I dir1 | |
419 | and | |
d2c3d8a8 | 420 | .IR dir2 , |
415f63be MK |
421 | and the file |
422 | .IR dir1/myfile . | |
423 | The following examples show some events that may be generated. | |
424 | .RS 4 | |
425 | .TP | |
d2c3d8a8 | 426 | link("dir1/myfile", "dir2/new"); |
415f63be MK |
427 | Generates an |
428 | .B IN_ATTRIB | |
429 | event for | |
430 | .IR myfile | |
431 | and an | |
432 | .B IN_CREATE | |
433 | event for | |
434 | .IR dir2 . | |
435 | .TP | |
436 | rename("dir1/myfile", "dir2/myfile"); | |
437 | Generates an | |
438 | .B IN_MOVED_FROM | |
439 | event for | |
440 | .IR dir1 , | |
441 | an | |
442 | .B IN_MOVED_TO | |
443 | event for | |
444 | .IR dir2 , | |
445 | and an | |
446 | .B IN_MOVE_SELF | |
447 | event for | |
448 | .IR myfile . | |
449 | The | |
450 | .B IN_MOVED_FROM | |
451 | and | |
452 | .B IN_MOVED_TO | |
453 | events will have the same | |
454 | .I cookie | |
455 | value. | |
456 | .RE | |
457 | .PP | |
458 | Suppose that | |
459 | .IR dir1/xx | |
460 | and | |
461 | .IR dir2/yy | |
462 | are (the only) links to the same file, and an application is watching | |
463 | .IR dir1 , | |
464 | .IR dir2 , | |
465 | .IR dir1/xx , | |
466 | and | |
467 | .IR dir2/yy . | |
468 | Executing the following calls in the order given below will generate | |
469 | the following events: | |
470 | .RS 4 | |
471 | .TP | |
472 | unlink("dir2/yy"); | |
d2c3d8a8 | 473 | Generates an |
415f63be MK |
474 | .BR IN_ATTRIB |
475 | event for | |
476 | .IR xx | |
477 | (because its link count changes) | |
478 | and an | |
479 | .B IN_DELETE | |
480 | event for | |
481 | .IR dir2 . | |
482 | .TP | |
483 | unlink("dir1/xx"); | |
484 | Generates | |
485 | .BR IN_ATTRIB , | |
486 | .BR IN_DELETE_SELF , | |
487 | and | |
488 | .BR IN_IGNORED | |
489 | events for | |
490 | .IR xx , | |
491 | and an | |
492 | .BR IN_DELETE | |
d2c3d8a8 | 493 | event for |
415f63be MK |
494 | .IR dir1 . |
495 | .RE | |
496 | .PP | |
497 | Suppose an application is watching the directory | |
498 | .IR dir | |
499 | and (the empty) directory | |
500 | .IR dir/subdir . | |
501 | The following examples show some events that may be generated. | |
502 | .RS 4 | |
503 | .TP | |
504 | mkdir("dir/new", mode); | |
505 | Generates an | |
506 | .B "IN_CREATE | IN_ISDIR" | |
507 | event for | |
508 | .IR dir . | |
509 | .TP | |
d2c3d8a8 | 510 | rmdir("dir/subdir"); |
415f63be MK |
511 | Generates |
512 | .B IN_DELETE_SELF | |
513 | and | |
514 | .B IN_IGNORED | |
515 | events for | |
516 | .IR subdir , | |
517 | and an | |
518 | .B "IN_DELETE | IN_ISDIR" | |
519 | event for | |
520 | .IR dir . | |
521 | .RE | |
4d2b74dd | 522 | .SS /proc interfaces |
c13182ef | 523 | The following interfaces can be used to limit the amount of |
4d2b74dd MK |
524 | kernel memory consumed by inotify: |
525 | .TP | |
0daa9e92 | 526 | .I /proc/sys/fs/inotify/max_queued_events |
4d2b74dd MK |
527 | The value in this file is used when an application calls |
528 | .BR inotify_init (2) | |
c13182ef | 529 | to set an upper limit on the number of events that can be |
4d2b74dd MK |
530 | queued to the corresponding inotify instance. |
531 | Events in excess of this limit are dropped, but an | |
532 | .B IN_Q_OVERFLOW | |
533 | event is always generated. | |
534 | .TP | |
0daa9e92 | 535 | .I /proc/sys/fs/inotify/max_user_instances |
c13182ef | 536 | This specifies an upper limit on the number of inotify instances |
4d2b74dd MK |
537 | that can be created per real user ID. |
538 | .TP | |
0daa9e92 | 539 | .I /proc/sys/fs/inotify/max_user_watches |
31546b46 VN |
540 | This specifies an upper limit on the number of watches |
541 | that can be created per real user ID. | |
47297adb | 542 | .SH VERSIONS |
2b2581ee MK |
543 | Inotify was merged into the 2.6.13 Linux kernel. |
544 | The required library interfaces were added to glibc in version 2.4. | |
545 | .RB ( IN_DONT_FOLLOW , | |
546 | .BR IN_MASK_ADD , | |
547 | and | |
548 | .B IN_ONLYDIR | |
64aa9bcb | 549 | were added in glibc version 2.5.) |
47297adb | 550 | .SH CONFORMING TO |
8382f16d | 551 | The inotify API is Linux-specific. |
47297adb | 552 | .SH NOTES |
4d2b74dd MK |
553 | Inotify file descriptors can be monitored using |
554 | .BR select (2), | |
555 | .BR poll (2), | |
c13182ef | 556 | and |
2315114c | 557 | .BR epoll (7). |
0000daa5 MK |
558 | When an event is available, the file descriptor indicates as readable. |
559 | ||
560 | Since Linux 2.6.25, | |
561 | signal-driven I/O notification is available for inotify file descriptors; | |
562 | see the discussion of | |
563 | .B F_SETFL | |
564 | (for setting the | |
565 | .B O_ASYNC | |
566 | flag), | |
567 | .BR F_SETOWN , | |
568 | and | |
569 | .B F_SETSIG | |
570 | in | |
571 | .BR fcntl (2). | |
572 | The | |
573 | .I siginfo_t | |
574 | structure (described in | |
575 | .BR sigaction (2)) | |
576 | that is passed to the signal handler has the following fields set: | |
577 | .IR si_fd | |
578 | is set to the inotify file descriptor number; | |
579 | .IR si_signo | |
580 | is set to the signal number; | |
581 | .IR si_code | |
582 | is set to | |
583 | .BR POLL_IN ; | |
584 | and | |
585 | .B POLLIN | |
586 | is set in | |
587 | .IR si_band . | |
4d2b74dd | 588 | |
c13182ef MK |
589 | If successive output inotify events produced on the |
590 | inotify file descriptor are identical (same | |
591 | .IR wd , | |
592 | .IR mask , | |
4d2b74dd MK |
593 | .IR cookie , |
594 | and | |
3f3698d8 | 595 | .IR name ), |
6f0ab035 MK |
596 | then they are coalesced into a single event if the |
597 | older event has not yet been read (but see BUGS). | |
8856aab8 MK |
598 | This reduces the amount of kernel memory required for the event queue, |
599 | but also means that an application can't use inotify to reliably count | |
600 | file events. | |
4d2b74dd | 601 | |
c13182ef MK |
602 | The events returned by reading from an inotify file descriptor |
603 | form an ordered queue. | |
604 | Thus, for example, it is guaranteed that when renaming from | |
605 | one directory to another, events will be produced in the | |
4d2b74dd MK |
606 | correct order on the inotify file descriptor. |
607 | ||
608 | The | |
609 | .B FIONREAD | |
63f6a20a | 610 | .BR ioctl (2) |
c13182ef | 611 | returns the number of bytes available to read from an |
4d2b74dd | 612 | inotify file descriptor. |
613836aa | 613 | .SS Limitations and caveats |
67898fdf MK |
614 | The inotify API provides no information about the user or process that |
615 | triggered the inotify event. | |
616 | In particular, there is no easy | |
617 | way for a process that is monitoring events via inotify | |
618 | to distinguish events that it triggers | |
619 | itself from those that are triggered by other processes. | |
620 | ||
e226bed7 MK |
621 | Inotify reports only events that a user-space program triggers through |
622 | the filesystem API. | |
623 | As a result, it does not catch remote events that occur | |
624 | on network filesystems. | |
625 | (Applications must fall back to polling the filesystem | |
626 | to catch such events.) | |
fa1d49a6 | 627 | Furthermore, various pseudo-filesystems such as |
e226bed7 MK |
628 | .IR /proc , |
629 | .IR /sys , | |
630 | and | |
631 | .IR /dev/pts | |
632 | are not monitorable with inotify. | |
633 | ||
e449e5f1 MK |
634 | The inotify API does not report file accesses and modifications that |
635 | may occur because of | |
636 | .BR mmap (2) | |
637 | and | |
638 | .BR msync (2). | |
639 | ||
67898fdf MK |
640 | The inotify API identifies affected files by filename. |
641 | However, by the time an application processes an inotify event, | |
642 | the filename may already have been deleted or renamed. | |
643 | ||
da977073 | 644 | The inotify API identifies events via watch descriptors. |
264276c6 MK |
645 | It is the application's responsibility to cache a mapping |
646 | (if one is needed) between watch descriptors and pathnames. | |
647 | Be aware that directory renamings may affect multiple cached pathnames. | |
648 | ||
4d2b74dd MK |
649 | Inotify monitoring of directories is not recursive: |
650 | to monitor subdirectories under a directory, | |
651 | additional watches must be created. | |
613836aa MK |
652 | This can take a significant amount time for large directory trees. |
653 | ||
a79d28b2 MK |
654 | If monitoring an entire directory subtree, |
655 | and a new subdirectory is created in that tree or an existing directory | |
656 | is renamed into that tree, | |
657 | be aware that by the time you create a watch for the new subdirectory, | |
658 | new files (and subdirectories) may already exist inside the subdirectory. | |
659 | Therefore, you may want to scan the contents of the subdirectory | |
660 | immediately after adding the watch (and, if desired, | |
661 | recursively add watches for any subdirectories that it contains). | |
662 | ||
613836aa MK |
663 | Note that the event queue can overflow. |
664 | In this case, events are lost. | |
09fa72fa | 665 | Robust applications should handle the possibility of |
613836aa | 666 | lost events gracefully. |
94d52c15 MK |
667 | For example, it may be necessary to rebuild part or all of |
668 | the application cache. | |
669 | (One simple, but possibly expensive, | |
670 | approach is to close the inotify file descriptor, empty the cache, | |
85e179c5 | 671 | create a new inotify file descriptor, |
94d52c15 MK |
672 | and then re-create watches and cache entries |
673 | for the objects to be monitored.) | |
85e179c5 | 674 | .SS Dealing with rename() events |
fa51f4d9 | 675 | As noted above, the |
85e179c5 MK |
676 | .B IN_MOVED_FROM |
677 | and | |
678 | .B IN_MOVED_TO | |
fa51f4d9 | 679 | event pair that is generated by |
85e179c5 | 680 | .BR rename (2) |
fa51f4d9 MK |
681 | can be matched up via their shared cookie value. |
682 | However, the task of matching has some challenges. | |
683 | ||
684 | These two events are usually consecutive in the event stream available | |
685 | when reading from the inotify file descriptor. | |
85e179c5 MK |
686 | However, this is not guaranteed. |
687 | If multiple processes are triggering events for monitored objects, | |
688 | then (on rare occasions) an arbitrary number of | |
689 | other events may appear between the | |
690 | .B IN_MOVED_FROM | |
691 | and | |
692 | .B IN_MOVED_TO | |
693 | events. | |
694 | ||
695 | Matching up the | |
696 | .B IN_MOVED_FROM | |
697 | and | |
698 | .B IN_MOVED_TO | |
699 | event pair generated by | |
700 | .BR rename (2) | |
701 | is thus inherently racy. | |
702 | (Don't forget that if an object is renamed outside of a monitored directory, | |
703 | there may not even be an | |
704 | .BR IN_MOVED_TO | |
705 | event.) | |
706 | Heuristic approaches (e.g., assume the events are always consecutive) | |
707 | can be used to ensure a match in most cases, | |
708 | but will inevitably miss some cases, | |
709 | causing the application to perceive the | |
710 | .B IN_MOVED_FROM | |
711 | and | |
712 | .B IN_MOVED_TO | |
713 | events as being unrelated. | |
714 | If watch descriptors are destroyed and re-created as a result, | |
715 | then those watch descriptors will be inconsistent with | |
716 | the watch descriptors in any pending events. | |
6f1a4954 | 717 | (Re-creating the inotify file descriptor and rebuilding the cache may |
85e179c5 MK |
718 | be useful to deal with this scenario.) |
719 | ||
720 | Applications should also allow for the possibility that the | |
721 | .B IN_MOVED_FROM | |
722 | event was the last event that could fit in the buffer | |
723 | returned by the current call to | |
724 | .BR read (2), | |
725 | and the accompanying | |
726 | .B IN_MOVED_TO | |
727 | event might be fetched only on the next | |
728 | .BR read (2). | |
47297adb | 729 | .SH BUGS |
a15ead5e MK |
730 | .\" FIXME kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321 |
731 | .\" implies that unmount events were buggy 2.6.11 to 2.6.36 | |
732 | .\" | |
ed7b0235 MK |
733 | In kernels before 2.6.16, the |
734 | .B IN_ONESHOT | |
c13182ef | 735 | .I mask |
ed7b0235 | 736 | flag does not work. |
6f0ab035 | 737 | |
7b5151b7 MK |
738 | As originally designed and implemented, the |
739 | .B IN_ONESHOT | |
740 | flag did not cause an | |
741 | .B IN_IGNORED | |
742 | event to be generated when the watch was dropped after one event. | |
743 | However, as an unintended effect of other changes, | |
744 | since Linux 2.6.36, an | |
745 | .B IN_IGNORED | |
746 | event is generated in this case. | |
747 | ||
6f0ab035 | 748 | Before kernel 2.6.25, |
22129aa9 | 749 | .\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2 |
9ed6b517 | 750 | the kernel code that was intended to coalesce successive identical events |
6f0ab035 MK |
751 | (i.e., the two most recent events could potentially be coalesced |
752 | if the older had not yet been read) | |
753 | instead checked if the most recent event could be coalesced with the | |
754 | .I oldest | |
755 | unread event. | |
ecd96f7c HS |
756 | .SH EXAMPLE |
757 | The following program demonstrates the usage of the inotify API. | |
758 | It marks the directories passed as a command-line arguments | |
759 | and waits for events of type | |
760 | .BR IN_OPEN , | |
761 | .BR IN_CLOSE_NOWRITE | |
762 | and | |
763 | .BR IN_CLOSE_WRITE . | |
764 | .PP | |
765 | The following output was recorded while editing the file | |
766 | .I /home/user/temp/foo | |
767 | and listing directory | |
768 | .IR /tmp . | |
769 | Before the file and the directory were opened, | |
770 | .B IN_OPEN | |
771 | events occurred. | |
772 | After the file was closed, an | |
773 | .B IN_CLOSE_WRITE | |
774 | event occurred. | |
775 | After the directory was closed, an | |
776 | .B IN_CLOSE_NOWRITE | |
777 | event occurred. | |
778 | Execution of the program ended when the user pressed the ENTER key. | |
779 | .SS Example output | |
780 | .in +4n | |
781 | .nf | |
782 | $ ./inotify.7.example /tmp /home/user/temp | |
783 | Press enter key to terminate. | |
784 | Listening for events. | |
785 | IN_OPEN: /home/user/temp/foo [file] | |
786 | IN_CLOSE_WRITE: /home/user/temp/foo [file] | |
787 | IN_OPEN: /tmp/ [directory] | |
788 | IN_CLOSE_NOWRITE: /tmp/ [directory] | |
789 | ||
790 | Listening for events stopped. | |
791 | .fi | |
792 | .in | |
793 | .SS Program source | |
794 | .nf | |
795 | #include <errno.h> | |
796 | #include <poll.h> | |
797 | #include <stdio.h> | |
798 | #include <stdlib.h> | |
799 | #include <sys/inotify.h> | |
800 | #include <unistd.h> | |
801 | ||
802 | /* Read all available inotify events from the file descriptor 'fd'. | |
803 | wd is the table of watch descriptors for the directories in argv. | |
804 | argc is the length of wd and argv. | |
805 | argv is the list of watched directories. | |
806 | Entry 0 of wd and argv is unused. */ | |
807 | ||
808 | static void | |
809 | handle_events(int fd, int *wd, int argc, char* argv[]) | |
810 | { | |
811 | /* Some systems cannot read integer variables if they are | |
812 | not properly aligned. On other systems incorrect alignment | |
813 | may decrease performance. | |
814 | Hence, the buffer used for reading from the inotify file | |
815 | descriptor should have the same alignment as | |
816 | struct inotify_event. */ | |
817 | char buf[4096] | |
818 | __attribute__ ((aligned(__alignof__(struct inotify_event)))); | |
819 | const struct inotify_event *event; | |
820 | int i; | |
821 | ssize_t len; | |
822 | char *ptr; | |
823 | ||
824 | /* Loop while events can be read from inotify file descriptor. */ | |
825 | ||
826 | for (;;) { | |
827 | ||
828 | /* Read some events. */ | |
829 | ||
830 | len = read(fd, buf, sizeof buf); | |
831 | if (len == \-1 && errno != EAGAIN) { | |
832 | perror("read"); | |
833 | exit(EXIT_FAILURE); | |
834 | } | |
835 | ||
836 | /* If the nonblocking read() found no events to read, then | |
837 | it returns \-1 with errno set to EAGAIN. In that case, | |
838 | we exit the loop. */ | |
839 | ||
840 | if (len <= 0) | |
841 | break; | |
842 | ||
843 | /* Loop over all events in the buffer */ | |
844 | ||
845 | for (ptr = buf; ptr < buf + len; | |
846 | ptr += sizeof(struct inotify_event) + event\->len) { | |
847 | ||
848 | event = (const struct inotify_event *) ptr; | |
849 | ||
850 | /* Print event type */ | |
851 | ||
852 | if (event\->mask & IN_OPEN) | |
853 | printf("IN_OPEN: "); | |
854 | if (event\->mask & IN_CLOSE_NOWRITE) | |
855 | printf("IN_CLOSE_NOWRITE: "); | |
856 | if (event\->mask & IN_CLOSE_WRITE) | |
857 | printf("IN_CLOSE_WRITE: "); | |
858 | ||
859 | /* Print the name of the watched directory */ | |
860 | ||
861 | for (i = 1; i < argc; ++i) { | |
862 | if (wd[i] == event\->wd) { | |
863 | printf("%s/", argv[i]); | |
864 | break; | |
865 | } | |
866 | } | |
867 | ||
868 | /* Print the name of the file */ | |
869 | ||
870 | if (event\->len) | |
871 | printf("%s", event\->name); | |
872 | ||
873 | /* Print type of filesystem object */ | |
874 | ||
875 | if (event\->mask & IN_ISDIR) | |
876 | printf(" [directory]\\n"); | |
877 | else | |
878 | printf(" [file]\\n"); | |
879 | ||
880 | } | |
881 | } | |
882 | } | |
883 | ||
884 | int | |
885 | main(int argc, char* argv[]) | |
886 | { | |
887 | char buf; | |
888 | int fd, i, poll_num; | |
889 | int *wd; | |
890 | nfds_t nfds; | |
891 | struct pollfd fds[2]; | |
892 | ||
893 | if (argc < 2) { | |
894 | printf("Usage: %s PATH [PATH ...]\\n", argv[0]); | |
895 | exit(EXIT_FAILURE); | |
896 | } | |
897 | ||
898 | printf("Press ENTER key to terminate.\\n"); | |
899 | ||
900 | /* Create the file descriptor for accessing the inotify API */ | |
901 | ||
902 | fd = inotify_init1(IN_NONBLOCK); | |
903 | if (fd == \-1) { | |
904 | perror("inotify_init1"); | |
905 | exit(EXIT_FAILURE); | |
906 | } | |
907 | ||
908 | /* Allocate memory for watch descriptors */ | |
909 | ||
910 | wd = calloc(argc, sizeof(int)); | |
911 | if (wd == NULL) { | |
912 | perror("calloc"); | |
913 | exit(EXIT_FAILURE); | |
914 | } | |
915 | ||
916 | /* Mark directories for events | |
917 | \- file was opened | |
918 | \- file was closed */ | |
919 | ||
920 | for (i = 1; i < argc; i++) { | |
921 | wd[i] = inotify_add_watch(fd, argv[i], | |
922 | IN_OPEN | IN_CLOSE); | |
923 | if (wd[i] == \-1) { | |
924 | fprintf(stderr, "Cannot watch '%s'\\n", argv[i]); | |
925 | perror("inotify_add_watch"); | |
926 | exit(EXIT_FAILURE); | |
927 | } | |
928 | } | |
929 | ||
930 | /* Prepare for polling */ | |
931 | ||
932 | nfds = 2; | |
933 | ||
934 | /* Console input */ | |
935 | ||
936 | fds[0].fd = STDIN_FILENO; | |
937 | fds[0].events = POLLIN; | |
938 | ||
939 | /* Inotify input */ | |
940 | ||
941 | fds[1].fd = fd; | |
942 | fds[1].events = POLLIN; | |
943 | ||
944 | /* Wait for events and/or terminal input */ | |
945 | ||
946 | printf("Listening for events.\\n"); | |
947 | while (1) { | |
948 | poll_num = poll(fds, nfds, \-1); | |
949 | if (poll_num == \-1) { | |
950 | if (errno == EINTR) | |
951 | continue; | |
952 | perror("poll"); | |
953 | exit(EXIT_FAILURE); | |
954 | } | |
955 | if (poll_num > 0) { | |
956 | ||
957 | if (fds[0].revents & POLLIN) { | |
958 | ||
959 | /* Console input is available. Empty stdin and quit */ | |
960 | ||
961 | while (read(STDIN_FILENO, &buf, 1) > 0 && buf != '\\n') | |
962 | continue; | |
963 | break; | |
964 | } | |
965 | if (fds[1].revents & POLLIN) { | |
966 | ||
967 | /* Inotify events are available */ | |
968 | handle_events(fd, wd, argc, argv); | |
969 | } | |
970 | } | |
971 | } | |
972 | ||
973 | /* Close inotify file descriptor */ | |
974 | ||
975 | close(fd); | |
976 | free(wd); | |
977 | printf("Listening for events stopped.\\n"); | |
978 | exit(EXIT_SUCCESS); | |
979 | } | |
980 | .fi | |
47297adb | 981 | .SH SEE ALSO |
f0afb16a MK |
982 | .BR inotifywait (1), |
983 | .BR inotifywatch (1), | |
4d2b74dd MK |
984 | .BR inotify_add_watch (2), |
985 | .BR inotify_init (2), | |
43bb5faf | 986 | .BR inotify_init1 (2), |
4d2b74dd MK |
987 | .BR inotify_rm_watch (2), |
988 | .BR read (2), | |
f75d27e6 HS |
989 | .BR stat (2), |
990 | .BR fanotify (7) | |
173fe7e7 DP |
991 | |
992 | .IR Documentation/filesystems/inotify.txt | |
993 | in the Linux kernel source tree |