1FANOTIFY_MARK(2) Linux Programmer's Manual FANOTIFY_MARK(2)
2
3
4
6 fanotify_mark - add, remove, or modify an fanotify mark on a filesystem
7 object
8
10 #include <sys/fanotify.h>
11
12 int fanotify_mark(int fanotify_fd, unsigned int flags,
13 uint64_t mask, int dirfd, const char *pathname);
14
16 For an overview of the fanotify API, see fanotify(7).
17
18 fanotify_mark() adds, removes, or modifies an fanotify mark on a
19 filesystem object. The caller must have read permission on the
20 filesystem object that is to be marked.
21
22 The fanotify_fd argument is a file descriptor returned by fan‐
23 otify_init(2).
24
25 flags is a bit mask describing the modification to perform. It must
26 include exactly one of the following values:
27
28 FAN_MARK_ADD
29 The events in mask will be added to the mark mask (or to the ig‐
30 nore mask). mask must be nonempty or the error EINVAL will oc‐
31 cur.
32
33 FAN_MARK_REMOVE
34 The events in argument mask will be removed from the mark mask
35 (or from the ignore mask). mask must be nonempty or the error
36 EINVAL will occur.
37
38 FAN_MARK_FLUSH
39 Remove either all marks for filesystems, all marks for mounts,
40 or all marks for directories and files from the fanotify group.
41 If flags contains FAN_MARK_MOUNT, all marks for mounts are re‐
42 moved from the group. If flags contains FAN_MARK_FILESYSTEM,
43 all marks for filesystems are removed from the group. Other‐
44 wise, all marks for directories and files are removed. No flag
45 other than and at most one of the flags FAN_MARK_MOUNT or
46 FAN_MARK_FILESYSTEM can be used in conjunction with
47 FAN_MARK_FLUSH. mask is ignored.
48
49 If none of the values above is specified, or more than one is speci‐
50 fied, the call fails with the error EINVAL.
51
52 In addition, zero or more of the following values may be ORed into
53 flags:
54
55 FAN_MARK_DONT_FOLLOW
56 If pathname is a symbolic link, mark the link itself, rather
57 than the file to which it refers. (By default, fanotify_mark()
58 dereferences pathname if it is a symbolic link.)
59
60 FAN_MARK_ONLYDIR
61 If the filesystem object to be marked is not a directory, the
62 error ENOTDIR shall be raised.
63
64 FAN_MARK_MOUNT
65 Mark the mount point specified by pathname. If pathname is not
66 itself a mount point, the mount point containing pathname will
67 be marked. All directories, subdirectories, and the contained
68 files of the mount point will be monitored. The events which
69 require that filesystem objects are identified by file handles,
70 such as FAN_CREATE, FAN_ATTRIB, FAN_MOVE, and FAN_DELETE_SELF,
71 cannot be provided as a mask when flags contains FAN_MARK_MOUNT.
72 Attempting to do so will result in the error EINVAL being re‐
73 turned.
74
75 FAN_MARK_FILESYSTEM (since Linux 4.20)
76 Mark the filesystem specified by pathname. The filesystem con‐
77 taining pathname will be marked. All the contained files and
78 directories of the filesystem from any mount point will be moni‐
79 tored.
80
81 FAN_MARK_IGNORED_MASK
82 The events in mask shall be added to or removed from the ignore
83 mask.
84
85 FAN_MARK_IGNORED_SURV_MODIFY
86 The ignore mask shall survive modify events. If this flag is
87 not set, the ignore mask is cleared when a modify event occurs
88 for the ignored file or directory.
89
90 mask defines which events shall be listened for (or which shall be ig‐
91 nored). It is a bit mask composed of the following values:
92
93 FAN_ACCESS
94 Create an event when a file or directory (but see BUGS) is ac‐
95 cessed (read).
96
97 FAN_MODIFY
98 Create an event when a file is modified (write).
99
100 FAN_CLOSE_WRITE
101 Create an event when a writable file is closed.
102
103 FAN_CLOSE_NOWRITE
104 Create an event when a read-only file or directory is closed.
105
106 FAN_OPEN
107 Create an event when a file or directory is opened.
108
109 FAN_OPEN_EXEC (since Linux 5.0)
110 Create an event when a file is opened with the intent to be exe‐
111 cuted. See NOTES for additional details.
112
113 FAN_ATTRIB (since Linux 5.1)
114 Create an event when the metadata for a file or directory has
115 changed. An fanotify group that identifies filesystem objects
116 by file handles is required.
117
118 FAN_CREATE (since Linux 5.1)
119 Create an event when a file or directory has been created in a
120 marked parent directory. An fanotify group that identifies
121 filesystem objects by file handles is required.
122
123 FAN_DELETE (since Linux 5.1)
124 Create an event when a file or directory has been deleted in a
125 marked parent directory. An fanotify group that identifies
126 filesystem objects by file handles is required.
127
128 FAN_DELETE_SELF (since Linux 5.1)
129 Create an event when a marked file or directory itself is
130 deleted. An fanotify group that identifies filesystem objects
131 by file handles is required.
132
133 FAN_MOVED_FROM (since Linux 5.1)
134 Create an event when a file or directory has been moved from a
135 marked parent directory. An fanotify group that identifies
136 filesystem objects by file handles is required.
137
138 FAN_MOVED_TO (since Linux 5.1)
139 Create an event when a file or directory has been moved to a
140 marked parent directory. An fanotify group that identifies
141 filesystem objects by file handles is required.
142
143 FAN_MOVE_SELF (since Linux 5.1)
144 Create an event when a marked file or directory itself has been
145 moved. An fanotify group that identifies filesystem objects by
146 file handles is required.
147
148 FAN_OPEN_PERM
149 Create an event when a permission to open a file or directory is
150 requested. An fanotify file descriptor created with
151 FAN_CLASS_PRE_CONTENT or FAN_CLASS_CONTENT is required.
152
153 FAN_OPEN_EXEC_PERM (since Linux 5.0)
154 Create an event when a permission to open a file for execution
155 is requested. An fanotify file descriptor created with
156 FAN_CLASS_PRE_CONTENT or FAN_CLASS_CONTENT is required. See
157 NOTES for additional details.
158
159 FAN_ACCESS_PERM
160 Create an event when a permission to read a file or directory is
161 requested. An fanotify file descriptor created with
162 FAN_CLASS_PRE_CONTENT or FAN_CLASS_CONTENT is required.
163
164 FAN_ONDIR
165 Create events for directories—for example, when opendir(3),
166 readdir(3) (but see BUGS), and closedir(3) are called. Without
167 this flag, events are created only for files. In the context of
168 directory entry events, such as FAN_CREATE, FAN_DELETE,
169 FAN_MOVED_FROM, and FAN_MOVED_TO, specifying the flag FAN_ONDIR
170 is required in order to create events when subdirectory entries
171 are modified (i.e., mkdir(2)/ rmdir(2)).
172
173 FAN_EVENT_ON_CHILD
174 Events for the immediate children of marked directories shall be
175 created. The flag has no effect when marking mounts and
176 filesystems. Note that events are not generated for children of
177 the subdirectories of marked directories. More specifically,
178 the directory entry modification events FAN_CREATE, FAN_DELETE,
179 FAN_MOVED_FROM, and FAN_MOVED_TO are not generated for any entry
180 modifications performed inside subdirectories of marked directo‐
181 ries. Note that the events FAN_DELETE_SELF and FAN_MOVE_SELF
182 are not generated for children of marked directories. To moni‐
183 tor complete directory trees it is necessary to mark the rele‐
184 vant mount or filesystem.
185
186 The following composed values are defined:
187
188 FAN_CLOSE
189 A file is closed (FAN_CLOSE_WRITE|FAN_CLOSE_NOWRITE).
190
191 FAN_MOVE
192 A file or directory has been moved
193 (FAN_MOVED_FROM|FAN_MOVED_TO).
194
195 The filesystem object to be marked is determined by the file descriptor
196 dirfd and the pathname specified in pathname:
197
198 * If pathname is NULL, dirfd defines the filesystem object to be
199 marked.
200
201 * If pathname is NULL, and dirfd takes the special value AT_FDCWD, the
202 current working directory is to be marked.
203
204 * If pathname is absolute, it defines the filesystem object to be
205 marked, and dirfd is ignored.
206
207 * If pathname is relative, and dirfd does not have the value AT_FDCWD,
208 then the filesystem object to be marked is determined by interpret‐
209 ing pathname relative the directory referred to by dirfd.
210
211 * If pathname is relative, and dirfd has the value AT_FDCWD, then the
212 filesystem object to be marked is determined by interpreting path‐
213 name relative the current working directory.
214
216 On success, fanotify_mark() returns 0. On error, -1 is returned, and
217 errno is set to indicate the error.
218
220 EBADF An invalid file descriptor was passed in fanotify_fd.
221
222 EINVAL An invalid value was passed in flags or mask, or fanotify_fd was
223 not an fanotify file descriptor.
224
225 EINVAL The fanotify file descriptor was opened with FAN_CLASS_NOTIF or
226 the fanotify group identifies filesystem objects by file handles
227 and mask contains a flag for permission events (FAN_OPEN_PERM or
228 FAN_ACCESS_PERM).
229
230 ENODEV The filesystem object indicated by pathname is not associated
231 with a filesystem that supports fsid (e.g., tmpfs(5)). This er‐
232 ror can be returned only with an fanotify group that identifies
233 filesystem objects by file handles.
234
235 ENOENT The filesystem object indicated by dirfd and pathname does not
236 exist. This error also occurs when trying to remove a mark from
237 an object which is not marked.
238
239 ENOMEM The necessary memory could not be allocated.
240
241 ENOSPC The number of marks exceeds the limit of 8192 and the FAN_UNLIM‐
242 ITED_MARKS flag was not specified when the fanotify file de‐
243 scriptor was created with fanotify_init(2).
244
245 ENOSYS This kernel does not implement fanotify_mark(). The fanotify
246 API is available only if the kernel was configured with CON‐
247 FIG_FANOTIFY.
248
249 ENOTDIR
250 flags contains FAN_MARK_ONLYDIR, and dirfd and pathname do not
251 specify a directory.
252
253 EOPNOTSUPP
254 The object indicated by pathname is associated with a filesystem
255 that does not support the encoding of file handles. This error
256 can be returned only with an fanotify group that identifies
257 filesystem objects by file handles.
258
259 EXDEV The filesystem object indicated by pathname resides within a
260 filesystem subvolume (e.g., btrfs(5)) which uses a different
261 fsid than its root superblock. This error can be returned only
262 with an fanotify group that identifies filesystem objects by
263 file handles.
264
266 fanotify_mark() was introduced in version 2.6.36 of the Linux kernel
267 and enabled in version 2.6.37.
268
270 This system call is Linux-specific.
271
273 FAN_OPEN_EXEC and FAN_OPEN_EXEC_PERM
274 When using either FAN_OPEN_EXEC or FAN_OPEN_EXEC_PERM within the mask,
275 events of these types will be returned only when the direct execution
276 of a program occurs. More specifically, this means that events of
277 these types will be generated for files that are opened using ex‐
278 ecve(2), execveat(2), or uselib(2). Events of these types will not be
279 raised in the situation where an interpreter is passed (or reads) a
280 file for interpretation.
281
282 Additionally, if a mark has also been placed on the Linux dynamic
283 linker, a user should also expect to receive an event for it when an
284 ELF object has been successfully opened using execve(2) or execveat(2).
285
286 For example, if the following ELF binary were to be invoked and a
287 FAN_OPEN_EXEC mark has been placed on /:
288
289 $ /bin/echo foo
290
291 The listening application in this case would receive FAN_OPEN_EXEC
292 events for both the ELF binary and interpreter, respectively:
293
294 /bin/echo
295 /lib64/ld-linux-x86-64.so.2
296
298 The following bugs were present in Linux kernels before version 3.16:
299
300 * If flags contains FAN_MARK_FLUSH, dirfd, and pathname must specify a
301 valid filesystem object, even though this object is not used.
302
303 * readdir(2) does not generate a FAN_ACCESS event.
304
305 * If fanotify_mark() is called with FAN_MARK_FLUSH, flags is not
306 checked for invalid values.
307
309 fanotify_init(2), fanotify(7)
310
312 This page is part of release 5.12 of the Linux man-pages project. A
313 description of the project, information about reporting bugs, and the
314 latest version of this page, can be found at
315 https://www.kernel.org/doc/man-pages/.
316
317
318
319Linux 2021-03-22 FANOTIFY_MARK(2)