1
2fanotify_init(2)              System Calls Manual             fanotify_init(2)
3
4
5

NAME

7       fanotify_init - create and initialize fanotify group
8

LIBRARY

10       Standard C library (libc, -lc)
11

SYNOPSIS

13       #include <fcntl.h>            /* Definition of O_* constants */
14       #include <sys/fanotify.h>
15
16       int fanotify_init(unsigned int flags, unsigned int event_f_flags);
17

DESCRIPTION

19       For an overview of the fanotify API, see fanotify(7).
20
21       fanotify_init() initializes a new fanotify group and returns a file de‐
22       scriptor for the event queue associated with the group.
23
24       The file descriptor is used in calls to fanotify_mark(2) to specify the
25       files,  directories,  mounts,  or filesystems for which fanotify events
26       shall be created.  These events are received by reading from  the  file
27       descriptor.   Some  events are only informative, indicating that a file
28       has been accessed.  Other events can be used to determine  whether  an‐
29       other  application is permitted to access a file or directory.  Permis‐
30       sion to access filesystem objects is granted by writing to the file de‐
31       scriptor.
32
33       Multiple  programs may be using the fanotify interface at the same time
34       to monitor the same files.
35
36       The number of fanotify groups per user is limited.  See fanotify(7) for
37       details about this limit.
38
39       The flags argument contains a multi-bit field defining the notification
40       class of the listening application and further single bit fields speci‐
41       fying the behavior of the file descriptor.
42
43       If  multiple  listeners  for  permission events exist, the notification
44       class is used to establish the sequence in which the listeners  receive
45       the events.
46
47       Only  one  of  the  following  notification classes may be specified in
48       flags:
49
50       FAN_CLASS_PRE_CONTENT
51              This value allows the receipt of events notifying  that  a  file
52              has  been accessed and events for permission decisions if a file
53              may be accessed.  It is intended for event listeners  that  need
54              to  access files before they contain their final data.  This no‐
55              tification class might be used by hierarchical storage managers,
56              for  example.  Use of this flag requires the CAP_SYS_ADMIN capa‐
57              bility.
58
59       FAN_CLASS_CONTENT
60              This value allows the receipt of events notifying  that  a  file
61              has  been accessed and events for permission decisions if a file
62              may be accessed.  It is intended for event listeners  that  need
63              to  access  files when they already contain their final content.
64              This notification class might be used by malware detection  pro‐
65              grams, for example.  Use of this flag requires the CAP_SYS_ADMIN
66              capability.
67
68       FAN_CLASS_NOTIF
69              This is the default value.  It does not need  to  be  specified.
70              This  value  only  allows the receipt of events notifying that a
71              file has been accessed.  Permission decisions before the file is
72              accessed are not possible.
73
74       Listeners  with  different  notification classes will receive events in
75       the order  FAN_CLASS_PRE_CONTENT,  FAN_CLASS_CONTENT,  FAN_CLASS_NOTIF.
76       The  order of notification for listeners in the same notification class
77       is undefined.
78
79       The following bits can additionally be set in flags:
80
81       FAN_CLOEXEC
82              Set the close-on-exec flag (FD_CLOEXEC) on the new file descrip‐
83              tor.  See the description of the O_CLOEXEC flag in open(2).
84
85       FAN_NONBLOCK
86              Enable  the  nonblocking flag (O_NONBLOCK) for the file descrip‐
87              tor.  Reading from the file descriptor will not block.  Instead,
88              if no data is available, read(2) fails with the error EAGAIN.
89
90       FAN_UNLIMITED_QUEUE
91              Remove  the  limit  on  the number of events in the event queue.
92              See fanotify(7) for details about this limit.  Use of this  flag
93              requires the CAP_SYS_ADMIN capability.
94
95       FAN_UNLIMITED_MARKS
96              Remove  the limit on the number of fanotify marks per user.  See
97              fanotify(7) for details about this limit.  Use of this flag  re‐
98              quires the CAP_SYS_ADMIN capability.
99
100       FAN_REPORT_TID (since Linux 4.20)
101              Report  thread  ID  (TID) instead of process ID (PID) in the pid
102              field of the struct fanotify_event_metadata supplied to  read(2)
103              (see  fanotify(7)).  Use of this flag requires the CAP_SYS_ADMIN
104              capability.
105
106       FAN_ENABLE_AUDIT (since Linux 4.15)
107              Enable generation of audit log records  about  access  mediation
108              performed  by  permission events.  The permission event response
109              has to be marked with the FAN_AUDIT flag for an audit log record
110              to  be generated.  Use of this flag requires the CAP_AUDIT_WRITE
111              capability.
112
113       FAN_REPORT_FID (since Linux 5.1)
114              This value allows the receipt of events which contain additional
115              information about the underlying filesystem object correlated to
116              an event.  An additional record of type  FAN_EVENT_INFO_TYPE_FID
117              encapsulates  the  information  about the object and is included
118              alongside the generic event metadata structure.   The  file  de‐
119              scriptor  that  is used to represent the object correlated to an
120              event is instead substituted with a file handle.  It is intended
121              for applications that may find the use of a file handle to iden‐
122              tify an object more suitable than a file descriptor.   Addition‐
123              ally,  it may be used for applications monitoring a directory or
124              a filesystem that are interested in the directory entry  modifi‐
125              cation  events FAN_CREATE, FAN_DELETE, FAN_MOVE, and FAN_RENAME,
126              or  in  events  such   as   FAN_ATTRIB,   FAN_DELETE_SELF,   and
127              FAN_MOVE_SELF.   All  the events above require an fanotify group
128              that identifies filesystem objects by file handles.   Note  that
129              without  the flag FAN_REPORT_TARGET_FID, for the directory entry
130              modification events, there is an information record that identi‐
131              fies  the  modified  directory and not the created/deleted/moved
132              child    object.     The    use    of    FAN_CLASS_CONTENT    or
133              FAN_CLASS_PRE_CONTENT  is  not permitted with this flag and will
134              result in the error EINVAL.  See fanotify(7) for additional  de‐
135              tails.
136
137       FAN_REPORT_DIR_FID (since Linux 5.9)
138              Events  for fanotify groups initialized with this flag will con‐
139              tain (see exceptions below) additional information about  a  di‐
140              rectory  object correlated to an event.  An additional record of
141              type FAN_EVENT_INFO_TYPE_DFID encapsulates the information about
142              the directory object and is included alongside the generic event
143              metadata structure.  For events that occur  on  a  non-directory
144              object,  the  additional  structure  includes a file handle that
145              identifies the parent directory filesystem  object.   Note  that
146              there  is no guarantee that the directory filesystem object will
147              be found at the location described by the file  handle  informa‐
148              tion  at the time the event is received.  When combined with the
149              flag FAN_REPORT_FID, two records may  be  reported  with  events
150              that  occur  on a non-directory object, one to identify the non-
151              directory object itself and one to identify the parent directory
152              object.   Note  that in some cases, a filesystem object does not
153              have a parent, for example, when an event occurs on an  unlinked
154              but  open file.  In that case, with the FAN_REPORT_FID flag, the
155              event will be reported with only one record to identify the non-
156              directory  object  itself, because there is no directory associ‐
157              ated with the event.  Without the FAN_REPORT_FID flag, no  event
158              will be reported.  See fanotify(7) for additional details.
159
160       FAN_REPORT_NAME (since Linux 5.9)
161              Events  for fanotify groups initialized with this flag will con‐
162              tain additional information about the name of the directory  en‐
163              try  correlated to an event.  This flag must be provided in con‐
164              junction with the flag FAN_REPORT_DIR_FID.  Providing this  flag
165              value  without  FAN_REPORT_DIR_FID will result in the error EIN‐
166              VAL.  This flag may be combined with  the  flag  FAN_REPORT_FID.
167              An  additional  record  of  type  FAN_EVENT_INFO_TYPE_DFID_NAME,
168              which encapsulates the information about the directory entry, is
169              included alongside the generic event metadata structure and sub‐
170              stitutes   the   additional   information   record    of    type
171              FAN_EVENT_INFO_TYPE_DFID.  The additional record includes a file
172              handle that identifies a directory filesystem object followed by
173              a  name that identifies an entry in that directory.  For the di‐
174              rectory entry modification events  FAN_CREATE,  FAN_DELETE,  and
175              FAN_MOVE, the reported name is that of the created/deleted/moved
176              directory entry.  The event FAN_RENAME may contain two  informa‐
177              tion  records.   One  of  type FAN_EVENT_INFO_TYPE_OLD_DFID_NAME
178              identifying  the  old  directory  entry,  and  another  of  type
179              FAN_EVENT_INFO_TYPE_NEW_DFID_NAME  identifying the new directory
180              entry.  For other events that occur on a directory  object,  the
181              reported  file handle is that of the directory object itself and
182              the reported name is '.'.  For other events that occur on a non-
183              directory object, the reported file handle is that of the parent
184              directory object and the reported name is the name of  a  direc‐
185              tory  entry  where  the  object  was  located at the time of the
186              event.  The rationale behind this logic is that the reported di‐
187              rectory file handle can be passed to open_by_handle_at(2) to get
188              an open directory file descriptor and that file descriptor along
189              with the reported name can be used to call fstatat(2).  The same
190              rule that applies to record type  FAN_EVENT_INFO_TYPE_DFID  also
191              applies  to record type FAN_EVENT_INFO_TYPE_DFID_NAME: if a non-
192              directory object has no parent, either the event will not be re‐
193              ported or it will be reported without the directory entry infor‐
194              mation.  Note that there is no guarantee that the filesystem ob‐
195              ject  will  be  found at the location described by the directory
196              entry information at the time the event is received.   See  fan‐
197              otify(7) for additional details.
198
199       FAN_REPORT_DFID_NAME
200              This is a synonym for (FAN_REPORT_DIR_FID|FAN_REPORT_NAME).
201
202       FAN_REPORT_TARGET_FID (since Linux 5.17)
203              Events  for fanotify groups initialized with this flag will con‐
204              tain additional information about the child correlated with  di‐
205              rectory  entry  modification events.  This flag must be provided
206              in conjunction with the flags FAN_REPORT_FID, FAN_REPORT_DIR_FID
207              and FAN_REPORT_NAME.  or else the error EINVAL will be returned.
208              For  the  directory  entry   modification   events   FAN_CREATE,
209              FAN_DELETE,  FAN_MOVE,  and  FAN_RENAME, an additional record of
210              type FAN_EVENT_INFO_TYPE_FID, is reported in addition to the in‐
211              formation     records    of    type    FAN_EVENT_INFO_TYPE_DFID,
212              FAN_EVENT_INFO_TYPE_DFID_NAME,
213              FAN_EVENT_INFO_TYPE_OLD_DFID_NAME,                           and
214              FAN_EVENT_INFO_TYPE_NEW_DFID_NAME.  The  additional  record  in‐
215              cludes a file handle that identifies the filesystem child object
216              that the directory entry is referring to.
217
218       FAN_REPORT_DFID_NAME_TARGET
219              This   is   a    synonym    for    (FAN_REPORT_DFID_NAME|FAN_RE‐
220              PORT_FID|FAN_REPORT_TARGET_FID).
221
222       FAN_REPORT_PIDFD (since Linux 5.15)
223              Events  for fanotify groups initialized with this flag will con‐
224              tain an additional information record alongside the generic fan‐
225              otify_event_metadata structure.  This information record will be
226              of type FAN_EVENT_INFO_TYPE_PIDFD and will contain a  pidfd  for
227              the  process  that  was  responsible for generating an event.  A
228              pidfd returned in this information record object is no different
229              to the pidfd that is returned when calling pidfd_open(2).  Usage
230              of this information record are for applications that may be  in‐
231              terested in reliably determining whether the process responsible
232              for generating an event has been recycled  or  terminated.   The
233              use  of  the  FAN_REPORT_TID flag along with FAN_REPORT_PIDFD is
234              currently not supported and attempting to do so will  result  in
235              the  error  EINVAL being returned.  This limitation is currently
236              imposed by the pidfd API as it currently only supports the  cre‐
237              ation  of  pidfds for thread-group leaders.  Creating pidfds for
238              non-thread-group leaders may be supported at some point  in  the
239              future,  so this restriction may eventually be lifted.  For more
240              details on information records, see fanotify(7).
241
242       The event_f_flags argument defines the file status flags that  will  be
243       set on the open file descriptions that are created for fanotify events.
244       For details of these flags, see the description of the flags values  in
245       open(2).  event_f_flags includes a multi-bit field for the access mode.
246       This field can take the following values:
247
248       O_RDONLY
249              This value allows only read access.
250
251       O_WRONLY
252              This value allows only write access.
253
254       O_RDWR This value allows read and write access.
255
256       Additional bits can be set in event_f_flags.  The  most  useful  values
257       are:
258
259       O_LARGEFILE
260              Enable  support  for  files exceeding 2 GB.  Failing to set this
261              flag will result in an EOVERFLOW error when  trying  to  open  a
262              large  file  which is monitored by an fanotify group on a 32-bit
263              system.
264
265       O_CLOEXEC (since Linux 3.18)
266              Enable the close-on-exec flag for the file descriptor.  See  the
267              description  of  the  O_CLOEXEC  flag in open(2) for reasons why
268              this may be useful.
269
270       The following are also allowable: O_APPEND, O_DSYNC, O_NOATIME,  O_NON‐
271       BLOCK,  and  O_SYNC.  Specifying any other flag in event_f_flags yields
272       the error EINVAL (but see BUGS).
273

