Inotify (inode notify) 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.
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.
Inotify is used through a series of system calls specifically created for inotify.
Include this header file to use inotify.
Creates an inotify instance. Returns a file descriptor from which all events are read. 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:
|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
Limitations imposed by inotify include the following:
- Inotify does not support recursively watching directories, meaning that a separate inotify watch must be created for every subdirectory.
- Inotify does report some but not all events in sysfs and procfs.
- Rename events are not handled directly; i.e., inotify issues two separate events that must be examined and matched in a context of potential race conditions.
- August 2005 - Merged into 2.6.13
- July 2004 - First release announcement
Advantages over dnotify
There are a number of advantages when using inotify when compared to the older dnotify API that it replaced. With dnotify, 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. Later, fanotify was created to overcome this issue. 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.
- "fa.linux.kernal post [RFC][PATCH] inotify 0.8". 2004-07-29. Retrieved 2013-08-19.
- Linux 2.6.13, kernelnewbies.org
- Robert Love, Linux system programming. O'Reilly, p. 236
- Michael Kerrisk (14 July 2014). "Filesystem notification, part 2: A deeper investigation of inotify". LWN.net.
- Why inotify?
- inotify README file
- 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.
- Filesystem notification, part 1: An overview of dnotify and inotify. LWN.net 2014.