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 = AnyEvent->io (
31 fh => $inofity->fileno, poll => 'r', cb => sub { $inotify->poll }
32 );
33
34 # manual event loop
35 1 while $inotify->poll;
36
37 Streaming Interface
38 use Linux::Inotify2 ;
39
40 # create a new object
41 my $inotify = new Linux::Inotify2
42 or die "Unable to create new inotify object: $!" ;
43
44 # create watch
45 $inotify->watch ("/etc/passwd", IN_ACCESS)
46 or die "watch creation failed" ;
47
48 while () {
49 my @events = $inotify->read;
50 unless (@events > 0) {
51 print "read error: $!";
52 last ;
53 }
54 printf "mask\t%d\n", $_->mask foreach @events ;
55 }
56
58 This module implements an interface to the Linux 2.6.13 and later
59 Inotify file/directory change notification sytem.
60
61 It has a number of advantages over the Linux::Inotify module:
62
63 - it is portable (Linux::Inotify only works on x86)
64 - the equivalent of fullname works correctly
65 - it is better documented
66 - it has callback-style interface, which is better suited for
67 integration.
68
69 The Linux::Inotify2 Class
70 my $inotify = new Linux::Inotify2
71 Create a new notify object and return it. A notify object is kind
72 of a container that stores watches on filesystem names and is
73 responsible for handling event data.
74
75 On error, "undef" is returned and $! will be set accordingly. The
76 followign errors are documented:
77
78 ENFILE The system limit on the total number of file descriptors has been reached.
79 EMFILE The user limit on the total number of inotify instances has been reached.
80 ENOMEM Insufficient kernel memory is available.
81
82 Example:
83
84 my $inotify = new Linux::Inotify2
85 or die "Unable to create new inotify object: $!";
86
87 $watch = $inotify->watch ($name, $mask[, $cb])
88 Add a new watcher to the given notifier. The watcher will create
89 events on the pathname $name as given in $mask, which can be any of
90 the following constants (all exported by default) ORed together.
91
92 "file" refers to any filesystem object in the watch'ed object
93 (always a directory), that is files, directories, symlinks, device
94 nodes etc., while "object" refers to the object the watch has been
95 set on itself:
96
97 IN_ACCESS object was accessed
98 IN_MODIFY object was modified
99 IN_ATTRIB object metadata changed
100 IN_CLOSE_WRITE writable fd to file / to object was closed
101 IN_CLOSE_NOWRITE readonly fd to file / to object closed
102 IN_OPEN object was opened
103 IN_MOVED_FROM file was moved from this object (directory)
104 IN_MOVED_TO file was moved to this object (directory)
105 IN_CREATE file was created in this object (directory)
106 IN_DELETE file was deleted from this object (directory)
107 IN_DELETE_SELF object itself was deleted
108 IN_MOVE_SELF object itself was moved
109 IN_ALL_EVENTS all of the above events
110
111 IN_ONESHOT only send event once
112 IN_ONLYDIR only watch the path if it is a directory
113 IN_DONT_FOLLOW don't follow a sym link
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 fileno for this notify object. You are responsible for
148 calling the "poll" method when this fileno becomes ready for
149 reading.
150
151 $inotify->blocking ($blocking)
152 Clears ($blocking true) or sets ($blocking false) the "O_NONBLOCK"
153 flag on the file descriptor.
154
155 $count = $inotify->poll
156 Reads events from the kernel and handles them. If the notify fileno
157 is blocking (the default), then this method waits for at least one
158 event (and thus returns true unless an error occurs). Otherwise it
159 returns immediately when no pending events could be read.
160
161 Returns the count of events that have been handled.
162
163 $count = $inotify->read
164 Reads events from the kernel. Blocks in blocking mode (default)
165 until any event arrives. Returns list of "Linux::Inotify2::Event"
166 objects or empty list if none (non-blocking mode) or error occured
167 ($! should be checked).
168
169 The Linux::Inotify2::Event Class
170 Objects of this class are handed as first argument to the watch
171 callback. It has the following members and methods:
172
173 $event->w
174 $event->{w}
175 The watcher object for this event.
176
177 $event->name
178 $event->{name}
179 The path of the filesystem object, relative to the watch name.
180
181 $watch->fullname
182 Returns the "full" name of the relevant object, i.e. including the
183 "name" member of the watcher (if the the watch is on a directory
184 and a dir entry is affected), or simply the "name" member itself
185 when the object is the watch object itself.
186
187 $event->mask
188 $event->{mask}
189 The received event mask. In addition the the events described for
190 "$inotify-"watch>, the following flags (exported by default) can be
191 set:
192
193 IN_ISDIR event object is a directory
194 IN_Q_OVERFLOW event queue overflowed
195
196 # when any of the following flags are set,
197 # then watchers for this event are automatically canceled
198 IN_UNMOUNT filesystem for watch'ed object was unmounted
199 IN_IGNORED file was ignored/is gone (no more events are delivered)
200 IN_ONESHOT only one event was generated
201
202 $event->IN_xxx
203 Returns a boolean that returns true if the event mask matches the
204 event. All of the "IN_xxx" constants can be used as methods.
205
206 $event->cookie
207 $event->{cookie}
208 The event cookie to "synchronize two events". Normally zero, this
209 value is set when two events relating to the same file are
210 generated. As far as I know, this only happens for "IN_MOVED_FROM"
211 and "IN_MOVED_TO" events, to identify the old and new name of a
212 file.
213
214 The Linux::Inotify2::Watch Class
215 Watch objects are created by calling the "watch" method of a notifier.
216
217 It has the following members and methods:
218
219 $watch->name
220 $watch->{name}
221 The name as specified in the "watch" call. For the object itself,
222 this is the empty string. For directory watches, this is the name
223 of the entry without leading path elements.
224
225 $watch->mask
226 $watch->{mask}
227 The mask as specified in the "watch" call.
228
229 $watch->cb ([new callback])
230 $watch->{cb}
231 The callback as specified in the "watch" call. Can optionally be
232 changed.
233
234 $watch->cancel
235 Cancels/removes this watch. Future events, even if already queued
236 queued, will not be handled and resources will be freed.
237
239 AnyEvent, Linux::Inotify.
240
242 Marc Lehmann <schmorp@schmorp.de>
243 http://home.schmorp.de/
244
245
246
247perl v5.12.0 2009-09-21 Inotify2(3)