Jump to content

Inotify: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎Limitations: Lowercase
→‎Usage: Clarifications; changed some passages to passive voice
Line 16: Line 16:
</syntaxhighlight>
</syntaxhighlight>


Include this header file to use inotify. It defines the C struct (<tt>inotify_event</tt>) which describes the packet returned by the kernel for a read() call, as well as several symbolic constants for event types.
Include this header file to use inotify.


<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
Line 22: Line 22:
</syntaxhighlight>
</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.
This function creates an inotify instance. It returns a file descriptor which is used to add watches, and from which all events are read. Calls to read() are used to receive event information. To prevent the need for polling, read() blocks until an event is received. However, select() and poll() may be used with the returned file descriptor if desired. The <tt>FIONREAD ioctl()</tt> may be called to indicate the number of bytes which can be read out of the returned file descriptor.


<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
Line 28: Line 28:
</syntaxhighlight>
</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).
This function starts watching the inode pointed to by pathname for events contained in mask, or modifies the active mask for pathname. It returns a watch descriptor which is unique (within this inotify instance) to the inode pointed to by the pathname (multiple pathnames can point to the same inode/watch descriptor, for example due to symbolic links being followed).


<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
Line 34: Line 34:
</syntaxhighlight>
</syntaxhighlight>


Cancels a watch on the given watch descriptor.
Calling this function cancels a watch on the given watch descriptor for the given inotify instance.


Events generated by inotify contain the following information:
Events generated by inotify contain the following information:
Line 40: Line 40:
|-
|-
! Identifier
! Identifier
! contents
! Contents
|-
|-
| <tt>wd</tt>
| <tt>wd</tt>
Line 46: Line 46:
|-
|-
| <tt>mask</tt>
| <tt>mask</tt>
| bitmask of events which happened to the pathname identified by <tt>wd</tt>
| event tag
|-
|-
| <tt>cookie</tt>
| <tt>cookie</tt>
| cookie used to synchronize between <tt>IN_MOVED_FROM</tt> and <tt>IN_MOVED_TO</tt>
| Opaque identifier (cookie) used to tie separate <tt>IN_MOVED_FROM</tt> and <tt>IN_MOVED_TO</tt> events together
|-
|-
| <tt>len</tt>
| <tt>len</tt>
| length of name field
| length of <tt>name</tt> field
|-
|-
| <tt>name</tt>
| <tt>name</tt>
| the (optional) filename associated with this event (local to parent directory)
| the (optional, NUL terminated) filename associated with this event (relative to its parent directory)
|}
|}

<tt>name</tt> occupies an integral number of <tt>inotify_event</tt>-sized bytes (i.e., <tt>len</tt> will be a multiple of the size of the struct). This is possible because the end of the string is marked by a NUL byte, so the returned packet is simply padded to match the correct length. If the read() call did not specify enough bytes to read an event (for example, if a single struct inotify_event-sized buffer is specified, and a name needs to be returned), the read() call will return 0. The process can then increase the number of bytes to read to receive the complete event description.


Some of the events that can be monitored for are:
Some of the events that can be monitored for are:
* <tt>IN_ACCESS</tt> - read of the file
* <tt>IN_ACCESS</tt> - read of the file
* <tt>IN_MODIFY</tt> - last modification
* <tt>IN_MODIFY</tt> - file was modified (e.g. <tt>write(2)</tt> was called)
* <tt>IN_ATTRIB</tt> - attributes of file change
* <tt>IN_ATTRIB</tt> - attributes of file changed (<tt>chmod(2), chown(2)</tt>, etc.)
* <tt>IN_OPEN</tt> - open of file
* <tt>IN_OPEN</tt> - file is opened
* <tt>IN_CLOSE_WRITE</tt> - sent when a file opened for writing is closed
* <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_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_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_DELETE</tt> - a file/directory unlinked (deleted)
* <tt>IN_CREATE</tt> - a file in a watched directory is created
* <tt>IN_CREATE</tt> - a file in a watched directory is created
* <tt>IN_DELETE_SELF</tt> - file monitored is deleted
* <tt>IN_DELETE_SELF</tt> - file monitored is deleted

Revision as of 15:07, 10 June 2015

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.

Inotify was created by John McCutchan,[1] and it 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]

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 directly by the kernel that a file has changed, indexing utilities can achieve change-to-reindexing times of only about a second.[citation needed]

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 calls specifically created for inotify.

#include <sys/inotify.h>

Include this header file to use inotify. It defines the C struct (inotify_event) which describes the packet returned by the kernel for a read() call, as well as several symbolic constants for event types.

int inotify_init(void)

This function creates an inotify instance. It returns a file descriptor which is used to add watches, and from which all events are read. Calls to read() are used to receive event information. To prevent the need for polling, read() blocks until an event is received. However, select() and poll() may be used with the returned file descriptor if desired. The FIONREAD ioctl() may be called to indicate the number of bytes which can be read out of the returned file descriptor.

int inotify_add_watch(int fd, const char *pathname, int mask)

This function starts watching the inode pointed to by pathname for events contained in mask, or modifies the active mask for pathname. It returns a watch descriptor which is unique (within this inotify instance) to the inode pointed to by the pathname (multiple pathnames can point to the same inode/watch descriptor, for example due to symbolic links being followed).

int inotify_rm_watch(int fd, int wd)

Calling this function cancels a watch on the given watch descriptor for the given inotify instance.

Events generated by inotify contain the following information:

Identifier contents
wd watch descriptor
mask bitmask of events which happened to the pathname identified by wd
cookie Opaque identifier (cookie) used to tie separate IN_MOVED_FROM and IN_MOVED_TO events together
len length of name field
name the (optional, NUL terminated) filename associated with this event (relative to its parent directory)

name occupies an integral number of inotify_event-sized bytes (i.e., len will be a multiple of the size of the struct). This is possible because the end of the string is marked by a NUL byte, so the returned packet is simply padded to match the correct length. If the read() call did not specify enough bytes to read an event (for example, if a single struct inotify_event-sized buffer is specified, and a name needs to be returned), the read() call will return 0. The process can then increase the number of bytes to read to receive the complete event description.

Some of the events that can be monitored for are:

  • IN_ACCESS - read of the file
  • IN_MODIFY - file was modified (e.g. write(2) was called)
  • IN_ATTRIB - attributes of file changed (chmod(2), chown(2), etc.)
  • IN_OPEN - file is opened
  • 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 unlinked (deleted)
  • IN_CREATE - a file in a watched directory is created
  • IN_DELETE_SELF - file monitored is deleted

Limitations

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]
  • 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

  • July 2004: the first release announcement[1]
  • 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

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

References

  1. ^ a b "fa.linux.kernal post [RFC][PATCH] inotify 0.8". 2004-07-29. Retrieved 2013-08-19.
  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. ^ Michael Kerrisk (14 July 2014). "Filesystem notification, part 2: A deeper investigation of inotify". LWN.net.
  6. ^ Why inotify?
  7. ^ inotify README file
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy