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           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->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 file
157           descriptor is blocking (the default), then this method waits for at
158           least one event. Otherwise it returns immediately when no pending
159           events could be read.
160
161           Returns the count of events that have been handled (which can be 0
162           in case events have been received but have been ignored or handled
163           internally).
164
165           Croaks when an error occurs.
166
167       @events = $inotify->read
168           Reads events from the kernel. Blocks when the file descriptor is in
169           blocking mode (default) until any event arrives. Returns list of
170           "Linux::Inotify2::Event" objects or empty list if none (non-
171           blocking mode or events got ignored).
172
173           Croaks on error.
174
175           Normally you shouldn't use this function, but instead use watcher
176           callbacks and call "->poll".
177
178       $inotify->on_overflow ($cb->($ev))
179           Sets the callback to be used for overflow handling (default:
180           "undef"): When "read" receives an event with "IN_Q_OVERFLOW" set,
181           it will invoke this callback with the event.
182
183           When the callback is "undef", then it broadcasts the event to all
184           registered watchers, i.e., "undef" is equivalent to:
185
186              sub { $inotify->broadcast ($_[0]) }
187
188       $inotify->broadcast ($ev)
189           Invokes all registered watcher callbacks and passes the given event
190           to them. Most useful in overflow handlers.
191
192   The Linux::Inotify2::Event Class
193       Objects of this class are handed as first argument to the watcher
194       callback. It has the following members and methods:
195
196       $event->w
197       $event->{w}
198           The watcher object for this event, if one is available. Generally,
199           you cna only rely on the value of this member inside watcher
200           callbacks.
201
202       $event->name
203       $event->{name}
204           The path of the file system object, relative to the watched name.
205
206       $event->fullname
207           Returns the "full" name of the relevant object, i.e. including the
208           "name" member of the watcher (if the watch object is on a directory
209           and a directory entry is affected), or simply the "name" member
210           itself when the object is the watch object itself.
211
212           This call requires "$event->{w}" to be valid, which is generally
213           only the case within watcher callbacks.
214
215       $event->mask
216       $event->{mask}
217           The received event mask. In addition to the events described for
218           "$inotify->watch", the following flags (exported by default) can be
219           set:
220
221            IN_ISDIR             event object is a directory
222            IN_Q_OVERFLOW        event queue overflowed
223
224            # when any of the following flags are set,
225            # then watchers for this event are automatically canceled
226            IN_UNMOUNT           filesystem for watched object was unmounted
227            IN_IGNORED           file was ignored/is gone (no more events are delivered)
228            IN_ONESHOT           only one event was generated
229            IN_Q_OVERFLOW        queue overflow - event might not be specific to a watcher
230
231       $event->IN_xxx
232           Returns a boolean that returns true if the event mask contains any
233           events specified by the mask. All of the "IN_xxx" constants can be
234           used as methods.
235
236       $event->cookie
237       $event->{cookie}
238           The event cookie to "synchronize two events". Normally zero, this
239           value is set when two events relating to the same file are
240           generated. As far as I know, this only happens for "IN_MOVED_FROM"
241           and "IN_MOVED_TO" events, to identify the old and new name of a
242           file.
243
244           Note that the inotify API makes it impossible to know whether there
245           will be a "IN_MOVED_TO" event - you might receive only one of the
246           events, and even if you receive both, there might be any number of
247           events in between. The best approach seems to be to implement a
248           small timeout after "IN_MOVED_FROM" to see if a matching
249           "IN_MOVED_TO" event will be received - 2ms seem to work relatively
250           well.
251
252   The Linux::Inotify2::Watch Class
253       Watcher objects are created by calling the "watch" method of a
254       notifier.
255
256       It has the following members and methods:
257
258       $watch->name
259       $watch->{name}
260           The name as specified in the "watch" call. For the object itself,
261           this is the empty string.  For directory watches, this is the name
262           of the entry without leading path elements.
263
264       $watch->mask
265       $watch->{mask}
266           The mask as specified in the "watch" call.
267
268       $watch->cb ([new callback])
269       $watch->{cb}
270           The callback as specified in the "watch" call. Can optionally be
271           changed.
272
273       $watch->cancel
274           Cancels/removes this watcher. Future events, even if already queued
275           queued, will not be handled and resources will be freed.
276

SEE ALSO

278       AnyEvent, Linux::Inotify.
279

AUTHOR

281        Marc Lehmann <schmorp@schmorp.de>
282        http://home.schmorp.de/
283
284
285
286perl v5.32.0                      2020-07-28                       Inotify2(3)
Impressum