1Inotify2(3) User Contributed Perl Documentation Inotify2(3)
2
3
4
6 Linux::Inotify2 - scalable directory/file change notification
7
9 Callback Interface
10 use Linux::Inotify2;
11
12 # create a new object
13 my $inotify = new Linux::Inotify2
14 or die "unable to create new inotify object: $!";
15
16 # add watchers
17 $inotify->watch ("/etc/passwd", IN_ACCESS, sub {
18 my $e = shift;
19 my $name = $e->fullname;
20 print "$name was accessed\n" if $e->IN_ACCESS;
21 print "$name is no longer mounted\n" if $e->IN_UNMOUNT;
22 print "$name is gone\n" if $e->IN_IGNORED;
23 print "events for $name have been lost\n" if $e->IN_Q_OVERFLOW;
24
25 # cancel this watcher: remove no further events
26 $e->w->cancel;
27 });
28
29 # integration into AnyEvent (works with EV, Glib, Tk, POE...)
30 my $inotify_w = AE::io $inotify->fileno, 0, sub { $inotify->poll };
31
32 # manual event loop
33 $inotify->poll while 1;
34
35 Streaming Interface
36 use Linux::Inotify2;
37
38 # create a new object
39 my $inotify = new Linux::Inotify2
40 or die "Unable to create new inotify object: $!";
41
42 # create watch
43 $inotify->watch ("/etc/passwd", IN_ACCESS)
44 or die "watch creation failed";
45
46 while () {
47 my @events = $inotify->read;
48 printf "mask\t%d\n", $_->mask foreach @events;
49 }
50
52 This module implements an interface to the Linux 2.6.13 and later
53 Inotify file/directory change notification system.
54
55 It has a number of advantages over the Linux::Inotify module:
56
57 - it is portable (Linux::Inotify only works on x86)
58 - the equivalent of fullname works correctly
59 - it is better documented
60 - it has callback-style interface, which is better suited for
61 integration.
62
63 As for the inotify API itself - it is a very tricky, and somewhat
64 unreliable API. For a good overview of the challenges you might run
65 into, see this LWN article: <https://lwn.net/Articles/605128/>.
66
67 The Linux::Inotify2 Class
68 my $inotify = new Linux::Inotify2
69 Create a new notify object and return it. A notify object is kind
70 of a container that stores watches on file system names and is
71 responsible for handling event data.
72
73 On error, "undef" is returned and $! will be set accordingly. The
74 following errors are documented:
75
76 ENFILE The system limit on the total number of file descriptors has been reached.
77 EMFILE The user limit on the total number of inotify instances has been reached.
78 ENOMEM Insufficient kernel memory is available.
79
80 Example:
81
82 my $inotify = new Linux::Inotify2
83 or die "Unable to create new inotify object: $!";
84
85 $watch = $inotify->watch ($name, $mask[, $cb])
86 Add a new watcher to the given notifier. The watcher will create
87 events on the pathname $name as given in $mask, which can be any of
88 the following constants (all exported by default) ORed together.
89 Constants unavailable on your system will evaluate to 0.
90
91 "file" refers to any file system object in the watched object
92 (always a directory), that is files, directories, symlinks, device
93 nodes etc., while "object" refers to the object the watcher has
94 been set on itself:
95
96 IN_ACCESS object was accessed
97 IN_MODIFY object was modified
98 IN_ATTRIB object metadata changed
99 IN_CLOSE_WRITE writable fd to file / to object was closed
100 IN_CLOSE_NOWRITE readonly fd to file / to object closed
101 IN_OPEN object was opened
102 IN_MOVED_FROM file was moved from this object (directory)
103 IN_MOVED_TO file was moved to this object (directory)
104 IN_CREATE file was created in this object (directory)
105 IN_DELETE file was deleted from this object (directory)
106 IN_DELETE_SELF object itself was deleted
107 IN_MOVE_SELF object itself was moved
108 IN_ALL_EVENTS all of the above events
109
110 IN_ONESHOT only send event once
111 IN_ONLYDIR only watch the path if it is a directory
112 IN_DONT_FOLLOW don't follow a sym link (Linux 2.6.15+)
113 IN_EXCL_UNLINK don't create events for unlinked objects (Linux 2.6.36+)
114 IN_MASK_ADD not supported with the current version of this module
115
116 IN_CLOSE same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
117 IN_MOVE same as IN_MOVED_FROM | IN_MOVED_TO
118
119 $cb is a perl code reference that, if given, is called for each
120 event. It receives a "Linux::Inotify2::Event" object.
121
122 The returned $watch object is of class "Linux::Inotify2::Watch".
123
124 On error, "undef" is returned and $! will be set accordingly. The
125 following errors are documented:
126
127 EBADF The given file descriptor is not valid.
128 EINVAL The given event mask contains no legal events.
129 ENOMEM Insufficient kernel memory was available.
130 ENOSPC The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource.
131 EACCESS Read access to the given file is not permitted.
132
133 Example, show when "/etc/passwd" gets accessed and/or modified
134 once:
135
136 $inotify->watch ("/etc/passwd", IN_ACCESS | IN_MODIFY, sub {
137 my $e = shift;
138 print "$e->{w}{name} was accessed\n" if $e->IN_ACCESS;
139 print "$e->{w}{name} was modified\n" if $e->IN_MODIFY;
140 print "$e->{w}{name} is no longer mounted\n" if $e->IN_UNMOUNT;
141 print "events for $e->{w}{name} have been lost\n" if $e->IN_Q_OVERFLOW;
142
143 $e->w->cancel;
144 });
145
146 $inotify->fileno
147 Returns the file descriptor for this notify object. When in non-
148 blocking mode, you are responsible for calling the "poll" method
149 when this file descriptor becomes ready for reading.
150
151 $inotify->fh
152 Similar to "fileno", but returns a perl file handle instead.
153
154 $inotify->blocking ($blocking)
155 Clears ($blocking true) or sets ($blocking false) the "O_NONBLOCK"
156 flag on the file descriptor.
157
158 $count = $inotify->poll
159 Reads events from the kernel and handles them. If the notify file
160 descriptor is blocking (the default), then this method waits for at
161 least one event. Otherwise it returns immediately when no pending
162 events could be read.
163
164 Returns the count of events that have been handled (which can be 0
165 in case events have been received but have been ignored or handled
166 internally).
167
168 Croaks when an error occurs.
169
170 @events = $inotify->read
171 Reads events from the kernel. Blocks when the file descriptor is in
172 blocking mode (default) until any event arrives. Returns list of
173 "Linux::Inotify2::Event" objects or empty list if none (non-
174 blocking mode or events got ignored).
175
176 Croaks on error.
177
178 Normally you shouldn't use this function, but instead use watcher
179 callbacks and call "->poll".
180
181 $inotify->on_overflow ($cb->($ev))
182 Sets the callback to be used for overflow handling (default:
183 "undef"): When "read" receives an event with "IN_Q_OVERFLOW" set,
184 it will invoke this callback with the event.
185
186 When the callback is "undef", then it broadcasts the event to all
187 registered watchers, i.e., "undef" is equivalent to:
188
189 sub { $inotify->broadcast ($_[0]) }
190
191 $inotify->broadcast ($ev)
192 Invokes all registered watcher callbacks and passes the given event
193 to them. Most useful in overflow handlers.
194
195 The Linux::Inotify2::Event Class
196 Objects of this class are handed as first argument to the watcher
197 callback. It has the following members and methods:
198
199 $event->w
200 $event->{w}
201 The watcher object for this event, if one is available. Generally,
202 you cna only rely on the value of this member inside watcher
203 callbacks.
204
205 $event->name
206 $event->{name}
207 The path of the file system object, relative to the watched name.
208
209 $event->fullname
210 Returns the "full" name of the relevant object, i.e. including the
211 "name" member of the watcher (if the watch object is on a directory
212 and a directory entry is affected), or simply the "name" member
213 itself when the object is the watch object itself.
214
215 This call requires "$event->{w}" to be valid, which is generally
216 only the case within watcher callbacks.
217
218 $event->mask
219 $event->{mask}
220 The received event mask. In addition to the events described for
221 "$inotify->watch", the following flags (exported by default) can be
222 set:
223
224 IN_ISDIR event object is a directory
225 IN_Q_OVERFLOW event queue overflowed
226
227 # when any of the following flags are set,
228 # then watchers for this event are automatically canceled
229 IN_UNMOUNT filesystem for watched object was unmounted
230 IN_IGNORED file was ignored/is gone (no more events are delivered)
231 IN_ONESHOT only one event was generated
232 IN_Q_OVERFLOW queue overflow - event might not be specific to a watcher
233
234 $event->IN_xxx
235 Returns a boolean that returns true if the event mask contains any
236 events specified by the mask. All of the "IN_xxx" constants can be
237 used as methods.
238
239 $event->cookie
240 $event->{cookie}
241 The event cookie to "synchronize two events". Normally zero, this
242 value is set when two events relating to the same file are
243 generated. As far as I know, this only happens for "IN_MOVED_FROM"
244 and "IN_MOVED_TO" events, to identify the old and new name of a
245 file.
246
247 Note that the inotify API makes it impossible to know whether there
248 will be a "IN_MOVED_TO" event - you might receive only one of the
249 events, and even if you receive both, there might be any number of
250 events in between. The best approach seems to be to implement a
251 small timeout after "IN_MOVED_FROM" to see if a matching
252 "IN_MOVED_TO" event will be received - 2ms seem to work relatively
253 well.
254
255 The Linux::Inotify2::Watch Class
256 Watcher objects are created by calling the "watch" method of a
257 notifier.
258
259 It has the following members and methods:
260
261 $watch->name
262 $watch->{name}
263 The name as specified in the "watch" call. For the object itself,
264 this is the empty string. For directory watches, this is the name
265 of the entry without leading path elements.
266
267 $watch->mask
268 $watch->{mask}
269 The mask as specified in the "watch" call.
270
271 $watch->cb ([new callback])
272 $watch->{cb}
273 The callback as specified in the "watch" call. Can optionally be
274 changed.
275
276 $watch->cancel
277 Cancels/removes this watcher. Future events, even if already queued
278 queued, will not be handled and resources will be freed.
279
281 AnyEvent, Linux::Inotify.
282
284 Marc Lehmann <schmorp@schmorp.de>
285 http://home.schmorp.de/
286
287
288
289perl v5.36.0 2023-01-20 Inotify2(3)