xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/inotify/inotify10.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) 2020 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  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Check that event is reported to watching parent and watching child
12*49cdfc7eSAndroid Build Coastguard Worker  * based on their interest.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * Test case #3 is a regression test for commit fecc4559780d that fixes
15*49cdfc7eSAndroid Build Coastguard Worker  * a bug introduced in kernel v5.9:
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  * fecc4559780d ("fsnotify: fix events reported to watching parent and child").
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_INOTIFY_H)
23*49cdfc7eSAndroid Build Coastguard Worker # include <sys/inotify.h>
24*49cdfc7eSAndroid Build Coastguard Worker #endif
25*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "inotify.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_INOTIFY_H)
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_MAX 10
33*49cdfc7eSAndroid Build Coastguard Worker /* Size of the event structure, not including the name */
34*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SIZE  (sizeof(struct inotify_event))
35*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_BUF_LEN        (EVENT_MAX * (EVENT_SIZE + 16))
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker struct event_t {
41*49cdfc7eSAndroid Build Coastguard Worker 	char name[BUF_SIZE];
42*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int mask;
43*49cdfc7eSAndroid Build Coastguard Worker 	int wd;
44*49cdfc7eSAndroid Build Coastguard Worker };
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker #define	TEST_DIR	"test_dir"
47*49cdfc7eSAndroid Build Coastguard Worker #define	TEST_FILE	"test_file"
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
50*49cdfc7eSAndroid Build Coastguard Worker 	const char *tname;
51*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int parent_mask;
52*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int subdir_mask;
53*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int child_mask;
54*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int parent_mask_other;
55*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int subdir_mask_other;
56*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int child_mask_other;
57*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
58*49cdfc7eSAndroid Build Coastguard Worker 	{
59*49cdfc7eSAndroid Build Coastguard Worker 		"Group with parent and child watches",
60*49cdfc7eSAndroid Build Coastguard Worker 		IN_ATTRIB, IN_ATTRIB, IN_ATTRIB,
61*49cdfc7eSAndroid Build Coastguard Worker 		0, 0, 0,
62*49cdfc7eSAndroid Build Coastguard Worker 	},
63*49cdfc7eSAndroid Build Coastguard Worker 	{
64*49cdfc7eSAndroid Build Coastguard Worker 		"Group with child watches and other group with parent watch",
65*49cdfc7eSAndroid Build Coastguard Worker 		0, IN_ATTRIB, IN_ATTRIB,
66*49cdfc7eSAndroid Build Coastguard Worker 		IN_ATTRIB, 0, 0,
67*49cdfc7eSAndroid Build Coastguard Worker 	},
68*49cdfc7eSAndroid Build Coastguard Worker 	{
69*49cdfc7eSAndroid Build Coastguard Worker 		"Group with parent watch and other group with child watches",
70*49cdfc7eSAndroid Build Coastguard Worker 		IN_ATTRIB, 0, 0,
71*49cdfc7eSAndroid Build Coastguard Worker 		0, IN_ATTRIB, IN_ATTRIB,
72*49cdfc7eSAndroid Build Coastguard Worker 	},
73*49cdfc7eSAndroid Build Coastguard Worker 	{
74*49cdfc7eSAndroid Build Coastguard Worker 		"Two Groups with parent and child watches for different events",
75*49cdfc7eSAndroid Build Coastguard Worker 		IN_ATTRIB, IN_OPEN, IN_OPEN,
76*49cdfc7eSAndroid Build Coastguard Worker 		IN_OPEN, IN_ATTRIB, IN_ATTRIB,
77*49cdfc7eSAndroid Build Coastguard Worker 	},
78*49cdfc7eSAndroid Build Coastguard Worker };
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker struct event_t event_set[EVENT_MAX];
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker char event_buf[EVENT_BUF_LEN];
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker int fd_notify, fd_notify_other;
85*49cdfc7eSAndroid Build Coastguard Worker 
verify_inotify(unsigned int n)86*49cdfc7eSAndroid Build Coastguard Worker static void verify_inotify(unsigned int n)
87*49cdfc7eSAndroid Build Coastguard Worker {
88*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
89*49cdfc7eSAndroid Build Coastguard Worker 	int i = 0, test_num = 0, len;
90*49cdfc7eSAndroid Build Coastguard Worker 	int wd_parent = 0, wd_subdir = 0, wd_child = 0;
91*49cdfc7eSAndroid Build Coastguard Worker 	int test_cnt = 0;
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Test #%d: %s", n, tc->tname);
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	fd_notify = SAFE_MYINOTIFY_INIT();
96*49cdfc7eSAndroid Build Coastguard Worker 	fd_notify_other = SAFE_MYINOTIFY_INIT();
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	/* Setup watches on parent dir and children */
99*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->parent_mask)
100*49cdfc7eSAndroid Build Coastguard Worker 		wd_parent = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, ".", tc->parent_mask);
101*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->subdir_mask)
102*49cdfc7eSAndroid Build Coastguard Worker 		wd_subdir = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, TEST_DIR, tc->subdir_mask);
103*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->child_mask)
104*49cdfc7eSAndroid Build Coastguard Worker 		wd_child = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, TEST_FILE, tc->child_mask);
105*49cdfc7eSAndroid Build Coastguard Worker 	/*
106*49cdfc7eSAndroid Build Coastguard Worker 	 * Setup watches on "other" group to verify no intereferecne with our group.
107*49cdfc7eSAndroid Build Coastguard Worker 	 * We do not check events reported to the "other" group.
108*49cdfc7eSAndroid Build Coastguard Worker 	 */
109*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->parent_mask_other)
110*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MYINOTIFY_ADD_WATCH(fd_notify_other, ".", tc->parent_mask_other);
111*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->subdir_mask_other)
112*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MYINOTIFY_ADD_WATCH(fd_notify_other, TEST_DIR, tc->subdir_mask_other);
113*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->child_mask_other)
114*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MYINOTIFY_ADD_WATCH(fd_notify_other, TEST_FILE, tc->child_mask_other);
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	/*
117*49cdfc7eSAndroid Build Coastguard Worker 	 * Generate IN_ATTRIB events on file and subdir that should be reported to parent
118*49cdfc7eSAndroid Build Coastguard Worker 	 * dir with name and to children without name if they have IN_ATTRIB in their mask.
119*49cdfc7eSAndroid Build Coastguard Worker 	 */
120*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(TEST_DIR, 0755);
121*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(TEST_FILE, 0644);
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker 	if (wd_parent && (tc->parent_mask & IN_ATTRIB)) {
124*49cdfc7eSAndroid Build Coastguard Worker 		event_set[test_cnt].wd = wd_parent;
125*49cdfc7eSAndroid Build Coastguard Worker 		event_set[test_cnt].mask = tc->parent_mask | IN_ISDIR;
126*49cdfc7eSAndroid Build Coastguard Worker 		strcpy(event_set[test_cnt].name, TEST_DIR);
127*49cdfc7eSAndroid Build Coastguard Worker 		test_cnt++;
128*49cdfc7eSAndroid Build Coastguard Worker 	}
129*49cdfc7eSAndroid Build Coastguard Worker 	if (wd_subdir && (tc->subdir_mask & IN_ATTRIB)) {
130*49cdfc7eSAndroid Build Coastguard Worker 		event_set[test_cnt].wd = wd_subdir;
131*49cdfc7eSAndroid Build Coastguard Worker 		event_set[test_cnt].mask = tc->subdir_mask | IN_ISDIR;
132*49cdfc7eSAndroid Build Coastguard Worker 		strcpy(event_set[test_cnt].name, "");
133*49cdfc7eSAndroid Build Coastguard Worker 		test_cnt++;
134*49cdfc7eSAndroid Build Coastguard Worker 	}
135*49cdfc7eSAndroid Build Coastguard Worker 	if (wd_parent && (tc->parent_mask & IN_ATTRIB)) {
136*49cdfc7eSAndroid Build Coastguard Worker 		event_set[test_cnt].wd = wd_parent;
137*49cdfc7eSAndroid Build Coastguard Worker 		event_set[test_cnt].mask = tc->parent_mask;
138*49cdfc7eSAndroid Build Coastguard Worker 		strcpy(event_set[test_cnt].name, TEST_FILE);
139*49cdfc7eSAndroid Build Coastguard Worker 		test_cnt++;
140*49cdfc7eSAndroid Build Coastguard Worker 	}
141*49cdfc7eSAndroid Build Coastguard Worker 	if (wd_child && (tc->child_mask & IN_ATTRIB)) {
142*49cdfc7eSAndroid Build Coastguard Worker 		event_set[test_cnt].wd = wd_child;
143*49cdfc7eSAndroid Build Coastguard Worker 		event_set[test_cnt].mask = tc->child_mask;
144*49cdfc7eSAndroid Build Coastguard Worker 		strcpy(event_set[test_cnt].name, "");
145*49cdfc7eSAndroid Build Coastguard Worker 		test_cnt++;
146*49cdfc7eSAndroid Build Coastguard Worker 	}
147*49cdfc7eSAndroid Build Coastguard Worker 
148*49cdfc7eSAndroid Build Coastguard Worker 	len = read(fd_notify, event_buf, EVENT_BUF_LEN);
149*49cdfc7eSAndroid Build Coastguard Worker 	if (len == -1)
150*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "read failed");
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker 	while (i < len) {
153*49cdfc7eSAndroid Build Coastguard Worker 		struct event_t *expected = &event_set[test_num];
154*49cdfc7eSAndroid Build Coastguard Worker 		struct inotify_event *event;
155*49cdfc7eSAndroid Build Coastguard Worker 		event = (struct inotify_event *)&event_buf[i];
156*49cdfc7eSAndroid Build Coastguard Worker 		if (test_num >= test_cnt) {
157*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL,
158*49cdfc7eSAndroid Build Coastguard Worker 				"got unnecessary event: "
159*49cdfc7eSAndroid Build Coastguard Worker 				"wd=%d mask=%04x len=%u "
160*49cdfc7eSAndroid Build Coastguard Worker 				"name=\"%.*s\"", event->wd, event->mask,
161*49cdfc7eSAndroid Build Coastguard Worker 				event->len, event->len, event->name);
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 		} else if (expected->wd == event->wd &&
164*49cdfc7eSAndroid Build Coastguard Worker 			   expected->mask == event->mask &&
165*49cdfc7eSAndroid Build Coastguard Worker 			   !strncmp(expected->name, event->name, event->len)) {
166*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS,
167*49cdfc7eSAndroid Build Coastguard Worker 				"got event: wd=%d mask=%04x "
168*49cdfc7eSAndroid Build Coastguard Worker 				"cookie=%u len=%u name=\"%.*s\"",
169*49cdfc7eSAndroid Build Coastguard Worker 				event->wd, event->mask, event->cookie,
170*49cdfc7eSAndroid Build Coastguard Worker 				event->len, event->len, event->name);
171*49cdfc7eSAndroid Build Coastguard Worker 
172*49cdfc7eSAndroid Build Coastguard Worker 		} else {
173*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "got event: wd=%d (expected %d) "
174*49cdfc7eSAndroid Build Coastguard Worker 				"mask=%04x (expected %x) len=%u "
175*49cdfc7eSAndroid Build Coastguard Worker 				"name=\"%.*s\" (expected \"%s\")",
176*49cdfc7eSAndroid Build Coastguard Worker 				event->wd, expected->wd,
177*49cdfc7eSAndroid Build Coastguard Worker 				event->mask, expected->mask,
178*49cdfc7eSAndroid Build Coastguard Worker 				event->len, event->len,
179*49cdfc7eSAndroid Build Coastguard Worker 				event->name, expected->name);
180*49cdfc7eSAndroid Build Coastguard Worker 		}
181*49cdfc7eSAndroid Build Coastguard Worker 		test_num++;
182*49cdfc7eSAndroid Build Coastguard Worker 		i += EVENT_SIZE + event->len;
183*49cdfc7eSAndroid Build Coastguard Worker 	}
184*49cdfc7eSAndroid Build Coastguard Worker 
185*49cdfc7eSAndroid Build Coastguard Worker 	for (; test_num < test_cnt; test_num++) {
186*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "didn't get event: mask=%04x ",
187*49cdfc7eSAndroid Build Coastguard Worker 			event_set[test_num].mask);
188*49cdfc7eSAndroid Build Coastguard Worker 	}
189*49cdfc7eSAndroid Build Coastguard Worker 
190*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd_notify);
191*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd_notify_other);
192*49cdfc7eSAndroid Build Coastguard Worker }
193*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)194*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
195*49cdfc7eSAndroid Build Coastguard Worker {
196*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TEST_DIR, 00700);
197*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(TEST_FILE, "1");
198*49cdfc7eSAndroid Build Coastguard Worker }
199*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)200*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
201*49cdfc7eSAndroid Build Coastguard Worker {
202*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_notify > 0)
203*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_notify);
204*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_notify_other > 0)
205*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_notify_other);
206*49cdfc7eSAndroid Build Coastguard Worker }
207*49cdfc7eSAndroid Build Coastguard Worker 
208*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
209*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
210*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
211*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
212*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_inotify,
213*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
214*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
215*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "fecc4559780d"},
216*49cdfc7eSAndroid Build Coastguard Worker 		{}
217*49cdfc7eSAndroid Build Coastguard Worker 	}
218*49cdfc7eSAndroid Build Coastguard Worker };
219*49cdfc7eSAndroid Build Coastguard Worker 
220*49cdfc7eSAndroid Build Coastguard Worker #else
221*49cdfc7eSAndroid Build Coastguard Worker 	TST_TEST_TCONF("system doesn't have required inotify support");
222*49cdfc7eSAndroid Build Coastguard Worker #endif /* defined(HAVE_SYS_INOTIFY_H) */
223