---
name: "fanotify_init"
section: "2"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/fanotify_init"
---

# fanotify\_init(2)

create and initialize fanotify group

## NAME

fanotify\_init - create and initialize fanotify group

## LIBRARY

Standard C library (*libc*, *-lc*)

## SYNOPSIS

```
#include <fcntl.h>            /* Definition of O_* constants */
#include <sys/fanotify.h>
```

**int fanotify\_init(unsigned int** *flags***, unsigned int** *event\_f\_flags***);**

## DESCRIPTION

For an overview of the fanotify API, see [**fanotify**(7)](/fanotify/7.md).

**fanotify\_init**() initializes a new fanotify group and returns a file descriptor for the event queue associated with the group.

The file descriptor is used in calls to [**fanotify\_mark**(2)](/fanotify_mark/2.md) to specify the files, directories, mounts, or filesystems for which fanotify events shall be created. These events are received by reading from the file descriptor. Some events are only informative, indicating that a file has been 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.

Multiple programs may be using the fanotify interface at the same time to monitor the same files.

The number of fanotify groups per user is limited. See [**fanotify**(7)](/fanotify/7.md) for details about this limit.

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.

If multiple listeners for permission events exist, the notification class is used to establish the sequence in which the listeners receive the events.

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 the **CAP\_SYS\_ADMIN** capability.

- **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 the **CAP\_SYS\_ADMIN** capability.

- **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.

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.

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 the **O\_CLOEXEC** flag in [**open**(2)](/open/2.md).

- **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)](/read/2.md) fails with the error **EAGAIN**.

- **FAN\_UNLIMITED\_QUEUE**

  Remove the limit on the number of events in the event queue. See [**fanotify**(7)](/fanotify/7.md) for details about this limit. Use of this flag requires the **CAP\_SYS\_ADMIN** capability.

- **FAN\_UNLIMITED\_MARKS**

  Remove the limit on the number of fanotify marks per user. See [**fanotify**(7)](/fanotify/7.md) for details about this limit. Use of this flag requires the **CAP\_SYS\_ADMIN** capability.

- **FAN\_REPORT\_TID** (since Linux 4.20)

  Report thread ID (TID) instead of process ID (PID) in the *pid* field of the *struct fanotify\_event\_metadata* supplied to [**read**(2)](/read/2.md) (see [**fanotify**(7)](/fanotify/7.md)). Use of this flag requires the **CAP\_SYS\_ADMIN** capability.

- **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 the **FAN\_AUDIT** flag for an audit log record to be generated. Use of this flag requires the **CAP\_AUDIT\_WRITE** capability.

- **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 type **FAN\_EVENT\_INFO\_TYPE\_FID** encapsulates 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 events **FAN\_CREATE**, **FAN\_DELETE**, **FAN\_MOVE**, and **FAN\_RENAME**, or in events such as **FAN\_ATTRIB**, **FAN\_DELETE\_SELF**, and **FAN\_MOVE\_SELF**. All the events above require an fanotify group that identifies filesystem objects by file handles. Note that without the flag **FAN\_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 of **FAN\_CLASS\_CONTENT** or **FAN\_CLASS\_PRE\_CONTENT** is not permitted with this flag and will result in the error **EINVAL**. See [**fanotify**(7)](/fanotify/7.md) for additional details.

- **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 type **FAN\_EVENT\_INFO\_TYPE\_DFID** encapsulates 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 flag **FAN\_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 the **FAN\_REPORT\_FID** flag, 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 the **FAN\_REPORT\_FID** flag, no event will be reported. See [**fanotify**(7)](/fanotify/7.md) for additional details.

- **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 flag **FAN\_REPORT\_DIR\_FID**. Providing this flag value without **FAN\_REPORT\_DIR\_FID** will result in the error **EINVAL**. This flag may be combined with the flag **FAN\_REPORT\_FID**. An additional record of type **FAN\_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 type **FAN\_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 events **FAN\_CREATE**, **FAN\_DELETE**, and **FAN\_MOVE**, the reported name is that of the created/deleted/moved directory entry. The event **FAN\_RENAME** may contain two information records. One of type **FAN\_EVENT\_INFO\_TYPE\_OLD\_DFID\_NAME** identifying the old directory entry, and another of type **FAN\_EVENT\_INFO\_TYPE\_NEW\_DFID\_NAME** identifying 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 to [**open\_by\_handle\_at**(2)](/open_by_handle_at/2.md) to get an open directory file descriptor and that file descriptor along with the reported name can be used to call [**fstatat**(2)](/fstatat/2.md). The same rule that applies to record type **FAN\_EVENT\_INFO\_TYPE\_DFID** also applies to record type **FAN\_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. See [**fanotify**(7)](/fanotify/7.md) for additional details.

- **FAN\_REPORT\_DFID\_NAME**

  This is a synonym for (**FAN\_REPORT\_DIR\_FID**\|**FAN\_REPORT\_NAME**).

