1FANOTIFY_INIT(2)           Linux Programmer's Manual          FANOTIFY_INIT(2)
2
3
4

NAME

6       fanotify_init - create and initialize fanotify group
7

SYNOPSIS

9       #include <fcntl.h>
10       #include <sys/fanotify.h>
11
12       int fanotify_init(unsigned int flags, unsigned int event_f_flags);
13

DESCRIPTION

15       For an overview of the fanotify API, see fanotify(7).
16
17       fanotify_init()  initializes  a  new  fanotify group and returns a file
18       descriptor for the event queue associated with the group.
19
20       The file descriptor is used in calls to fanotify_mark(2) to specify the
21       files,  directories,  mounts  or  filesystems for which fanotify events
22       shall be created.  These events are received by reading from  the  file
23       descriptor.   Some  events are only informative, indicating that a file
24       has been accessed.  Other events  can  be  used  to  determine  whether
25       another  application  is permitted to access a file or directory.  Per‐
26       mission to access filesystem objects is granted by writing to the  file
27       descriptor.
28
29       Multiple  programs may be using the fanotify interface at the same time
30       to monitor the same files.
31
32       In the current implementation, the number of fanotify groups  per  user
33       is limited to 128.  This limit cannot be overridden.
34
35       Calling  fanotify_init()  requires  the CAP_SYS_ADMIN capability.  This
36       constraint might be relaxed in future versions of the API.   Therefore,
37       certain additional capability checks have been implemented as indicated
38       below.
39
40       The flags argument contains a multi-bit field defining the notification
41       class of the listening application and further single bit fields speci‐
42       fying the behavior of the file descriptor.
43
44       If multiple listeners for permission  events  exist,  the  notification
45       class  is used to establish the sequence in which the listeners receive
46       the events.
47
48       Only one of the following notification  classes  may  be  specified  in
49       flags:
50
51       FAN_CLASS_PRE_CONTENT
52              This  value  allows  the receipt of events notifying that a file
53              has been accessed and events for permission decisions if a  file
54              may  be  accessed.  It is intended for event listeners that need
55              to access files before they  contain  their  final  data.   This
56              notification  class  might  be used by hierarchical storage man‐
57              agers, for example.
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.
66
67       FAN_CLASS_NOTIF
68              This  is  the  default value.  It does not need to be specified.
69              This value only allows the receipt of events  notifying  that  a
70              file has been accessed.  Permission decisions before the file is
71              accessed are not possible.
72
73       Listeners with different notification classes will  receive  events  in
74       the  order  FAN_CLASS_PRE_CONTENT,  FAN_CLASS_CONTENT, FAN_CLASS_NOTIF.
75       The order of notification for listeners in the same notification  class
76       is undefined.
77
78       The following bits can additionally be set in flags:
79
80       FAN_CLOEXEC
81              Set the close-on-exec flag (FD_CLOEXEC) on the new file descrip‐
82              tor.  See the description of the O_CLOEXEC flag in open(2).
83
84       FAN_NONBLOCK
85              Enable the nonblocking flag (O_NONBLOCK) for the  file  descrip‐
86              tor.  Reading from the file descriptor will not block.  Instead,
87              if no data is available, read(2) fails with the error EAGAIN.
88
89       FAN_UNLIMITED_QUEUE
90              Remove the limit of 16384 events for the event  queue.   Use  of
91              this flag requires the CAP_SYS_ADMIN capability.
92
93       FAN_UNLIMITED_MARKS
94              Remove  the  limit of 8192 marks.  Use of this flag requires the
95              CAP_SYS_ADMIN capability.
96
97       FAN_REPORT_TID (since Linux 4.20)
98              Report thread ID (TID) instead of process ID (PID)  in  the  pid
99              field  of the struct fanotify_event_metadata supplied to read(2)
100              (see fanotify(7)).
101
102       FAN_REPORT_FID (since Linux 5.1)
103              This value allows the receipt of events which contain additional
104              information about the underlying filesystem object correlated to
105              an event.  An additional structure encapsulates the  information
106              about  the  object  and  is included alongside the generic event
107              metadata structure.  The file descriptor that is used to  repre‐
108              sent  the  object  correlated to an event is instead substituted
109              with a file handle.  It is intended for  applications  that  may
110              find  the  use of a file handle to identify an object more suit‐
111              able than a file descriptor.  Additionally, it may be  used  for
112              applications that are interested in directory entry events, such
113              as FAN_CREATE, FAN_ATTRIB, FAN_MOVE, and FAN_DELETE for example.
114              Note  that the use of directory modification events are not sup‐
115              ported when monitoring a mount point.  The use of FAN_CLASS_CON‐
116              TENT  or  FAN_CLASS_PRE_CONTENT  is not permitted with this flag
117              and will result in the error EINVAL.  See fanotify(7) for  addi‐
118              tional information.
119
120       The  event_f_flags  argument defines the file status flags that will be
121       set on the open file descriptions that are created for fanotify events.
122       For  details of these flags, see the description of the flags values in
123       open(2).  event_f_flags includes a multi-bit field for the access mode.
124       This field can take the following values:
125
126       O_RDONLY
127              This value allows only read access.
128
129       O_WRONLY
130              This value allows only write access.
131
132       O_RDWR This value allows read and write access.
133
134       Additional  bits  can  be set in event_f_flags.  The most useful values
135       are:
136
137       O_LARGEFILE
138              Enable support for files exceeding 2 GB.  Failing  to  set  this
139              flag  will  result  in  an EOVERFLOW error when trying to open a
140              large file which is monitored by an fanotify group on  a  32-bit
141              system.
142
143       O_CLOEXEC (since Linux 3.18)
144              Enable  the close-on-exec flag for the file descriptor.  See the
145              description of the O_CLOEXEC flag in  open(2)  for  reasons  why
146              this may be useful.
147
148       The  following are also allowable: O_APPEND, O_DSYNC, O_NOATIME, O_NON‐
149       BLOCK, and O_SYNC.  Specifying any other flag in  event_f_flags  yields
150       the error EINVAL (but see BUGS).
151

