]>
Commit | Line | Data |
---|---|---|
415f63be | 1 | .\" Copyright (C) 2006, 2014 Michael Kerrisk <mtk.manpages@gmail.com> |
ecd96f7c | 2 | .\" Copyright (C) 2014 Heinrich Schuchardt <xypron.glpk@gmx.de> |
4d2b74dd | 3 | .\" |
5fbde956 | 4 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
4d2b74dd | 5 | .\" |
4c1c5274 | 6 | .TH inotify 7 (date) "Linux man-pages (unreleased)" |
4d2b74dd | 7 | .SH NAME |
9ee4a2b6 | 8 | inotify \- monitoring filesystem events |
4d2b74dd MK |
9 | .SH DESCRIPTION |
10 | The | |
c13182ef | 11 | .I inotify |
9ee4a2b6 | 12 | API provides a mechanism for monitoring filesystem events. |
4d2b74dd MK |
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. | |
a721e8b2 | 17 | .PP |
c13182ef | 18 | The following system calls are used with this API: |
cdede5cd | 19 | .IP \[bu] 3 |
4d2b74dd | 20 | .BR inotify_init (2) |
c13182ef | 21 | creates an inotify instance and returns a file descriptor |
a2cc46ca | 22 | referring to the inotify instance. |
43bb5faf MK |
23 | The more recent |
24 | .BR inotify_init1 (2) | |
25 | is like | |
26 | .BR inotify_init (2), | |
356911f6 | 27 | but has a |
1ae6b2c7 | 28 | .I flags |
356911f6 | 29 | argument that provides access to some extra functionality. |
cdede5cd | 30 | .IP \[bu] |
4d2b74dd | 31 | .BR inotify_add_watch (2) |
a2cc46ca | 32 | manipulates the "watch list" associated with an inotify instance. |
3a065ac0 | 33 | Each item ("watch") in the watch list specifies the pathname of |
c13182ef | 34 | a file or directory, |
4d2b74dd MK |
35 | along with some set of events that the kernel should monitor for the |
36 | file referred to by that pathname. | |
63f6a20a | 37 | .BR inotify_add_watch (2) |
4d2b74dd MK |
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 | |
63f6a20a | 41 | .BR inotify_add_watch (2) |
4d2b74dd | 42 | when the watch is created. |
cdede5cd | 43 | .IP \[bu] |
356911f6 MK |
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). | |
cdede5cd | 49 | .IP \[bu] |
4d2b74dd MK |
50 | .BR inotify_rm_watch (2) |
51 | removes an item from an inotify watch list. | |
cdede5cd | 52 | .IP \[bu] |
c13182ef | 53 | When all file descriptors referring to an inotify |
356911f6 MK |
54 | instance have been closed (using |
55 | .BR close (2)), | |
c13182ef | 56 | the underlying object and its resources are |
3b777aff | 57 | freed for reuse by the kernel; |
4d2b74dd | 58 | all associated watches are automatically freed. |
f070e954 | 59 | .PP |
6b1cc2c9 MK |
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. | |
e3a0ac0e | 66 | It is probably wise to do some consistency checking, |
6b1cc2c9 | 67 | and rebuild the cache when inconsistencies are detected. |
ff6e2397 | 68 | .SS Reading events from an inotify file descriptor |
4d2b74dd MK |
69 | To determine what events have occurred, an application |
70 | .BR read (2)s | |
71 | from the inotify file descriptor. | |
c13182ef | 72 | If no events have so far occurred, then, |
11da88fb | 73 | assuming a blocking file descriptor, |
63f6a20a | 74 | .BR read (2) |
01538d0d MK |
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)). | |
a721e8b2 | 81 | .PP |
4d2b74dd | 82 | Each successful |
63f6a20a | 83 | .BR read (2) |
4d2b74dd | 84 | returns a buffer containing one or more of the following structures: |
e646a1ba | 85 | .PP |
a08ea57c | 86 | .in +4n |
e646a1ba | 87 | .EX |
4d2b74dd MK |
88 | struct inotify_event { |
89 | int wd; /* Watch descriptor */ | |
24bbe02c MK |
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 | |
75c018a1 | 93 | .\" glibc bug filed: http://sources.redhat.com/bugzilla/show_bug.cgi?id=7040 |
425ad621 | 94 | uint32_t mask; /* Mask describing event */ |
c13182ef | 95 | uint32_t cookie; /* Unique cookie associating related |
4d2b74dd | 96 | events (for rename(2)) */ |
84c517a4 | 97 | uint32_t len; /* Size of \fIname\fP field */ |
d064d41a | 98 | char name[]; /* Optional null\-terminated name */ |
4d2b74dd | 99 | }; |
b8302363 | 100 | .EE |
a08ea57c | 101 | .in |
a721e8b2 | 102 | .PP |
4d2b74dd MK |
103 | .I wd |
104 | identifies the watch for which this event occurs. | |
c13182ef | 105 | It is one of the watch descriptors returned by a previous call to |
63f6a20a | 106 | .BR inotify_add_watch (2). |
a721e8b2 | 107 | .PP |
4d2b74dd MK |
108 | .I mask |
109 | contains bits that describe the event that occurred (see below). | |
a721e8b2 | 110 | .PP |
4d2b74dd MK |
111 | .I cookie |
112 | is a unique integer that connects related events. | |
34d4e61d | 113 | Currently, this is used only for rename events, and |
4d2b74dd | 114 | allows the resulting pair of |
bc636d8a | 115 | .B IN_MOVED_FROM |
c13182ef | 116 | and |
bc636d8a | 117 | .B IN_MOVED_TO |
4d2b74dd | 118 | events to be connected by the application. |
591b7a5f MK |
119 | For all other event types, |
120 | .I cookie | |
121 | is set to 0. | |
a721e8b2 | 122 | .PP |
c13182ef | 123 | The |
4d2b74dd | 124 | .I name |
33a0ccb2 | 125 | field is present only when an event is returned |
c13182ef | 126 | for a file inside a watched directory; |
d1203f45 | 127 | it identifies the filename within the watched directory. |
454ab872 | 128 | This filename is null-terminated, |
d6c1998e AC |
129 | and may include further null bytes (\[aq]\e0\[aq]) |
130 | to align subsequent reads to a suitable address boundary. | |
a721e8b2 | 131 | .PP |
4d2b74dd MK |
132 | The |
133 | .I len | |
c13182ef | 134 | field counts all of the bytes in |
4d2b74dd | 135 | .IR name , |
c13182ef | 136 | including the null bytes; |
4d2b74dd MK |
137 | the length of each |
138 | .I inotify_event | |
139 | structure is thus | |
655684a9 | 140 | .IR "sizeof(struct inotify_event)+len" . |
a721e8b2 | 141 | .PP |
988db661 | 142 | The behavior when the buffer given to |
c7e3ee6f | 143 | .BR read (2) |
988db661 | 144 | is too small to return information about the next event depends |
b324e17d | 145 | on the kernel version: before Linux 2.6.21, |
c7e3ee6f | 146 | .BR read (2) |
b324e17d | 147 | returns 0; since Linux 2.6.21, |
c7e3ee6f MK |
148 | .BR read (2) |
149 | fails with the error | |
150 | .BR EINVAL . | |
4ba272b3 | 151 | Specifying a buffer of size |
a721e8b2 | 152 | .PP |
1ae6b2c7 AC |
153 | .in +4n |
154 | .EX | |
155 | sizeof(struct inotify_event) + NAME_MAX + 1 | |
156 | .EE | |
157 | .in | |
a721e8b2 | 158 | .PP |
4ba272b3 | 159 | will be sufficient to read at least one event. |
4d2b74dd | 160 | .SS inotify events |
c13182ef | 161 | The |
4d2b74dd MK |
162 | .BR inotify_add_watch (2) |
163 | .I mask | |
c13182ef | 164 | argument and the |
4d2b74dd MK |
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 | |
63f6a20a | 175 | .BR inotify_add_watch (2) |
c13182ef | 176 | and may be returned in the |
4d2b74dd MK |
177 | .I mask |
178 | field returned by | |
63f6a20a | 179 | .BR read (2): |
64aa9bcb | 180 | .RS 4 |
c577b95c | 181 | .TP |
f279ad49 | 182 | .BR IN_ACCESS " (+)" |
70f70c9d MK |
183 | File was accessed (e.g., |
184 | .BR read (2), | |
f23fc716 | 185 | .BR execve (2)). |
3f174f7d | 186 | .TP |
f23fc716 | 187 | .BR IN_ATTRIB " (*)" |
36546c38 | 188 | Metadata changed\[em]for example, permissions (e.g., |
b0e1ac43 MK |
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., | |
a4439a50 MK |
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. | |
b0e1ac43 MK |
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)). | |
3f174f7d | 205 | .TP |
858bbaaa | 206 | .BR IN_CLOSE_WRITE " (+)" |
f23fc716 | 207 | File opened for writing was closed. |
3f174f7d | 208 | .TP |
f23fc716 | 209 | .BR IN_CLOSE_NOWRITE " (*)" |
7465ffa6 | 210 | File or directory not opened for writing was closed. |
3f174f7d | 211 | .TP |
e95b1911 | 212 | .BR IN_CREATE " (+)" |
7a64793b MK |
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), | |
1a737afd | 218 | .BR symlink (2), |
7a64793b MK |
219 | .BR bind (2) |
220 | on a UNIX domain socket). | |
3f174f7d | 221 | .TP |
e95b1911 | 222 | .BR IN_DELETE " (+)" |
f23fc716 | 223 | File/directory deleted from watched directory. |
3f174f7d MK |
224 | .TP |
225 | .B IN_DELETE_SELF | |
226 | Watched file/directory was itself deleted. | |
4a1e4cca MK |
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 | |
49b07b8f | 231 | then deletes it from the original filesystem.) |
39f43968 MK |
232 | In addition, an |
233 | .B IN_IGNORED | |
234 | event will subsequently be generated for the watch descriptor. | |
3f174f7d | 235 | .TP |
e06fc20c | 236 | .BR IN_MODIFY " (+)" |
f54a1255 MK |
237 | File was modified (e.g., |
238 | .BR write (2), | |
239 | .BR truncate (2)). | |
3f174f7d MK |
240 | .TP |
241 | .B IN_MOVE_SELF | |
242 | Watched file/directory was itself moved. | |
243 | .TP | |
e95b1911 | 244 | .BR IN_MOVED_FROM " (+)" |
107375cf | 245 | Generated for the directory containing the old filename |
f23fc716 | 246 | when a file is renamed. |
3f174f7d | 247 | .TP |
e95b1911 | 248 | .BR IN_MOVED_TO " (+)" |
107375cf | 249 | Generated for the directory containing the new filename |
f23fc716 | 250 | when a file is renamed. |
3f174f7d | 251 | .TP |
f23fc716 | 252 | .BR IN_OPEN " (*)" |
7465ffa6 | 253 | File or directory was opened. |
64aa9bcb | 254 | .RE |
4d2b74dd | 255 | .PP |
7954e552 MK |
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). | |
a721e8b2 | 260 | .PP |
e95b1911 | 261 | When monitoring a directory: |
cdede5cd | 262 | .IP \[bu] 3 |
e95b1911 MK |
263 | the events marked above with an asterisk (*) can occur both |
264 | for the directory itself and for objects inside the directory; and | |
cdede5cd | 265 | .IP \[bu] |
e95b1911 MK |
266 | the events marked with a plus sign (+) occur only for objects |
267 | inside the directory (not for the directory itself). | |
268 | .PP | |
705bb673 MK |
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. | |
a721e8b2 | 274 | .PP |
e95b1911 | 275 | When events are generated for objects inside a watched directory, the |
4d2b74dd MK |
276 | .I name |
277 | field in the returned | |
278 | .I inotify_event | |
279 | structure identifies the name of the file within the directory. | |
280 | .PP | |
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 | |
63f6a20a | 287 | .BR inotify_add_watch (2). |
a721e8b2 | 288 | .PP |
dede00fe | 289 | Two additional convenience macros are defined: |
64aa9bcb | 290 | .RS 4 |
dede00fe | 291 | .TP |
1ae6b2c7 | 292 | .B IN_MOVE |
dede00fe MK |
293 | Equates to |
294 | .BR "IN_MOVED_FROM | IN_MOVED_TO" . | |
295 | .TP | |
1ae6b2c7 | 296 | .B IN_CLOSE |
dede00fe MK |
297 | Equates to |
298 | .BR "IN_CLOSE_WRITE | IN_CLOSE_NOWRITE" . | |
64aa9bcb | 299 | .RE |
4d2b74dd | 300 | .PP |
c13182ef | 301 | The following further bits can be specified in |
4d2b74dd MK |
302 | .I mask |
303 | when calling | |
63f6a20a | 304 | .BR inotify_add_watch (2): |
64aa9bcb | 305 | .RS 4 |
c577b95c | 306 | .TP |
31daf529 | 307 | .BR IN_DONT_FOLLOW " (since Linux 2.6.15)" |
aeb9b6a6 MK |
308 | Don't dereference |
309 | .I pathname | |
310 | if it is a symbolic link. | |
dda869a4 | 311 | .TP |
0ff2cc88 | 312 | .BR IN_EXCL_UNLINK " (since Linux 2.6.36)" |
b3ad7609 MK |
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 | |
dda869a4 | 328 | .B IN_MASK_ADD |
7747b84b MK |
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 | |
381b7a91 HW |
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. | |
dda869a4 MK |
339 | .TP |
340 | .B IN_ONESHOT | |
dcaff23c | 341 | Monitor the filesystem object corresponding to |
aeb9b6a6 MK |
342 | .I pathname |
343 | for one event, then remove from | |
dda869a4 MK |
344 | watch list. |
345 | .TP | |
31daf529 | 346 | .BR IN_ONLYDIR " (since Linux 2.6.15)" |
4e9362fe | 347 | Watch |
aeb9b6a6 | 348 | .I pathname |
390795d7 MK |
349 | only if it is a directory; |
350 | the error | |
351 | .B ENOTDIR | |
352 | results if | |
353 | .I pathname | |
354 | is not a directory. | |
18d92a14 | 355 | Using this flag provides an application with a race-free way of |
9dcdfde4 | 356 | ensuring that the monitored object is a directory. |
381b7a91 | 357 | .TP |
859758b6 | 358 | .BR IN_MASK_CREATE " (since Linux 4.18)" |
381b7a91 HW |
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 | |
859758b6 | 366 | is already being watched. |
5ef4a59d | 367 | .IP |
381b7a91 | 368 | Using this flag provides an application with a way of ensuring |
859758b6 | 369 | that new watches do not modify existing ones. |
5ef4a59d MK |
370 | This is useful because multiple paths may refer to the same inode, |
371 | and multiple calls to | |
859758b6 | 372 | .BR inotify_add_watch (2) |
381b7a91 | 373 | without this flag may clobber existing watch masks. |
64aa9bcb | 374 | .RE |
4d2b74dd MK |
375 | .PP |
376 | The following bits may be set in the | |
377 | .I mask | |
378 | field returned by | |
63f6a20a | 379 | .BR read (2): |
64aa9bcb | 380 | .RS 4 |
c577b95c | 381 | .TP |
dda869a4 | 382 | .B IN_IGNORED |
aeb9b6a6 MK |
383 | Watch was removed explicitly |
384 | .RB ( inotify_rm_watch (2)) | |
9ee4a2b6 | 385 | or automatically (file was deleted, or filesystem was unmounted). |
7b5151b7 | 386 | See also BUGS. |
dda869a4 MK |
387 | .TP |
388 | .B IN_ISDIR | |
389 | Subject of this event is a directory. | |
390 | .TP | |
391 | .B IN_Q_OVERFLOW | |
aeb9b6a6 MK |
392 | Event queue overflowed |
393 | .RI ( wd | |
394 | is \-1 for this event). | |
dda869a4 MK |
395 | .TP |
396 | .B IN_UNMOUNT | |
9ee4a2b6 | 397 | Filesystem containing watched object was unmounted. |
b01c936e MK |
398 | In addition, an |
399 | .B IN_IGNORED | |
400 | event will subsequently be generated for the watch descriptor. | |
64aa9bcb | 401 | .RE |
415f63be MK |
402 | .SS Examples |
403 | Suppose an application is watching the directory | |
404 | .I dir | |
405 | and the file | |
1ae6b2c7 | 406 | .I dir/myfile |
415f63be MK |
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 | .PP | |
453 | Suppose an application is watching the directories | |
454 | .I dir1 | |
455 | and | |
d2c3d8a8 | 456 | .IR dir2 , |
415f63be MK |
457 | and the file |
458 | .IR dir1/myfile . | |
459 | The following examples show some events that may be generated. | |
460 | .RS 4 | |
461 | .TP | |
d2c3d8a8 | 462 | link("dir1/myfile", "dir2/new"); |
415f63be MK |
463 | Generates an |
464 | .B IN_ATTRIB | |
465 | event for | |
1ae6b2c7 | 466 | .I myfile |
415f63be MK |
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 | .PP | |
494 | Suppose that | |
1ae6b2c7 | 495 | .I dir1/xx |
415f63be | 496 | and |
1ae6b2c7 | 497 | .I dir2/yy |
415f63be MK |
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"); | |
d2c3d8a8 | 509 | Generates an |
1ae6b2c7 | 510 | .B IN_ATTRIB |
415f63be | 511 | event for |
1ae6b2c7 | 512 | .I xx |
415f63be MK |
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 | |
1ae6b2c7 | 524 | .B IN_IGNORED |
415f63be MK |
525 | events for |
526 | .IR xx , | |
527 | and an | |
1ae6b2c7 | 528 | .B IN_DELETE |
d2c3d8a8 | 529 | event for |
415f63be MK |
530 | .IR dir1 . |
531 | .RE | |
532 | .PP | |
533 | Suppose an application is watching the directory | |
1ae6b2c7 | 534 | .I dir |
415f63be MK |
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 | |
d2c3d8a8 | 546 | rmdir("dir/subdir"); |
415f63be MK |
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 | |
4d2b74dd | 558 | .SS /proc interfaces |
c13182ef | 559 | The following interfaces can be used to limit the amount of |
4d2b74dd MK |
560 | kernel memory consumed by inotify: |
561 | .TP | |
0daa9e92 | 562 | .I /proc/sys/fs/inotify/max_queued_events |
4d2b74dd MK |
563 | The value in this file is used when an application calls |
564 | .BR inotify_init (2) | |
c13182ef | 565 | to set an upper limit on the number of events that can be |
4d2b74dd MK |
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 | |
0daa9e92 | 571 | .I /proc/sys/fs/inotify/max_user_instances |
c13182ef | 572 | This specifies an upper limit on the number of inotify instances |
4d2b74dd MK |
573 | that can be created per real user ID. |
574 | .TP | |
0daa9e92 | 575 | .I /proc/sys/fs/inotify/max_user_watches |
31546b46 VN |
576 | This specifies an upper limit on the number of watches |
577 | that can be created per real user ID. | |
47297adb | 578 | .SH VERSIONS |
b324e17d AC |
579 | Inotify was merged into Linux 2.6.13. |
580 | The required library interfaces were added in glibc 2.4. | |
2b2581ee MK |
581 | .RB ( IN_DONT_FOLLOW , |
582 | .BR IN_MASK_ADD , | |
583 | and | |
584 | .B IN_ONLYDIR | |
b324e17d | 585 | were added in glibc 2.5.) |
3113c7f3 | 586 | .SH STANDARDS |
8382f16d | 587 | The inotify API is Linux-specific. |
47297adb | 588 | .SH NOTES |
4d2b74dd MK |
589 | Inotify file descriptors can be monitored using |
590 | .BR select (2), | |
591 | .BR poll (2), | |
c13182ef | 592 | and |
2315114c | 593 | .BR epoll (7). |
0000daa5 | 594 | When an event is available, the file descriptor indicates as readable. |
a721e8b2 | 595 | .PP |
0000daa5 MK |
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: | |
1ae6b2c7 | 613 | .I si_fd |
0000daa5 | 614 | is set to the inotify file descriptor number; |
1ae6b2c7 | 615 | .I si_signo |
0000daa5 | 616 | is set to the signal number; |
1ae6b2c7 | 617 | .I si_code |
0000daa5 MK |
618 | is set to |
619 | .BR POLL_IN ; | |
620 | and | |
621 | .B POLLIN | |
622 | is set in | |
623 | .IR si_band . | |
a721e8b2 | 624 | .PP |
c13182ef MK |
625 | If successive output inotify events produced on the |
626 | inotify file descriptor are identical (same | |
627 | .IR wd , | |
628 | .IR mask , | |
4d2b74dd MK |
629 | .IR cookie , |
630 | and | |
3f3698d8 | 631 | .IR name ), |
6f0ab035 MK |
632 | then they are coalesced into a single event if the |
633 | older event has not yet been read (but see BUGS). | |
8856aab8 MK |
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. | |
a721e8b2 | 637 | .PP |
c13182ef MK |
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 | |
4d2b74dd | 642 | correct order on the inotify file descriptor. |
a721e8b2 | 643 | .PP |
d742465f MK |
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 | |
1ae6b2c7 | 647 | .IR /proc/ pid /fdinfo |
d742465f MK |
648 | directory. |
649 | See | |
650 | .BR proc (5) | |
651 | for further details. | |
4d2b74dd MK |
652 | The |
653 | .B FIONREAD | |
63f6a20a | 654 | .BR ioctl (2) |
c13182ef | 655 | returns the number of bytes available to read from an |
4d2b74dd | 656 | inotify file descriptor. |
613836aa | 657 | .SS Limitations and caveats |
67898fdf MK |
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. | |
a721e8b2 | 664 | .PP |
e226bed7 MK |
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.) | |
fa1d49a6 | 671 | Furthermore, various pseudo-filesystems such as |
e226bed7 MK |
672 | .IR /proc , |
673 | .IR /sys , | |
674 | and | |
1ae6b2c7 | 675 | .I /dev/pts |
e226bed7 | 676 | are not monitorable with inotify. |
a721e8b2 | 677 | .PP |
e449e5f1 MK |
678 | The inotify API does not report file accesses and modifications that |
679 | may occur because of | |
7a8110f6 HS |
680 | .BR mmap (2), |
681 | .BR msync (2), | |
e449e5f1 | 682 | and |
7a8110f6 | 683 | .BR munmap (2). |
a721e8b2 | 684 | .PP |
67898fdf MK |
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. | |
a721e8b2 | 688 | .PP |
da977073 | 689 | The inotify API identifies events via watch descriptors. |
264276c6 MK |
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. | |
a721e8b2 | 693 | .PP |
4d2b74dd MK |
694 | Inotify monitoring of directories is not recursive: |
695 | to monitor subdirectories under a directory, | |
696 | additional watches must be created. | |
613836aa | 697 | This can take a significant amount time for large directory trees. |
a721e8b2 | 698 | .PP |
a79d28b2 MK |
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). | |
a721e8b2 | 707 | .PP |
613836aa MK |
708 | Note that the event queue can overflow. |
709 | In this case, events are lost. | |
09fa72fa | 710 | Robust applications should handle the possibility of |
613836aa | 711 | lost events gracefully. |
94d52c15 MK |
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, | |
85e179c5 | 716 | create a new inotify file descriptor, |
94d52c15 MK |
717 | and then re-create watches and cache entries |
718 | for the objects to be monitored.) | |
a721e8b2 | 719 | .PP |
c659c68e MK |
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 | .\" | |
85e179c5 | 727 | .SS Dealing with rename() events |
fa51f4d9 | 728 | As noted above, the |
85e179c5 MK |
729 | .B IN_MOVED_FROM |
730 | and | |
731 | .B IN_MOVED_TO | |
fa51f4d9 | 732 | event pair that is generated by |
85e179c5 | 733 | .BR rename (2) |
fa51f4d9 MK |
734 | can be matched up via their shared cookie value. |
735 | However, the task of matching has some challenges. | |
a721e8b2 | 736 | .PP |
fa51f4d9 MK |
737 | These two events are usually consecutive in the event stream available |
738 | when reading from the inotify file descriptor. | |
85e179c5 MK |
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. | |
003798c7 MK |
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. | |
a721e8b2 | 753 | .PP |
85e179c5 MK |
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 | |
1ae6b2c7 | 763 | .B IN_MOVED_TO |
85e179c5 MK |
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. | |
6f1a4954 | 776 | (Re-creating the inotify file descriptor and rebuilding the cache may |
85e179c5 | 777 | be useful to deal with this scenario.) |
a721e8b2 | 778 | .PP |
85e179c5 MK |
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 | |
3da91159 MK |
787 | .BR read (2), |
788 | which should be done with a (small) timeout to allow for the fact that | |
789 | insertion of the | |
57c230fd | 790 | .BR IN_MOVED_FROM + IN_MOVED_TO |
3da91159 MK |
791 | event pair is not atomic, |
792 | and also the possibility that there may not be any | |
793 | .B IN_MOVED_TO | |
794 | event. | |
47297adb | 795 | .SH BUGS |
f9bfa911 | 796 | Before Linux 3.19, |
360d3116 | 797 | .BR fallocate (2) |
f9bfa911 MK |
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. | |
a721e8b2 | 806 | .PP |
bea08fec | 807 | .\" FIXME . kernel commit 611da04f7a31b2208e838be55a42c7a1310ae321 |
b324e17d | 808 | .\" implies that unmount events were buggy since Linux 2.6.11 to Linux 2.6.36 |
a15ead5e | 809 | .\" |
b324e17d | 810 | Before Linux 2.6.16, the |
ed7b0235 | 811 | .B IN_ONESHOT |
c13182ef | 812 | .I mask |
ed7b0235 | 813 | flag does not work. |
a721e8b2 | 814 | .PP |
7b5151b7 MK |
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. | |
a721e8b2 | 824 | .PP |
b324e17d | 825 | Before Linux 2.6.25, |
22129aa9 | 826 | .\" commit 1c17d18e3775485bf1e0ce79575eb637a94494a2 |
9ed6b517 | 827 | the kernel code that was intended to coalesce successive identical events |
6f0ab035 MK |
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. | |
a721e8b2 | 833 | .PP |
ec976a53 | 834 | When a watch descriptor is removed by calling |
e530e367 MK |
835 | .BR inotify_rm_watch (2) |
836 | (or because a watch file is deleted or the filesystem | |
837 | that contains it is unmounted), | |
ec976a53 HS |
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 (0 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 | |
cbe0e644 | 849 | might then read those events and interpret them as belonging to |
ec976a53 | 850 | the file associated with the newly recycled watch descriptor. |
2bbf2299 MK |
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 | |
08063d8f | 856 | watch descriptor in the queue, |
2bbf2299 MK |
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, | |
bea08fec | 861 | .\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=77111 |
2bbf2299 | 862 | no kernel changes have yet been made to eliminate this possible bug. |
a14af333 | 863 | .SH EXAMPLES |
ecd96f7c HS |
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 , | |
d556548b | 868 | .BR IN_CLOSE_NOWRITE , |
ecd96f7c HS |
869 | and |
870 | .BR IN_CLOSE_WRITE . | |
871 | .PP | |
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 | |
b8302363 | 888 | .EX |
2483209a | 889 | $ \fB./a.out /tmp /home/user/temp\fP |
ecd96f7c HS |
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. | |
b8302363 | 898 | .EE |
ecd96f7c HS |
899 | .in |
900 | .SS Program source | |
c7885256 | 901 | \& |
e7d0bb47 | 902 | .EX |
ecd96f7c HS |
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> | |
113a073b | 909 | #include <string.h> |
ecd96f7c | 910 | |
b957f81f | 911 | /* Read all available inotify events from the file descriptor \[aq]fd\[aq]. |
ecd96f7c HS |
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 | { | |
2483209a MK |
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 | |
ecd96f7c | 924 | struct inotify_event. */ |
2483209a | 925 | |
ecd96f7c | 926 | char buf[4096] |
2483209a | 927 | __attribute__ ((aligned(__alignof__(struct inotify_event)))); |
ecd96f7c | 928 | const struct inotify_event *event; |
ecd96f7c | 929 | ssize_t len; |
ecd96f7c HS |
930 | |
931 | /* Loop while events can be read from inotify file descriptor. */ | |
932 | ||
933 | for (;;) { | |
934 | ||
935 | /* Read some events. */ | |
936 | ||
b5cd2470 | 937 | len = read(fd, buf, sizeof(buf)); |
ecd96f7c HS |
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 | ||
c6beb8a1 | 950 | /* Loop over all events in the buffer. */ |
ecd96f7c | 951 | |
88893a77 | 952 | for (char *ptr = buf; ptr < buf + len; |
2483209a | 953 | ptr += sizeof(struct inotify_event) + event\->len) { |
ecd96f7c HS |
954 | |
955 | event = (const struct inotify_event *) ptr; | |
956 | ||
c6beb8a1 | 957 | /* Print event type. */ |
ecd96f7c HS |
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 | ||
c6beb8a1 | 966 | /* Print the name of the watched directory. */ |
ecd96f7c | 967 | |
b42296e4 | 968 | for (size_t i = 1; i < argc; ++i) { |
ecd96f7c HS |
969 | if (wd[i] == event\->wd) { |
970 | printf("%s/", argv[i]); | |
971 | break; | |
972 | } | |
973 | } | |
974 | ||
c6beb8a1 | 975 | /* Print the name of the file. */ |
ecd96f7c HS |
976 | |
977 | if (event\->len) | |
978 | printf("%s", event\->name); | |
979 | ||
c6beb8a1 | 980 | /* Print type of filesystem object. */ |
ecd96f7c HS |
981 | |
982 | if (event\->mask & IN_ISDIR) | |
d1a71985 | 983 | printf(" [directory]\en"); |
ecd96f7c | 984 | else |
d1a71985 | 985 | printf(" [file]\en"); |
ecd96f7c HS |
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) { | |
d1a71985 | 1000 | printf("Usage: %s PATH [PATH ...]\en", argv[0]); |
ecd96f7c HS |
1001 | exit(EXIT_FAILURE); |
1002 | } | |
1003 | ||
d1a71985 | 1004 | printf("Press ENTER key to terminate.\en"); |
ecd96f7c | 1005 | |
c6beb8a1 | 1006 | /* Create the file descriptor for accessing the inotify API. */ |
ecd96f7c HS |
1007 | |
1008 | fd = inotify_init1(IN_NONBLOCK); | |
1009 | if (fd == \-1) { | |
1010 | perror("inotify_init1"); | |
1011 | exit(EXIT_FAILURE); | |
1012 | } | |
1013 | ||
c6beb8a1 | 1014 | /* Allocate memory for watch descriptors. */ |
ecd96f7c HS |
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) { | |
b957f81f | 1030 | fprintf(stderr, "Cannot watch \[aq]%s\[aq]: %s\en", |
5687d86f | 1031 | argv[i], strerror(errno)); |
ecd96f7c HS |
1032 | exit(EXIT_FAILURE); |
1033 | } | |
1034 | } | |
1035 | ||
c6beb8a1 | 1036 | /* Prepare for polling. */ |
ecd96f7c HS |
1037 | |
1038 | nfds = 2; | |
1039 | ||
a8114045 | 1040 | fds[0].fd = STDIN_FILENO; /* Console input */ |
ecd96f7c HS |
1041 | fds[0].events = POLLIN; |
1042 | ||
a8114045 | 1043 | fds[1].fd = fd; /* Inotify input */ |
ecd96f7c HS |
1044 | fds[1].events = POLLIN; |
1045 | ||
c6beb8a1 | 1046 | /* Wait for events and/or terminal input. */ |
ecd96f7c | 1047 | |
d1a71985 | 1048 | printf("Listening for events.\en"); |
ecd96f7c HS |
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 | } | |
2483209a | 1057 | |
ecd96f7c HS |
1058 | if (poll_num > 0) { |
1059 | ||
1060 | if (fds[0].revents & POLLIN) { | |
1061 | ||
c6beb8a1 | 1062 | /* Console input is available. Empty stdin and quit. */ |
ecd96f7c | 1063 | |
b957f81f | 1064 | while (read(STDIN_FILENO, &buf, 1) > 0 && buf != \[aq]\en\[aq]) |
ecd96f7c HS |
1065 | continue; |
1066 | break; | |
1067 | } | |
2483209a | 1068 | |
ecd96f7c HS |
1069 | if (fds[1].revents & POLLIN) { |
1070 | ||
c6beb8a1 | 1071 | /* Inotify events are available. */ |
2483209a | 1072 | |
ecd96f7c HS |
1073 | handle_events(fd, wd, argc, argv); |
1074 | } | |
1075 | } | |
1076 | } | |
1077 | ||
d1a71985 | 1078 | printf("Listening for events stopped.\en"); |
2483209a | 1079 | |
c6beb8a1 | 1080 | /* Close inotify file descriptor. */ |
ecd96f7c HS |
1081 | |
1082 | close(fd); | |
2483209a | 1083 | |
ecd96f7c | 1084 | free(wd); |
ecd96f7c HS |
1085 | exit(EXIT_SUCCESS); |
1086 | } | |
e7d0bb47 | 1087 | .EE |
47297adb | 1088 | .SH SEE ALSO |
f0afb16a MK |
1089 | .BR inotifywait (1), |
1090 | .BR inotifywatch (1), | |
4d2b74dd MK |
1091 | .BR inotify_add_watch (2), |
1092 | .BR inotify_init (2), | |
43bb5faf | 1093 | .BR inotify_init1 (2), |
4d2b74dd MK |
1094 | .BR inotify_rm_watch (2), |
1095 | .BR read (2), | |
f75d27e6 HS |
1096 | .BR stat (2), |
1097 | .BR fanotify (7) | |
a721e8b2 | 1098 | .PP |
1ae6b2c7 | 1099 | .I Documentation/filesystems/inotify.txt |
173fe7e7 | 1100 | in the Linux kernel source tree |