ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-Inotify2/README
Revision: 1.11
Committed: Thu Oct 25 01:19:37 2018 UTC (7 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-2_0, rel-2_1
Changes since 1.10: +51 -19 lines
Log Message:
2.0

File Contents

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