Inotify
From Wikipedia, the free encyclopedia
- The correct title of this article is inotify. The initial letter is shown capitalized due to technical restrictions.
inotify is a Linux kernel subsystem that provides file system event notification. It was written by Robert Love and John McCutchan to replace dnotify. It was included in the mainline kernel from release 2.6.13, and could be compiled into 2.6.12 and possibly earlier releases by use of a patch. Its function is essentially an extension to filesystems to notice changes to the filesystem, and report those changes to applications.
Its major use is therefore arguably in desktop search utilities like Beagle, where its functionality permits reindexing of changed files without scanning the filesystem for changes every few minutes, which would entail a lengthy delay and be very CPU intensive. By being told that a file has changed directly by the kernel, rather than actively looking, Beagle and such utilities can achieve change-to-reindexing times of only about a second, with very small performance hits (inotify therefore enables the use of such programs in a sensible manner; daemons are generally not accepted by distributors if they drain system performance noticeably to provide userland functionality.)
It can also be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.
Contents |
[edit] Advantages
Inotify has many advantages over dnotify, the module that it replaced. With the older module, a program had to use one file descriptor for each directory that it was monitoring. This can become a bottleneck since the limit of file descriptor per process can be reached. The use of file descriptors along with dnotify proved to be a problem when using removable media. Devices couldn't be unmounted since file descriptors kept the resource busy.
Another drawback of dnotify, is the level of granularity since you can only monitor changes at the directory level. To get more information about the exact changes that happened when you get a notification you need to use a stat structure. Programmers are forced to keep a cache of stat structures and when a change is made to the files within a directory a new stat structure needs to be generated and compared against the cached one.
Inotify uses a simple and elegant API that uses minimal file descriptors allowing programmers to use the select and poll interface that they are more comfortable with compared to the signal notification system used by dnotify. It also makes integration with existing select- or poll-based libraries (like GLib) easier. Inotify also provides more granularity than dnotify. The interface to inotify is provided through a node.
[edit] How it Works
Inotify is used through a series of system calls specifically created for inotify.
int inotify_init ()
Creates an inotify instance. Returns a file descriptor which all events are read from.
int inotify_add_watch (int fd, const char* pathname, int mask)
Starts a watch for pathname for events contained in mask. Returns a watch descriptor which is unique (within this inotify instance) to the inode pointed to by the pathname (NOTE: Multiple pathnames can point to the same inode/watch descriptor).
int inotify_rm_watch (int fd, int wd)
Cancels a watch on the given watch descriptor.
Events generated by inotify contain the following information:
identifier | contents |
---|---|
wd | watch descriptor |
mask | event tag |
cookie | cookie used to synchronize between IN_MOVED_FROM and IN_MOVED_TO |
len | length of name field |
name | the (optional) filename associated with this event (local to parent directory) |
Some of the events that can be monitored for are:
- IN_ACCESS - last access of the file
- IN_MODIFY - last modification
- IN_ATTRIB - attributes of file change
- IN_OPEN and IN_CLOSE - open or close of file
- IN_MOVED_FROM and IN_MOVED_TO - when the file is moved or renamed
- IN_CREATE_SUBDIR and IN_DELETE_SUBDIR - create or delete of directory
- IN_CREATE_FILE and IN_DELETE_FILE create or delete file in a directory
- IN_DELETE_SELF - file monitored is deleted
- IN_UNMOUNT - when the device where the file is stored is unmounted
[edit] Notes on using inotify
To use inotify correctly and robustly you must take the following into consideration:
- Tracking moves: information needed
- When not to watch a file directly: If you want to know when a pathname is deleted you must watch the parent directory instead of the pathname directly
- Allowing for multiple pathnames to map to same watch descriptor
[edit] History of inotify
- August 2005 - Merged into 2.6.13
- July 2004 - First release announcement
[edit] See also
[edit] External links
- inotify-tools A suite of userland utilities, based on inotify, to watch and log modifications on files and directories
- Pyinotify A python interface to inotify.
- Official inotify README
- LWN Article on Inotify Watching filesystem events with inotify (partly out of date)
- IBM Article Monitor Linux file system events with inotify (partly out of date)