< Back to index
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.
Advantages
Inotifiy has many advantages over dnotifiy, 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. Inotify also provides more granularity than dnotify. The interface to inotify is provided through a node.
How it Works
To use inotify you need to open /dev/inotify and issue one of two ioctl commands to it:
* INOTIFY_WATCH - This call provides a filename and a mask of desired events; inotify will begin watching the given file (or directory) for activity.
* INOTIFY_IGNORE - This call will stop the stream of events for the given file.
Some of the events that can be monitored 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
Look at this [http://www-128.ibm.com/developerworks/linux/library/l-inotify.html article] for some sample code on how to use inotify.
This entry uses material from from Wikipedia, the leading user-contributed encyclopedia. It is licensed under the GNU Free Documentation License. Disclaimer.