Inotify allows one to monitor files or directories for one or more events, like dnotify but better. All about the Linux kernels inotify component, brief sample code and updates about removal of /dev/inotify in favour of system calls.
This section is in a work in progress.
What is inotify?
Inotify is a replacement for dnotify, it's written by Robert Love and John McCutchan.
It's not fully part of the kernel, there is are patchs for Linux >=2.6
The interface is via device node (/dev/inotify) and it's controlled via IOCTL, super easy!
The inotify interface is now done via system calls inotify_ini(), inotify_add_watch(), inotify_rm_watch()
and then using plain old read()
.
There are 15 different events that inotify will tell you about directories or files that are being watched including READ, APPEND, CREATE, MOVE and CLOSE_WRITE
.
- inotify main page - http://www.kernel.org/pub/linux/kernel/people/rml/inotify/
- inotify latest: - inotify 0.24 - as of this writing.
Before proceeding you should understand the Linux kernel build process, how to use commands at a bash prompt, using patch and make. Knowledge of C or PERL is useful in addition to being required to understand what is happening here.
How does one get inotify
Fetch the source for the Linux kernel version 2.6.8.1; no other versions have been tested. Then apply the patch. Please note that the patch provided here is different that what is provided by Robert or John. Their patches are primarily meant for developers and kernel hacker types. This patch is for administrators or application developers who need the functionality of inotify. It's been edited to work in the environment described below and fit the expecations of the commands below.
- Setup the Linux source
- Wget the source for Linux 2.6.x into /usr/src/, bunzip and untar, symlink to /usr/src/linux
- Apply the patch
- Wget the patch into /usr/src/linux and say
patch -p0 < inotify-XXX.patch
- Configure
- Say make menuconfig. inotify will be found under Device Drivers -->Character Devices -->Inotify file change notification support. You may either enable or disable inotify, there is no module support.
- Build it
- Build your kernel, copy to /boot and configure your bootloader to recognize it.
Now you can use the PERL program below to check it out.
Notes
- Most of the Kernels that are supplied with Gentoo have Inotify.
- Some Inotify parameters are tunable in
/proc/sys/fs/inotify/
.
C Code Snip
Incomplete, for a better example look at the Beagle source code in glue/inotify-glue.c
.
// Initialize Inotify fd = inotify_init (); if (fd < 0) return -errno; // Add a Watch int wd; wd = inotify_add_watch (fd, filename, mask); if (wd < 0) return -errno; // Read an inotify event (buffer should be at least the size of static struc inotify_event *buffer = NULL; buffer_size = sizeof (struct inotify_event); *nr = read (fd, buffer, buffer_size); inotify_rm_watch(wd);
PERL Code Snip
Using Linux::Inotify2, this package also allows the use of (really cool) callback functions on the watch. It's then up to the developer to call poll(), see the Linux::Inotify2 page for more information. This sample takes a more procedural approach.
use Linux::Inotify2; my $inotify = new Linux::Inotify2(); foreach (@ARGV) { $inotify->watch($_, IN_ALL_EVENTS); } while (1) { # By default this will block until something is read my @events = $inotify->read(); if (scalar(@events)==0) { print "read error: $!"; last; } foreach (@events) { printf "File: %s; Mask: %d\n", $_->fullname, $_->mask; } }
See Also
- inotify-tools
- PERL Linux::Inotify - This guy beat me to packging it up - good work!
- PERL Linux::Inotify2 - Another package for Inotify - Edoceo currently uses this one, don't maintain ours anymore.
ChangeLog
- 2007-02-08 - Updated for inotify in kernel >= 2.6.13, link to Linux::Inotify2