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