1506 lines
44 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
inotify - monitoring filesystem events
|
|
<A NAME="lbAC"> </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 "watch list" associated with an inotify instance.
|
|
Each item ("watch") 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 "watch descriptor", 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"> </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> int wd; /* Watch descriptor */
|
|
|
|
|
|
|
|
|
|
<BR> uint32_t mask; /* Mask describing event */
|
|
<BR> uint32_t cookie; /* Unique cookie associating related
|
|
<BR> events (for <A HREF="/cgi-bin/man/man2html?2+rename">rename</A>(2)) */
|
|
<BR> uint32_t len; /* Size of <I>name</I> field */
|
|
<BR> char name[]; /* Optional null-terminated name */
|
|
};
|
|
|
|
|
|
<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> sizeof(struct inotify_event) + NAME_MAX + 1
|
|
<P>
|
|
|
|
will be sufficient to read at least one event.
|
|
<A NAME="lbAE"> </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"> </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("dir/myfile", 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("dir1/myfile", "dir2/new");<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("dir1/myfile", "dir2/myfile");<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("dir2/yy");<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("dir1/xx");<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("dir/new", mode);<DD>
|
|
Generates an
|
|
<B>IN_CREATE | IN_ISDIR</B>
|
|
|
|
event for
|
|
<I>dir</I>.
|
|
|
|
<DT id="51">rmdir("dir/subdir");<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"> </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"> </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"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
The inotify API is Linux-specific.
|
|
<A NAME="lbAJ"> </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"> </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"> </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"> </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"> </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"> </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"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
#include <<A HREF="file:///usr/include/poll.h">poll.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/inotify.h">sys/inotify.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
<P>
|
|
/* Read all available inotify events from the file descriptor 'fd'.
|
|
<BR> wd is the table of watch descriptors for the directories in argv.
|
|
<BR> argc is the length of wd and argv.
|
|
<BR> argv is the list of watched directories.
|
|
<BR> Entry 0 of wd and argv is unused. */
|
|
<P>
|
|
static void
|
|
handle_events(int fd, int *wd, int argc, char* argv[])
|
|
{
|
|
<BR> /* Some systems cannot read integer variables if they are not
|
|
<BR> properly aligned. On other systems, incorrect alignment may
|
|
<BR> decrease performance. Hence, the buffer used for reading from
|
|
<BR> the inotify file descriptor should have the same alignment as
|
|
<BR> struct inotify_event. */
|
|
<P>
|
|
<BR> char buf[4096]
|
|
<BR> __attribute__ ((aligned(__alignof__(struct inotify_event))));
|
|
<BR> const struct inotify_event *event;
|
|
<BR> int i;
|
|
<BR> ssize_t len;
|
|
<BR> char *ptr;
|
|
<P>
|
|
<BR> /* Loop while events can be read from inotify file descriptor. */
|
|
<P>
|
|
<BR> for (;;) {
|
|
<P>
|
|
<BR> /* Read some events. */
|
|
<P>
|
|
<BR> len = read(fd, buf, sizeof buf);
|
|
<BR> if (len == -1 && errno != EAGAIN) {
|
|
<BR> perror("read");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* If the nonblocking read() found no events to read, then
|
|
<BR> it returns -1 with errno set to EAGAIN. In that case,
|
|
<BR> we exit the loop. */
|
|
<P>
|
|
<BR> if (len <= 0)
|
|
<BR> break;
|
|
<P>
|
|
<BR> /* Loop over all events in the buffer */
|
|
<P>
|
|
<BR> for (ptr = buf; ptr < buf + len;
|
|
<BR> ptr += sizeof(struct inotify_event) + event->len) {
|
|
<P>
|
|
<BR> event = (const struct inotify_event *) ptr;
|
|
<P>
|
|
<BR> /* Print event type */
|
|
<P>
|
|
<BR> if (event->mask & IN_OPEN)
|
|
<BR> printf("IN_OPEN: ");
|
|
<BR> if (event->mask & IN_CLOSE_NOWRITE)
|
|
<BR> printf("IN_CLOSE_NOWRITE: ");
|
|
<BR> if (event->mask & IN_CLOSE_WRITE)
|
|
<BR> printf("IN_CLOSE_WRITE: ");
|
|
<P>
|
|
<BR> /* Print the name of the watched directory */
|
|
<P>
|
|
<BR> for (i = 1; i < argc; ++i) {
|
|
<BR> if (wd[i] == event->wd) {
|
|
<BR> printf("%s/", argv[i]);
|
|
<BR> break;
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Print the name of the file */
|
|
<P>
|
|
<BR> if (event->len)
|
|
<BR> printf("%s", event->name);
|
|
<P>
|
|
<BR> /* Print type of filesystem object */
|
|
<P>
|
|
<BR> if (event->mask & IN_ISDIR)
|
|
<BR> printf(" [directory]\n");
|
|
<BR> else
|
|
<BR> printf(" [file]\n");
|
|
<BR> }
|
|
<BR> }
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char* argv[])
|
|
{
|
|
<BR> char buf;
|
|
<BR> int fd, i, poll_num;
|
|
<BR> int *wd;
|
|
<BR> nfds_t nfds;
|
|
<BR> struct pollfd fds[2];
|
|
<P>
|
|
<BR> if (argc < 2) {
|
|
<BR> printf("Usage: %s PATH [PATH ...]\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("Press ENTER key to terminate.\n");
|
|
<P>
|
|
<BR> /* Create the file descriptor for accessing the inotify API */
|
|
<P>
|
|
<BR> fd = inotify_init1(IN_NONBLOCK);
|
|
<BR> if (fd == -1) {
|
|
<BR> perror("inotify_init1");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Allocate memory for watch descriptors */
|
|
<P>
|
|
<BR> wd = calloc(argc, sizeof(int));
|
|
<BR> if (wd == NULL) {
|
|
<BR> perror("calloc");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Mark directories for events
|
|
<BR> - file was opened
|
|
<BR> - file was closed */
|
|
<P>
|
|
<BR> for (i = 1; i < argc; i++) {
|
|
<BR> wd[i] = inotify_add_watch(fd, argv[i],
|
|
<BR> IN_OPEN | IN_CLOSE);
|
|
<BR> if (wd[i] == -1) {
|
|
<BR> fprintf(stderr, "Cannot watch '%s': %s\n",
|
|
<BR> argv[i], strerror(errno));
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Prepare for polling */
|
|
<P>
|
|
<BR> nfds = 2;
|
|
<P>
|
|
<BR> /* Console input */
|
|
<P>
|
|
<BR> fds[0].fd = STDIN_FILENO;
|
|
<BR> fds[0].events = POLLIN;
|
|
<P>
|
|
<BR> /* Inotify input */
|
|
<P>
|
|
<BR> fds[1].fd = fd;
|
|
<BR> fds[1].events = POLLIN;
|
|
<P>
|
|
<BR> /* Wait for events and/or terminal input */
|
|
<P>
|
|
<BR> printf("Listening for events.\n");
|
|
<BR> while (1) {
|
|
<BR> poll_num = poll(fds, nfds, -1);
|
|
<BR> if (poll_num == -1) {
|
|
<BR> if (errno == EINTR)
|
|
<BR> continue;
|
|
<BR> perror("poll");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (poll_num > 0) {
|
|
<P>
|
|
<BR> if (fds[0].revents & POLLIN) {
|
|
<P>
|
|
<BR> /* Console input is available. Empty stdin and quit */
|
|
<P>
|
|
<BR> while (read(STDIN_FILENO, &buf, 1) > 0 && buf != '\n')
|
|
<BR> continue;
|
|
<BR> break;
|
|
<BR> }
|
|
<P>
|
|
<BR> if (fds[1].revents & POLLIN) {
|
|
<P>
|
|
<BR> /* Inotify events are available */
|
|
<P>
|
|
<BR> handle_events(fd, wd, argc, argv);
|
|
<BR> }
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("Listening for events stopped.\n");
|
|
<P>
|
|
<BR> /* Close inotify file descriptor */
|
|
<P>
|
|
<BR> close(fd);
|
|
<P>
|
|
<BR> free(wd);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAQ"> </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"> </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"> </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>
|