RETURN VALUE

275       On success, fanotify_init() returns a new file descriptor.   On  error,
276       -1 is returned, and errno is set to indicate the error.
277

ERRORS

279       EINVAL An   invalid   value  was  passed  in  flags  or  event_f_flags.
280              FAN_ALL_INIT_FLAGS (deprecated since Linux 4.20) defines all al‐
281              lowable bits for flags.
282
283       EMFILE The  number  of fanotify groups for this user exceeds the limit.
284              See fanotify(7) for details about this limit.
285
286       EMFILE The per-process limit on the number of open file descriptors has
287              been reached.
288
289       ENOMEM The allocation of memory for the notification group failed.
290
291       ENOSYS This  kernel  does  not implement fanotify_init().  The fanotify
292              API is available only if the kernel  was  configured  with  CON‐
293              FIG_FANOTIFY.
294
295       EPERM  The  operation  is  not permitted because the caller lacks a re‐
296              quired capability.
297

VERSIONS

299       Prior to Linux 5.13, calling fanotify_init() required the CAP_SYS_ADMIN
300       capability.   Since  Linux 5.13, users may call fanotify_init() without
301       the CAP_SYS_ADMIN capability to create and initialize an fanotify group
302       with limited functionality.
303
304       The  limitations imposed on an event listener created by a user without
305       the
306              CAP_SYS_ADMIN capability are as follows:
307
308              •  The user cannot request for an unlimited event queue by using
309                 FAN_UNLIMITED_QUEUE.
310
311              •  The  user  cannot request for an unlimited number of marks by
312                 using FAN_UNLIMITED_MARKS.
313
314              •  The user cannot request to use  either  notification  classes
315                 FAN_CLASS_CONTENT  or FAN_CLASS_PRE_CONTENT.  This means that
316                 user cannot request permission events.
317
318              •  The user is  required  to  create  a  group  that  identifies
319                 filesystem objects by file handles, for example, by providing
320                 the FAN_REPORT_FID flag.
321
322              •  The user is limited to only mark inodes.  The ability to mark
323                 a  mount or filesystem via fanotify_mark() through the use of
324                 FAN_MARK_MOUNT or FAN_MARK_FILESYSTEM is not permitted.
325
326              •  The event object in the event queue is limited  in  terms  of
327                 the  information  that  is made available to the unprivileged
328                 user.  A user will also not receive the  pid  that  generated
329                 the  event, unless the listening process itself generated the
330                 event.
331

STANDARDS

333       Linux.
334

HISTORY

336       Linux 2.6.37.
337

BUGS

339       The following bug was present before Linux 3.18:
340
341       •  The O_CLOEXEC is ignored when passed in event_f_flags.
342
343       The following bug was present before Linux 3.14:
344
345       •  The event_f_flags argument is not checked for invalid flags.   Flags
346          that  are intended only for internal use, such as FMODE_EXEC, can be
347          set, and will consequently be set for the file descriptors  returned
348          when reading from the fanotify file descriptor.
349

SEE ALSO

351       fanotify_mark(2), fanotify(7)
352
353
354
355Linux man-pages 6.04              2023-03-30                  fanotify_init(2)
Impressum