RETURN VALUE

153       On  success,  fanotify_init() returns a new file descriptor.  On error,
154       -1 is returned, and errno is set to indicate the error.
155

ERRORS

157       EINVAL An  invalid  value  was  passed  in  flags   or   event_f_flags.
158              FAN_ALL_INIT_FLAGS  (deprecated since Linux kernel version 4.20)
159              defines all allowable bits for flags.
160
161       EMFILE The number of fanotify groups for this user exceeds 128.
162
163       EMFILE The per-process limit on the number of open file descriptors has
164              been reached.
165
166       ENOMEM The allocation of memory for the notification group failed.
167
168       ENOSYS This  kernel  does  not implement fanotify_init().  The fanotify
169              API is available only if the kernel  was  configured  with  CON‐
170              FIG_FANOTIFY.
171
172       EPERM  The  operation  is  not  permitted  because the caller lacks the
173              CAP_SYS_ADMIN capability.
174

VERSIONS

176       fanotify_init() was introduced in version 2.6.36 of  the  Linux  kernel
177       and enabled in version 2.6.37.
178

CONFORMING TO

180       This system call is Linux-specific.
181

BUGS

183       The following bug was present in Linux kernels before version 3.18:
184
185       *  The O_CLOEXEC is ignored when passed in event_f_flags.
186
187       The following bug was present in Linux kernels before version 3.14:
188
189       *  The  event_f_flags argument is not checked for invalid flags.  Flags
190          that are intended only for internal use, such as FMODE_EXEC, can  be
191          set,  and will consequently be set for the file descriptors returned
192          when reading from the fanotify file descriptor.
193

SEE ALSO

195       fanotify_mark(2), fanotify(7)
196

COLOPHON

198       This page is part of release 5.07 of the Linux  man-pages  project.   A
199       description  of  the project, information about reporting bugs, and the
200       latest    version    of    this    page,    can     be     found     at
201       https://www.kernel.org/doc/man-pages/.
202
203
204
205Linux                             2020-06-09                  FANOTIFY_INIT(2)
Impressum