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