xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/inotify/inotify11.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2022 CTERA Networks. All Rights Reserved.
4  *
5  * Started by Amir Goldstein <[email protected]>
6  * based on reproducer from Ivan Delalande <[email protected]>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Test opening files after receiving IN_DELETE.
13  *
14  * Kernel v5.13 has a regression allowing files to be open after IN_DELETE.
15  *
16  * The problem has been fixed by commit:
17  * a37d9a17f099 ("fsnotify: invalidate dcache before IN_DELETE event").
18  */
19 
20 #include "config.h"
21 
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <sys/wait.h>
27 
28 #include "tst_test.h"
29 #include "tst_safe_macros.h"
30 #include "inotify.h"
31 
32 #if defined(HAVE_SYS_INOTIFY_H)
33 #include <sys/inotify.h>
34 
35 /* Number of files to test */
36 #define CHURN_FILES 9999
37 
38 #define EVENT_MAX 32
39 /* Size of the event structure, not including the name */
40 #define EVENT_SIZE	(sizeof(struct inotify_event))
41 #define EVENT_BUF_LEN	(EVENT_MAX * (EVENT_SIZE + 16))
42 
43 static pid_t pid;
44 
45 static char event_buf[EVENT_BUF_LEN];
46 
churn(void)47 static void churn(void)
48 {
49 	char path[10];
50 	int i;
51 
52 	for (i = 0; i <= CHURN_FILES; ++i) {
53 		snprintf(path, sizeof(path), "%d", i);
54 		SAFE_FILE_PRINTF(path, "1");
55 		SAFE_UNLINK(path);
56 	}
57 }
58 
verify_inotify(void)59 static void verify_inotify(void)
60 {
61 	int nevents = 0, opened = 0;
62 	struct inotify_event *event;
63 	int inotify_fd;
64 
65 	inotify_fd = SAFE_MYINOTIFY_INIT();
66 	SAFE_MYINOTIFY_ADD_WATCH(inotify_fd, ".", IN_DELETE);
67 
68 	pid = SAFE_FORK();
69 	if (pid == 0) {
70 		SAFE_CLOSE(inotify_fd);
71 		churn();
72 		return;
73 	}
74 
75 	while (!opened && nevents < CHURN_FILES) {
76 		int i, fd, len;
77 
78 		len = SAFE_READ(0, inotify_fd, event_buf, EVENT_BUF_LEN);
79 
80 		for (i = 0; i < len; i += EVENT_SIZE + event->len) {
81 			event = (struct inotify_event *)&event_buf[i];
82 
83 			if (!(event->mask & IN_DELETE))
84 				continue;
85 
86 			nevents++;
87 
88 			/* Open file after IN_DELETE should fail */
89 			fd = open(event->name, O_RDONLY);
90 			if (fd < 0)
91 				continue;
92 
93 			tst_res(TFAIL, "File %s opened after IN_DELETE", event->name);
94 			SAFE_CLOSE(fd);
95 			opened = 1;
96 			break;
97 		}
98 	}
99 
100 	SAFE_CLOSE(inotify_fd);
101 
102 	if (!nevents)
103 		tst_res(TFAIL, "Didn't get any IN_DELETE events");
104 	else if (!opened)
105 		tst_res(TPASS, "Got %d IN_DELETE events", nevents);
106 
107 	/* Kill the child creating / deleting files and wait for it */
108 	SAFE_KILL(pid, SIGKILL);
109 	pid = 0;
110 	SAFE_WAIT(NULL);
111 }
112 
cleanup(void)113 static void cleanup(void)
114 {
115 	if (pid) {
116 		SAFE_KILL(pid, SIGKILL);
117 		SAFE_WAIT(NULL);
118 	}
119 }
120 
121 static struct tst_test test = {
122 	.needs_tmpdir = 1,
123 	.forks_child = 1,
124 	.cleanup = cleanup,
125 	.test_all = verify_inotify,
126 	.tags = (const struct tst_tag[]) {
127 		{"linux-git", "a37d9a17f099"},
128 		{}
129 	}
130 };
131 
132 #else
133 	TST_TEST_TCONF("system doesn't have required inotify support");
134 #endif
135