1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2018 CTERA Networks. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Started by Amir Goldstein <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker * Check that fanotify handles events on children correctly when both parent and
11*49cdfc7eSAndroid Build Coastguard Worker * subdir or mountpoint marks exist.
12*49cdfc7eSAndroid Build Coastguard Worker */
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker /*
15*49cdfc7eSAndroid Build Coastguard Worker * This is a regression test for commit:
16*49cdfc7eSAndroid Build Coastguard Worker *
17*49cdfc7eSAndroid Build Coastguard Worker * 54a307ba8d3c fanotify: fix logic of events on child
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * Test case #1 is a regression test for commit:
20*49cdfc7eSAndroid Build Coastguard Worker *
21*49cdfc7eSAndroid Build Coastguard Worker * b469e7e47c8a fanotify: fix handling of events on child sub-directory
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * Test case #2 is a regression test for commit:
24*49cdfc7eSAndroid Build Coastguard Worker *
25*49cdfc7eSAndroid Build Coastguard Worker * 55bf882c7f13 fanotify: fix merging marks masks with FAN_ONDIR
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * Test case #5 is a regression test for commit:
28*49cdfc7eSAndroid Build Coastguard Worker *
29*49cdfc7eSAndroid Build Coastguard Worker * 7372e79c9eb9 fanotify: fix logic of reporting name info with watched parent
30*49cdfc7eSAndroid Build Coastguard Worker *
31*49cdfc7eSAndroid Build Coastguard Worker * Test cases #6-#7 are regression tests for commit:
32*49cdfc7eSAndroid Build Coastguard Worker * (from v5.19, unlikely to be backported thus not in .tags):
33*49cdfc7eSAndroid Build Coastguard Worker *
34*49cdfc7eSAndroid Build Coastguard Worker * e730558adffb fanotify: consistent behavior for parent not watching children
35*49cdfc7eSAndroid Build Coastguard Worker */
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
38*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <stdint.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_FANOTIFY_H
51*49cdfc7eSAndroid Build Coastguard Worker #include "fanotify.h"
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_MAX 1024
54*49cdfc7eSAndroid Build Coastguard Worker /* size of the event structure, not counting name */
55*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SIZE (sizeof(struct fanotify_event_metadata))
56*49cdfc7eSAndroid Build Coastguard Worker /* reasonable guess as to size of 1024 events */
57*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE)
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker #define NUM_GROUPS 3
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
62*49cdfc7eSAndroid Build Coastguard Worker static char fname[BUF_SIZE];
63*49cdfc7eSAndroid Build Coastguard Worker static char symlnk[BUF_SIZE];
64*49cdfc7eSAndroid Build Coastguard Worker static char fdpath[BUF_SIZE];
65*49cdfc7eSAndroid Build Coastguard Worker static int fd_notify[NUM_GROUPS];
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker static char event_buf[EVENT_BUF_LEN];
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker #define MOUNT_PATH "fs_mnt"
70*49cdfc7eSAndroid Build Coastguard Worker #define MOUNT_NAME "mntpoint"
71*49cdfc7eSAndroid Build Coastguard Worker #define DIR_NAME "testdir"
72*49cdfc7eSAndroid Build Coastguard Worker #define FILE2_NAME "testfile"
73*49cdfc7eSAndroid Build Coastguard Worker static int mount_created;
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker static int fan_report_dfid_unsupported;
76*49cdfc7eSAndroid Build Coastguard Worker static int ignore_mark_unsupported;
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
79*49cdfc7eSAndroid Build Coastguard Worker const char *tname;
80*49cdfc7eSAndroid Build Coastguard Worker struct fanotify_mark_type mark;
81*49cdfc7eSAndroid Build Coastguard Worker unsigned int ondir;
82*49cdfc7eSAndroid Build Coastguard Worker unsigned int ignore;
83*49cdfc7eSAndroid Build Coastguard Worker unsigned int ignore_flags;
84*49cdfc7eSAndroid Build Coastguard Worker unsigned int report_name;
85*49cdfc7eSAndroid Build Coastguard Worker const char *event_path;
86*49cdfc7eSAndroid Build Coastguard Worker int nevents;
87*49cdfc7eSAndroid Build Coastguard Worker unsigned int nonfirst_event;
88*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on non-dir child with both parent and mount marks",
91*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
92*49cdfc7eSAndroid Build Coastguard Worker .event_path = DIR_NAME,
93*49cdfc7eSAndroid Build Coastguard Worker .nevents = 1,
94*49cdfc7eSAndroid Build Coastguard Worker },
95*49cdfc7eSAndroid Build Coastguard Worker {
96*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on non-dir child and subdir with both parent and mount marks",
97*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
98*49cdfc7eSAndroid Build Coastguard Worker .ondir = FAN_ONDIR,
99*49cdfc7eSAndroid Build Coastguard Worker .event_path = DIR_NAME,
100*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
101*49cdfc7eSAndroid Build Coastguard Worker },
102*49cdfc7eSAndroid Build Coastguard Worker {
103*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on non-dir child and parent with both parent and mount marks",
104*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
105*49cdfc7eSAndroid Build Coastguard Worker .ondir = FAN_ONDIR,
106*49cdfc7eSAndroid Build Coastguard Worker .event_path = ".",
107*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
108*49cdfc7eSAndroid Build Coastguard Worker },
109*49cdfc7eSAndroid Build Coastguard Worker {
110*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on non-dir child and subdir with both parent and subdir marks",
111*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(INODE),
112*49cdfc7eSAndroid Build Coastguard Worker .ondir = FAN_ONDIR,
113*49cdfc7eSAndroid Build Coastguard Worker .event_path = DIR_NAME,
114*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
115*49cdfc7eSAndroid Build Coastguard Worker },
116*49cdfc7eSAndroid Build Coastguard Worker {
117*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on non-dir children with both parent and mount marks",
118*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
119*49cdfc7eSAndroid Build Coastguard Worker .event_path = FILE2_NAME,
120*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
121*49cdfc7eSAndroid Build Coastguard Worker .nonfirst_event = FAN_CLOSE_NOWRITE,
122*49cdfc7eSAndroid Build Coastguard Worker },
123*49cdfc7eSAndroid Build Coastguard Worker {
124*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on non-dir child with both parent and mount marks and filename info",
125*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
126*49cdfc7eSAndroid Build Coastguard Worker .report_name = FAN_REPORT_DFID_NAME,
127*49cdfc7eSAndroid Build Coastguard Worker .event_path = FILE2_NAME,
128*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
129*49cdfc7eSAndroid Build Coastguard Worker .nonfirst_event = FAN_CLOSE_NOWRITE,
130*49cdfc7eSAndroid Build Coastguard Worker },
131*49cdfc7eSAndroid Build Coastguard Worker {
132*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on non-dir child with ignore mask on parent",
133*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
134*49cdfc7eSAndroid Build Coastguard Worker .ignore = FAN_MARK_IGNORED_MASK,
135*49cdfc7eSAndroid Build Coastguard Worker .event_path = DIR_NAME,
136*49cdfc7eSAndroid Build Coastguard Worker .nevents = 1,
137*49cdfc7eSAndroid Build Coastguard Worker },
138*49cdfc7eSAndroid Build Coastguard Worker {
139*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on non-dir children with surviving ignore mask on parent",
140*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
141*49cdfc7eSAndroid Build Coastguard Worker .ignore = FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY,
142*49cdfc7eSAndroid Build Coastguard Worker .event_path = FILE2_NAME,
143*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
144*49cdfc7eSAndroid Build Coastguard Worker .nonfirst_event = FAN_CLOSE_NOWRITE,
145*49cdfc7eSAndroid Build Coastguard Worker },
146*49cdfc7eSAndroid Build Coastguard Worker /* FAN_MARK_IGNORE test cases: */
147*49cdfc7eSAndroid Build Coastguard Worker {
148*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on dir with ignore mask that does not apply to dirs",
149*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
150*49cdfc7eSAndroid Build Coastguard Worker .ondir = FAN_ONDIR,
151*49cdfc7eSAndroid Build Coastguard Worker .ignore = FAN_MARK_IGNORE_SURV,
152*49cdfc7eSAndroid Build Coastguard Worker .event_path = ".",
153*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
154*49cdfc7eSAndroid Build Coastguard Worker .nonfirst_event = FAN_CLOSE_NOWRITE,
155*49cdfc7eSAndroid Build Coastguard Worker },
156*49cdfc7eSAndroid Build Coastguard Worker {
157*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on dir with ignore mask that does apply to dirs",
158*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
159*49cdfc7eSAndroid Build Coastguard Worker .ondir = FAN_ONDIR,
160*49cdfc7eSAndroid Build Coastguard Worker .ignore = FAN_MARK_IGNORE_SURV,
161*49cdfc7eSAndroid Build Coastguard Worker .ignore_flags = FAN_ONDIR,
162*49cdfc7eSAndroid Build Coastguard Worker .event_path = ".",
163*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
164*49cdfc7eSAndroid Build Coastguard Worker },
165*49cdfc7eSAndroid Build Coastguard Worker {
166*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on child with ignore mask on parent that does not apply to children",
167*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
168*49cdfc7eSAndroid Build Coastguard Worker .ignore = FAN_MARK_IGNORE_SURV,
169*49cdfc7eSAndroid Build Coastguard Worker .event_path = FILE2_NAME,
170*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
171*49cdfc7eSAndroid Build Coastguard Worker .nonfirst_event = FAN_CLOSE_NOWRITE,
172*49cdfc7eSAndroid Build Coastguard Worker },
173*49cdfc7eSAndroid Build Coastguard Worker {
174*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on child with ignore mask on parent that does apply to children",
175*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
176*49cdfc7eSAndroid Build Coastguard Worker .ignore = FAN_MARK_IGNORE_SURV,
177*49cdfc7eSAndroid Build Coastguard Worker .ignore_flags = FAN_EVENT_ON_CHILD,
178*49cdfc7eSAndroid Build Coastguard Worker .event_path = FILE2_NAME,
179*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
180*49cdfc7eSAndroid Build Coastguard Worker },
181*49cdfc7eSAndroid Build Coastguard Worker {
182*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on subdir with ignore mask on parent that does not apply to children",
183*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
184*49cdfc7eSAndroid Build Coastguard Worker .ondir = FAN_ONDIR,
185*49cdfc7eSAndroid Build Coastguard Worker .ignore = FAN_MARK_IGNORE_SURV,
186*49cdfc7eSAndroid Build Coastguard Worker .ignore_flags = FAN_ONDIR,
187*49cdfc7eSAndroid Build Coastguard Worker .event_path = DIR_NAME,
188*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
189*49cdfc7eSAndroid Build Coastguard Worker .nonfirst_event = FAN_CLOSE_NOWRITE,
190*49cdfc7eSAndroid Build Coastguard Worker },
191*49cdfc7eSAndroid Build Coastguard Worker {
192*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on subdir with ignore mask on parent that does not apply to dirs",
193*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
194*49cdfc7eSAndroid Build Coastguard Worker .ondir = FAN_ONDIR,
195*49cdfc7eSAndroid Build Coastguard Worker .ignore = FAN_MARK_IGNORE_SURV,
196*49cdfc7eSAndroid Build Coastguard Worker .ignore_flags = FAN_EVENT_ON_CHILD,
197*49cdfc7eSAndroid Build Coastguard Worker .event_path = DIR_NAME,
198*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
199*49cdfc7eSAndroid Build Coastguard Worker .nonfirst_event = FAN_CLOSE_NOWRITE,
200*49cdfc7eSAndroid Build Coastguard Worker },
201*49cdfc7eSAndroid Build Coastguard Worker {
202*49cdfc7eSAndroid Build Coastguard Worker .tname = "Events on subdir with ignore mask on parent that does apply to subdirs",
203*49cdfc7eSAndroid Build Coastguard Worker .mark = INIT_FANOTIFY_MARK_TYPE(MOUNT),
204*49cdfc7eSAndroid Build Coastguard Worker .ondir = FAN_ONDIR,
205*49cdfc7eSAndroid Build Coastguard Worker .ignore = FAN_MARK_IGNORE_SURV,
206*49cdfc7eSAndroid Build Coastguard Worker .ignore_flags = FAN_EVENT_ON_CHILD | FAN_ONDIR,
207*49cdfc7eSAndroid Build Coastguard Worker .event_path = DIR_NAME,
208*49cdfc7eSAndroid Build Coastguard Worker .nevents = 2,
209*49cdfc7eSAndroid Build Coastguard Worker },
210*49cdfc7eSAndroid Build Coastguard Worker };
211*49cdfc7eSAndroid Build Coastguard Worker
create_fanotify_groups(struct tcase * tc)212*49cdfc7eSAndroid Build Coastguard Worker static void create_fanotify_groups(struct tcase *tc)
213*49cdfc7eSAndroid Build Coastguard Worker {
214*49cdfc7eSAndroid Build Coastguard Worker struct fanotify_mark_type *mark = &tc->mark;
215*49cdfc7eSAndroid Build Coastguard Worker int i;
216*49cdfc7eSAndroid Build Coastguard Worker
217*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < NUM_GROUPS; i++) {
218*49cdfc7eSAndroid Build Coastguard Worker /*
219*49cdfc7eSAndroid Build Coastguard Worker * The first group may request events with filename info and
220*49cdfc7eSAndroid Build Coastguard Worker * events on subdirs and always request events on children.
221*49cdfc7eSAndroid Build Coastguard Worker */
222*49cdfc7eSAndroid Build Coastguard Worker unsigned int report_name = tc->report_name;
223*49cdfc7eSAndroid Build Coastguard Worker unsigned int mask_flags = tc->ondir | FAN_EVENT_ON_CHILD;
224*49cdfc7eSAndroid Build Coastguard Worker unsigned int parent_mask, ignore_mask, ignore = 0;
225*49cdfc7eSAndroid Build Coastguard Worker
226*49cdfc7eSAndroid Build Coastguard Worker /*
227*49cdfc7eSAndroid Build Coastguard Worker * The non-first groups may request events on children and
228*49cdfc7eSAndroid Build Coastguard Worker * subdirs only when setting an ignore mask on parent dir.
229*49cdfc7eSAndroid Build Coastguard Worker * The parent ignore mask may request to ignore events on
230*49cdfc7eSAndroid Build Coastguard Worker * children or subdirs.
231*49cdfc7eSAndroid Build Coastguard Worker */
232*49cdfc7eSAndroid Build Coastguard Worker if (i > 0) {
233*49cdfc7eSAndroid Build Coastguard Worker ignore = tc->ignore;
234*49cdfc7eSAndroid Build Coastguard Worker report_name = 0;
235*49cdfc7eSAndroid Build Coastguard Worker if (!ignore)
236*49cdfc7eSAndroid Build Coastguard Worker mask_flags = 0;
237*49cdfc7eSAndroid Build Coastguard Worker }
238*49cdfc7eSAndroid Build Coastguard Worker
239*49cdfc7eSAndroid Build Coastguard Worker fd_notify[i] = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | report_name |
240*49cdfc7eSAndroid Build Coastguard Worker FAN_NONBLOCK, O_RDONLY);
241*49cdfc7eSAndroid Build Coastguard Worker
242*49cdfc7eSAndroid Build Coastguard Worker /*
243*49cdfc7eSAndroid Build Coastguard Worker * Add subdir or mount mark for each group with CLOSE event,
244*49cdfc7eSAndroid Build Coastguard Worker * but only the first group requests events on dir.
245*49cdfc7eSAndroid Build Coastguard Worker */
246*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify[i],
247*49cdfc7eSAndroid Build Coastguard Worker FAN_MARK_ADD | mark->flag,
248*49cdfc7eSAndroid Build Coastguard Worker FAN_CLOSE_NOWRITE | mask_flags,
249*49cdfc7eSAndroid Build Coastguard Worker AT_FDCWD, tc->event_path);
250*49cdfc7eSAndroid Build Coastguard Worker
251*49cdfc7eSAndroid Build Coastguard Worker /*
252*49cdfc7eSAndroid Build Coastguard Worker * Add inode mark on parent for each group with MODIFY event,
253*49cdfc7eSAndroid Build Coastguard Worker * but only the first group requests events on child.
254*49cdfc7eSAndroid Build Coastguard Worker * The one mark with FAN_EVENT_ON_CHILD is needed for
255*49cdfc7eSAndroid Build Coastguard Worker * setting the DCACHE_FSNOTIFY_PARENT_WATCHED dentry flag.
256*49cdfc7eSAndroid Build Coastguard Worker *
257*49cdfc7eSAndroid Build Coastguard Worker * The inode mark on non-first group is either with FAN_MODIFY
258*49cdfc7eSAndroid Build Coastguard Worker * in mask or FAN_CLOSE_NOWRITE in ignore mask. In either case,
259*49cdfc7eSAndroid Build Coastguard Worker * it is not expected to get the modify event on a child, nor
260*49cdfc7eSAndroid Build Coastguard Worker * the close event on dir.
261*49cdfc7eSAndroid Build Coastguard Worker */
262*49cdfc7eSAndroid Build Coastguard Worker parent_mask = FAN_MODIFY | tc->ondir | mask_flags;
263*49cdfc7eSAndroid Build Coastguard Worker ignore_mask = FAN_CLOSE_NOWRITE | tc->ignore_flags;
264*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify[i], FAN_MARK_ADD | ignore,
265*49cdfc7eSAndroid Build Coastguard Worker ignore ? ignore_mask : parent_mask,
266*49cdfc7eSAndroid Build Coastguard Worker AT_FDCWD, ".");
267*49cdfc7eSAndroid Build Coastguard Worker }
268*49cdfc7eSAndroid Build Coastguard Worker }
269*49cdfc7eSAndroid Build Coastguard Worker
cleanup_fanotify_groups(void)270*49cdfc7eSAndroid Build Coastguard Worker static void cleanup_fanotify_groups(void)
271*49cdfc7eSAndroid Build Coastguard Worker {
272*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
273*49cdfc7eSAndroid Build Coastguard Worker
274*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < NUM_GROUPS; i++) {
275*49cdfc7eSAndroid Build Coastguard Worker if (fd_notify[i] > 0)
276*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_notify[i]);
277*49cdfc7eSAndroid Build Coastguard Worker }
278*49cdfc7eSAndroid Build Coastguard Worker }
279*49cdfc7eSAndroid Build Coastguard Worker
check_ignore_mask(int fd)280*49cdfc7eSAndroid Build Coastguard Worker static void check_ignore_mask(int fd)
281*49cdfc7eSAndroid Build Coastguard Worker {
282*49cdfc7eSAndroid Build Coastguard Worker unsigned int ignored_mask, mflags;
283*49cdfc7eSAndroid Build Coastguard Worker char procfdinfo[100];
284*49cdfc7eSAndroid Build Coastguard Worker
285*49cdfc7eSAndroid Build Coastguard Worker sprintf(procfdinfo, "/proc/%d/fdinfo/%d", (int)getpid(), fd);
286*49cdfc7eSAndroid Build Coastguard Worker if (FILE_LINES_SCANF(procfdinfo, "fanotify ino:%*x sdev:%*x mflags: %x mask:0 ignored_mask:%x",
287*49cdfc7eSAndroid Build Coastguard Worker &mflags, &ignored_mask) || !ignored_mask) {
288*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "The ignore mask did not survive");
289*49cdfc7eSAndroid Build Coastguard Worker } else {
290*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Found mark with ignore mask (ignored_mask=%x, mflags=%x) in %s",
291*49cdfc7eSAndroid Build Coastguard Worker ignored_mask, mflags, procfdinfo);
292*49cdfc7eSAndroid Build Coastguard Worker }
293*49cdfc7eSAndroid Build Coastguard Worker }
294*49cdfc7eSAndroid Build Coastguard Worker
event_res(int ttype,int group,struct fanotify_event_metadata * event,const char * filename)295*49cdfc7eSAndroid Build Coastguard Worker static void event_res(int ttype, int group,
296*49cdfc7eSAndroid Build Coastguard Worker struct fanotify_event_metadata *event,
297*49cdfc7eSAndroid Build Coastguard Worker const char *filename)
298*49cdfc7eSAndroid Build Coastguard Worker {
299*49cdfc7eSAndroid Build Coastguard Worker if (event->fd != FAN_NOFD) {
300*49cdfc7eSAndroid Build Coastguard Worker int len = 0;
301*49cdfc7eSAndroid Build Coastguard Worker
302*49cdfc7eSAndroid Build Coastguard Worker sprintf(symlnk, "/proc/self/fd/%d", event->fd);
303*49cdfc7eSAndroid Build Coastguard Worker len = readlink(symlnk, fdpath, sizeof(fdpath));
304*49cdfc7eSAndroid Build Coastguard Worker if (len < 0)
305*49cdfc7eSAndroid Build Coastguard Worker len = 0;
306*49cdfc7eSAndroid Build Coastguard Worker fdpath[len] = 0;
307*49cdfc7eSAndroid Build Coastguard Worker filename = fdpath;
308*49cdfc7eSAndroid Build Coastguard Worker }
309*49cdfc7eSAndroid Build Coastguard Worker
310*49cdfc7eSAndroid Build Coastguard Worker tst_res(ttype, "group %d got event: mask %llx pid=%u fd=%d filename=%s",
311*49cdfc7eSAndroid Build Coastguard Worker group, (unsigned long long)event->mask,
312*49cdfc7eSAndroid Build Coastguard Worker (unsigned int)event->pid, event->fd, filename);
313*49cdfc7eSAndroid Build Coastguard Worker }
314*49cdfc7eSAndroid Build Coastguard Worker
event_filename(struct fanotify_event_metadata * event)315*49cdfc7eSAndroid Build Coastguard Worker static const char *event_filename(struct fanotify_event_metadata *event)
316*49cdfc7eSAndroid Build Coastguard Worker {
317*49cdfc7eSAndroid Build Coastguard Worker struct fanotify_event_info_fid *event_fid;
318*49cdfc7eSAndroid Build Coastguard Worker struct file_handle *file_handle;
319*49cdfc7eSAndroid Build Coastguard Worker const char *filename, *end;
320*49cdfc7eSAndroid Build Coastguard Worker
321*49cdfc7eSAndroid Build Coastguard Worker if (event->event_len <= FAN_EVENT_METADATA_LEN)
322*49cdfc7eSAndroid Build Coastguard Worker return "";
323*49cdfc7eSAndroid Build Coastguard Worker
324*49cdfc7eSAndroid Build Coastguard Worker event_fid = (struct fanotify_event_info_fid *)(event + 1);
325*49cdfc7eSAndroid Build Coastguard Worker file_handle = (struct file_handle *)event_fid->handle;
326*49cdfc7eSAndroid Build Coastguard Worker filename = (char *)file_handle->f_handle + file_handle->handle_bytes;
327*49cdfc7eSAndroid Build Coastguard Worker end = (char *)event_fid + event_fid->hdr.len;
328*49cdfc7eSAndroid Build Coastguard Worker
329*49cdfc7eSAndroid Build Coastguard Worker /* End of event_fid could have name, zero padding, both or none */
330*49cdfc7eSAndroid Build Coastguard Worker return (filename == end) ? "" : filename;
331*49cdfc7eSAndroid Build Coastguard Worker }
332*49cdfc7eSAndroid Build Coastguard Worker
verify_event(int group,struct fanotify_event_metadata * event,uint32_t expect,const char * expect_filename)333*49cdfc7eSAndroid Build Coastguard Worker static void verify_event(int group, struct fanotify_event_metadata *event,
334*49cdfc7eSAndroid Build Coastguard Worker uint32_t expect, const char *expect_filename)
335*49cdfc7eSAndroid Build Coastguard Worker {
336*49cdfc7eSAndroid Build Coastguard Worker const char *filename = event_filename(event);
337*49cdfc7eSAndroid Build Coastguard Worker
338*49cdfc7eSAndroid Build Coastguard Worker if (event->mask != expect) {
339*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "group %d got event: mask %llx (expected %llx) "
340*49cdfc7eSAndroid Build Coastguard Worker "pid=%u fd=%d filename=%s", group, (unsigned long long)event->mask,
341*49cdfc7eSAndroid Build Coastguard Worker (unsigned long long)expect,
342*49cdfc7eSAndroid Build Coastguard Worker (unsigned int)event->pid, event->fd, filename);
343*49cdfc7eSAndroid Build Coastguard Worker } else if (event->pid != getpid()) {
344*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "group %d got event: mask %llx pid=%u "
345*49cdfc7eSAndroid Build Coastguard Worker "(expected %u) fd=%d filename=%s", group,
346*49cdfc7eSAndroid Build Coastguard Worker (unsigned long long)event->mask, (unsigned int)event->pid,
347*49cdfc7eSAndroid Build Coastguard Worker (unsigned int)getpid(), event->fd, filename);
348*49cdfc7eSAndroid Build Coastguard Worker } else if (strcmp(filename, expect_filename)) {
349*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "group %d got event: mask %llx pid=%u "
350*49cdfc7eSAndroid Build Coastguard Worker "fd=%d filename='%s' (expected '%s')", group,
351*49cdfc7eSAndroid Build Coastguard Worker (unsigned long long)event->mask, (unsigned int)event->pid,
352*49cdfc7eSAndroid Build Coastguard Worker event->fd, filename, expect_filename);
353*49cdfc7eSAndroid Build Coastguard Worker } else {
354*49cdfc7eSAndroid Build Coastguard Worker event_res(TPASS, group, event, filename);
355*49cdfc7eSAndroid Build Coastguard Worker }
356*49cdfc7eSAndroid Build Coastguard Worker if (event->fd != FAN_NOFD)
357*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(event->fd);
358*49cdfc7eSAndroid Build Coastguard Worker }
359*49cdfc7eSAndroid Build Coastguard Worker
close_event_fds(struct fanotify_event_metadata * event,int buflen)360*49cdfc7eSAndroid Build Coastguard Worker static void close_event_fds(struct fanotify_event_metadata *event, int buflen)
361*49cdfc7eSAndroid Build Coastguard Worker {
362*49cdfc7eSAndroid Build Coastguard Worker /* Close all file descriptors of read events */
363*49cdfc7eSAndroid Build Coastguard Worker for (; FAN_EVENT_OK(event, buflen); FAN_EVENT_NEXT(event, buflen)) {
364*49cdfc7eSAndroid Build Coastguard Worker if (event->fd != FAN_NOFD)
365*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(event->fd);
366*49cdfc7eSAndroid Build Coastguard Worker }
367*49cdfc7eSAndroid Build Coastguard Worker }
368*49cdfc7eSAndroid Build Coastguard Worker
test_fanotify(unsigned int n)369*49cdfc7eSAndroid Build Coastguard Worker static void test_fanotify(unsigned int n)
370*49cdfc7eSAndroid Build Coastguard Worker {
371*49cdfc7eSAndroid Build Coastguard Worker int ret, dirfd;
372*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
373*49cdfc7eSAndroid Build Coastguard Worker struct fanotify_event_metadata *event;
374*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
375*49cdfc7eSAndroid Build Coastguard Worker
376*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Test #%d: %s", n, tc->tname);
377*49cdfc7eSAndroid Build Coastguard Worker
378*49cdfc7eSAndroid Build Coastguard Worker if (fan_report_dfid_unsupported && tc->report_name) {
379*49cdfc7eSAndroid Build Coastguard Worker FANOTIFY_INIT_FLAGS_ERR_MSG(FAN_REPORT_DFID_NAME, fan_report_dfid_unsupported);
380*49cdfc7eSAndroid Build Coastguard Worker return;
381*49cdfc7eSAndroid Build Coastguard Worker }
382*49cdfc7eSAndroid Build Coastguard Worker
383*49cdfc7eSAndroid Build Coastguard Worker if (tc->ignore && tst_kvercmp(5, 19, 0) < 0) {
384*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "ignored mask on parent dir has undefined "
385*49cdfc7eSAndroid Build Coastguard Worker "behavior on kernel < 5.19");
386*49cdfc7eSAndroid Build Coastguard Worker return;
387*49cdfc7eSAndroid Build Coastguard Worker }
388*49cdfc7eSAndroid Build Coastguard Worker
389*49cdfc7eSAndroid Build Coastguard Worker if (ignore_mark_unsupported && tc->ignore & FAN_MARK_IGNORE) {
390*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "FAN_MARK_IGNORE not supported in kernel?");
391*49cdfc7eSAndroid Build Coastguard Worker return;
392*49cdfc7eSAndroid Build Coastguard Worker }
393*49cdfc7eSAndroid Build Coastguard Worker
394*49cdfc7eSAndroid Build Coastguard Worker create_fanotify_groups(tc);
395*49cdfc7eSAndroid Build Coastguard Worker
396*49cdfc7eSAndroid Build Coastguard Worker /*
397*49cdfc7eSAndroid Build Coastguard Worker * generate MODIFY event and no FAN_CLOSE_NOWRITE event.
398*49cdfc7eSAndroid Build Coastguard Worker */
399*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(fname, "1");
400*49cdfc7eSAndroid Build Coastguard Worker /*
401*49cdfc7eSAndroid Build Coastguard Worker * generate FAN_CLOSE_NOWRITE event on a child, subdir or "."
402*49cdfc7eSAndroid Build Coastguard Worker */
403*49cdfc7eSAndroid Build Coastguard Worker dirfd = SAFE_OPEN(tc->event_path, O_RDONLY);
404*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(dirfd);
405*49cdfc7eSAndroid Build Coastguard Worker
406*49cdfc7eSAndroid Build Coastguard Worker /*
407*49cdfc7eSAndroid Build Coastguard Worker * First verify the first group got the file MODIFY event and got just
408*49cdfc7eSAndroid Build Coastguard Worker * one FAN_CLOSE_NOWRITE event.
409*49cdfc7eSAndroid Build Coastguard Worker */
410*49cdfc7eSAndroid Build Coastguard Worker ret = read(fd_notify[0], event_buf, EVENT_BUF_LEN);
411*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0) {
412*49cdfc7eSAndroid Build Coastguard Worker if (errno == EAGAIN) {
413*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "first group did not get event");
414*49cdfc7eSAndroid Build Coastguard Worker } else {
415*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO,
416*49cdfc7eSAndroid Build Coastguard Worker "reading fanotify events failed");
417*49cdfc7eSAndroid Build Coastguard Worker }
418*49cdfc7eSAndroid Build Coastguard Worker }
419*49cdfc7eSAndroid Build Coastguard Worker event = (struct fanotify_event_metadata *)event_buf;
420*49cdfc7eSAndroid Build Coastguard Worker if (ret < tc->nevents * (int)FAN_EVENT_METADATA_LEN) {
421*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
422*49cdfc7eSAndroid Build Coastguard Worker "short read when reading fanotify events (%d < %d)",
423*49cdfc7eSAndroid Build Coastguard Worker ret, tc->nevents * (int)FAN_EVENT_METADATA_LEN);
424*49cdfc7eSAndroid Build Coastguard Worker }
425*49cdfc7eSAndroid Build Coastguard Worker if (FAN_EVENT_OK(event, ret)) {
426*49cdfc7eSAndroid Build Coastguard Worker verify_event(0, event, FAN_MODIFY, tc->report_name ? fname : "");
427*49cdfc7eSAndroid Build Coastguard Worker event = FAN_EVENT_NEXT(event, ret);
428*49cdfc7eSAndroid Build Coastguard Worker }
429*49cdfc7eSAndroid Build Coastguard Worker if (tc->nevents > 1 && FAN_EVENT_OK(event, ret)) {
430*49cdfc7eSAndroid Build Coastguard Worker verify_event(0, event, FAN_CLOSE_NOWRITE,
431*49cdfc7eSAndroid Build Coastguard Worker tc->report_name ? (tc->ondir ? "." : tc->event_path) : "");
432*49cdfc7eSAndroid Build Coastguard Worker event = FAN_EVENT_NEXT(event, ret);
433*49cdfc7eSAndroid Build Coastguard Worker }
434*49cdfc7eSAndroid Build Coastguard Worker if (ret > 0) {
435*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
436*49cdfc7eSAndroid Build Coastguard Worker "first group got more than %d events (%d bytes)",
437*49cdfc7eSAndroid Build Coastguard Worker tc->nevents, ret);
438*49cdfc7eSAndroid Build Coastguard Worker }
439*49cdfc7eSAndroid Build Coastguard Worker close_event_fds(event, ret);
440*49cdfc7eSAndroid Build Coastguard Worker
441*49cdfc7eSAndroid Build Coastguard Worker /*
442*49cdfc7eSAndroid Build Coastguard Worker * Then verify the rest of the groups did not get the MODIFY event and
443*49cdfc7eSAndroid Build Coastguard Worker * got the FAN_CLOSE_NOWRITE event only on a non-directory.
444*49cdfc7eSAndroid Build Coastguard Worker */
445*49cdfc7eSAndroid Build Coastguard Worker for (i = 1; i < NUM_GROUPS; i++) {
446*49cdfc7eSAndroid Build Coastguard Worker /*
447*49cdfc7eSAndroid Build Coastguard Worker * Verify that ignore mask survived the modify event on child,
448*49cdfc7eSAndroid Build Coastguard Worker * which was not supposed to be sent to this group.
449*49cdfc7eSAndroid Build Coastguard Worker */
450*49cdfc7eSAndroid Build Coastguard Worker if (tc->ignore)
451*49cdfc7eSAndroid Build Coastguard Worker check_ignore_mask(fd_notify[i]);
452*49cdfc7eSAndroid Build Coastguard Worker
453*49cdfc7eSAndroid Build Coastguard Worker ret = read(fd_notify[i], event_buf, EVENT_BUF_LEN);
454*49cdfc7eSAndroid Build Coastguard Worker if (ret > 0) {
455*49cdfc7eSAndroid Build Coastguard Worker event = (struct fanotify_event_metadata *)event_buf;
456*49cdfc7eSAndroid Build Coastguard Worker verify_event(i, event, tc->nonfirst_event, "");
457*49cdfc7eSAndroid Build Coastguard Worker event = FAN_EVENT_NEXT(event, ret);
458*49cdfc7eSAndroid Build Coastguard Worker
459*49cdfc7eSAndroid Build Coastguard Worker close_event_fds(event, ret);
460*49cdfc7eSAndroid Build Coastguard Worker continue;
461*49cdfc7eSAndroid Build Coastguard Worker }
462*49cdfc7eSAndroid Build Coastguard Worker
463*49cdfc7eSAndroid Build Coastguard Worker if (ret == 0) {
464*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "group %d zero length read from fanotify fd", i);
465*49cdfc7eSAndroid Build Coastguard Worker continue;
466*49cdfc7eSAndroid Build Coastguard Worker }
467*49cdfc7eSAndroid Build Coastguard Worker
468*49cdfc7eSAndroid Build Coastguard Worker if (errno != EAGAIN) {
469*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO,
470*49cdfc7eSAndroid Build Coastguard Worker "reading fanotify events failed");
471*49cdfc7eSAndroid Build Coastguard Worker }
472*49cdfc7eSAndroid Build Coastguard Worker
473*49cdfc7eSAndroid Build Coastguard Worker if (tc->nonfirst_event)
474*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "group %d expected and got no event", i);
475*49cdfc7eSAndroid Build Coastguard Worker else
476*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "group %d got no event as expected", i);
477*49cdfc7eSAndroid Build Coastguard Worker }
478*49cdfc7eSAndroid Build Coastguard Worker cleanup_fanotify_groups();
479*49cdfc7eSAndroid Build Coastguard Worker }
480*49cdfc7eSAndroid Build Coastguard Worker
setup(void)481*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
482*49cdfc7eSAndroid Build Coastguard Worker {
483*49cdfc7eSAndroid Build Coastguard Worker fan_report_dfid_unsupported = fanotify_flags_supported_on_fs(FAN_REPORT_DFID_NAME,
484*49cdfc7eSAndroid Build Coastguard Worker FAN_MARK_MOUNT,
485*49cdfc7eSAndroid Build Coastguard Worker FAN_OPEN, MOUNT_PATH);
486*49cdfc7eSAndroid Build Coastguard Worker ignore_mark_unsupported = fanotify_mark_supported_on_fs(FAN_MARK_IGNORE_SURV,
487*49cdfc7eSAndroid Build Coastguard Worker MOUNT_PATH);
488*49cdfc7eSAndroid Build Coastguard Worker
489*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(MOUNT_NAME, 0755);
490*49cdfc7eSAndroid Build Coastguard Worker SAFE_MOUNT(MOUNT_PATH, MOUNT_NAME, "none", MS_BIND, NULL);
491*49cdfc7eSAndroid Build Coastguard Worker mount_created = 1;
492*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHDIR(MOUNT_NAME);
493*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(DIR_NAME, 0755);
494*49cdfc7eSAndroid Build Coastguard Worker
495*49cdfc7eSAndroid Build Coastguard Worker sprintf(fname, "tfile_%d", getpid());
496*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(fname, "1");
497*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(FILE2_NAME, "1");
498*49cdfc7eSAndroid Build Coastguard Worker }
499*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)500*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
501*49cdfc7eSAndroid Build Coastguard Worker {
502*49cdfc7eSAndroid Build Coastguard Worker cleanup_fanotify_groups();
503*49cdfc7eSAndroid Build Coastguard Worker
504*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHDIR("../");
505*49cdfc7eSAndroid Build Coastguard Worker
506*49cdfc7eSAndroid Build Coastguard Worker if (mount_created)
507*49cdfc7eSAndroid Build Coastguard Worker SAFE_UMOUNT(MOUNT_NAME);
508*49cdfc7eSAndroid Build Coastguard Worker }
509*49cdfc7eSAndroid Build Coastguard Worker
510*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
511*49cdfc7eSAndroid Build Coastguard Worker .test = test_fanotify,
512*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
513*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
514*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
515*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
516*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MOUNT_PATH,
517*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
518*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
519*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "54a307ba8d3c"},
520*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "b469e7e47c8a"},
521*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "55bf882c7f13"},
522*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "7372e79c9eb9"},
523*49cdfc7eSAndroid Build Coastguard Worker {}
524*49cdfc7eSAndroid Build Coastguard Worker }
525*49cdfc7eSAndroid Build Coastguard Worker };
526*49cdfc7eSAndroid Build Coastguard Worker
527*49cdfc7eSAndroid Build Coastguard Worker #else
528*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("system doesn't have required fanotify support");
529*49cdfc7eSAndroid Build Coastguard Worker #endif
530