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