Inotify
Encyclopedia
inotify is a Linux kernel
subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications. It replaces an earlier facility, dnotify
, which had similar goals.
The original developers of inotify were John McCutchan, Robert Love
and Amy Griffis. It has been included in the mainline Linux 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.
One major use is 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.
It can also be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.
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 descriptors per process could be reached. The use of file descriptors along with dnotify also 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.
The inotify API 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.
s specifically created for inotify.
#include
Include this header file to use inotify.
int inotify_init(void)
Creates an inotify instance. Returns a file descriptor which all events are read from. Use read calls to receive event information. To prevent polling, read blocks until an event is received.
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:
Some of the events that can be monitored for are:
Linux kernel
The Linux kernel is an operating system kernel used by the Linux family of Unix-like operating systems. It is one of the most prominent examples of free and open source software....
subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications. It replaces an earlier facility, dnotify
Dnotify
dnotify is a file system event monitor for the Linux kernel, one of the subfeatures of the fcntl call. It was introduced in the 2.4 kernel series...
, which had similar goals.
The original developers of inotify were John McCutchan, Robert Love
Robert Love
Robert M. Love is an American author, speaker, Google engineer, and open source software developer.Love is best known for his contributions to the Linux kernel, with notable work including the preemptive kernel, process scheduler, kernel event layer, virtual memory subsystem, and inotify...
and Amy Griffis. It has been included in the mainline Linux 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.
One major use is in desktop search
Desktop search
Desktop search is the name for the field of search tools which search the contents of a user's own computer files, rather than searching the Internet...
utilities like Beagle
Beagle (software)
Beagle is a search system for Linux and other such modern Unix-like systems, enabling the user to search documents, chat logs, email and contact lists in a similar way to Spotlight in Mac OS X, and Windows Search or Google Desktop under Linux or Microsoft Windows...
, where its functionality permits reindexing
Index (information technology)
In computer science, an index can be:# an integer that identifies an array element# a data structure that enables sublinear-time lookup -Array element identifier:...
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.
It can also be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.
Advantages
There are a number of advantages when using inotify when compared to the older dnotifyDnotify
dnotify is a file system event monitor for the Linux kernel, one of the subfeatures of the fcntl call. It was introduced in the 2.4 kernel series...
module
Module
Module or modular may refer to the concept of modularity. It may also refer to:-Computing and engineering:* Modular design, the engineering discipline of designing complex devices using separately designed sub-components...
that it replaced. With the older module, a program had to use one file descriptor
File descriptor
In computer programming, a file descriptor is an abstract indicator for accessing a file. The term is generally used in POSIX operating systems...
for each directory that it was monitoring. This can become a bottleneck since the limit of file descriptors per process could be reached. The use of file descriptors along with dnotify also 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.
The inotify API uses fewer file descriptors, allowing programmers to use the established select and poll interface, rather than the signal notification system used by dnotify
Dnotify
dnotify is a file system event monitor for the Linux kernel, one of the subfeatures of the fcntl call. It was introduced in the 2.4 kernel series...
. This also makes integration with existing select- or poll-based libraries (like GLib
GLib
GLib is a cross-platform software utility library that began as part of the GTK+ project. However, before releasing version 2 of GTK+, the project's developers decided to separate non-GUI-specific code from the GTK+ platform, thus creating GLib as a separate product...
) easier.
How it works
Inotify is used through a series of system callSystem call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...
s specifically created for inotify.
#include
Include this header file to use inotify.
int inotify_init(void)
Creates an inotify instance. Returns a file descriptor which all events are read from. Use read calls to receive event information. To prevent polling, read blocks until an event is received.
int inotify_add_watch(int fd, const char* pathname, int mask)
Starts watching the inode
Inode
In computing, an inode is a data structure on a traditional Unix-style file system such as UFS. An inode stores all the information about a regular file, directory, or other file system object, except its data and name....
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 - open of file
- IN_CLOSE_WRITE - sent when a file opened for writing is closed
- IN_CLOSE_NOWRITE - sent when a file opened not for writing is closed
- IN_MOVED_FROM and IN_MOVED_TO - when the file is moved or renamed
- IN_DELETE - a file/directory deleted
- IN_CREATE - a file in a watched directory is created
- IN_DELETE_SELF - file monitored is deleted
Disadvantages
Inotify does not support recursively watching directories, meaning that a separate inotify watch must be created for every subdirectory.History
- August 2005 - Merged into 2.6.13
- July 2004 - First release announcement
External links
- 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.