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