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