man-pages/man7/inotify.7.html
2021-03-31 01:06:50 +01:00

1506 lines
44 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of INOTIFY</TITLE>
</HEAD><BODY>
<H1>INOTIFY</H1>
Section: Linux Programmer's Manual (7)<BR>Updated: 2020-02-09<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
inotify - monitoring filesystem events
<A NAME="lbAC">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<I>inotify</I>
API provides a mechanism for monitoring filesystem events.
Inotify can be used to monitor individual files,
or to monitor directories.
When a directory is monitored, inotify will return events
for the directory itself, and for files inside the directory.
<P>
The following system calls are used with this API:
<DL COMPACT>
<DT id="1">*<DD>
<B><A HREF="/cgi-bin/man/man2html?2+inotify_init">inotify_init</A></B>(2)
creates an inotify instance and returns a file descriptor
referring to the inotify instance.
The more recent
<B><A HREF="/cgi-bin/man/man2html?2+inotify_init1">inotify_init1</A></B>(2)
is like
<B><A HREF="/cgi-bin/man/man2html?2+inotify_init">inotify_init</A></B>(2),
but has a
<I>flags</I>
argument that provides access to some extra functionality.
<DT id="2">*<DD>
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2)
manipulates the &quot;watch list&quot; associated with an inotify instance.
Each item (&quot;watch&quot;) in the watch list specifies the pathname of
a file or directory,
along with some set of events that the kernel should monitor for the
file referred to by that pathname.
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2)
either creates a new watch item, or modifies an existing watch.
Each watch has a unique &quot;watch descriptor&quot;, an integer
returned by
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2)
when the watch is created.
<DT id="3">*<DD>
When events occur for monitored files and directories,
those events are made available to the application as structured data that
can be read from the inotify file descriptor using
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
(see below).
<DT id="4">*<DD>
<B><A HREF="/cgi-bin/man/man2html?2+inotify_rm_watch">inotify_rm_watch</A></B>(2)
removes an item from an inotify watch list.
<DT id="5">*<DD>
When all file descriptors referring to an inotify
instance have been closed (using
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2)),
the underlying object and its resources are
freed for reuse by the kernel;
all associated watches are automatically freed.
</DL>
<P>
With careful programming,
an application can use inotify to efficiently monitor and cache
the state of a set of filesystem objects.
However, robust applications should allow for the fact that bugs
in the monitoring logic or races of the kind described below
may leave the cache inconsistent with the filesystem state.
It is probably wise to do some consistency checking,
and rebuild the cache when inconsistencies are detected.
<A NAME="lbAD">&nbsp;</A>
<H3>Reading events from an inotify file descriptor</H3>
To determine what events have occurred, an application
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)s
from the inotify file descriptor.
If no events have so far occurred, then,
assuming a blocking file descriptor,
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
will block until at least one event occurs
(unless interrupted by a signal,
in which case the call fails with the error
<B>EINTR</B>;
see
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7)).
<P>
Each successful
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
returns a buffer containing one or more of the following structures:
<P>
struct inotify_event {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wd;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Watch&nbsp;descriptor&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;uint32_t&nbsp;mask;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Mask&nbsp;describing&nbsp;event&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;uint32_t&nbsp;cookie;&nbsp;&nbsp;&nbsp;/*&nbsp;Unique&nbsp;cookie&nbsp;associating&nbsp;related
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;events&nbsp;(for&nbsp;<A HREF="/cgi-bin/man/man2html?2+rename">rename</A>(2))&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;uint32_t&nbsp;len;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Size&nbsp;of&nbsp;<I>name</I>&nbsp;field&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name[];&nbsp;&nbsp;&nbsp;/*&nbsp;Optional&nbsp;null-terminated&nbsp;name&nbsp;*/
};
<P>
<I>wd</I>
identifies the watch for which this event occurs.
It is one of the watch descriptors returned by a previous call to
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2).
<P>
<I>mask</I>
contains bits that describe the event that occurred (see below).
<P>
<I>cookie</I>
is a unique integer that connects related events.
Currently, this is used only for rename events, and
allows the resulting pair of
<B>IN_MOVED_FROM</B>
and
<B>IN_MOVED_TO</B>
events to be connected by the application.
For all other event types,
<I>cookie</I>
is set to 0.
<P>
The
<I>name</I>
field is present only when an event is returned
for a file inside a watched directory;
it identifies the filename within the watched directory.
This filename is null-terminated,
and may include further null bytes ('\0') to align subsequent reads to a
suitable address boundary.
<P>
The
<I>len</I>
field counts all of the bytes in
<I>name</I>,
including the null bytes;
the length of each
<I>inotify_event</I>
structure is thus
<I>sizeof(struct inotify_event)+len</I>.
<P>
The behavior when the buffer given to
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
is too small to return information about the next event depends
on the kernel version: in kernels before 2.6.21,
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
returns 0; since kernel 2.6.21,
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
fails with the error
<B>EINVAL</B>.
Specifying a buffer of size
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sizeof(struct&nbsp;inotify_event)&nbsp;+&nbsp;NAME_MAX&nbsp;+&nbsp;1
<P>
will be sufficient to read at least one event.
<A NAME="lbAE">&nbsp;</A>
<H3>inotify events</H3>
The
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2)
<I>mask</I>
argument and the
<I>mask</I>
field of the
<I>inotify_event</I>
structure returned when
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)ing
an inotify file descriptor are both bit masks identifying
inotify events.
The following bits can be specified in
<I>mask</I>
when calling
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2)
and may be returned in the
<I>mask</I>
field returned by
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2):
<DL COMPACT><DT id="6"><DD>
<DL COMPACT>
<DT id="7"><B>IN_ACCESS</B> (+)
<DD>
File was accessed (e.g.,
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)).
<DT id="8"><B>IN_ATTRIB</B> (*)
<DD>
Metadata changed---for example, permissions (e.g.,
<B><A HREF="/cgi-bin/man/man2html?2+chmod">chmod</A></B>(2)),
timestamps (e.g.,
<B><A HREF="/cgi-bin/man/man2html?2+utimensat">utimensat</A></B>(2)),
extended attributes
(<B><A HREF="/cgi-bin/man/man2html?2+setxattr">setxattr</A></B>(2)),
link count (since Linux 2.6.25; e.g.,
for the target of
<B><A HREF="/cgi-bin/man/man2html?2+link">link</A></B>(2)
and for
<B><A HREF="/cgi-bin/man/man2html?2+unlink">unlink</A></B>(2)),
and user/group ID (e.g.,
<B><A HREF="/cgi-bin/man/man2html?2+chown">chown</A></B>(2)).
<DT id="9"><B>IN_CLOSE_WRITE</B> (+)
<DD>
File opened for writing was closed.
<DT id="10"><B>IN_CLOSE_NOWRITE</B> (*)
<DD>
File or directory not opened for writing was closed.
<DT id="11"><B>IN_CREATE</B> (+)
<DD>
File/directory created in watched directory (e.g.,
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
<B>O_CREAT</B>,
<B><A HREF="/cgi-bin/man/man2html?2+mkdir">mkdir</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+link">link</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+symlink">symlink</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
on a UNIX domain socket).
<DT id="12"><B>IN_DELETE</B> (+)
<DD>
File/directory deleted from watched directory.
<DT id="13"><B>IN_DELETE_SELF</B>
<DD>
Watched file/directory was itself deleted.
(This event also occurs if an object is moved to another filesystem,
since
<B><A HREF="/cgi-bin/man/man2html?1+mv">mv</A></B>(1)
in effect copies the file to the other filesystem and
then deletes it from the original filesystem.)
In addition, an
<B>IN_IGNORED</B>
event will subsequently be generated for the watch descriptor.
<DT id="14"><B>IN_MODIFY</B> (+)
<DD>
File was modified (e.g.,
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+truncate">truncate</A></B>(2)).
<DT id="15"><B>IN_MOVE_SELF</B>
<DD>
Watched file/directory was itself moved.
<DT id="16"><B>IN_MOVED_FROM</B> (+)
<DD>
Generated for the directory containing the old filename
when a file is renamed.
<DT id="17"><B>IN_MOVED_TO</B> (+)
<DD>
Generated for the directory containing the new filename
when a file is renamed.
<DT id="18"><B>IN_OPEN</B> (*)
<DD>
File or directory was opened.
</DL>
</DL>
<P>
Inotify monitoring is inode-based: when monitoring a file
(but not when monitoring the directory containing a file),
an event can be generated for activity on any link to the file
(in the same or a different directory).
<P>
When monitoring a directory:
<DL COMPACT>
<DT id="19">*<DD>
the events marked above with an asterisk (*) can occur both
for the directory itself and for objects inside the directory; and
<DT id="20">*<DD>
the events marked with a plus sign (+) occur only for objects
inside the directory (not for the directory itself).
</DL>
<P>
<I>Note</I>:
when monitoring a directory,
events are not generated for the files inside the directory
when the events are performed via a pathname (i.e., a link)
that lies outside the monitored directory.
<P>
When events are generated for objects inside a watched directory, the
<I>name</I>
field in the returned
<I>inotify_event</I>
structure identifies the name of the file within the directory.
<P>
The
<B>IN_ALL_EVENTS</B>
macro is defined as a bit mask of all of the above events.
This macro can be used as the
<I>mask</I>
argument when calling
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2).
<P>
Two additional convenience macros are defined:
<DL COMPACT><DT id="21"><DD>
<DL COMPACT>
<DT id="22"><B>IN_MOVE</B>
<DD>
Equates to
<B>IN_MOVED_FROM | IN_MOVED_TO</B>.
<DT id="23"><B>IN_CLOSE</B>
<DD>
Equates to
<B>IN_CLOSE_WRITE | IN_CLOSE_NOWRITE</B>.
</DL>
</DL>
<P>
The following further bits can be specified in
<I>mask</I>
when calling
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2):
<DL COMPACT><DT id="24"><DD>
<DL COMPACT>
<DT id="25"><B>IN_DONT_FOLLOW</B> (since Linux 2.6.15)
<DD>
Don't dereference
<I>pathname</I>
if it is a symbolic link.
<DT id="26"><B>IN_EXCL_UNLINK</B> (since Linux 2.6.36)
<DD>
By default, when watching events on the children of a directory,
events are generated for children even after they have been unlinked
from the directory.
This can result in large numbers of uninteresting events for
some applications (e.g., if watching
<I>/tmp</I>,
in which many applications create temporary files whose
names are immediately unlinked).
Specifying
<B>IN_EXCL_UNLINK</B>
changes the default behavior,
so that events are not generated for children after
they have been unlinked from the watched directory.
<DT id="27"><B>IN_MASK_ADD</B>
<DD>
If a watch instance already exists for the filesystem object corresponding to
<I>pathname</I>,
add (OR) the events in
<I>mask</I>
to the watch mask (instead of replacing the mask);
the error
<B>EINVAL</B>
results if
<B>IN_MASK_CREATE</B>
is also specified.
<DT id="28"><B>IN_ONESHOT</B>
<DD>
Monitor the filesystem object corresponding to
<I>pathname</I>
for one event, then remove from
watch list.
<DT id="29"><B>IN_ONLYDIR</B> (since Linux 2.6.15)
<DD>
Watch
<I>pathname</I>
only if it is a directory;
the error
<B>ENOTDIR</B>
results if
<I>pathname</I>
is not a directory.
Using this flag provides an application with a race-free way of
ensuring that the monitored object is a directory.
<DT id="30"><B>IN_MASK_CREATE</B> (since Linux 4.18)
<DD>
Watch
<I>pathname</I>
only if it does not already have a watch associated with it;
the error
<B>EEXIST</B>
results if
<I>pathname</I>
is already being watched.
<DT id="31"><DD>
Using this flag provides an application with a way of ensuring
that new watches do not modify existing ones.
This is useful because multiple paths may refer to the same inode,
and multiple calls to
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2)
without this flag may clobber existing watch masks.
</DL>
</DL>
<P>
The following bits may be set in the
<I>mask</I>
field returned by
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2):
<DL COMPACT><DT id="32"><DD>
<DL COMPACT>
<DT id="33"><B>IN_IGNORED</B>
<DD>
Watch was removed explicitly
(<B><A HREF="/cgi-bin/man/man2html?2+inotify_rm_watch">inotify_rm_watch</A></B>(2))
or automatically (file was deleted, or filesystem was unmounted).
See also BUGS.
<DT id="34"><B>IN_ISDIR</B>
<DD>
Subject of this event is a directory.
<DT id="35"><B>IN_Q_OVERFLOW</B>
<DD>
Event queue overflowed
(<I>wd</I>
is -1 for this event).
<DT id="36"><B>IN_UNMOUNT</B>
<DD>
Filesystem containing watched object was unmounted.
In addition, an
<B>IN_IGNORED</B>
event will subsequently be generated for the watch descriptor.
</DL>
</DL>
<A NAME="lbAF">&nbsp;</A>
<H3>Examples</H3>
Suppose an application is watching the directory
<I>dir</I>
and the file
<I>dir/myfile</I>
for all events.
The examples below show some events that will be generated
for these two objects.
<DL COMPACT><DT id="37"><DD>
<DL COMPACT>
<DT id="38">fd = open(&quot;dir/myfile&quot;, O_RDWR);<DD>
Generates
<B>IN_OPEN</B>
events for both
<I>dir</I>
and
<I>dir/myfile</I>.
<DT id="39">read(fd, buf, count);<DD>
Generates
<B>IN_ACCESS</B>
events for both
<I>dir</I>
and
<I>dir/myfile</I>.
<DT id="40">write(fd, buf, count);<DD>
Generates
<B>IN_MODIFY</B>
events for both
<I>dir</I>
and
<I>dir/myfile</I>.
<DT id="41">fchmod(fd, mode);<DD>
Generates
<B>IN_ATTRIB</B>
events for both
<I>dir</I>
and
<I>dir/myfile</I>.
<DT id="42">close(fd);<DD>
Generates
<B>IN_CLOSE_WRITE</B>
events for both
<I>dir</I>
and
<I>dir/myfile</I>.
</DL>
</DL>
<P>
Suppose an application is watching the directories
<I>dir1</I>
and
<I>dir2</I>,
and the file
<I>dir1/myfile</I>.
The following examples show some events that may be generated.
<DL COMPACT><DT id="43"><DD>
<DL COMPACT>
<DT id="44">link(&quot;dir1/myfile&quot;, &quot;dir2/new&quot;);<DD>
Generates an
<B>IN_ATTRIB</B>
event for
<I>myfile</I>
and an
<B>IN_CREATE</B>
event for
<I>dir2</I>.
<DT id="45">rename(&quot;dir1/myfile&quot;, &quot;dir2/myfile&quot;);<DD>
Generates an
<B>IN_MOVED_FROM</B>
event for
<I>dir1</I>,
an
<B>IN_MOVED_TO</B>
event for
<I>dir2</I>,
and an
<B>IN_MOVE_SELF</B>
event for
<I>myfile</I>.
The
<B>IN_MOVED_FROM</B>
and
<B>IN_MOVED_TO</B>
events will have the same
<I>cookie</I>
value.
</DL>
</DL>
<P>
Suppose that
<I>dir1/xx</I>
and
<I>dir2/yy</I>
are (the only) links to the same file, and an application is watching
<I>dir1</I>,
<I>dir2</I>,
<I>dir1/xx</I>,
and
<I>dir2/yy</I>.
Executing the following calls in the order given below will generate
the following events:
<DL COMPACT><DT id="46"><DD>
<DL COMPACT>
<DT id="47">unlink(&quot;dir2/yy&quot;);<DD>
Generates an
<B>IN_ATTRIB</B>
event for
<I>xx</I>
(because its link count changes)
and an
<B>IN_DELETE</B>
event for
<I>dir2</I>.
<DT id="48">unlink(&quot;dir1/xx&quot;);<DD>
Generates
<B>IN_ATTRIB</B>,
<B>IN_DELETE_SELF</B>,
and
<B>IN_IGNORED</B>
events for
<I>xx</I>,
and an
<B>IN_DELETE</B>
event for
<I>dir1</I>.
</DL>
</DL>
<P>
Suppose an application is watching the directory
<I>dir</I>
and (the empty) directory
<I>dir/subdir</I>.
The following examples show some events that may be generated.
<DL COMPACT><DT id="49"><DD>
<DL COMPACT>
<DT id="50">mkdir(&quot;dir/new&quot;, mode);<DD>
Generates an
<B>IN_CREATE | IN_ISDIR</B>
event for
<I>dir</I>.
<DT id="51">rmdir(&quot;dir/subdir&quot;);<DD>
Generates
<B>IN_DELETE_SELF</B>
and
<B>IN_IGNORED</B>
events for
<I>subdir</I>,
and an
<B>IN_DELETE | IN_ISDIR</B>
event for
<I>dir</I>.
</DL>
</DL>
<A NAME="lbAG">&nbsp;</A>
<H3>/proc interfaces</H3>
The following interfaces can be used to limit the amount of
kernel memory consumed by inotify:
<DL COMPACT>
<DT id="52"><I>/proc/sys/fs/inotify/max_queued_events</I>
<DD>
The value in this file is used when an application calls
<B><A HREF="/cgi-bin/man/man2html?2+inotify_init">inotify_init</A></B>(2)
to set an upper limit on the number of events that can be
queued to the corresponding inotify instance.
Events in excess of this limit are dropped, but an
<B>IN_Q_OVERFLOW</B>
event is always generated.
<DT id="53"><I>/proc/sys/fs/inotify/max_user_instances</I>
<DD>
This specifies an upper limit on the number of inotify instances
that can be created per real user ID.
<DT id="54"><I>/proc/sys/fs/inotify/max_user_watches</I>
<DD>
This specifies an upper limit on the number of watches
that can be created per real user ID.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H2>VERSIONS</H2>
Inotify was merged into the 2.6.13 Linux kernel.
The required library interfaces were added to glibc in version 2.4.
(<B>IN_DONT_FOLLOW</B>,
<B>IN_MASK_ADD</B>,
and
<B>IN_ONLYDIR</B>
were added in glibc version 2.5.)
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
The inotify API is Linux-specific.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
Inotify file descriptors can be monitored using
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2),
and
<B><A HREF="/cgi-bin/man/man2html?7+epoll">epoll</A></B>(7).
When an event is available, the file descriptor indicates as readable.
<P>
Since Linux 2.6.25,
signal-driven I/O notification is available for inotify file descriptors;
see the discussion of
<B>F_SETFL</B>
(for setting the
<B>O_ASYNC</B>
flag),
<B>F_SETOWN</B>,
and
<B>F_SETSIG</B>
in
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2).
The
<I>siginfo_t</I>
structure (described in
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2))
that is passed to the signal handler has the following fields set:
<I>si_fd</I>
is set to the inotify file descriptor number;
<I>si_signo</I>
is set to the signal number;
<I>si_code</I>
is set to
<B>POLL_IN</B>;
and
<B>POLLIN</B>
is set in
<I>si_band</I>.
<P>
If successive output inotify events produced on the
inotify file descriptor are identical (same
<I>wd</I>,
<I>mask</I>,
<I>cookie</I>,
and
<I>name</I>),
then they are coalesced into a single event if the
older event has not yet been read (but see BUGS).
This reduces the amount of kernel memory required for the event queue,
but also means that an application can't use inotify to reliably count
file events.
<P>
The events returned by reading from an inotify file descriptor
form an ordered queue.
Thus, for example, it is guaranteed that when renaming from
one directory to another, events will be produced in the
correct order on the inotify file descriptor.
<P>
The set of watch descriptors that is being monitored via
an inotify file descriptor can be viewed via the entry for
the inotify file descriptor in the process's
<I>/proc/[pid]/fdinfo</I>
directory.
See
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5)
for further details.
The
<B>FIONREAD</B>
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2)
returns the number of bytes available to read from an
inotify file descriptor.
<A NAME="lbAK">&nbsp;</A>
<H3>Limitations and caveats</H3>
The inotify API provides no information about the user or process that
triggered the inotify event.
In particular, there is no easy
way for a process that is monitoring events via inotify
to distinguish events that it triggers
itself from those that are triggered by other processes.
<P>
Inotify reports only events that a user-space program triggers through
the filesystem API.
As a result, it does not catch remote events that occur
on network filesystems.
(Applications must fall back to polling the filesystem
to catch such events.)
Furthermore, various pseudo-filesystems such as
<I>/proc</I>,
<I>/sys</I>,
and
<I>/dev/pts</I>
are not monitorable with inotify.
<P>
The inotify API does not report file accesses and modifications that
may occur because of
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+msync">msync</A></B>(2),
and
<B><A HREF="/cgi-bin/man/man2html?2+munmap">munmap</A></B>(2).
<P>
The inotify API identifies affected files by filename.
However, by the time an application processes an inotify event,
the filename may already have been deleted or renamed.
<P>
The inotify API identifies events via watch descriptors.
It is the application's responsibility to cache a mapping
(if one is needed) between watch descriptors and pathnames.
Be aware that directory renamings may affect multiple cached pathnames.
<P>
Inotify monitoring of directories is not recursive:
to monitor subdirectories under a directory,
additional watches must be created.
This can take a significant amount time for large directory trees.
<P>
If monitoring an entire directory subtree,
and a new subdirectory is created in that tree or an existing directory
is renamed into that tree,
be aware that by the time you create a watch for the new subdirectory,
new files (and subdirectories) may already exist inside the subdirectory.
Therefore, you may want to scan the contents of the subdirectory
immediately after adding the watch (and, if desired,
recursively add watches for any subdirectories that it contains).
<P>
Note that the event queue can overflow.
In this case, events are lost.
Robust applications should handle the possibility of
lost events gracefully.
For example, it may be necessary to rebuild part or all of
the application cache.
(One simple, but possibly expensive,
approach is to close the inotify file descriptor, empty the cache,
create a new inotify file descriptor,
and then re-create watches and cache entries
for the objects to be monitored.)
<P>
If a filesystem is mounted on top of a monitored directory,
no event is generated, and no events are generated
for objects immediately under the new mount point.
If the filesystem is subsequently unmounted,
events will subsequently be generated for the directory and
the objects it contains.
<A NAME="lbAL">&nbsp;</A>
<H3>Dealing with rename() events</H3>
As noted above, the
<B>IN_MOVED_FROM</B>
and
<B>IN_MOVED_TO</B>
event pair that is generated by
<B><A HREF="/cgi-bin/man/man2html?2+rename">rename</A></B>(2)
can be matched up via their shared cookie value.
However, the task of matching has some challenges.
<P>
These two events are usually consecutive in the event stream available
when reading from the inotify file descriptor.
However, this is not guaranteed.
If multiple processes are triggering events for monitored objects,
then (on rare occasions) an arbitrary number of
other events may appear between the
<B>IN_MOVED_FROM</B>
and
<B>IN_MOVED_TO</B>
events.
Furthermore, it is not guaranteed that the event pair is atomically
inserted into the queue: there may be a brief interval where the
<B>IN_MOVED_FROM</B>
has appeared, but the
<B>IN_MOVED_TO</B>
has not.
<P>
Matching up the
<B>IN_MOVED_FROM</B>
and
<B>IN_MOVED_TO</B>
event pair generated by
<B><A HREF="/cgi-bin/man/man2html?2+rename">rename</A></B>(2)
is thus inherently racy.
(Don't forget that if an object is renamed outside of a monitored directory,
there may not even be an
<B>IN_MOVED_TO</B>
event.)
Heuristic approaches (e.g., assume the events are always consecutive)
can be used to ensure a match in most cases,
but will inevitably miss some cases,
causing the application to perceive the
<B>IN_MOVED_FROM</B>
and
<B>IN_MOVED_TO</B>
events as being unrelated.
If watch descriptors are destroyed and re-created as a result,
then those watch descriptors will be inconsistent with
the watch descriptors in any pending events.
(Re-creating the inotify file descriptor and rebuilding the cache may
be useful to deal with this scenario.)
<P>
Applications should also allow for the possibility that the
<B>IN_MOVED_FROM</B>
event was the last event that could fit in the buffer
returned by the current call to
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
and the accompanying
<B>IN_MOVED_TO</B>
event might be fetched only on the next
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
which should be done with a (small) timeout to allow for the fact that
insertion of the
<B>IN_MOVED_FROM</B>-<B>IN_MOVED_TO</B>
event pair is not atomic,
and also the possibility that there may not be any
<B>IN_MOVED_TO</B>
event.
<A NAME="lbAM">&nbsp;</A>
<H2>BUGS</H2>
Before Linux 3.19,
<B><A HREF="/cgi-bin/man/man2html?2+fallocate">fallocate</A></B>(2)
did not create any inotify events.
Since Linux 3.19,
calls to
<B><A HREF="/cgi-bin/man/man2html?2+fallocate">fallocate</A></B>(2)
generate
<B>IN_MODIFY</B>
events.
<P>
In kernels before 2.6.16, the
<B>IN_ONESHOT</B>
<I>mask</I>
flag does not work.
<P>
As originally designed and implemented, the
<B>IN_ONESHOT</B>
flag did not cause an
<B>IN_IGNORED</B>
event to be generated when the watch was dropped after one event.
However, as an unintended effect of other changes,
since Linux 2.6.36, an
<B>IN_IGNORED</B>
event is generated in this case.
<P>
Before kernel 2.6.25,
the kernel code that was intended to coalesce successive identical events
(i.e., the two most recent events could potentially be coalesced
if the older had not yet been read)
instead checked if the most recent event could be coalesced with the
<I>oldest</I>
unread event.
<P>
When a watch descriptor is removed by calling
<B><A HREF="/cgi-bin/man/man2html?2+inotify_rm_watch">inotify_rm_watch</A></B>(2)
(or because a watch file is deleted or the filesystem
that contains it is unmounted),
any pending unread events for that watch descriptor remain available to read.
As watch descriptors are subsequently allocated with
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2),
the kernel cycles through the range of possible watch descriptors (0 to
<B>INT_MAX</B>)
incrementally.
When allocating a free watch descriptor, no check is made to see whether that
watch descriptor number has any pending unread events in the inotify queue.
Thus, it can happen that a watch descriptor is reallocated even
when pending unread events exist for a previous incarnation of
that watch descriptor number, with the result that the application
might then read those events and interpret them as belonging to
the file associated with the newly recycled watch descriptor.
In practice, the likelihood of hitting this bug may be extremely low,
since it requires that an application cycle through
<B>INT_MAX</B>
watch descriptors,
release a watch descriptor while leaving unread events for that
watch descriptor in the queue,
and then recycle that watch descriptor.
For this reason, and because there have been no reports
of the bug occurring in real-world applications,
as of Linux 3.15,
no kernel changes have yet been made to eliminate this possible bug.
<A NAME="lbAN">&nbsp;</A>
<H2>EXAMPLE</H2>
The following program demonstrates the usage of the inotify API.
It marks the directories passed as a command-line arguments
and waits for events of type
<B>IN_OPEN</B>,
<B>IN_CLOSE_NOWRITE</B>
and
<B>IN_CLOSE_WRITE</B>.
<P>
The following output was recorded while editing the file
<I>/home/user/temp/foo</I>
and listing directory
<I>/tmp</I>.
Before the file and the directory were opened,
<B>IN_OPEN</B>
events occurred.
After the file was closed, an
<B>IN_CLOSE_WRITE</B>
event occurred.
After the directory was closed, an
<B>IN_CLOSE_NOWRITE</B>
event occurred.
Execution of the program ended when the user pressed the ENTER key.
<A NAME="lbAO">&nbsp;</A>
<H3>Example output</H3>
$ <B>./a.out /tmp /home/user/temp</B>
Press enter key to terminate.
Listening for events.
IN_OPEN: /home/user/temp/foo [file]
IN_CLOSE_WRITE: /home/user/temp/foo [file]
IN_OPEN: /tmp/ [directory]
IN_CLOSE_NOWRITE: /tmp/ [directory]
<P>
Listening for events stopped.
<A NAME="lbAP">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/poll.h">poll.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/inotify.h">sys/inotify.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
<P>
/* Read all available inotify events from the file descriptor 'fd'.
<BR>&nbsp;&nbsp;&nbsp;wd&nbsp;is&nbsp;the&nbsp;table&nbsp;of&nbsp;watch&nbsp;descriptors&nbsp;for&nbsp;the&nbsp;directories&nbsp;in&nbsp;argv.
<BR>&nbsp;&nbsp;&nbsp;argc&nbsp;is&nbsp;the&nbsp;length&nbsp;of&nbsp;wd&nbsp;and&nbsp;argv.
<BR>&nbsp;&nbsp;&nbsp;argv&nbsp;is&nbsp;the&nbsp;list&nbsp;of&nbsp;watched&nbsp;directories.
<BR>&nbsp;&nbsp;&nbsp;Entry&nbsp;0&nbsp;of&nbsp;wd&nbsp;and&nbsp;argv&nbsp;is&nbsp;unused.&nbsp;*/
<P>
static void
handle_events(int fd, int *wd, int argc, char* argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Some&nbsp;systems&nbsp;cannot&nbsp;read&nbsp;integer&nbsp;variables&nbsp;if&nbsp;they&nbsp;are&nbsp;not
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;properly&nbsp;aligned.&nbsp;On&nbsp;other&nbsp;systems,&nbsp;incorrect&nbsp;alignment&nbsp;may
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decrease&nbsp;performance.&nbsp;Hence,&nbsp;the&nbsp;buffer&nbsp;used&nbsp;for&nbsp;reading&nbsp;from
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;inotify&nbsp;file&nbsp;descriptor&nbsp;should&nbsp;have&nbsp;the&nbsp;same&nbsp;alignment&nbsp;as
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;inotify_event.&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf[4096]
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__attribute__&nbsp;((aligned(__alignof__(struct&nbsp;inotify_event))));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;struct&nbsp;inotify_event&nbsp;*event;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;len;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*ptr;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Loop&nbsp;while&nbsp;events&nbsp;can&nbsp;be&nbsp;read&nbsp;from&nbsp;inotify&nbsp;file&nbsp;descriptor.&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(;;)&nbsp;{
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Read&nbsp;some&nbsp;events.&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len&nbsp;=&nbsp;read(fd,&nbsp;buf,&nbsp;sizeof&nbsp;buf);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(len&nbsp;==&nbsp;-1&nbsp;&amp;&amp;&nbsp;errno&nbsp;!=&nbsp;EAGAIN)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;read&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;If&nbsp;the&nbsp;nonblocking&nbsp;read()&nbsp;found&nbsp;no&nbsp;events&nbsp;to&nbsp;read,&nbsp;then
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it&nbsp;returns&nbsp;-1&nbsp;with&nbsp;errno&nbsp;set&nbsp;to&nbsp;EAGAIN.&nbsp;In&nbsp;that&nbsp;case,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;we&nbsp;exit&nbsp;the&nbsp;loop.&nbsp;*/
<P>
<BR>&nbsp;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IR)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;[directory]\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;[file]\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
}
<P>
int
main(int argc, char* argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;fd,&nbsp;i,&nbsp;poll_num;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;*wd;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;nfds_t&nbsp;nfds;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;pollfd&nbsp;fds[2];
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&lt;&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Usage:&nbsp;%s&nbsp;PATH&nbsp;[PATH&nbsp;...]\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Press&nbsp;ENTER&nbsp;key&nbsp;to&nbsp;terminate.\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Create&nbsp;the&nbsp;file&nbsp;descriptor&nbsp;for&nbsp;accessing&nbsp;the&nbsp;inotify&nbsp;API&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fd&nbsp;=&nbsp;inotify_init1(IN_NONBLOCK);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fd&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;inotify_init1&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Allocate&nbsp;memory&nbsp;for&nbsp;watch&nbsp;descriptors&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;wd&nbsp;=&nbsp;calloc(argc,&nbsp;sizeof(int));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(wd&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;calloc&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Mark&nbsp;directories&nbsp;for&nbsp;events
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;file&nbsp;was&nbsp;opened
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;file&nbsp;was&nbsp;closed&nbsp;*/
<P>
<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ling&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;nfds&nbsp;=&nbsp;2;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Console&nbsp;input&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fds[0].fd&nbsp;=&nbsp;STDIN_FILENO;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fds[0].events&nbsp;=&nbsp;POLLIN;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Inotify&nbsp;input&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fds[1].fd&nbsp;=&nbsp;fd;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fds[1].events&nbsp;=&nbsp;POLLIN;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Wait&nbsp;for&nbsp;events&nbsp;and/or&nbsp;terminal&nbsp;input&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Listening&nbsp;for&nbsp;events.\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;poll_num&nbsp;=&nbsp;poll(fds,&nbsp;nfds,&nbsp;-1);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(poll_num&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(errno&nbsp;==&nbsp;EINTR)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;
<BR>&nbsp;&nbsp;&nbsp;&n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bsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fds[1].revents&nbsp;&amp;&nbsp;POLLIN)&nbsp;{
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Inotify&nbsp;events&nbsp;are&nbsp;available&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_events(fd,&nbsp;wd,&nbsp;argc,&nbsp;argv);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Listening&nbsp;for&nbsp;events&nbsp;stopped.\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Close&nbsp;inotify&nbsp;file&nbsp;descriptor&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;close(fd);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;free(wd);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAQ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+inotifywait">inotifywait</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+inotifywatch">inotifywatch</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+inotify_add_watch">inotify_add_watch</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+inotify_init">inotify_init</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+inotify_init1">inotify_init1</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+inotify_rm_watch">inotify_rm_watch</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+fanotify">fanotify</A></B>(7)
<P>
<I>Documentation/filesystems/inotify.txt</I>
in the Linux kernel source tree
<A NAME="lbAR">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="55"><A HREF="#lbAB">NAME</A><DD>
<DT id="56"><A HREF="#lbAC">DESCRIPTION</A><DD>
<DL>
<DT id="57"><A HREF="#lbAD">Reading events from an inotify file descriptor</A><DD>
<DT id="58"><A HREF="#lbAE">inotify events</A><DD>
<DT id="59"><A HREF="#lbAF">Examples</A><DD>
<DT id="60"><A HREF="#lbAG">/proc interfaces</A><DD>
</DL>
<DT id="61"><A HREF="#lbAH">VERSIONS</A><DD>
<DT id="62"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="63"><A HREF="#lbAJ">NOTES</A><DD>
<DL>
<DT id="64"><A HREF="#lbAK">Limitations and caveats</A><DD>
<DT id="65"><A HREF="#lbAL">Dealing with rename() events</A><DD>
</DL>
<DT id="66"><A HREF="#lbAM">BUGS</A><DD>
<DT id="67"><A HREF="#lbAN">EXAMPLE</A><DD>
<DL>
<DT id="68"><A HREF="#lbAO">Example output</A><DD>
<DT id="69"><A HREF="#lbAP">Program source</A><DD>
</DL>
<DT id="70"><A HREF="#lbAQ">SEE ALSO</A><DD>
<DT id="71"><A HREF="#lbAR">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:06:08 GMT, March 31, 2021
</BODY>
</HTML>