- **FAN\_REPORT\_TARGET\_FID** (since Linux 5.17, 5.15.154, and 5.10.220)

  Events for fanotify groups initialized with this flag will contain additional information about the child correlated with directory entry modification events. This flag must be provided in conjunction with the flags **FAN\_REPORT\_FID**, **FAN\_REPORT\_DIR\_FID** and **FAN\_REPORT\_NAME**. or else the error **EINVAL** will be returned. For the directory entry modification events **FAN\_CREATE**, **FAN\_DELETE**, **FAN\_MOVE**, and **FAN\_RENAME**, an additional record of type **FAN\_EVENT\_INFO\_TYPE\_FID**, is reported in addition to the information records of type **FAN\_EVENT\_INFO\_TYPE\_DFID**, **FAN\_EVENT\_INFO\_TYPE\_DFID\_NAME**, **FAN\_EVENT\_INFO\_TYPE\_OLD\_DFID\_NAME**, and **FAN\_EVENT\_INFO\_TYPE\_NEW\_DFID\_NAME**. The additional record includes a file handle that identifies the filesystem child object that the directory entry is referring to.

- **FAN\_REPORT\_DFID\_NAME\_TARGET**

  This is a synonym for (**FAN\_REPORT\_DFID\_NAME**\|**FAN\_REPORT\_FID**\|**FAN\_REPORT\_TARGET\_FID**).

- **FAN\_REPORT\_MNT** (since Linux 6.14)

  This value allows the receipt of events which contain additional information about the underlying mount correlated to an event. An additional record of type **FAN\_EVENT\_INFO\_TYPE\_MNT** encapsulates the information about the mount and is included alongside the generic event metadata structure. The use of **FAN\_CLASS\_CONTENT**, **FAN\_CLASS\_PRE\_CONTENT**, or any of the **FAN\_REPORT\_DFID\_NAME\_TARGET** flags along with this flag is not permitted and will result in the error **EINVAL**. See [**fanotify**(7)](/fanotify/7.md) for additional details.

- **FAN\_REPORT\_PIDFD** (since Linux 5.15 and 5.10.220)

  Events for fanotify groups initialized with this flag will contain an additional information record alongside the generic *fanotify\_event\_metadata* structure. This information record will be of type **FAN\_EVENT\_INFO\_TYPE\_PIDFD** and will contain a pidfd for the process that was responsible for generating an event. A pidfd returned in this information record object is no different to the pidfd that is returned when calling [**pidfd\_open**(2)](/pidfd_open/2.md). Usage of this information record are for applications that may be interested in reliably determining whether the process responsible for generating an event has been recycled or terminated. The use of the **FAN\_REPORT\_TID** flag along with **FAN\_REPORT\_PIDFD** is currently not supported and attempting to do so will result in the error **EINVAL** being returned. This limitation is currently imposed by the pidfd API as it currently only supports the creation of pidfds for thread-group leaders. 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 details on information records, see [**fanotify**(7)](/fanotify/7.md).

- **FAN\_REPORT\_FD\_ERROR** (since Linux 6.13 and 6.12.4 and 6.6.66)

  Events for fanotify groups initialized with this flag may contain an error code that explains the reason for failure to open a file descriptor. The *.fd* member of the *fanotify\_event\_metadata* structure normally contains an open file descriptor associated with the object of the event or FAN\_NOFD in case a file descriptor could not be opened. For a group initialized with this flag, instead of FAN\_NOFD, the *.fd* member of the *fanotify\_event\_metadata* structure will contain a negative error value. When the group is also initialized with flag **FAN\_REPORT\_PIDFD**, in case a process file descriptor could not be opened, the *.pidfd* member of the *fanotify\_event\_info\_pidfd* structure will also contain a negative error value. For more details, see [**fanotify**(7)](/fanotify/7.md).

The *event\_f\_flags* argument defines the file status flags that will be set on the open file descriptions that are created for fanotify events. For details of these flags, see the description of the *flags* values in [**open**(2)](/open/2.md). *event\_f\_flags* includes a multi-bit field for the access mode. This 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 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 an **EOVERFLOW** error when trying to open a large file which is monitored by an fanotify group on a 32-bit system.

- **O\_CLOEXEC** (since Linux 3.18)

  Enable the close-on-exec flag for the file descriptor. See the description of the **O\_CLOEXEC** flag in [**open**(2)](/open/2.md) for 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).

## 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 in *flags* or *event\_f\_flags*. **FAN\_ALL\_INIT\_FLAGS** (deprecated since Linux 4.20) defines all allowable bits for *flags*.

- **EMFILE**

  The number of fanotify groups for this user exceeds the limit. See [**fanotify**(7)](/fanotify/7.md) for details about this limit.

- **EMFILE**

  The per-process limit on the number of open file descriptors has been reached.

- **ENOMEM**

  The allocation of memory for the notification group failed.

- **ENOSYS**

  This kernel does not implement **fanotify\_init**(). The fanotify API is available only if the kernel was configured with **CONFIG\_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\_CONTENT** or **FAN\_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\_FID** flag.

  - •

    The user is limited to only mark inodes. The ability to mark a mount or filesystem via **fanotify\_mark**() through the use of **FAN\_MARK\_MOUNT** or **FAN\_MARK\_FILESYSTEM** is 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.

## HISTORY

Linux 2.6.37.

## BUGS

The following bug was present before Linux 3.18:

- •

  The **O\_CLOEXEC** is ignored when passed in *event\_f\_flags*.

The following bug was present before Linux 3.14:

- •

  The *event\_f\_flags* argument is not checked for invalid flags. Flags that are intended only for internal use, such as **FMODE\_EXEC**, can be set, and will consequently be set for the file descriptors returned when reading from the fanotify file descriptor.

## SEE ALSO

[**fanotify\_mark**(2)](/fanotify_mark/2.md), [**fanotify**(7)](/fanotify/7.md)
