{"id":3961,"date":"2022-12-20T17:28:31","date_gmt":"2022-12-20T20:28:31","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/fanotify-man7\/"},"modified":"2022-12-20T17:28:31","modified_gmt":"2022-12-20T20:28:31","slug":"fanotify-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/fanotify-man7\/","title":{"rendered":"FANOTIFY (man7)"},"content":{"rendered":"<h1 align=\"center\">FANOTIFY<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#VERSIONS\">VERSIONS<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#BUGS\">BUGS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">fanotify \u2212 monitoring filesystem events<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The fanotify API provides notification and interception of filesystem events. Use cases include virus scanning and hierarchical storage management. In the original fanotify API, only a limited set of events was supported. In particular, there was no support for create, delete, and move events. The support for those events was added in Linux 5.1. (See <b>inotify<\/b>(7) for details of an API that did notify those events pre Linux 5.1.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Additional capabilities compared to the <b>inotify<\/b>(7) API include the ability to monitor all of the objects in a mounted filesystem, the ability to make access permission decisions, and the possibility to read or modify files before access by other applications.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following system calls are used with this API: <b>fanotify_init<\/b>(2), <b>fanotify_mark<\/b>(2), <b>read<\/b>(2), <b>write<\/b>(2), and <b>close<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>fanotify_init(), fanotify_mark(), and notification groups<\/b> <br \/> The <b>fanotify_init<\/b>(2) system call creates and initializes an fanotify notification group and returns a file descriptor referring to it.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">An fanotify notification group is a kernel-internal object that holds a list of files, directories, filesystems, and mount points for which events shall be created.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For each entry in an fanotify notification group, two bit masks exist: the <i>mark<\/i> mask and the <i>ignore<\/i> mask. The mark mask defines file activities for which an event shall be created. The ignore mask defines activities for which no event shall be generated. Having these two types of masks permits a filesystem, mount point, or directory to be marked for receiving events, while at the same time ignoring events for specific objects under a mount point or directory.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>fanotify_mark<\/b>(2) system call adds a file, directory, filesystem or mount point to a notification group and specifies which events shall be reported (or ignored), or removes or modifies such an entry.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A possible usage of the ignore mask is for a file cache. Events of interest for a file cache are modification of a file and closing of the same. Hence, the cached directory or mount point is to be marked to receive these events. After receiving the first event informing that a file has been modified, the corresponding cache entry will be invalidated. No further modification events for this file are of interest until the file is closed. Hence, the modify event can be added to the ignore mask. Upon receiving the close event, the modify event can be removed from the ignore mask and the file cache entry can be updated.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The entries in the fanotify notification groups refer to files and directories via their inode number and to mounts via their mount ID. If files or directories are renamed or moved within the same mount, the respective entries survive. If files or directories are deleted or moved to another mount or if filesystems or mounts are unmounted, the corresponding entries are deleted.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>The event queue<\/b> <br \/> As events occur on the filesystem objects monitored by a notification group, the fanotify system generates events that are collected in a queue. These events can then be read (using <b>read<\/b>(2) or similar) from the fanotify file descriptor returned by <b>fanotify_init<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Two types of events are generated: <i>notification<\/i> events and <i>permission<\/i> events. Notification events are merely informative and require no action to be taken by the receiving application with one exception: if a valid file descriptor is provided within a generic event, the file descriptor must be closed. Permission events are requests to the receiving application to decide whether permission for a file access shall be granted. For these events, the recipient must write a response which decides whether access is granted or not.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">An event is removed from the event queue of the fanotify group when it has been read. Permission events that have been read are kept in an internal list of the fanotify group until either a permission decision has been taken by writing to the fanotify file descriptor or the fanotify file descriptor is closed.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Reading fanotify events<\/b> <br \/> Calling <b>read<\/b>(2) for the file descriptor returned by <b>fanotify_init<\/b>(2) blocks (if the flag <b>FAN_NONBLOCK<\/b> is not specified in the call to <b>fanotify_init<\/b>(2)) until either a file event occurs or the call is interrupted by a signal (see <b>signal<\/b>(7)).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The use of one of the flags <b>FAN_REPORT_FID<\/b>, <b>FAN_REPORT_DIR_FID<\/b> in <b>fanotify_init<\/b>(2) influences what data structures are returned to the event listener for each event. Events reported to a group initialized with one of these flags will use file handles to identify filesystem objects instead of file descriptors. <br \/> After a successful<\/p>\n<p style=\"margin-left:22%;\"><b>read<\/b>(2), the read buffer contains one or more of the following structures:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct fanotify_event_metadata { <br \/> __u32 event_len; <br \/> __u8 vers; <br \/> __u8 reserved; <br \/> __u16 metadata_len; <br \/> __aligned_u64 mask; <br \/> __s32 fd; <br \/> __s32 pid; <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In case of an fanotify group that identifies filesystem objects by file handles, you should also expect to receive one or more additional information records of the structure detailed below following the generic <i>fanotify_event_metadata<\/i> structure within the read buffer:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct fanotify_event_info_header { <br \/> __u8 info_type; <br \/> __u8 pad; <br \/> __u16 len; <br \/> };<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct fanotify_event_info_fid { <br \/> struct fanotify_event_info_header hdr; <br \/> __kernel_fsid_t fsid; <br \/> unsigned char file_handle[0]; <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For performance reasons, it is recommended to use a large buffer size (for example, 4096 bytes), so that multiple events can be retrieved by a single <b>read<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The return value of <b>read<\/b>(2) is the number of bytes placed in the buffer, or \u22121 in case of an error (but see BUGS).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The fields of the <i>fanotify_event_metadata<\/i> structure are as follows: <i><br \/> event_len<\/i><\/p>\n<p style=\"margin-left:22%;\">This is the length of the data for the current event and the offset to the next event in the buffer. Unless the group identifies filesystem objects by file handles, the value of <i>event_len<\/i> is always <b>FAN_EVENT_METADATA_LEN<\/b>. For a group that identifies filesystem objects by file handles, <i>event_len<\/i> also includes the variable length file identifier records.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"6%\">\n<p><i>vers<\/i><\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"78%\">\n<p>This field holds a version number for the structure. It must be compared to <b>FANOTIFY_METADATA_VERSION<\/b> to verify that the structures returned at run time match the structures defined at compile time. In case of a mismatch, the application should abandon trying to use the fanotify file descriptor.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><i>reserved<\/i><\/p>\n<p style=\"margin-left:22%;\">This field is not used.<\/p>\n<p style=\"margin-left:11%;\"><i>metadata_len<\/i><\/p>\n<p style=\"margin-left:22%;\">This is the length of the structure. The field was introduced to facilitate the implementation of optional headers per event type. No such optional headers exist in the current implementation.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"6%\">\n<p><i>mask<\/i><\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"78%\">\n<p>This is a bit mask describing the event (see below).<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"6%\">\n<p><i>fd<\/i><\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"78%\">\n<p>This is an open file descriptor for the object being accessed, or <b>FAN_NOFD<\/b> if a queue overflow occurred. With an fanotify group that identifies filesystem objects by file handles, applications should expect this value to be set to <b>FAN_NOFD<\/b> for each event that is received. The file descriptor can be used to access the contents of the monitored file or directory. The reading application is responsible for closing this file descriptor.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:22%; margin-top: 1em\">When calling <b>fanotify_init<\/b>(2), the caller may specify (via the <i>event_f_flags<\/i> argument) various file status flags that are to be set on the open file description that corresponds to this file descriptor. In addition, the (kernel-internal) <b>FMODE_NONOTIFY<\/b> file status flag is set on the open file description. This flag suppresses fanotify event generation. Hence, when the receiver of the fanotify event accesses the notified file or directory using this file descriptor, no additional events will be created.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"4%\">\n<p style=\"margin-top: 1em\"><i>pid<\/i><\/p>\n<\/td>\n<td width=\"7%\"><\/td>\n<td width=\"78%\">\n<p style=\"margin-top: 1em\">If flag <b>FAN_REPORT_TID<\/b> was set in <b>fanotify_init<\/b>(2), this is the TID of the thread that caused the event. Otherwise, this the PID of the process that caused the event.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">A program listening to fanotify events can compare this PID to the PID returned by <b>getpid<\/b>(2), to determine whether the event is caused by the listener itself, or is due to a file access by another process.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The bit mask in <i>mask<\/i> indicates which events have occurred for a single filesystem object. Multiple bits may be set in this mask, if more than one event occurred for the monitored filesystem object. In particular, consecutive events for the same filesystem object and originating from the same process may be merged into a single event, with the exception that two permission events are never merged into one queue entry.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The bits that may appear in <i>mask<\/i> are as follows: <b><br \/> FAN_ACCESS<\/b><\/p>\n<p style=\"margin-left:22%;\">A file or a directory (but see BUGS) was accessed (read).<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_OPEN<\/b><\/p>\n<p style=\"margin-left:22%;\">A file or a directory was opened.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_OPEN_EXEC<\/b><\/p>\n<p style=\"margin-left:22%;\">A file was opened with the intent to be executed. See NOTES in <b>fanotify_mark<\/b>(2) for additional details.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_ATTRIB<\/b><\/p>\n<p style=\"margin-left:22%;\">A file or directory metadata was changed.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_CREATE<\/b><\/p>\n<p style=\"margin-left:22%;\">A child file or directory was created in a watched parent.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_DELETE<\/b><\/p>\n<p style=\"margin-left:22%;\">A child file or directory was deleted in a watched parent.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_DELETE_SELF<\/b><\/p>\n<p style=\"margin-left:22%;\">A watched file or directory was deleted.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_MOVED_FROM<\/b><\/p>\n<p style=\"margin-left:22%;\">A file or directory has been moved from a watched parent directory.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_MOVED_TO<\/b><\/p>\n<p style=\"margin-left:22%;\">A file or directory has been moved to a watched parent directory.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_MOVE_SELF<\/b><\/p>\n<p style=\"margin-left:22%;\">A watched file or directory was moved.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_MODIFY<\/b><\/p>\n<p style=\"margin-left:22%;\">A file was modified.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_CLOSE_WRITE<\/b><\/p>\n<p style=\"margin-left:22%;\">A file that was opened for writing (<b>O_WRONLY<\/b> or <b>O_RDWR<\/b>) was closed.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_CLOSE_NOWRITE<\/b><\/p>\n<p style=\"margin-left:22%;\">A file or directory that was opened read-only (<b>O_RDONLY<\/b>) was closed.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_Q_OVERFLOW<\/b><\/p>\n<p style=\"margin-left:22%;\">The event queue exceeded the limit of 16384 entries. This limit can be overridden by specifying the <b>FAN_UNLIMITED_QUEUE<\/b> flag when calling <b>fanotify_init<\/b>(2).<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_ACCESS_PERM<\/b><\/p>\n<p style=\"margin-left:22%;\">An application wants to read a file or directory, for example using <b>read<\/b>(2) or <b>readdir<\/b>(2). The reader must write a response (as described below) that determines whether the permission to access the filesystem object shall be granted.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_OPEN_PERM<\/b><\/p>\n<p style=\"margin-left:22%;\">An application wants to open a file or directory. The reader must write a response that determines whether the permission to open the filesystem object shall be granted.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_OPEN_EXEC_PERM<\/b><\/p>\n<p style=\"margin-left:22%;\">An application wants to open a file for execution. The reader must write a response that determines whether the permission to open the filesystem object for execution shall be granted. See NOTES in <b>fanotify_mark<\/b>(2) for additional details.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">To check for any close event, the following bit mask may be used: <b><br \/> FAN_CLOSE<\/b><\/p>\n<p style=\"margin-left:22%;\">A file was closed. This is a synonym for:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">To check for any move event, the following bit mask may be used: <b><br \/> FAN_MOVE<\/b><\/p>\n<p style=\"margin-left:22%;\">A file or directory was moved. This is a synonym for:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">FAN_MOVED_FROM | FAN_MOVED_TO<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following bits may appear in <i>mask<\/i> only in conjunction with other event type bits: <b><br \/> FAN_ONDIR<\/b><\/p>\n<p style=\"margin-left:22%;\">The events described in the <i>mask<\/i> have occurred on a directory object. Reporting events on directories requires setting this flag in the mark mask. See <b>fanotify_mark<\/b>(2) for additional details. The <b>FAN_ONDIR<\/b> flag is reported in an event mask only if the fanotify group identifies filesystem objects by file handles.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The fields of the <i>fanotify_event_info_fid<\/i> structure are as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"6%\">\n<p><i>hdr<\/i><\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"78%\">\n<p>This is a structure of type <i>fanotify_event_info_header<\/i>. It is a generic header that contains information used to describe an additional information record attached to the event. For example, when an fanotify file descriptor is created using <b>FAN_REPORT_FID<\/b>, a single information record is expected to be attached to the event with <i>info_type<\/i> field value of <b>FAN_EVENT_INFO_TYPE_FID<\/b>. When an fanotify file descriptor is created using the combination of <b>FAN_REPORT_FID<\/b> and <b>FAN_REPORT_DIR_FID<\/b>, there may be two information records attached to the event: one with <i>info_type<\/i> field value of <b>FAN_EVENT_INFO_TYPE_DFID<\/b>, identifying a parent directory object, and one with <i>info_type<\/i> field value of <b>FAN_EVENT_INFO_TYPE_FID<\/b>, identifying a non-directory object. The <i>fanotify_event_info_header<\/i> contains a <i>len<\/i> field. The value of <i>len<\/i> is the size of the additional information record including the <i>fanotify_event_info_header<\/i> itself. The total size of all additional information records is not expected to be bigger than ( <i>event_len<\/i> \u2212 <i>metadata_len<\/i> ).<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"6%\">\n<p><i>fsid<\/i><\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"78%\">\n<p>This is a unique identifier of the filesystem containing the object associated with the event. It is a structure of type <i>__kernel_fsid_t<\/i> and contains the same value as <i>f_fsid<\/i> when calling <b>statfs<\/b>(2).<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><i>file_handle<\/i><\/p>\n<p style=\"margin-left:22%;\">This is a variable length structure of type struct file_handle. It is an opaque handle that corresponds to a specified object on a filesystem as returned by <b>name_to_handle_at<\/b>(2). It can be used to uniquely identify a file on a filesystem and can be passed as an argument to <b>open_by_handle_at<\/b>(2). Note that for the directory entry modification events <b>FAN_CREATE<\/b>, <b>FAN_DELETE<\/b>, and <b>FAN_MOVE<\/b>, the <i>file_handle<\/i> identifies the modified directory and not the created\/deleted\/moved child object. If the value of <i>info_type<\/i> field is <b>FAN_EVENT_INFO_TYPE_DFID_NAME<\/b>, the file handle is followed by a null terminated string that identifies the created\/deleted\/moved directory entry name. For other events such as <b>FAN_OPEN<\/b>, <b>FAN_ATTRIB<\/b>, <b>FAN_DELETE_SELF<\/b>, and <b>FAN_MOVE_SELF<\/b>, if the value of <i>info_type<\/i> field is <b>FAN_EVENT_INFO_TYPE_FID<\/b>, the <i>file_handle<\/i> identifies the object correlated to the event. If the value of <i>info_type<\/i> field is <b>FAN_EVENT_INFO_TYPE_DFID<\/b>, the <i>file_handle<\/i> identifies the directory object correlated to the event or the parent directory of a non-directory object correlated to the event. If the value of <i>info_type<\/i> field is <b>FAN_EVENT_INFO_TYPE_DFID_NAME<\/b>, the <i>file_handle<\/i> identifies the same directory object that would be reported with <b>FAN_EVENT_INFO_TYPE_DFID<\/b> and the file handle is followed by a null terminated string that identifies the name of a directory entry in that directory, or \u2019.\u2019 to identify the directory object itself.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following macros are provided to iterate over a buffer containing fanotify event metadata returned by a <b>read<\/b>(2) from an fanotify file descriptor: <b><br \/> FAN_EVENT_OK(meta, len)<\/b><\/p>\n<p style=\"margin-left:22%;\">This macro checks the remaining length <i>len<\/i> of the buffer <i>meta<\/i> against the length of the metadata structure and the <i>event_len<\/i> field of the first metadata structure in the buffer.<\/p>\n<p style=\"margin-left:11%;\"><b>FAN_EVENT_NEXT(meta, len)<\/b><\/p>\n<p style=\"margin-left:22%;\">This macro uses the length indicated in the <i>event_len<\/i> field of the metadata structure pointed to by <i>meta<\/i> to calculate the address of the next metadata structure that follows <i>meta<\/i>. <i>len<\/i> is the number of bytes of metadata that currently remain in the buffer. The macro returns a pointer to the next metadata structure that follows <i>meta<\/i>, and reduces <i>len<\/i> by the number of bytes in the metadata structure that has been skipped over (i.e., it subtracts <i>meta\u2212>event_len<\/i> from <i>len<\/i>).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In addition, there is: <b><br \/> FAN_EVENT_METADATA_LEN<\/b><\/p>\n<p style=\"margin-left:22%;\">This macro returns the size (in bytes) of the structure <i>fanotify_event_metadata<\/i>. This is the minimum size (and currently the only size) of any event metadata.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Monitoring an fanotify file descriptor for events<\/b> <br \/> When an fanotify event occurs, the fanotify file descriptor indicates as readable when passed to <b>epoll<\/b>(7), <b>poll<\/b>(2), or <b>select<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Dealing with permission events<\/b> <br \/> For permission events, the application must <b>write<\/b>(2) a structure of the following form to the fanotify file descriptor:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct fanotify_response { <br \/> __s32 fd; <br \/> __u32 response; <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The fields of this structure are as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p><i>fd<\/i><\/p>\n<\/td>\n<td width=\"8%\"><\/td>\n<td width=\"78%\">\n<p>This is the file descriptor from the structure <i>fanotify_event_metadata<\/i>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><i>response<\/i><\/p>\n<p style=\"margin-left:22%;\">This field indicates whether or not the permission is to be granted. Its value must be either <b>FAN_ALLOW<\/b> to allow the file operation or <b>FAN_DENY<\/b> to deny the file operation.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If access is denied, the requesting application call will receive an <b>EPERM<\/b> error.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Closing the fanotify file descriptor<\/b> <br \/> When all file descriptors referring to the fanotify notification group are closed, the fanotify group is released and its resources are freed for reuse by the kernel. Upon <b>close<\/b>(2), outstanding permission events will be set to allowed.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>\/proc\/[pid]\/fdinfo<\/b> <br \/> The file <i>\/proc\/[pid]\/fdinfo\/[fd]<\/i> contains information about fanotify marks for file descriptor <i>fd<\/i> of process <i>pid<\/i>. See <b>proc<\/b>(5) for details.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">In addition to the usual errors for <b>read<\/b>(2), the following errors can occur when reading from the fanotify file descriptor:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The buffer is too small to hold the event.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EMFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The per-process limit on the number of open files has been reached. See the description of <b>RLIMIT_NOFILE<\/b> in <b>getrlimit<\/b>(2).<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The system-wide limit on the total number of open files has been reached. See <i>\/proc\/sys\/fs\/file\u2212max<\/i> in <b>proc<\/b>(5).<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ETXTBSY<\/b><\/p>\n<p style=\"margin-left:22%;\">This error is returned by <b>read<\/b>(2) if <b>O_RDWR<\/b> or <b>O_WRONLY<\/b> was specified in the <i>event_f_flags<\/i> argument when calling <b>fanotify_init<\/b>(2) and an event occurred for a monitored file that is currently being executed.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In addition to the usual errors for <b>write<\/b>(2), the following errors can occur when writing to the fanotify file descriptor:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Fanotify access permissions are not enabled in the kernel configuration or the value of <i>response<\/i> in the response structure is not valid.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOENT<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The file descriptor <i>fd<\/i> in the response structure is not valid. This may occur when a response for the permission event has already been written.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>VERSIONS <a name=\"VERSIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The fanotify API was introduced in version 2.6.36 of the Linux kernel and enabled in version 2.6.37. Fdinfo support was added in version 3.8.<\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The fanotify API is Linux-specific.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The fanotify API is available only if the kernel was built with the <b>CONFIG_FANOTIFY<\/b> configuration option enabled. In addition, fanotify permission handling is available only if the <b>CONFIG_FANOTIFY_ACCESS_PERMISSIONS<\/b> configuration option is enabled.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Limitations and caveats<\/b> <br \/> Fanotify 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The fanotify API does not report file accesses and modifications that may occur because of <b>mmap<\/b>(2), <b>msync<\/b>(2), and <b>munmap<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Events for directories are created only if the directory itself is opened, read, and closed. Adding, removing, or changing children of a marked directory does not create events for the monitored directory itself.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Fanotify monitoring of directories is not recursive: to monitor subdirectories under a directory, additional marks must be created. The <b>FAN_CREATE<\/b> event can be used for detecting when a subdirectory has been created under a marked directory. An additional mark must then be set on the newly created subdirectory. This approach is racy, because it can lose events that occurred inside the newly created subdirectory, before a mark is added on that subdirectory. Monitoring mounts offers the capability to monitor a whole directory tree in a race-free manner. Monitoring filesystems offers the capability to monitor changes made from any mount of a filesystem instance in a race-free manner.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The event queue can overflow. In this case, events are lost.<\/p>\n<h2>BUGS <a name=\"BUGS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Before Linux 3.19, <b>fallocate<\/b>(2) did not generate fanotify events. Since Linux 3.19, calls to <b>fallocate<\/b>(2) generate <b>FAN_MODIFY<\/b> events.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">As of Linux 3.17, the following bugs exist:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p style=\"margin-top: 1em\">On Linux, a filesystem object may be accessible through multiple paths, for example, a part of a filesystem may be remounted using the <i>\u2212\u2212bind<\/i> option of <b>mount<\/b>(8). A listener that marked a mount will be notified only of events that were triggered for a filesystem object using the same mount. Any other event will pass unnoticed.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>When an event is generated, no check is made to see whether the user ID of the receiving process has authorization to read or write the file before passing a file descriptor for that file. This poses a security risk, when the <b>CAP_SYS_ADMIN<\/b> capability is set for programs executed by unprivileged users.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>If a call to <b>read<\/b>(2) processes multiple events from the fanotify queue and an error occurs, the return value will be the total length of the events successfully copied to the user-space buffer before the error occurred. The return value will not be \u22121, and <i>errno<\/i> will not be set. Thus, the reading application has no way to detect the error.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The two example programs below demonstrate the usage of the fanotify API.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Example program: fanotify_example.c<\/b> <br \/> The first program is an example of fanotify being used with its event object information passed in the form of a file descriptor. The program marks the mount point passed as a command-line argument and waits for events of type <b>FAN_OPEN_PERM<\/b> and <b>FAN_CLOSE_WRITE<\/b>. When a permission event occurs, a <b>FAN_ALLOW<\/b> response is given.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following shell session shows an example of running this program. This session involved editing the file <i>\/home\/user\/temp\/notes<\/i>. Before the file was opened, a <b>FAN_OPEN_PERM<\/b> event occurred. After the file was closed, a <b>FAN_CLOSE_WRITE<\/b> event occurred. Execution of the program ends when the user presses the ENTER key.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"># <b>.\/fanotify_example \/home<\/b> <br \/> Press enter key to terminate. <br \/> Listening for events. <br \/> FAN_OPEN_PERM: File \/home\/user\/temp\/notes <br \/> FAN_CLOSE_WRITE: File \/home\/user\/temp\/notes<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Listening for events stopped.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Program source: fanotify_example.c<\/b> <br \/> #define _GNU_SOURCE \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Needed to get O_LARGEFILE definition bodies\/ usr\/ <br \/> #include <errno.h> <br \/> #include <fcntl.h> <br \/> #include <limits.h> #include <poll.h> #include <stdio.h> <br \/> #include <stdlib.h> <br \/> #include <sys\/fanotify.h> <br \/> #include <unistd.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Read all available fanotify events from the file descriptor &#8216;fd&#8217; *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">static void <br \/> handle_events(int fd) <br \/> { <br \/> const struct fanotify_event_metadata *metadata; <br \/> struct fanotify_event_metadata buf[200]; <br \/> ssize_t len; <br \/> char path[PATH_MAX]; <br \/> ssize_t path_len; <br \/> char procfd_path[PATH_MAX]; <br \/> struct fanotify_response response;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Loop while events can be read from fanotify file descriptor *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for (;;) {<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Read some events *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">len = read(fd, buf, sizeof(buf)); <br \/> if (len == \u22121 &#038;&#038; errno != EAGAIN) { <br \/> perror(&#8220;read&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Check if end of available data reached *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (len <= 0) <br \/> break;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Point to the first event in the buffer *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">metadata = buf;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Loop over all events in the buffer *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">while (FAN_EVENT_OK(metadata, len)) {<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Check that run\u2212time and compile\u2212time structures match *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (metadata\u2212>vers != FANOTIFY_METADATA_VERSION) { <br \/> fprintf(stderr, <br \/> &#8220;Mismatch of fanotify metadata version.n&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* metadata\u2212>fd contains either FAN_NOFD, indicating a <br \/> queue overflow, or a file descriptor (a nonnegative <br \/> integer). Here, we simply ignore queue overflow. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (metadata\u2212>fd >= 0) {<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Handle open permission event *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (metadata\u2212>mask &#038; FAN_OPEN_PERM) { <br \/> printf(&#8220;FAN_OPEN_PERM: &#8220;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Allow file to be opened *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">response.fd = metadata\u2212>fd; <br \/> response.response = FAN_ALLOW; <br \/> write(fd, &#038;response, sizeof(response)); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Handle closing of writable file event *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (metadata\u2212>mask &#038; FAN_CLOSE_WRITE) <br \/> printf(&#8220;FAN_CLOSE_WRITE: &#8220;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Retrieve and print pathname of the accessed file *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">snprintf(procfd_path, sizeof(procfd_path), <br \/> &#8220;\/proc\/self\/fd\/%d&#8221;, metadata\u2212>fd); <br \/> path_len = readlink(procfd_path, path, <br \/> sizeof(path) \u2212 1); <br \/> if (path_len == \u22121) { <br \/> perror(&#8220;readlink&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">path[path_len] = &#8216;\u0000&#8217;; <br \/> printf(&#8220;File %sn&#8221;, path);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Close the file descriptor of the event *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">close(metadata\u2212>fd); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Advance to next event *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">metadata = FAN_EVENT_NEXT(metadata, len); <br \/> } <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(int argc, char *argv[]) <br \/> { <br \/> char buf; <br \/> int fd, poll_num; <br \/> nfds_t nfds; <br \/> struct pollfd fds[2];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Check mount point is supplied *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (argc != 2) { <br \/> fprintf(stderr, &#8220;Usage: %s MOUNTn&#8221;, argv[0]); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;Press enter key to terminate.n&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Create the file descriptor for accessing the fanotify API *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">fd = fanotify_init(FAN_CLOEXEC | FAN_CLASS_CONTENT | FAN_NONBLOCK, <br \/> O_RDONLY | O_LARGEFILE); <br \/> if (fd == \u22121) { <br \/> perror(&#8220;fanotify_init&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Mark the mount for: <br \/> \u2212 permission events before opening files <br \/> \u2212 notification events after closing a write\u2212enabled <br \/> file descriptor *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, <br \/> FAN_OPEN_PERM | FAN_CLOSE_WRITE, AT_FDCWD, <br \/> argv[1]) == \u22121) { <br \/> perror(&#8220;fanotify_mark&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Prepare for polling *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">nfds = 2;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Console input *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">fds[0].fd = STDIN_FILENO; <br \/> fds[0].events = POLLIN;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Fanotify input *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">fds[1].fd = fd; <br \/> fds[1].events = POLLIN;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* This is the loop to wait for incoming events *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;Listening for events.n&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">while (1) { <br \/> poll_num = poll(fds, nfds, \u22121); <br \/> if (poll_num == \u22121) { <br \/> if (errno == EINTR) \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Interrupted by a signal bodies\/ usr\/ <br \/> continue; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Restart poll() *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">perror(&#8220;poll&#8221;); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Unexpected error bodies\/ usr\/ <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (poll_num > 0) { <br \/> if (fds[0].revents &#038; POLLIN) {<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Console input is available: empty stdin and quit *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">while (read(STDIN_FILENO, &#038;buf, 1) > 0 &#038;&#038; buf != &#8216;n&#8217;) <br \/> continue; <br \/> break; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (fds[1].revents &#038; POLLIN) {<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Fanotify events are available *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">handle_events(fd); <br \/> } <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;Listening for events stopped.n&#8221;); <br \/> exit(EXIT_SUCCESS); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Example program: fanotify_fid.c<\/b> <br \/> The second program is an example of fanotify being used with a group that identifies objects by file handles. The program marks the filesystem object that is passed as a command-line argument and waits until an event of type <b>FAN_CREATE<\/b> has occurred. The event mask indicates which type of filesystem object\u2014either a file or a directory\u2014was created. Once all events have been read from the buffer and processed accordingly, the program simply terminates.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following shell sessions show two different invocations of this program, with different actions performed on a watched object.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The first session shows a mark being placed on <i>\/home\/user<\/i>. This is followed by the creation of a regular file, <i>\/home\/user\/testfile.txt<\/i>. This results in a <b>FAN_CREATE<\/b> event being generated and reported against the file\u2019s parent watched directory object and with the created file name. Program execution ends once all events captured within the buffer have been processed.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"># <b>.\/fanotify_fid \/home\/user<\/b> <br \/> Listening for events. <br \/> FAN_CREATE (file created): <br \/> Directory \/home\/user has been modified. <br \/> Entry &#8216;testfile.txt&#8217; is not a subdirectory. <br \/> All events processed successfully. Program exiting.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>touch \/home\/user\/testfile.txt<\/b> # In another terminal<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The second session shows a mark being placed on <i>\/home\/user<\/i>. This is followed by the creation of a directory, <i>\/home\/user\/testdir<\/i>. This specific action results in a <b>FAN_CREATE<\/b> event being generated and is reported with the <b>FAN_ONDIR<\/b> flag set and with the created directory name.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"># <b>.\/fanotify_fid \/home\/user<\/b> <br \/> Listening for events. <br \/> FAN_CREATE | FAN_ONDIR (subdirectory created): <br \/> Directory \/home\/user has been modified. <br \/> Entry &#8216;testdir&#8217; is a subdirectory. <br \/> All events processed successfully. Program exiting.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>mkdir \u2212p \/home\/user\/testdir<\/b> # In another terminal<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Program source: fanotify_fid.c<\/b> <br \/> #define _GNU_SOURCE <br \/> #include <errno.h> <br \/> #include <fcntl.h> <br \/> #include <limits.h> #include <stdio.h> <br \/> #include <stdlib.h> <br \/> #include <sys\/types.h> <br \/> #include <sys\/stat.h> <br \/> #include <sys\/fanotify.h> <br \/> #include <unistd.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#define BUF_SIZE 256<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(int argc, char **argv) <br \/> { <br \/> int fd, ret, event_fd, mount_fd; <br \/> ssize_t len, path_len; <br \/> char path[PATH_MAX]; <br \/> char procfd_path[PATH_MAX]; <br \/> char events_buf[BUF_SIZE]; <br \/> struct file_handle *file_handle; <br \/> struct fanotify_event_metadata *metadata; <br \/> struct fanotify_event_info_fid *fid; <br \/> const char *file_name; <br \/> struct stat sb;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (argc != 2) { <br \/> fprintf(stderr, &#8220;Invalid number of command line arguments.n&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">mount_fd = open(argv[1], O_DIRECTORY | O_RDONLY); <br \/> if (mount_fd == \u22121) { <br \/> perror(argv[1]); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Create an fanotify file descriptor with FAN_REPORT_DFID_NAME as <br \/> a flag so that program can receive fid events with directory <br \/> entry name. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">fd = fanotify_init(FAN_CLASS_NOTIF | FAN_REPORT_DFID_NAME, 0); <br \/> if (fd == \u22121) { <br \/> perror(&#8220;fanotify_init&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Place a mark on the filesystem object supplied in argv[1]. *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ret = fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_ONLYDIR, <br \/> FAN_CREATE | FAN_ONDIR, <br \/> AT_FDCWD, argv[1]); <br \/> if (ret == \u22121) { <br \/> perror(&#8220;fanotify_mark&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;Listening for events.n&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Read events from the event queue into a buffer *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">len = read(fd, events_buf, sizeof(events_buf)); <br \/> if (len == \u22121 &#038;&#038; errno != EAGAIN) { <br \/> perror(&#8220;read&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Process all events within the buffer *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for (metadata = (struct fanotify_event_metadata *) events_buf; <br \/> FAN_EVENT_OK(metadata, len); <br \/> metadata = FAN_EVENT_NEXT(metadata, len)) { <br \/> fid = (struct fanotify_event_info_fid *) (metadata + 1); <br \/> file_handle = (struct file_handle *) fid\u2212>handle;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Ensure that the event info is of the correct type *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (fid\u2212>hdr.info_type == FAN_EVENT_INFO_TYPE_FID || <br \/> fid\u2212>hdr.info_type == FAN_EVENT_INFO_TYPE_DFID) { <br \/> file_name = NULL; <br \/> } else if (fid\u2212>hdr.info_type == FAN_EVENT_INFO_TYPE_DFID_NAME) { <br \/> file_name = file_handle->f_handle + <br \/> file_handle->handle_bytes; <br \/> } else { <br \/> fprintf(stderr, &#8220;Received unexpected event info type.n&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (metadata\u2212>mask == FAN_CREATE) <br \/> printf(&#8220;FAN_CREATE (file created):n&#8221;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (metadata\u2212>mask == (FAN_CREATE | FAN_ONDIR)) <br \/> printf(&#8220;FAN_CREATE | FAN_ONDIR (subdirectory created):n&#8221;);<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"92%\">\n<p>\/* metadata\u2212>fd is set to FAN_NOFD when the group identifies<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"92%\">\n<p>objects by file handles. To obtain a file descriptor for<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"92%\">\n<p>the file object corresponding to an event you can use the<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"92%\">\n<p>struct file_handle that&#8217;s provided within the<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"92%\">\n<p>fanotify_event_info_fid in conjunction with the<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"92%\">\n<p>open_by_handle_at(2) system call. A check for ESTALE is<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"92%\">\n<p>done to accommodate for the situation where the file handle<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"92%\">\n<p>for the object was deleted prior to this system call. *\/<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">event_fd = open_by_handle_at(mount_fd, file_handle, O_RDONLY); <br \/> if (event_fd == \u22121) { <br \/> if (errno == ESTALE) { <br \/> printf(&#8220;File handle is no longer valid. &#8221; <br \/> &#8220;File has been deletedn&#8221;); <br \/> continue; <br \/> } else { <br \/> perror(&#8220;open_by_handle_at&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">snprintf(procfd_path, sizeof(procfd_path), &#8220;\/proc\/self\/fd\/%d&#8221;, <br \/> event_fd);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Retrieve and print the path of the modified dentry *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">path_len = readlink(procfd_path, path, sizeof(path) \u2212 1); <br \/> if (path_len == \u22121) { <br \/> perror(&#8220;readlink&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">path[path_len] = &#8216;\u0000&#8217;; <br \/> printf(&#8220;tDirectory &#8216;%s&#8217; has been modified.n&#8221;, path);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (file_name) { <br \/> ret = fstatat(event_fd, file_name, &#038;sb, 0); <br \/> if (ret == \u22121) { <br \/> if (errno != ENOENT) { <br \/> perror(&#8220;fstatat&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> } <br \/> printf(&#8220;tEntry &#8216;%s&#8217; does not exist.n&#8221;, file_name); <br \/> } else if ((sb.st_mode &#038; S_IFMT) == S_IFDIR) { <br \/> printf(&#8220;tEntry &#8216;%s&#8217; is a subdirectory.n&#8221;, file_name); <br \/> } else { <br \/> printf(&#8220;tEntry &#8216;%s&#8217; is not a subdirectory.n&#8221;, <br \/> file_name); <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Close associated file descriptor for this event *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">close(event_fd); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;All events processed successfully. Program exiting.n&#8221;); <br \/> exit(EXIT_SUCCESS); <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>fanotify_init<\/b>(2), <b>fanotify_mark<\/b>(2), <b>inotify<\/b>(7)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 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 https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  fanotify \u2212 monitoring filesystem events <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,1051,972],"class_list":["post-3961","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-fanotify","tag-man7"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=3961"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3961\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=3961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=3961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=3961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}