xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fanotify/fanotify03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) 2013 SUSE.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Started by Jan Kara <[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 permission events work.
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
14*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_FANOTIFY_H
29*49cdfc7eSAndroid Build Coastguard Worker # include "fanotify.h"
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_MAX 1024
32*49cdfc7eSAndroid Build Coastguard Worker /* size of the event structure, not counting name */
33*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SIZE  (sizeof(struct fanotify_event_metadata))
34*49cdfc7eSAndroid Build Coastguard Worker /* reasonable guess as to size of 1024 events */
35*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_BUF_LEN        (EVENT_MAX * EVENT_SIZE)
36*49cdfc7eSAndroid Build Coastguard Worker /* Size large enough to hold a reasonable amount of expected event objects */
37*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SET_MAX 16
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
40*49cdfc7eSAndroid Build Coastguard Worker #define TST_TOTAL 3
41*49cdfc7eSAndroid Build Coastguard Worker #define TEST_APP "fanotify_child"
42*49cdfc7eSAndroid Build Coastguard Worker #define MOUNT_PATH "fs_mnt"
43*49cdfc7eSAndroid Build Coastguard Worker #define FILE_EXEC_PATH MOUNT_PATH"/"TEST_APP
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker static char fname[BUF_SIZE];
46*49cdfc7eSAndroid Build Coastguard Worker static char buf[BUF_SIZE];
47*49cdfc7eSAndroid Build Coastguard Worker static volatile int fd_notify;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static pid_t child_pid;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker static char event_buf[EVENT_BUF_LEN];
52*49cdfc7eSAndroid Build Coastguard Worker static int exec_events_unsupported;
53*49cdfc7eSAndroid Build Coastguard Worker static int filesystem_mark_unsupported;
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker struct event {
56*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long long mask;
57*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int response;
58*49cdfc7eSAndroid Build Coastguard Worker };
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
61*49cdfc7eSAndroid Build Coastguard Worker 	const char *tname;
62*49cdfc7eSAndroid Build Coastguard Worker 	struct fanotify_mark_type mark;
63*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long long mask;
64*49cdfc7eSAndroid Build Coastguard Worker 	int event_count;
65*49cdfc7eSAndroid Build Coastguard Worker 	struct event event_set[EVENT_SET_MAX];
66*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
67*49cdfc7eSAndroid Build Coastguard Worker 	{
68*49cdfc7eSAndroid Build Coastguard Worker 		"inode mark, FAN_OPEN_PERM | FAN_ACCESS_PERM events",
69*49cdfc7eSAndroid Build Coastguard Worker 		INIT_FANOTIFY_MARK_TYPE(INODE),
70*49cdfc7eSAndroid Build Coastguard Worker 		FAN_OPEN_PERM | FAN_ACCESS_PERM, 3,
71*49cdfc7eSAndroid Build Coastguard Worker 		{
72*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_OPEN_PERM, FAN_ALLOW},
73*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_ACCESS_PERM, FAN_DENY},
74*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_OPEN_PERM, FAN_DENY}
75*49cdfc7eSAndroid Build Coastguard Worker 		}
76*49cdfc7eSAndroid Build Coastguard Worker 	},
77*49cdfc7eSAndroid Build Coastguard Worker 	{
78*49cdfc7eSAndroid Build Coastguard Worker 		"inode mark, FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM events",
79*49cdfc7eSAndroid Build Coastguard Worker 		INIT_FANOTIFY_MARK_TYPE(INODE),
80*49cdfc7eSAndroid Build Coastguard Worker 		FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM, 2,
81*49cdfc7eSAndroid Build Coastguard Worker 		{
82*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_ACCESS_PERM, FAN_DENY},
83*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_OPEN_EXEC_PERM, FAN_DENY}
84*49cdfc7eSAndroid Build Coastguard Worker 		}
85*49cdfc7eSAndroid Build Coastguard Worker 	},
86*49cdfc7eSAndroid Build Coastguard Worker 	{
87*49cdfc7eSAndroid Build Coastguard Worker 		"mount mark, FAN_OPEN_PERM | FAN_ACCESS_PERM events",
88*49cdfc7eSAndroid Build Coastguard Worker 		INIT_FANOTIFY_MARK_TYPE(MOUNT),
89*49cdfc7eSAndroid Build Coastguard Worker 		FAN_OPEN_PERM | FAN_ACCESS_PERM, 3,
90*49cdfc7eSAndroid Build Coastguard Worker 		{
91*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_OPEN_PERM, FAN_ALLOW},
92*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_ACCESS_PERM, FAN_DENY},
93*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_OPEN_PERM, FAN_DENY}
94*49cdfc7eSAndroid Build Coastguard Worker 		}
95*49cdfc7eSAndroid Build Coastguard Worker 	},
96*49cdfc7eSAndroid Build Coastguard Worker 	{
97*49cdfc7eSAndroid Build Coastguard Worker 		"mount mark, FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM events",
98*49cdfc7eSAndroid Build Coastguard Worker 		INIT_FANOTIFY_MARK_TYPE(MOUNT),
99*49cdfc7eSAndroid Build Coastguard Worker 		FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM, 2,
100*49cdfc7eSAndroid Build Coastguard Worker 		{
101*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_ACCESS_PERM, FAN_DENY},
102*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_OPEN_EXEC_PERM, FAN_DENY}
103*49cdfc7eSAndroid Build Coastguard Worker 		}
104*49cdfc7eSAndroid Build Coastguard Worker 	},
105*49cdfc7eSAndroid Build Coastguard Worker 	{
106*49cdfc7eSAndroid Build Coastguard Worker 		"filesystem mark, FAN_OPEN_PERM | FAN_ACCESS_PERM events",
107*49cdfc7eSAndroid Build Coastguard Worker 		INIT_FANOTIFY_MARK_TYPE(FILESYSTEM),
108*49cdfc7eSAndroid Build Coastguard Worker 		FAN_OPEN_PERM | FAN_ACCESS_PERM, 3,
109*49cdfc7eSAndroid Build Coastguard Worker 		{
110*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_OPEN_PERM, FAN_ALLOW},
111*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_ACCESS_PERM, FAN_DENY},
112*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_OPEN_PERM, FAN_DENY}
113*49cdfc7eSAndroid Build Coastguard Worker 		}
114*49cdfc7eSAndroid Build Coastguard Worker 	},
115*49cdfc7eSAndroid Build Coastguard Worker 	{
116*49cdfc7eSAndroid Build Coastguard Worker 		"filesystem mark, FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM events",
117*49cdfc7eSAndroid Build Coastguard Worker 		INIT_FANOTIFY_MARK_TYPE(FILESYSTEM),
118*49cdfc7eSAndroid Build Coastguard Worker 		FAN_ACCESS_PERM | FAN_OPEN_EXEC_PERM, 2,
119*49cdfc7eSAndroid Build Coastguard Worker 		{
120*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_ACCESS_PERM, FAN_DENY},
121*49cdfc7eSAndroid Build Coastguard Worker 			{FAN_OPEN_EXEC_PERM, FAN_DENY}
122*49cdfc7eSAndroid Build Coastguard Worker 		}
123*49cdfc7eSAndroid Build Coastguard Worker 	},
124*49cdfc7eSAndroid Build Coastguard Worker };
125*49cdfc7eSAndroid Build Coastguard Worker 
generate_events(void)126*49cdfc7eSAndroid Build Coastguard Worker static void generate_events(void)
127*49cdfc7eSAndroid Build Coastguard Worker {
128*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
129*49cdfc7eSAndroid Build Coastguard Worker 	char *const argv[] = {FILE_EXEC_PATH, NULL};
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	/*
132*49cdfc7eSAndroid Build Coastguard Worker 	 * Generate sequence of events
133*49cdfc7eSAndroid Build Coastguard Worker 	 */
134*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700);
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ANY, fd, fname, 1);
137*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(fd, 0, SEEK_SET);
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker 	if (read(fd, buf, BUF_SIZE) != -1)
140*49cdfc7eSAndroid Build Coastguard Worker 		exit(3);
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	if (execve(FILE_EXEC_PATH, argv, environ) != -1)
145*49cdfc7eSAndroid Build Coastguard Worker 		exit(5);
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker 
child_handler(int tmp)148*49cdfc7eSAndroid Build Coastguard Worker static void child_handler(int tmp)
149*49cdfc7eSAndroid Build Coastguard Worker {
150*49cdfc7eSAndroid Build Coastguard Worker 	(void)tmp;
151*49cdfc7eSAndroid Build Coastguard Worker 	/*
152*49cdfc7eSAndroid Build Coastguard Worker 	 * Close notification fd so that we cannot block while reading
153*49cdfc7eSAndroid Build Coastguard Worker 	 * from it
154*49cdfc7eSAndroid Build Coastguard Worker 	 */
155*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd_notify);
156*49cdfc7eSAndroid Build Coastguard Worker 	fd_notify = -1;
157*49cdfc7eSAndroid Build Coastguard Worker }
158*49cdfc7eSAndroid Build Coastguard Worker 
run_child(void)159*49cdfc7eSAndroid Build Coastguard Worker static void run_child(void)
160*49cdfc7eSAndroid Build Coastguard Worker {
161*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction child_action;
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 	child_action.sa_handler = child_handler;
164*49cdfc7eSAndroid Build Coastguard Worker 	sigemptyset(&child_action.sa_mask);
165*49cdfc7eSAndroid Build Coastguard Worker 	child_action.sa_flags = SA_NOCLDSTOP;
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGCHLD, &child_action, NULL) < 0) {
168*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO,
169*49cdfc7eSAndroid Build Coastguard Worker 			"sigaction(SIGCHLD, &child_action, NULL) failed");
170*49cdfc7eSAndroid Build Coastguard Worker 	}
171*49cdfc7eSAndroid Build Coastguard Worker 
172*49cdfc7eSAndroid Build Coastguard Worker 	child_pid = SAFE_FORK();
173*49cdfc7eSAndroid Build Coastguard Worker 
174*49cdfc7eSAndroid Build Coastguard Worker 	if (child_pid == 0) {
175*49cdfc7eSAndroid Build Coastguard Worker 		/* Child will generate events now */
176*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_notify);
177*49cdfc7eSAndroid Build Coastguard Worker 		generate_events();
178*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
179*49cdfc7eSAndroid Build Coastguard Worker 	}
180*49cdfc7eSAndroid Build Coastguard Worker }
181*49cdfc7eSAndroid Build Coastguard Worker 
check_child(void)182*49cdfc7eSAndroid Build Coastguard Worker static void check_child(void)
183*49cdfc7eSAndroid Build Coastguard Worker {
184*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction child_action;
185*49cdfc7eSAndroid Build Coastguard Worker 	int child_ret;
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker 	child_action.sa_handler = SIG_IGN;
188*49cdfc7eSAndroid Build Coastguard Worker 	sigemptyset(&child_action.sa_mask);
189*49cdfc7eSAndroid Build Coastguard Worker 	child_action.sa_flags = SA_NOCLDSTOP;
190*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGCHLD, &child_action, NULL) < 0) {
191*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO,
192*49cdfc7eSAndroid Build Coastguard Worker 			"sigaction(SIGCHLD, &child_action, NULL) failed");
193*49cdfc7eSAndroid Build Coastguard Worker 	}
194*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(-1, &child_ret, 0);
195*49cdfc7eSAndroid Build Coastguard Worker 
196*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFEXITED(child_ret) && WEXITSTATUS(child_ret) == 0)
197*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "child exited correctly");
198*49cdfc7eSAndroid Build Coastguard Worker 	else
199*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "child %s", tst_strstatus(child_ret));
200*49cdfc7eSAndroid Build Coastguard Worker }
201*49cdfc7eSAndroid Build Coastguard Worker 
setup_mark(unsigned int n)202*49cdfc7eSAndroid Build Coastguard Worker static int setup_mark(unsigned int n)
203*49cdfc7eSAndroid Build Coastguard Worker {
204*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i = 0;
205*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
206*49cdfc7eSAndroid Build Coastguard Worker 	struct fanotify_mark_type *mark = &tc->mark;
207*49cdfc7eSAndroid Build Coastguard Worker 	char *const files[] = {fname, FILE_EXEC_PATH};
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Test #%d: %s", n, tc->tname);
210*49cdfc7eSAndroid Build Coastguard Worker 
211*49cdfc7eSAndroid Build Coastguard Worker 	if (exec_events_unsupported && tc->mask & FAN_OPEN_EXEC_PERM) {
212*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "FAN_OPEN_EXEC_PERM not supported in kernel?");
213*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
214*49cdfc7eSAndroid Build Coastguard Worker 	}
215*49cdfc7eSAndroid Build Coastguard Worker 
216*49cdfc7eSAndroid Build Coastguard Worker 	if (filesystem_mark_unsupported && mark->flag == FAN_MARK_FILESYSTEM) {
217*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "FAN_MARK_FILESYSTEM not supported in kernel?");
218*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
219*49cdfc7eSAndroid Build Coastguard Worker 	}
220*49cdfc7eSAndroid Build Coastguard Worker 
221*49cdfc7eSAndroid Build Coastguard Worker 	fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_CONTENT, O_RDONLY);
222*49cdfc7eSAndroid Build Coastguard Worker 
223*49cdfc7eSAndroid Build Coastguard Worker 	for (; i < ARRAY_SIZE(files); i++) {
224*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | mark->flag,
225*49cdfc7eSAndroid Build Coastguard Worker 				  tc->mask, AT_FDCWD, files[i]);
226*49cdfc7eSAndroid Build Coastguard Worker 	}
227*49cdfc7eSAndroid Build Coastguard Worker 
228*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
229*49cdfc7eSAndroid Build Coastguard Worker }
230*49cdfc7eSAndroid Build Coastguard Worker 
test_fanotify(unsigned int n)231*49cdfc7eSAndroid Build Coastguard Worker static void test_fanotify(unsigned int n)
232*49cdfc7eSAndroid Build Coastguard Worker {
233*49cdfc7eSAndroid Build Coastguard Worker 	int ret, len = 0, i = 0, test_num = 0;
234*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
235*49cdfc7eSAndroid Build Coastguard Worker 	struct event *event_set = tc->event_set;
236*49cdfc7eSAndroid Build Coastguard Worker 
237*49cdfc7eSAndroid Build Coastguard Worker 	if (setup_mark(n) != 0)
238*49cdfc7eSAndroid Build Coastguard Worker 		return;
239*49cdfc7eSAndroid Build Coastguard Worker 
240*49cdfc7eSAndroid Build Coastguard Worker 	run_child();
241*49cdfc7eSAndroid Build Coastguard Worker 
242*49cdfc7eSAndroid Build Coastguard Worker 	/*
243*49cdfc7eSAndroid Build Coastguard Worker 	 * Process events
244*49cdfc7eSAndroid Build Coastguard Worker 	 *
245*49cdfc7eSAndroid Build Coastguard Worker 	 * tc->count + 1 is to accommodate for checking the child process
246*49cdfc7eSAndroid Build Coastguard Worker 	 * return value
247*49cdfc7eSAndroid Build Coastguard Worker 	 */
248*49cdfc7eSAndroid Build Coastguard Worker 	while (test_num < tc->event_count + 1 && fd_notify != -1) {
249*49cdfc7eSAndroid Build Coastguard Worker 		struct fanotify_event_metadata *event;
250*49cdfc7eSAndroid Build Coastguard Worker 
251*49cdfc7eSAndroid Build Coastguard Worker 		if (i == len) {
252*49cdfc7eSAndroid Build Coastguard Worker 			/* Get more events */
253*49cdfc7eSAndroid Build Coastguard Worker 			ret = read(fd_notify, event_buf + len,
254*49cdfc7eSAndroid Build Coastguard Worker 				   EVENT_BUF_LEN - len);
255*49cdfc7eSAndroid Build Coastguard Worker 			if (fd_notify == -1)
256*49cdfc7eSAndroid Build Coastguard Worker 				break;
257*49cdfc7eSAndroid Build Coastguard Worker 			if (ret < 0) {
258*49cdfc7eSAndroid Build Coastguard Worker 				tst_brk(TBROK,
259*49cdfc7eSAndroid Build Coastguard Worker 					"read(%d, buf, %zu) failed",
260*49cdfc7eSAndroid Build Coastguard Worker 					fd_notify, EVENT_BUF_LEN);
261*49cdfc7eSAndroid Build Coastguard Worker 			}
262*49cdfc7eSAndroid Build Coastguard Worker 			len += ret;
263*49cdfc7eSAndroid Build Coastguard Worker 		}
264*49cdfc7eSAndroid Build Coastguard Worker 
265*49cdfc7eSAndroid Build Coastguard Worker 		event = (struct fanotify_event_metadata *)&event_buf[i];
266*49cdfc7eSAndroid Build Coastguard Worker 		/* Permission events cannot be merged, so the event mask
267*49cdfc7eSAndroid Build Coastguard Worker 		 * reported should exactly match the event mask within the
268*49cdfc7eSAndroid Build Coastguard Worker 		 * event set.
269*49cdfc7eSAndroid Build Coastguard Worker 		 */
270*49cdfc7eSAndroid Build Coastguard Worker 		if (event->mask != event_set[test_num].mask) {
271*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL,
272*49cdfc7eSAndroid Build Coastguard Worker 				"got event: mask=%llx (expected %llx) "
273*49cdfc7eSAndroid Build Coastguard Worker 				"pid=%u fd=%d",
274*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned long long)event->mask,
275*49cdfc7eSAndroid Build Coastguard Worker 				event_set[test_num].mask,
276*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned int)event->pid, event->fd);
277*49cdfc7eSAndroid Build Coastguard Worker 		} else if (event->pid != child_pid) {
278*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL,
279*49cdfc7eSAndroid Build Coastguard Worker 				"got event: mask=%llx pid=%u "
280*49cdfc7eSAndroid Build Coastguard Worker 				"(expected %u) fd=%d",
281*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned long long)event->mask,
282*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned int)event->pid,
283*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned int)child_pid,
284*49cdfc7eSAndroid Build Coastguard Worker 				event->fd);
285*49cdfc7eSAndroid Build Coastguard Worker 		} else {
286*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS,
287*49cdfc7eSAndroid Build Coastguard Worker 				"got event: mask=%llx pid=%u fd=%d",
288*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned long long)event->mask,
289*49cdfc7eSAndroid Build Coastguard Worker 				(unsigned int)event->pid, event->fd);
290*49cdfc7eSAndroid Build Coastguard Worker 		}
291*49cdfc7eSAndroid Build Coastguard Worker 
292*49cdfc7eSAndroid Build Coastguard Worker 		/* Write response to the permission event */
293*49cdfc7eSAndroid Build Coastguard Worker 		if (event_set[test_num].mask & LTP_ALL_PERM_EVENTS) {
294*49cdfc7eSAndroid Build Coastguard Worker 			struct fanotify_response resp;
295*49cdfc7eSAndroid Build Coastguard Worker 
296*49cdfc7eSAndroid Build Coastguard Worker 			resp.fd = event->fd;
297*49cdfc7eSAndroid Build Coastguard Worker 			resp.response = event_set[test_num].response;
298*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_WRITE(SAFE_WRITE_ALL, fd_notify, &resp, sizeof(resp));
299*49cdfc7eSAndroid Build Coastguard Worker 		}
300*49cdfc7eSAndroid Build Coastguard Worker 
301*49cdfc7eSAndroid Build Coastguard Worker 		i += event->event_len;
302*49cdfc7eSAndroid Build Coastguard Worker 
303*49cdfc7eSAndroid Build Coastguard Worker 		if (event->fd != FAN_NOFD)
304*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_CLOSE(event->fd);
305*49cdfc7eSAndroid Build Coastguard Worker 
306*49cdfc7eSAndroid Build Coastguard Worker 		test_num++;
307*49cdfc7eSAndroid Build Coastguard Worker 	}
308*49cdfc7eSAndroid Build Coastguard Worker 
309*49cdfc7eSAndroid Build Coastguard Worker 	for (; test_num < tc->event_count; test_num++) {
310*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "didn't get event: mask=%llx",
311*49cdfc7eSAndroid Build Coastguard Worker 			event_set[test_num].mask);
312*49cdfc7eSAndroid Build Coastguard Worker 
313*49cdfc7eSAndroid Build Coastguard Worker 	}
314*49cdfc7eSAndroid Build Coastguard Worker 
315*49cdfc7eSAndroid Build Coastguard Worker 	check_child();
316*49cdfc7eSAndroid Build Coastguard Worker 
317*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_notify > 0)
318*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_notify);
319*49cdfc7eSAndroid Build Coastguard Worker }
320*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)321*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
322*49cdfc7eSAndroid Build Coastguard Worker {
323*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(fname, MOUNT_PATH"/fname_%d", getpid());
324*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(fname, "1");
325*49cdfc7eSAndroid Build Coastguard Worker 
326*49cdfc7eSAndroid Build Coastguard Worker 	require_fanotify_access_permissions_supported_on_fs(fname);
327*49cdfc7eSAndroid Build Coastguard Worker 	filesystem_mark_unsupported = fanotify_mark_supported_on_fs(FAN_MARK_FILESYSTEM, fname);
328*49cdfc7eSAndroid Build Coastguard Worker 	exec_events_unsupported = fanotify_flags_supported_on_fs(FAN_CLASS_CONTENT,
329*49cdfc7eSAndroid Build Coastguard Worker 					0, FAN_OPEN_EXEC_PERM, fname);
330*49cdfc7eSAndroid Build Coastguard Worker 
331*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CP(TEST_APP, FILE_EXEC_PATH);
332*49cdfc7eSAndroid Build Coastguard Worker }
333*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)334*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
335*49cdfc7eSAndroid Build Coastguard Worker {
336*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_notify > 0)
337*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_notify);
338*49cdfc7eSAndroid Build Coastguard Worker }
339*49cdfc7eSAndroid Build Coastguard Worker 
340*49cdfc7eSAndroid Build Coastguard Worker static const char *const resource_files[] = {
341*49cdfc7eSAndroid Build Coastguard Worker 	TEST_APP,
342*49cdfc7eSAndroid Build Coastguard Worker 	NULL
343*49cdfc7eSAndroid Build Coastguard Worker };
344*49cdfc7eSAndroid Build Coastguard Worker 
345*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
346*49cdfc7eSAndroid Build Coastguard Worker 	.test = test_fanotify,
347*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
348*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
349*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
350*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
351*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
352*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
353*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MOUNT_PATH,
354*49cdfc7eSAndroid Build Coastguard Worker 	.resource_files = resource_files
355*49cdfc7eSAndroid Build Coastguard Worker };
356*49cdfc7eSAndroid Build Coastguard Worker 
357*49cdfc7eSAndroid Build Coastguard Worker #else
358*49cdfc7eSAndroid Build Coastguard Worker 	TST_TEST_TCONF("system doesn't have required fanotify support");
359*49cdfc7eSAndroid Build Coastguard Worker #endif
360