1Inotify2(3)           User Contributed Perl Documentation          Inotify2(3)
2
3
4

NAME

6       Linux::Inotify2 - scalable directory/file change notification
7

SYNOPSIS

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

DESCRIPTION

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
90           "file" refers to any file system object in the watched object
91           (always a directory), that is files, directories, symlinks, device
92           nodes etc., while "object" refers to the object the watcher has
93           been set on itself:
94
95            IN_ACCESS            object was accessed
96            IN_MODIFY            object was modified
97            IN_ATTRIB            object metadata changed
98            IN_CLOSE_WRITE       writable fd to file / to object was closed
99            IN_CLOSE_NOWRITE     readonly fd to file / to object closed
100            IN_OPEN              object was opened
101            IN_MOVED_FROM        file was moved from this object (directory)
102            IN_MOVED_TO          file was moved to this object (directory)
103            IN_CREATE            file was created in this object (directory)
104            IN_DELETE            file was deleted from this object (directory)
105            IN_DELETE_SELF       object itself was deleted
106            IN_MOVE_SELF         object itself was moved
107            IN_ALL_EVENTS        all of the above events
108
109            IN_ONESHOT           only send event once
110            IN_ONLYDIR           only watch the path if it is a directory
111            IN_DONT_FOLLOW       don't follow a sym link (Linux 2.6.15+)
112            IN_EXCL_UNLINK       don't create events for unlinked objects (Linux 2.6.36+)
113            IN_MASK_ADD          not supported with the current version of this module
114
115            IN_CLOSE             same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
116            IN_MOVE              same as IN_MOVED_FROM | IN_MOVED_TO
117
118           $cb is a perl code reference that, if given, is called for each
119           event. It receives a "Linux::Inotify2::Event" object.
120
121           The returned $watch object is of class "Linux::Inotify2::Watch".
122
123           On error, "undef" is returned and $! will be set accordingly. The
124           following errors are documented:
125
126            EBADF    The given file descriptor is not valid.
127            EINVAL   The given event mask contains no legal events.
128            ENOMEM   Insufficient kernel memory was available.
129            ENOSPC   The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource.
130            EACCESS  Read access to the given file is not permitted.
131
132           Example, show when "/etc/passwd" gets accessed and/or modified
133           once:
134
135              $inotify->watch ("/etc/passwd", IN_ACCESS | IN_MODIFY, sub {
136                 my $e = shift;
137                 print "$e->{w}{name} was accessed\n" if $e->IN_ACCESS;
138                 print "$e->{w}{name} was modified\n" if $e->IN_MODIFY;
139                 print "$e->{w}{name} is no longer mounted\n" if $e->IN_UNMOUNT;
140                 print "events for $e->{w}{name} have been lost\n" if $e->IN_Q_OVERFLOW;
141
142                 $e->w->cancel;
143              });
144
145       $inotify->fileno
146           Returns the file descriptor for this notify object. When in non-
147           blocking mode, you are responsible for calling the "poll" method
148           when this file descriptor becomes ready for reading.
149
150       $inotify->blocking ($blocking)
151           Clears ($blocking true) or sets ($blocking false) the "O_NONBLOCK"
152           flag on the file descriptor.
153
154       $count = $inotify->poll
155           Reads events from the kernel and handles them. If the notify file
156           descriptor is blocking (the default), then this method waits for at
157           least one event. Otherwise it returns immediately when no pending
158           events could be read.
159
160           Returns the count of events that have been handled (which can be 0
161           in case events have been received but have been ignored or handled
162           internally).
163
164           Croaks when an error occurs.
165
166       @events = $inotify->read
167           Reads events from the kernel. Blocks when the file descriptor is in
168           blocking mode (default) until any event arrives. Returns list of
169           "Linux::Inotify2::Event" objects or empty list if none (non-
170           blocking mode or events got ignored).
171
172           Croaks on error.
173
174           Normally you shouldn't use this function, but instead use watcher
175           callbacks and call "->poll".
176
177       $inotify->on_overflow ($cb->($ev))
178           Sets the callback to be used for overflow handling (default:
179           "undef"): When "read" receives an event with "IN_Q_OVERFLOW" set,
180           it will invoke this callback with the event.
181
182           When the callback is "undef", then it broadcasts the event to all
183           registered watchers, i.e., "undef" is equivalent to:
184
185              sub { $inotify->broadcast ($_[0]) }
186
187       $inotify->broadcast ($ev)
188           Invokes all registered watcher callbacks and passes the given event
189           to them. Most useful in overflow handlers.
190
191   The Linux::Inotify2::Event Class
192       Objects of this class are handed as first argument to the watcher
193       callback. It has the following members and methods:
194
195       $event->w
196       $event->{w}
197           The watcher object for this event, if one is available. Generally,
198           you cna only rely on the value of this member inside watcher
199           callbacks.
200
201       $event->name
202       $event->{name}
203           The path of the file system object, relative to the watched name.
204
205       $event->fullname
206           Returns the "full" name of the relevant object, i.e. including the
207           "name" member of the watcher (if the watch object is on a directory
208           and a directory entry is affected), or simply the "name" member
209           itself when the object is the watch object itself.
210
211           This call requires "$event->{w}" to be valid, which is generally
212           only the case within watcher callbacks.
213
214       $event->mask
215       $event->{mask}
216           The received event mask. In addition to the events described for
217           "$inotify->watch", the following flags (exported by default) can be
218           set:
219
220            IN_ISDIR             event object is a directory
221            IN_Q_OVERFLOW        event queue overflowed
222
223            # when any of the following flags are set,
224            # then watchers for this event are automatically canceled
225            IN_UNMOUNT           filesystem for watched object was unmounted
226            IN_IGNORED           file was ignored/is gone (no more events are delivered)
227            IN_ONESHOT           only one event was generated
228            IN_Q_OVERFLOW        queue overflow - event might not be specific to a watcher
229
230       $event->IN_xxx
231           Returns a boolean that returns true if the event mask contains any
232           events specified by the mask. All of the "IN_xxx" constants can be
233           used as methods.
234
235       $event->cookie
236       $event->{cookie}
237           The event cookie to "synchronize two events". Normally zero, this
238           value is set when two events relating to the same file are
239           generated. As far as I know, this only happens for "IN_MOVED_FROM"
240           and "IN_MOVED_TO" events, to identify the old and new name of a
241           file.
242
243           Note that the inotify API makes it impossible to know whether there
244           will be a "IN_MOVED_TO" event - you might receive only one of the
245           events, and even if you receive both, there might be any number of
246           events in between. The best approach seems to be to implement a
247           small timeout after "IN_MOVED_FROM" to see if a matching
248           "IN_MOVED_TO" event will be received - 2ms seem to work relatively
249           well.
250
251   The Linux::Inotify2::Watch Class
252       Watcher objects are created by calling the "watch" method of a
253       notifier.
254
255       It has the following members and methods:
256
257       $watch->name
258       $watch->{name}
259           The name as specified in the "watch" call. For the object itself,
260           this is the empty string.  For directory watches, this is the name
261           of the entry without leading path elements.
262
263       $watch->mask
264       $watch->{mask}
265           The mask as specified in the "watch" call.
266
267       $watch->cb ([new callback])
268       $watch->{cb}
269           The callback as specified in the "watch" call. Can optionally be
270           changed.
271
272       $watch->cancel
273           Cancels/removes this watcher. Future events, even if already queued
274           queued, will not be handled and resources will be freed.
275

SEE ALSO

277       AnyEvent, Linux::Inotify.
278

AUTHOR

280        Marc Lehmann <schmorp@schmorp.de>
281        http://home.schmorp.de/
282
283
284
285perl v5.28.1                      2018-10-26                       Inotify2(3)
Impressum