Jump to content

inotify

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Ottre (talk | contribs) at 14:06, 23 October 2008 (Advantages: ce). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

inotify is a Linux kernel subsystem that provides file system event notification. It was written by John McCutchan with help from Robert Love and later Amy Griffis to replace dnotify. It was included in the mainline kernel from release 2.6.13 (June 18, 2005), 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 be very inefficient. 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.[citation needed]

It can also be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.

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 could not be unmounted since file descriptors kept the resource busy.

Another drawback of dnotify is the level of granularity, since programmers can only monitor changes at the directory level. To access detailed information about the environmental changes that occur when a notification message is sent, a stat structure must be used; this is considered a necessary evil in that a cache of stat structures has to be maintained, for every new stat structure generated a comparison is run against the cached one.

Inotify uses an API that uses fewer file descriptors, allowing programmers to use the established select and poll interface, rather than the signal notification system used by dnotify. This also makes integration with existing select- or poll-based libraries (like GLib) easier.

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 watching the inode pointed to by 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 - read 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_DELETE - a file/directory deleted
  • IN_CREATE - a file/directory created
  • IN_DELETE_SELF - file monitored is deleted

History

See also

  • inotify-tools A suite of userland utilities, based on inotify, to watch and log modifications on files and directories
  • Official inotify README
  • Kernel Korner - Intro to inotify by Robert Love (2005)
  • LWN Article on Inotify Watching filesystem events with inotify (partly out of date)
  • IBM Article Monitor Linux file system events with inotify
  • incron An inotify cron system. It works like a regular cron but handles filesystem events rather than time periods.
  • inotail A tail (Unix) clone that uses inotify to watch for modified files in -f (follow) mode.

Programming APIs