- Inotify
inotify is a
Linux kernel subsystem that provides file system event notification. It was written byJohn McCutchan with help fromRobert Love and laterAmy Griffis to replacednotify . It was included in the mainline kernel from release 2.6.13 (2005-06-18), and could be compiled into 2.6.12 and possibly earlier releases by use of a patch. Its function is essentially an extension tofilesystem s 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.Fact|date=September 2008)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 , themodule that it replaced. With the older module, a program had to use onefile 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 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 (likeGLib ) 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:
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 deletedHistory
* August 2005 - Merged into 2.6.13
* July 2004 - [http://groups.google.com/group/fa.linux.kernel/browse_thread/thread/6366aaa10cb23bcc/a54e97d545ad66fe First release announcement]See also
*
File alteration monitor
*Gamin
*DMAPI
*FSEvents External links
* [http://inotify-tools.sourceforge.net inotify-tools] A suite of userland utilities, based on inotify, to watch and log modifications on files and directories
* [http://www.kernel.org/pub/linux/kernel/people/rml/inotify/README Official inotify README]
* [http://www.linuxjournal.com/article/8478 Kernel Korner ] - Intro to inotify by Robert Love (2005)
* [http://lwn.net/Articles/104343/ LWN Article on Inotify] Watching filesystem events with inotify (partly out of date)
* [http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html?ca=drs- IBM Article] Monitor Linux file system events with inotify
* [http://incron.aiken.cz/ incron] An inotify cron system. It works like a regular cron but handles filesystem events rather than time periods.
* [http://distanz.ch/inotail/ inotail] Atail (Unix) clone that uses inotify to watch for modified files in -f (follow) mode.Programming APIs
* [http://inotify-cxx.aiken.cz/ inotify-cxx] C++
* [http://code.google.com/p/inotify-java/ inotify-java] Java
* [http://search.cpan.org/~mlehmann/Linux-Inotify2-1.1/Inotify2.pm Linux::Inotify2] perl
* [http://pyinotify.sourceforge.net/ Pyinotify] python
* [http://code.activestate.com/recipes/576375/ inotify wrapper] Python
* [http://jnotify.sourceforge.net/ JNotify] java
* [http://tab.snarc.org/projects/ocaml_inotify/ Ocaml Inotify] Ocaml
* [http://developer.berlios.de/projects/php-inotify/ PHP Inotify] PHP
* [http://haskell.org/~kolmodin/code/hinotify/README.html Hinotify] Haskell
Wikimedia Foundation. 2010.