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 * Author: Amir Goldstein <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Test special inotify mask flags.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * Regression test for kernel commit:
14*49cdfc7eSAndroid Build Coastguard Worker * a32e697cda27 ("inotify: show inotify mask flags in proc fdinfo").
15*49cdfc7eSAndroid Build Coastguard Worker */
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "inotify.h"
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_INOTIFY_H)
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/inotify.h>
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_MAX 32
33*49cdfc7eSAndroid Build Coastguard Worker /* Size of the event structure, not including the name */
34*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SIZE (sizeof(struct inotify_event))
35*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16))
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "test_file"
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker static char event_buf[EVENT_BUF_LEN];
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
42*49cdfc7eSAndroid Build Coastguard Worker const char *tname;
43*49cdfc7eSAndroid Build Coastguard Worker unsigned int mask;
44*49cdfc7eSAndroid Build Coastguard Worker int expect_events;
45*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker "Watch for multi events",
48*49cdfc7eSAndroid Build Coastguard Worker IN_MODIFY,
49*49cdfc7eSAndroid Build Coastguard Worker 2,
50*49cdfc7eSAndroid Build Coastguard Worker },
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker "Watch for single event",
53*49cdfc7eSAndroid Build Coastguard Worker IN_MODIFY | IN_ONESHOT,
54*49cdfc7eSAndroid Build Coastguard Worker 1,
55*49cdfc7eSAndroid Build Coastguard Worker },
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker "Watch for events on linked file",
58*49cdfc7eSAndroid Build Coastguard Worker IN_MODIFY | IN_EXCL_UNLINK,
59*49cdfc7eSAndroid Build Coastguard Worker 1,
60*49cdfc7eSAndroid Build Coastguard Worker },
61*49cdfc7eSAndroid Build Coastguard Worker };
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker static int fd_notify;
64*49cdfc7eSAndroid Build Coastguard Worker
verify_inotify(unsigned int n)65*49cdfc7eSAndroid Build Coastguard Worker static void verify_inotify(unsigned int n)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
68*49cdfc7eSAndroid Build Coastguard Worker int fd, len;
69*49cdfc7eSAndroid Build Coastguard Worker unsigned int tmpmask;
70*49cdfc7eSAndroid Build Coastguard Worker char procfdinfo[100];
71*49cdfc7eSAndroid Build Coastguard Worker struct inotify_event *event = (struct inotify_event *)event_buf;
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Test #%d: %s", n, tc->tname);
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker fd_notify = SAFE_MYINOTIFY_INIT1(O_NONBLOCK);
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(TEST_FILE, "1");
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker SAFE_MYINOTIFY_ADD_WATCH(fd_notify, ".", tc->mask);
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker sprintf(procfdinfo, "/proc/%d/fdinfo/%d", (int)getpid(), fd_notify);
82*49cdfc7eSAndroid Build Coastguard Worker if (FILE_LINES_SCANF(procfdinfo, "inotify wd:%*d ino:%*x sdev:%*x mask:%x",
83*49cdfc7eSAndroid Build Coastguard Worker &tmpmask)) {
84*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Could not parse inotify fdinfo");
85*49cdfc7eSAndroid Build Coastguard Worker } else if (tmpmask != tc->mask) {
86*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Incorrect mask %x in inotify fdinfo (expected %x)",
87*49cdfc7eSAndroid Build Coastguard Worker tmpmask, tc->mask);
88*49cdfc7eSAndroid Build Coastguard Worker } else {
89*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Correct mask in inotify fdinfo");
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TEST_FILE, O_RDWR);
93*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd, "2", 1);
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker /*
96*49cdfc7eSAndroid Build Coastguard Worker * Read the 1st IN_MODIFY event
97*49cdfc7eSAndroid Build Coastguard Worker */
98*49cdfc7eSAndroid Build Coastguard Worker len = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker if (len < (int)sizeof(*event)) {
101*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Got no events");
102*49cdfc7eSAndroid Build Coastguard Worker } else if (event->mask == IN_MODIFY) {
103*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Got 1st event as expected");
104*49cdfc7eSAndroid Build Coastguard Worker } else {
105*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Got event 0x%x (expected 0x%x)",
106*49cdfc7eSAndroid Build Coastguard Worker event->mask, IN_MODIFY);
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker /*
110*49cdfc7eSAndroid Build Coastguard Worker * Unlink file so IN_EXCL_UNLINK won't get IN_ACCESS event.
111*49cdfc7eSAndroid Build Coastguard Worker * IN_ONESHOT won't get IN_ACCESS event because IN_MODIFY
112*49cdfc7eSAndroid Build Coastguard Worker * was already generated.
113*49cdfc7eSAndroid Build Coastguard Worker */
114*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK(TEST_FILE);
115*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd, "3", 1);
116*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard Worker /*
119*49cdfc7eSAndroid Build Coastguard Worker * Possibly read the 2nd IN_MODIFY event
120*49cdfc7eSAndroid Build Coastguard Worker */
121*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
122*49cdfc7eSAndroid Build Coastguard Worker len = read(fd_notify, event_buf, EVENT_BUF_LEN);
123*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_notify);
124*49cdfc7eSAndroid Build Coastguard Worker if (len < 0 && errno == EAGAIN) {
125*49cdfc7eSAndroid Build Coastguard Worker /* Treat no event same as we treat IN_IGNORED */
126*49cdfc7eSAndroid Build Coastguard Worker event->mask = IN_IGNORED;
127*49cdfc7eSAndroid Build Coastguard Worker } else if (len < (int)sizeof(*event)) {
128*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO, "Failed to read events");
129*49cdfc7eSAndroid Build Coastguard Worker return;
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker if (event->mask == IN_MODIFY) {
133*49cdfc7eSAndroid Build Coastguard Worker if (tc->expect_events > 1)
134*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Got 2nd event as expected");
135*49cdfc7eSAndroid Build Coastguard Worker else
136*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Got unexpected 2nd event");
137*49cdfc7eSAndroid Build Coastguard Worker } else if (event->mask == IN_IGNORED) {
138*49cdfc7eSAndroid Build Coastguard Worker if (tc->expect_events == 1)
139*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Got no more events as expected");
140*49cdfc7eSAndroid Build Coastguard Worker else
141*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Got only one event (expected %d)",
142*49cdfc7eSAndroid Build Coastguard Worker tc->expect_events);
143*49cdfc7eSAndroid Build Coastguard Worker } else {
144*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Got unexpected event 0x%x",
145*49cdfc7eSAndroid Build Coastguard Worker event->mask);
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker }
148*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)149*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
150*49cdfc7eSAndroid Build Coastguard Worker {
151*49cdfc7eSAndroid Build Coastguard Worker if (fd_notify > 0)
152*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_notify);
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
156*49cdfc7eSAndroid Build Coastguard Worker .max_runtime = 10,
157*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
158*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
159*49cdfc7eSAndroid Build Coastguard Worker .test = verify_inotify,
160*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
161*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
162*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "a32e697cda27"},
163*49cdfc7eSAndroid Build Coastguard Worker {}
164*49cdfc7eSAndroid Build Coastguard Worker },
165*49cdfc7eSAndroid Build Coastguard Worker };
166*49cdfc7eSAndroid Build Coastguard Worker
167*49cdfc7eSAndroid Build Coastguard Worker #else
168*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("system doesn't have required inotify support");
169*49cdfc7eSAndroid Build Coastguard Worker #endif
170