xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/inotify/inotify04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2012 Linux Test Project.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Ngie Cooper, April 2012
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Test for inotify IN_DELETE_SELF event.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * [Algorithm]
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * This testcase creates a temporary directory, then add watches to a
15*49cdfc7eSAndroid Build Coastguard Worker  * predefined file and subdirectory, and delete the file and directory
16*49cdfc7eSAndroid Build Coastguard Worker  * to ensure that the IN_DELETE_SELF event is captured properly.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  * Because of how the inotify(7) API is designed, we also need to catch
19*49cdfc7eSAndroid Build Coastguard Worker  * the IN_ATTRIB and IN_IGNORED events.
20*49cdfc7eSAndroid Build Coastguard Worker  */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_INOTIFY_H)
25*49cdfc7eSAndroid Build Coastguard Worker # include <sys/inotify.h>
26*49cdfc7eSAndroid Build Coastguard Worker #endif
27*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
30*49cdfc7eSAndroid Build Coastguard Worker #include "inotify.h"
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_INOTIFY_H)
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_MAX 1024
35*49cdfc7eSAndroid Build Coastguard Worker /* size of the event structure, not counting name */
36*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SIZE  (sizeof(struct inotify_event))
37*49cdfc7eSAndroid Build Coastguard Worker /* reasonable guess as to size of 1024 events */
38*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_BUF_LEN        (EVENT_MAX * (EVENT_SIZE + 16))
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker struct event_t {
44*49cdfc7eSAndroid Build Coastguard Worker 	char name[BUF_SIZE];
45*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int mask;
46*49cdfc7eSAndroid Build Coastguard Worker };
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker #define	TEST_DIR	"test_dir"
49*49cdfc7eSAndroid Build Coastguard Worker #define	TEST_FILE	"test_file"
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker struct event_t event_set[EVENT_MAX];
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker char event_buf[EVENT_BUF_LEN];
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker int fd_notify, reap_wd_file, reap_wd_dir, wd_dir, wd_file;
56*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)57*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker 	if (reap_wd_dir && myinotify_rm_watch(fd_notify, wd_dir) == -1)
60*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN,
61*49cdfc7eSAndroid Build Coastguard Worker 			"inotify_rm_watch(%d, %d) [1] failed", fd_notify,
62*49cdfc7eSAndroid Build Coastguard Worker 			wd_dir);
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	if (reap_wd_file && myinotify_rm_watch(fd_notify, wd_file) == -1)
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN,
66*49cdfc7eSAndroid Build Coastguard Worker 			"inotify_rm_watch(%d, %d) [2] failed", fd_notify,
67*49cdfc7eSAndroid Build Coastguard Worker 			wd_file);
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_notify > 0)
70*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_notify);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)73*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker 	fd_notify = SAFE_MYINOTIFY_INIT();
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker 
verify_inotify(void)78*49cdfc7eSAndroid Build Coastguard Worker void verify_inotify(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker 	int i = 0, test_num = 0, len;
81*49cdfc7eSAndroid Build Coastguard Worker 	int test_cnt = 0;
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TEST_DIR, 00700);
84*49cdfc7eSAndroid Build Coastguard Worker 	close(SAFE_CREAT(TEST_FILE, 00600));
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	wd_dir = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, TEST_DIR, IN_ALL_EVENTS);
87*49cdfc7eSAndroid Build Coastguard Worker 	reap_wd_dir = 1;
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	wd_file = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, TEST_FILE, IN_ALL_EVENTS);
90*49cdfc7eSAndroid Build Coastguard Worker 	reap_wd_file = 1;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_RMDIR(TEST_DIR);
93*49cdfc7eSAndroid Build Coastguard Worker 	reap_wd_dir = 0;
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	event_set[test_cnt].mask = IN_DELETE_SELF;
96*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(event_set[test_cnt].name, "");
97*49cdfc7eSAndroid Build Coastguard Worker 	test_cnt++;
98*49cdfc7eSAndroid Build Coastguard Worker 	event_set[test_cnt].mask = IN_IGNORED;
99*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(event_set[test_cnt].name, "");
100*49cdfc7eSAndroid Build Coastguard Worker 	test_cnt++;
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(TEST_FILE);
103*49cdfc7eSAndroid Build Coastguard Worker 	reap_wd_file = 0;
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	/*
106*49cdfc7eSAndroid Build Coastguard Worker 	 * When a file is unlinked, the link count is reduced by 1, and when it
107*49cdfc7eSAndroid Build Coastguard Worker 	 * hits 0 the file is removed.
108*49cdfc7eSAndroid Build Coastguard Worker 	 *
109*49cdfc7eSAndroid Build Coastguard Worker 	 * This isn't well documented in inotify(7), but it's intuitive if you
110*49cdfc7eSAndroid Build Coastguard Worker 	 * understand how Unix works.
111*49cdfc7eSAndroid Build Coastguard Worker 	 */
112*49cdfc7eSAndroid Build Coastguard Worker 	event_set[test_cnt].mask = IN_ATTRIB;
113*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(event_set[test_cnt].name, "");
114*49cdfc7eSAndroid Build Coastguard Worker 	test_cnt++;
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	event_set[test_cnt].mask = IN_DELETE_SELF;
117*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(event_set[test_cnt].name, TEST_FILE);
118*49cdfc7eSAndroid Build Coastguard Worker 	test_cnt++;
119*49cdfc7eSAndroid Build Coastguard Worker 	event_set[test_cnt].mask = IN_IGNORED;
120*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(event_set[test_cnt].name, "");
121*49cdfc7eSAndroid Build Coastguard Worker 	test_cnt++;
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker 	len = read(fd_notify, event_buf, EVENT_BUF_LEN);
124*49cdfc7eSAndroid Build Coastguard Worker 	if (len == -1)
125*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "read failed");
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 	while (i < len) {
128*49cdfc7eSAndroid Build Coastguard Worker 		struct inotify_event *event;
129*49cdfc7eSAndroid Build Coastguard Worker 		event = (struct inotify_event *)&event_buf[i];
130*49cdfc7eSAndroid Build Coastguard Worker 		if (test_num >= test_cnt) {
131*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL,
132*49cdfc7eSAndroid Build Coastguard Worker 				"got unnecessary event: "
133*49cdfc7eSAndroid Build Coastguard Worker 				"wd=%d mask=%04x cookie=%u len=%u "
134*49cdfc7eSAndroid Build Coastguard Worker 				"name=\"%.*s\"", event->wd, event->mask,
135*49cdfc7eSAndroid Build Coastguard Worker 				event->cookie, event->len, event->len, event->name);
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker 		} else if ((event_set[test_num].mask == event->mask)
138*49cdfc7eSAndroid Build Coastguard Worker 			   &&
139*49cdfc7eSAndroid Build Coastguard Worker 			   (!strncmp
140*49cdfc7eSAndroid Build Coastguard Worker 			    (event_set[test_num].name, event->name,
141*49cdfc7eSAndroid Build Coastguard Worker 			     event->len))) {
142*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS,
143*49cdfc7eSAndroid Build Coastguard Worker 				"got event: wd=%d mask=%04x "
144*49cdfc7eSAndroid Build Coastguard Worker 				"cookie=%u len=%u name=\"%.*s\"",
145*49cdfc7eSAndroid Build Coastguard Worker 				event->wd, event->mask, event->cookie,
146*49cdfc7eSAndroid Build Coastguard Worker 				event->len, event->len, event->name);
147*49cdfc7eSAndroid Build Coastguard Worker 
148*49cdfc7eSAndroid Build Coastguard Worker 		} else {
149*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "got event: wd=%d mask=%04x "
150*49cdfc7eSAndroid Build Coastguard Worker 				"(expected %x) cookie=%u len=%u "
151*49cdfc7eSAndroid Build Coastguard Worker 				"name=\"%.*s\" (expected \"%s\") %d",
152*49cdfc7eSAndroid Build Coastguard Worker 				event->wd, event->mask,
153*49cdfc7eSAndroid Build Coastguard Worker 				event_set[test_num].mask,
154*49cdfc7eSAndroid Build Coastguard Worker 				event->cookie, event->len,
155*49cdfc7eSAndroid Build Coastguard Worker 				event->len, event->name,
156*49cdfc7eSAndroid Build Coastguard Worker 				event_set[test_num].name,
157*49cdfc7eSAndroid Build Coastguard Worker 				strncmp(event_set[test_num].name, event->name, event->len));
158*49cdfc7eSAndroid Build Coastguard Worker 		}
159*49cdfc7eSAndroid Build Coastguard Worker 		test_num++;
160*49cdfc7eSAndroid Build Coastguard Worker 		i += EVENT_SIZE + event->len;
161*49cdfc7eSAndroid Build Coastguard Worker 	}
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 	for (; test_num < test_cnt; test_num++) {
164*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "didn't get event: mask=%04x ",
165*49cdfc7eSAndroid Build Coastguard Worker 			event_set[test_num].mask);
166*49cdfc7eSAndroid Build Coastguard Worker 	}
167*49cdfc7eSAndroid Build Coastguard Worker 
168*49cdfc7eSAndroid Build Coastguard Worker }
169*49cdfc7eSAndroid Build Coastguard Worker 
170*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
171*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
172*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
173*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
174*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_inotify,
175*49cdfc7eSAndroid Build Coastguard Worker };
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker #else
178*49cdfc7eSAndroid Build Coastguard Worker 	TST_TEST_TCONF("system doesn't have required inotify support");
179*49cdfc7eSAndroid Build Coastguard Worker #endif /* defined(HAVE_SYS_INOTIFY_H) */
180