fanotify_init(2)
πΈ NAME πΈ
okay bestie, meet fanotify_init β the syscall from section 2 that creates and initializes a fanotify group. it's giving "I watch files for a living" energy. π
π LIBRARY π
Standard C library (libc, -lc). no cap.
π» SYNOPSIS π»
#include <fcntl.h> /* Definition of O_* constants */
#include <sys/fanotify.h>
int fanotify_init(unsigned int flags, unsigned int event_f_flags);
β¨ DESCRIPTION β¨
First things first, bestie: for the whole fanotify API tea, go read fanotify(7). π«
So fanotify_init() initializes a brand new fanotify group and returns a file descriptor for the event queue that's associated with that group. main character energy fr fr. π
That file descriptor is used in calls to fanotify_mark(2) to specify the files, directories, mounts, or filesystems for which fanotify events shall be created. Those events get received by reading from the file descriptor. Some events are only informative β like "hey, this file got accessed." Other events can be used to determine whether another application is permitted to access a file or directory. Permission to access filesystem objects is granted by writing to the file descriptor. periodt. βοΈ
Multiple programs may be using the fanotify interface at the same time to monitor the same files. sharing is caring, lowkey. π€
The number of fanotify groups per user is limited. See fanotify(7) for details about this limit. don't test it, bestie. π«
The flags argument contains a multi-bit field defining the notification class of the listening application and further single bit fields specifying the behavior of the file descriptor. so it's like a little combo platter of vibes. π±
If multiple listeners for permission events exist, the notification class is used to establish the sequence in which the listeners receive the events. order matters, iykyk. π’
Only one of the following notification classes may be specified in flags:
FAN_CLASS_PRE_CONTENTβ This value allows the receipt of events notifying that a file has been accessed and events for permission decisions if a file may be accessed. It is intended for event listeners that may need to write data to files before their final data can be accessed. This notification class might be used by hierarchical storage managers, for example. Use of this flag requires theCAP_SYS_ADMINcapability. so if you're not admin-capable, this is not your moment. π ββοΈFAN_CLASS_CONTENTβ This value allows the receipt of events notifying that a file has been accessed and events for permission decisions if a file may be accessed. It is intended for event listeners that need to access files when they already contain their final content. This notification class might be used by malware detection programs, for example. Use of this flag requires theCAP_SYS_ADMINcapability. malware detection is giving superhero energy. π¦ΈββοΈFAN_CLASS_NOTIFβ This is the default value. It does not need to be specified. This value only allows the receipt of events notifying that a file has been accessed. Permission decisions before the file is accessed are not possible. so it's just the chill notif vibe. π
Listeners with different notification classes will receive events in the order FAN_CLASS_PRE_CONTENT, FAN_CLASS_CONTENT, FAN_CLASS_NOTIF. The order of notification for listeners in the same notification class is undefined. so don't @ me about fairness. π€·ββοΈ
The following bits can additionally be set in flags:
FAN_CLOEXECβ Set the close-on-exec flag (FD_CLOEXEC) on the new file descriptor. See the description of theO_CLOEXECflag inopen(2). classic fd hygiene, bestie. π§ΌFAN_NONBLOCKβ Enable the nonblocking flag (O_NONBLOCK) for the file descriptor. Reading from the file descriptor will not block. Instead, if no data is available,read(2)fails with the errorEAGAIN. so it's like, "I'm not waiting around." πFAN_UNLIMITED_QUEUEβ Remove the limit on the number of events in the event queue. Seefanotify(7)for details about this limit. Use of this flag requires theCAP_SYS_ADMINcapability. queue go brrr. πFAN_UNLIMITED_MARKSβ Remove the limit on the number of fanotify marks per user. Seefanotify(7)for details about this limit. Use of this flag requires theCAP_SYS_ADMINcapability. marks for days. β¨FAN_REPORT_TID(since Linux 4.20) β Report thread ID (TID) instead of process ID (PID) in thepidfield of thestruct fanotify_event_metadatasupplied toread(2)(seefanotify(7)). Use of this flag requires theCAP_SYS_ADMINcapability. threads gotta thread. π§΅FAN_ENABLE_AUDIT(since Linux 4.15) β Enable generation of audit log records about access mediation performed by permission events. The permission event response has to be marked with theFAN_AUDITflag for an audit log record to be generated. Use of this flag requires theCAP_AUDIT_WRITEcapability. audit tea, spilled for the logs. π΅FAN_REPORT_FID(since Linux 5.1) β This value allows the receipt of events which contain additional information about the underlying filesystem object correlated to an event. An additional record of typeFAN_EVENT_INFO_TYPE_FIDencapsulates the information about the object and is included alongside the generic event metadata structure. The file descriptor that is used to represent the object correlated to an event is instead substituted with a file handle. It is intended for applications that may find the use of a file handle to identify an object more suitable than a file descriptor. Additionally, it may be used for applications monitoring a directory or a filesystem that are interested in the directory entry modification eventsFAN_CREATE,FAN_DELETE,FAN_MOVE, andFAN_RENAME, or in events such asFAN_ATTRIB,FAN_DELETE_SELF, andFAN_MOVE_SELF. All the events above require an fanotify group that identifies filesystem objects by file handles. Note that without the flagFAN_REPORT_TARGET_FID, for the directory entry modification events, there is an information record that identifies the modified directory and not the created/deleted/moved child object. The use ofFAN_CLASS_CONTENTorFAN_CLASS_PRE_CONTENTis not permitted with this flag and will result in the errorEINVAL. Seefanotify(7)for additional details. FID is the vibes. πFAN_REPORT_DIR_FID(since Linux 5.9) β Events for fanotify groups initialized with this flag will contain (see exceptions below) additional information about a directory object correlated to an event. An additional record of typeFAN_EVENT_INFO_TYPE_DFIDencapsulates the information about the directory object and is included alongside the generic event metadata structure. For events that occur on a non-directory object, the additional structure includes a file handle that identifies the parent directory filesystem object. Note that there is no guarantee that the directory filesystem object will be found at the location described by the file handle information at the time the event is received. When combined with the flagFAN_REPORT_FID, two records may be reported with events that occur on a non-directory object, one to identify the non-directory object itself and one to identify the parent directory object. Note that in some cases, a filesystem object does not have a parent, for example, when an event occurs on an unlinked but open file. In that case, with theFAN_REPORT_FIDflag, the event will be reported with only one record to identify the non-directory object itself, because there is no directory associated with the event. Without theFAN_REPORT_FIDflag, no event will be reported. Seefanotify(7)for additional details. directory handle drama, but make it informative. πFAN_REPORT_NAME(since Linux 5.9) β Events for fanotify groups initialized with this flag will contain additional information about the name of the directory entry correlated to an event. This flag must be provided in conjunction with the flagFAN_REPORT_DIR_FID. Providing this flag value withoutFAN_REPORT_DIR_FIDwill result in the errorEINVAL. This flag may be combined with the flagFAN_REPORT_FID. An additional record of typeFAN_EVENT_INFO_TYPE_DFID_NAME, which encapsulates the information about the directory entry, is included alongside the generic event metadata structure and substitutes the additional information record of typeFAN_EVENT_INFO_TYPE_DFID. The additional record includes a file handle that identifies a directory filesystem object followed by a name that identifies an entry in that directory. For the directory entry modification eventsFAN_CREATE,FAN_DELETE, andFAN_MOVE, the reported name is that of the created/deleted/moved directory entry. The eventFAN_RENAMEmay contain two information records. One of typeFAN_EVENT_INFO_TYPE_OLD_DFID_NAMEidentifying the old directory entry, and another of typeFAN_EVENT_INFO_TYPE_NEW_DFID_NAMEidentifying the new directory entry. For other events that occur on a directory object, the reported file handle is that of the directory object itself and the reported name is'.'. For other events that occur on a non-directory object, the reported file handle is that of the parent directory object and the reported name is the name of a directory entry where the object was located at the time of the event. The rationale behind this logic is that the reported directory file handle can be passed toopen_by_handle_at(2)to get an open directory file descriptor and that file descriptor along with the reported name can be used to callfstatat(2). The same rule that applies to record typeFAN_EVENT_INFO_TYPE_DFIDalso applies to record typeFAN_EVENT_INFO_TYPE_DFID_NAME: if a non-directory object has no parent, either the event will not be reported or it will be reported without the directory entry information. Note that there is no guarantee that the filesystem object will be found at the location described by the directory entry information at the time the event is received. Seefanotify(7)for additional details. name drop done right. πFAN_REPORT_DFID_NAMEβ This is a synonym for (FAN_REPORT_DIR_FID|FAN_REPORT_NAME). short and sweet, iconic. πFAN_REPORT_TARGET_FID(since Linux 5.17, 5.15.154, and 5.10.220) β bestie, if you init your fanotify group with this flag, the events are gonna come with extra info about the child that's correlated with directory-entry-modification events. β¨ But you cannot run this solo: it must be provided together withFAN_REPORT_FID,FAN_REPORT_DIR_FIDandFAN_REPORT_NAMEβ or else the errorEINVALwill be returned, no cap. For the directory entry modification eventsFAN_CREATE,FAN_DELETE,FAN_MOVE, andFAN_RENAME, an additional record of typeFAN_EVENT_INFO_TYPE_FIDgets reported on top of the info records of typeFAN_EVENT_INFO_TYPE_DFID,FAN_EVENT_INFO_TYPE_DFID_NAME,FAN_EVENT_INFO_TYPE_OLD_DFID_NAME, andFAN_EVENT_INFO_TYPE_NEW_DFID_NAME. That extra record carries a file handle that identifies the filesystem child object the directory entry is pointing at. π«ΆFAN_REPORT_DFID_NAME_TARGETβ this one is literally just a synonym for (FAN_REPORT_DFID_NAME|FAN_REPORT_FID|FAN_REPORT_TARGET_FID). Periodt. πFAN_REPORT_MNT(since Linux 6.14) β this value lets you receive events that carry extra info about the underlying mount correlated to an event. π An extra record of typeFAN_EVENT_INFO_TYPE_MNTwraps up the mount info and comes along for the ride right alongside the generic event metadata structure. But heads up, because usingFAN_CLASS_CONTENT,FAN_CLASS_PRE_CONTENT, or any of theFAN_REPORT_DFID_NAME_TARGETflags together with this flag is not permitted and will result in the errorEINVALbeing returned. π« Seefanotify(7)for the extra deets.FAN_REPORT_PIDFD(since Linux 5.15 and 5.10.220) β groups initialized with this flag get an additional info record sitting pretty alongside the genericfanotify_event_metadatastructure. That record is of typeFAN_EVENT_INFO_TYPE_PIDFDand it contains a pidfd for the process that was responsible for generating the event. π And a pidfd returned in this info record object is no different from the pidfd you get when you callpidfd_open(2)β same energy. This info record's whole thing is for apps that wanna reliably figure out whether the process responsible for generating an event has been recycled or terminated. π Now the downside: using theFAN_REPORT_TIDflag together withFAN_REPORT_PIDFDis currently not supported, and attempting it will result in the errorEINVALbeing returned. This limitation is currently imposed by the pidfd API, since it only supports the creation of pidfds for thread-group leaders rn. Creating pidfds for non-thread-group leaders may be supported at some point in the future, so this restriction may eventually be lifted. π For more deets on info records, seefanotify(7).FAN_REPORT_FD_ERROR(since Linux 6.13 and 6.12.4 and 6.6.66) β events for groups initialized with this flag may contain an error code that explains the reason for the failure to open a file descriptor. The.fdmember of thefanotify_event_metadatastructure normally contains an open file descriptor associated with the object of the event, orFAN_NOFDin case a file descriptor could not be opened. But for a group initialized with this flag, instead ofFAN_NOFD, the.fdmember of thefanotify_event_metadatastructure will contain a negative error value. And when the group is also initialized with the flagFAN_REPORT_PIDFD, in case a process file descriptor could not be opened, the.pidfdmember of thefanotify_event_info_pidfdstructure will also contain a negative error value. For more details, seefanotify(7). π
Okay so now we're moving on to event_f_flags. π§ The event_f_flags argument defines the file status flags that get set on the open file descriptions that are created for fanotify events. For the details of these flags, go peek at the flag values description in open(2). And event_f_flags includes a multi-bit field for the access mode. π That field can take the following values:
O_RDONLYβ this value allows only read access. πO_WRONLYβ this value allows only write access. βοΈO_RDWRβ this value allows read and write access. π«
Additional bits can also be set in event_f_flags. The most useful values are:
O_LARGEFILEβ enable support for files exceeding 2 GB. π Failing to set this flag will result in anEOVERFLOWerror when trying to open a large file which is monitored by an fanotify group on a 32-bit system. Not-a-vibe, so set it.O_CLOEXEC(since Linux 3.18) β enable the close-on-exec flag for the file descriptor. See the description of theO_CLOEXECflag inopen(2)for the reasons why this may be useful. π
The following are also allowable: O_APPEND, O_DSYNC, O_NOATIME, O_NONBLOCK, and O_SYNC. β¨ Specifying any other flag in event_f_flags yields the error EINVAL (but see BUGS, bestie, there's lore there).
π RETURN VALUE π
On success, fanotify_init() returns a new file descriptor. π₯³ On error, -1 is returned, and errno is set to indicate the error. π
π ERRORS π
EINVALβ an invalid value was passed inflagsorevent_f_flags.FAN_ALL_INIT_FLAGS(deprecated since Linux 4.20) defines all allowable bits forflags. πEMFILEβ the number of fanotify groups for this user exceeds the limit. Seefanotify(7)for details about this limit. π¬EMFILEβ the per-process limit on the number of open file descriptors has been reached. (Yes, it's listed twice, that's just how it is, we don't make the rules. π)ENOMEMβ the allocation of memory for the notification group failed. π«ENOSYSβ this kernel does not implementfanotify_init(). The fanotify API is available only if the kernel was configured withCONFIG_FANOTIFY. π§EPERMβ the operation is not permitted because the caller lacks a required capability. π«
πΈ VERSIONS πΈ
Prior to Linux 5.13 (and 5.10.220), calling fanotify_init() required the CAP_SYS_ADMIN capability. Since Linux 5.13 (and 5.10.220), users may call fanotify_init() without the CAP_SYS_ADMIN capability to create and initialize an fanotify group with limited functionality. π
The limitations imposed on an event listener created by a user without the CAP_SYS_ADMIN capability are as follows:
- The user cannot request for an unlimited event queue by using
FAN_UNLIMITED_QUEUE. π - The user cannot request for an unlimited number of marks by using
FAN_UNLIMITED_MARKS. π - The user cannot request to use either notification classes
FAN_CLASS_CONTENTorFAN_CLASS_PRE_CONTENT. This means that user cannot request permission events. π - The user is required to create a group that identifies filesystem objects by file handles, for example, by providing the
FAN_REPORT_FIDflag. πͺͺ - The user is limited to only mark inodes. The ability to mark a mount or filesystem via
fanotify_mark()through the use ofFAN_MARK_MOUNTorFAN_MARK_FILESYSTEMis not permitted. π - The event object in the event queue is limited in terms of the information that is made available to the unprivileged user. A user will also not receive the pid that generated the event, unless the listening process itself generated the event. π΅οΈ
β STANDARDS β
Linux. π§ That's it, that's the standard.
π¦ HISTORY π¦
Linux 2.6.37. ποΈ Ancient. Iconic. The origin story.
π BUGS π
The following bug was present before Linux 3.18:
- The
O_CLOEXECis ignored when passed inevent_f_flags. π€
The following bug was present before Linux 3.14:
- The
event_f_flagsargument is not checked for invalid flags. Flags that are intended only for internal use, such asFMODE_EXEC, can be set, and will consequently be set for the file descriptors returned when reading from the fanotify file descriptor. π¨
Okay bestie real talk β that second one is giving "leaked internals" energy. FMODE_EXEC was never meant for you to set, and on old kernels nothing stops you. Keep your kernels fresh, iykyk. π«Ά
π₯ SEE ALSO π₯
And that's the whole page, bestie!! π You made it to the end, main character energy fr fr. π This one came out of Linux man-pages 6.20, dated 2025-05-17 β go forth and fanotify responsibly, and remember: the vibe changes, the facts never do. β¨π ππ¦
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms