Inotify: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎top: Added an article
Undid revision 1181860231 by Richfaber (talk) - MOS:NOTSEEALSO.
 
(48 intermediate revisions by 34 users not shown)
Line 1: Line 1:
{{Short description|Linux subsystem for filesystem monitoring}}
{{lowercase}}
{{Lowercase title}}
'''Inotify''' ([[inode]] notify) is a [[Linux kernel]] subsystem that acts to extend [[filesystem]]s to notice changes to the filesystem, and report those changes to applications. It replaces an earlier facility, [[dnotify]], which had similar goals.
'''inotify''' ([[inode]] notify) is a [[Linux kernel]] subsystem created by John McCutchan, which monitors changes to the [[filesystem]], and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload. The ''inotifywait'' and ''inotifywatch'' commands (maintained by Eric Curtin as part of the inotify-tools project) allow using the inotify subsystem from the command line.<ref name="inotify-tools">[https://github.com/inotify-tools/inotify-tools/wiki inotify-tools wiki]</ref> One major use is in [[desktop search]] utilities like [[Beagle (software)|Beagle]], where its functionality permits [[Search engine indexing|reindexing]] of changed files without scanning the filesystem for changes every few minutes, which would be very inefficient.


Inotify was created by [[John McCutchan]],<ref name="inotify-patch">{{cite web | url = http://groups.google.com/group/fa.linux.kernel/browse_thread/thread/6366aaa10cb23bcc/a54e97d545ad66fe | title = fa.linux.kernal post '''[RFC][PATCH] inotify 0.8''' | date = 2004-07-29 | accessdate = 2013-08-19}}</ref> and it was merged into the [[Linux kernel mainline]] in kernel version 2.6.13, released on August 29, 2005;<ref name="kernelnewbies">[http://kernelnewbies.org/Linux_2_6_13 Linux 2.6.13, kernelnewbies.org]</ref> later kernel versions included further improvements. The required library interfaces were added into the [[GNU C Library]] (glibc) in its version 2.4, released in March 2006, while the support for inotify was completed in glibc version 2.5, released in September 2006.<ref name="man-page">[http://man7.org/linux/man-pages/man7/inotify.7.html inotify man page]</ref>
inotify replaced an earlier facility, [[dnotify]], which had similar goals. Inotify was merged into the [[Linux kernel mainline]] in kernel version 2.6.13, released on August 29, 2005;<ref name="kernelnewbies">[http://kernelnewbies.org/Linux_2_6_13 Linux 2.6.13, kernelnewbies.org]</ref> later kernel versions included further improvements. The required library interfaces were added into the [[GNU C Library]] (glibc) in its version 2.4, released in March 2006, while the support for inotify was completed in glibc version 2.5, released in September 2006.<ref name="man-page">[http://man7.org/linux/man-pages/man7/inotify.7.html inotify man page]</ref>

One major use is in [[desktop search]] utilities like [[Beagle (software)|Beagle]], where its functionality permits [[Index (search engine)|reindexing]] of changed files without scanning the filesystem for changes every few minutes, which would be very inefficient. By being ''told'' directly by the kernel that a file has changed, indexing utilities can achieve change-to-reindexing times of only about a second.{{Citation needed|date = November 2014}}

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

== Usage ==

Inotify is used through a series of [[system call]]s specifically created for inotify.

<syntaxhighlight lang="c">
#include <sys/inotify.h>
</syntaxhighlight>

Include this header file to use inotify.

<syntaxhighlight lang="c">
int inotify_init(void)
</syntaxhighlight>

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.

<syntaxhighlight lang="c">
int inotify_add_watch(int fd, const char *pathname, int mask)
</syntaxhighlight>

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).

<syntaxhighlight lang="c">
int inotify_rm_watch(int fd, int wd)
</syntaxhighlight>

Cancels a watch on the given watch descriptor.

Events generated by inotify contain the following information:
{| class="wikitable"
|-
! Identifier
! Contents
|-
| <tt>wd</tt>
| watch descriptor
|-
| <tt>mask</tt>
| event tag
|-
| <tt>cookie</tt>
| cookie used to synchronize between <tt>IN_MOVED_FROM</tt> and <tt>IN_MOVED_TO</tt>
|-
| <tt>len</tt>
| length of name field
|-
| <tt>name</tt>
| the (optional) filename associated with this event (local to parent directory)
|}

Some of the events that can be monitored for are:
* <tt>IN_ACCESS</tt> - read of the file
* <tt>IN_MODIFY</tt> - last modification
* <tt>IN_ATTRIB</tt> - attributes of file change
* <tt>IN_OPEN</tt> - open of file
* <tt>IN_CLOSE_WRITE</tt> - sent when a file opened for writing is closed
* <tt>IN_CLOSE_NOWRITE</tt> - sent when a file opened not for writing is closed
* <tt>IN_MOVED_FROM</tt> and <tt>IN_MOVED_TO</tt> - when the file is moved or renamed
* <tt>IN_DELETE</tt> - a file/directory deleted
* <tt>IN_CREATE</tt> - a file in a watched directory is created
* <tt>IN_DELETE_SELF</tt> - file monitored is deleted


== Limitations ==
== Limitations ==
Limitations imposed by inotify include the following:
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.<ref>Robert Love, [http://books.google.com/books?id=k_ocKY0iegsC&pg=PA236&hl=en&ei=hU3STo3pO6iL4gSWsrkw&sa=X&oi=book_result&ct=result&resnum=1&ved=0CC4Q6AEwAA#v=onepage&q&f=false Linux system programming]. O'Reilly, p. 236</ref>
* Inotify does not support recursively watching directories, meaning that a separate inotify watch must be created for every subdirectory.<ref>Robert Love, [https://books.google.com/books?id=k_ocKY0iegsC&pg=PA236 Linux system programming]. O'Reilly, p. 236</ref> In contrast, the command ''inotifywait'' does provide recursive watching of directories.<ref>inotifywait man page, [https://man.archlinux.org/man/inotifywait.1#r,]</ref>
* Inotify does report some but not all events in [[sysfs]] and [[procfs]].
* Inotify does report some but not all events in [[sysfs]] and [[procfs]].
* Notification via Inotify requires the kernel to be aware of all relevant filesystem events, which is not always possible for networked filesystems such as [[Network File System|NFS]] where changes made by one client are not immediately broadcast to other clients.
* Notification via inotify requires the kernel to be aware of all relevant filesystem events, which is not always possible for networked filesystems such as [[Network File System|NFS]] where changes made by one client are not immediately broadcast to other clients.
* 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.
* 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.


== History ==
== History ==
* July 2004: the first release announcement<ref name="inotify-patch" />
* July 2004: the first release announcement<ref name="inotify-patch">{{cite web | url = https://groups.google.com/group/fa.linux.kernel/browse_thread/thread/6366aaa10cb23bcc/a54e97d545ad66fe | title = fa.linux.kernel post [RFC][PATCH] inotify 0.8 | date = 2004-07-29 | access-date = 2013-08-19}}</ref>
* August 29, 2005: Linux kernel version 2.6.13 released, containing merged inotify code<ref name="kernelnewbies" />
* August 29, 2005: Linux kernel version 2.6.13 released, containing merged inotify code<ref name="kernelnewbies" />
* March 2006: GNU C Library (glibc) version 2.4 released, bringing initial inotify support<ref name="man-page" />
* March 2006: GNU C Library (glibc) version 2.4 released, bringing initial inotify support<ref name="man-page" />
Line 85: Line 20:


== {{Anchor|FANOTIFY}}Advantages over dnotify ==
== {{Anchor|FANOTIFY}}Advantages over dnotify ==
There are a number of advantages when using ''inotify'' when compared to the older [[dnotify]] API that it replaced.<ref name=lwn-inotify>{{cite web |author=[[Michael Kerrisk]] |date={{date|2014-07-14}} |title=Filesystem notification, part 2: A deeper investigation of inotify |publisher=[[LWN.net]] |url=https://lwn.net/SubscriberLink/605128/19d8b7ae42dfc036/ }}</ref><ref>[http://www.developertutorials.com/tutorials/linux/monitor-linux-inotify-050531/page2.html Why inotify?]</ref><ref>[http://www.kernel.org/pub/linux/kernel/people/rml/inotify/README ''inotify'' README file]</ref> 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.
There are a number of advantages when using inotify when compared to the older [[dnotify]] API that it replaced.<ref name=lwn-inotify>{{cite web |author=Michael Kerrisk |author-link=Michael Kerrisk |date=14 July 2014 |title=Filesystem notification, part 2: A deeper investigation of inotify |publisher=[[LWN.net]] |url=https://lwn.net/Articles/605128/ }}</ref><ref>[http://www.developertutorials.com/tutorials/linux/monitor-linux-inotify-050531/page2.html Why inotify?] {{webarchive|url=https://web.archive.org/web/20100116105916/http://www.developertutorials.com/tutorials/linux/monitor-linux-inotify-050531/page2.html |date=2010-01-16 }}</ref><ref>[https://www.kernel.org/pub/linux/kernel/people/rml/inotify/README inotify README file]</ref> 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.
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.
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.


== See also ==
== See also ==
{{Portal|Computer programming|Linux}}
{{Portal|Computer programming|Linux}}
* [[File Alteration Monitor]]
* [[File Alteration Monitor]] (SGI)
* [[Gamin]]
* [[DMAPI]]
* [[DMAPI]]
* [[kqueue]] (FreeBSD, NetBSD, OpenBSD, DragonFly BSD, and macOS)
* [[kqueue]]
* [[FSEvents]]
* [[FSEvents]] (macOS)


==References==
==References==
{{reflist}}
{{Reflist}}


== External links ==
== External links ==
* [http://www.linuxjournal.com/article/8478 Kernel Korner ] - Intro to inotify by Robert Love (2005)
* [http://www.linuxjournal.com/article/8478 Kernel Korner ]{{snd}} an introduction to inotify by Robert Love (2005)
* [http://lwn.net/Articles/104343/ LWN Article on Inotify] Watching filesystem events with inotify (partly out of date)
* [https://lwn.net/Articles/104343/ LWN Article on Inotify]{{snd}} 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://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html?ca=drs- IBM Article]{{snd}} monitoring Linux filesystem events with inotify (6 September 2008).
*[https://lwn.net/Articles/604686/ Filesystem notification, part 1: An overview of dnotify and inotify]. LWN.net 2014.
* [https://lwn.net/Articles/604686/ Filesystem notification, part 1: An overview of dnotify and inotify]{{snd}} an LWN.net article by Michael Kerrisk (2014)


{{Linux}}
{{Linux}}

Latest revision as of 06:10, 14 November 2023

inotify (inode notify) is a Linux kernel subsystem created by John McCutchan, which monitors changes to the filesystem, and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload. The inotifywait and inotifywatch commands (maintained by Eric Curtin as part of the inotify-tools project) allow using the inotify subsystem from the command line.[1] 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.

inotify replaced an earlier facility, dnotify, which had similar goals. Inotify was merged into the Linux kernel mainline in kernel version 2.6.13, released on August 29, 2005;[2] later kernel versions included further improvements. The required library interfaces were added into the GNU C Library (glibc) in its version 2.4, released in March 2006, while the support for inotify was completed in glibc version 2.5, released in September 2006.[3]

Limitations[edit]

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.[4] In contrast, the command inotifywait does provide recursive watching of directories.[5]
  • Inotify does report some but not all events in sysfs and procfs.
  • Notification via inotify requires the kernel to be aware of all relevant filesystem events, which is not always possible for networked filesystems such as NFS where changes made by one client are not immediately broadcast to other clients.
  • 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.

History[edit]

  • July 2004: the first release announcement[6]
  • August 29, 2005: Linux kernel version 2.6.13 released, containing merged inotify code[2]
  • March 2006: GNU C Library (glibc) version 2.4 released, bringing initial inotify support[3]
  • September 2006: Glibc version 2.5 released, bringing complete inotify support[3]

Advantages over dnotify[edit]

There are a number of advantages when using inotify when compared to the older dnotify API that it replaced.[7][8][9] 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.

See also[edit]

References[edit]

  1. ^ inotify-tools wiki
  2. ^ a b Linux 2.6.13, kernelnewbies.org
  3. ^ a b c inotify man page
  4. ^ Robert Love, Linux system programming. O'Reilly, p. 236
  5. ^ inotifywait man page, [1]
  6. ^ "fa.linux.kernel post [RFC][PATCH] inotify 0.8". 2004-07-29. Retrieved 2013-08-19.
  7. ^ Michael Kerrisk (14 July 2014). "Filesystem notification, part 2: A deeper investigation of inotify". LWN.net.
  8. ^ Why inotify? Archived 2010-01-16 at the Wayback Machine
  9. ^ inotify README file

External links[edit]