1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2014 SUSE Linux. All Rights Reserved.
4 *
5 * Started by Jan Kara <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 * Check that fanotify overflow event is properly generated.
11 *
12 * [Algorithm]
13 * Generate enough events without reading them and check that overflow
14 * event is generated.
15 */
16
17 #define _GNU_SOURCE
18 #include "config.h"
19
20 #include <stdio.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <errno.h>
24 #include <libgen.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/syscall.h>
28 #include "tst_test.h"
29 #include "tst_timer.h"
30
31 #ifdef HAVE_SYS_FANOTIFY_H
32 #include "fanotify.h"
33
34 #define MOUNT_PATH "fs_mnt"
35 #define FNAME_PREFIX "fname_"
36 #define FNAME_PREFIX_LEN 6
37 #define PATH_PREFIX MOUNT_PATH "/" FNAME_PREFIX
38
39 #define SYSFS_MAX_EVENTS "/proc/sys/fs/fanotify/max_queued_events"
40
41 /* In older kernels this limit is fixed in kernel */
42 #define DEFAULT_MAX_EVENTS 16384
43
44 static int max_events;
45
46 static struct tcase {
47 const char *tname;
48 unsigned int init_flags;
49 } tcases[] = {
50 {
51 "Limited queue",
52 FAN_CLASS_NOTIF,
53 },
54 {
55 "Unlimited queue",
56 FAN_CLASS_NOTIF | FAN_UNLIMITED_QUEUE,
57 },
58 };
59
60 #define BUF_SIZE 256
61 static char fname[BUF_SIZE];
62 static char symlnk[BUF_SIZE];
63 static char fdpath[BUF_SIZE];
64 static int fd, fd_notify;
65
66 static struct fanotify_event_metadata event;
67
event_res(struct fanotify_event_metadata * event,int i)68 static void event_res(struct fanotify_event_metadata *event, int i)
69 {
70 int len = 0;
71 const char *filename;
72
73 sprintf(symlnk, "/proc/self/fd/%d", event->fd);
74 len = readlink(symlnk, fdpath, sizeof(fdpath));
75 if (len < 0)
76 len = 0;
77 fdpath[len] = 0;
78 filename = basename(fdpath);
79
80 if (len > FNAME_PREFIX_LEN && atoi(filename + FNAME_PREFIX_LEN) != i)
81 tst_res(TFAIL, "Got event #%d out of order filename=%s", i, filename);
82 else if (i == 0)
83 tst_res(TINFO, "Got event #%d filename=%s", i, filename);
84 }
85
generate_events(int open_flags,int num_files)86 static void generate_events(int open_flags, int num_files)
87 {
88 long long elapsed_ms;
89 int i;
90
91 tst_timer_start(CLOCK_MONOTONIC);
92
93 for (i = 0; i < num_files; i++) {
94 sprintf(fname, PATH_PREFIX "%d", i);
95 fd = SAFE_OPEN(fname, open_flags, 0644);
96 SAFE_CLOSE(fd);
97 }
98
99 tst_timer_stop();
100
101 elapsed_ms = tst_timer_elapsed_ms();
102
103 tst_res(TINFO, "%s %d files in %llims",
104 (open_flags & O_CREAT) ? "Created" : "Opened", i, elapsed_ms);
105 }
106
test_fanotify(unsigned int n)107 static void test_fanotify(unsigned int n)
108 {
109 struct tcase *tc = &tcases[n];
110 int len, nevents = 0, got_overflow = 0;
111 int num_files = max_events + 1;
112 int expect_overflow = !(tc->init_flags & FAN_UNLIMITED_QUEUE);
113
114 tst_res(TINFO, "Test #%d: %s", n, tc->tname);
115
116 fd_notify = SAFE_FANOTIFY_INIT(tc->init_flags | FAN_NONBLOCK, O_RDONLY);
117
118 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_MOUNT | FAN_MARK_ADD, FAN_OPEN,
119 AT_FDCWD, MOUNT_PATH);
120
121 /*
122 * Generate events on unique files so they won't be merged
123 */
124 generate_events(O_RDWR | O_CREAT, num_files);
125
126 /*
127 * Generate more events on the same files that me be merged
128 */
129 generate_events(O_RDONLY, num_files);
130
131 while (1) {
132 /*
133 * get list on events
134 */
135 len = read(fd_notify, &event, sizeof(event));
136 if (len < 0) {
137 if (errno != EAGAIN) {
138 tst_brk(TBROK | TERRNO,
139 "read of notification event failed");
140 }
141 if (!got_overflow)
142 tst_res(expect_overflow ? TFAIL : TPASS, "Overflow event not generated!\n");
143 break;
144 }
145 if (event.fd != FAN_NOFD) {
146 /*
147 * Verify that events generated on unique files
148 * are received by the same order they were generated.
149 */
150 if (nevents < num_files)
151 event_res(&event, nevents);
152 close(event.fd);
153 }
154 nevents++;
155
156 /*
157 * check events
158 */
159 if (event.mask != FAN_OPEN &&
160 event.mask != FAN_Q_OVERFLOW) {
161 tst_res(TFAIL,
162 "got event: mask=%llx (expected %llx) pid=%u fd=%d",
163 (unsigned long long)event.mask,
164 (unsigned long long)FAN_OPEN,
165 (unsigned int)event.pid, event.fd);
166 break;
167 }
168 if (event.mask == FAN_Q_OVERFLOW) {
169 if (got_overflow || event.fd != FAN_NOFD) {
170 tst_res(TFAIL,
171 "%s overflow event: mask=%llx pid=%u fd=%d",
172 got_overflow ? "unexpected" : "invalid",
173 (unsigned long long)event.mask,
174 (unsigned int)event.pid,
175 event.fd);
176 break;
177 }
178 tst_res(expect_overflow ? TPASS : TFAIL,
179 "Got an overflow event: pid=%u fd=%d",
180 (unsigned int)event.pid, event.fd);
181 got_overflow = 1;
182 }
183 }
184 tst_res(TINFO, "Got %d events", nevents);
185 SAFE_CLOSE(fd_notify);
186 }
187
setup(void)188 static void setup(void)
189 {
190 int fd;
191
192 /* Check for kernel fanotify support */
193 fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY);
194 SAFE_CLOSE(fd);
195
196 /* In older kernels this limit is fixed in kernel */
197 if (access(SYSFS_MAX_EVENTS, F_OK) && errno == ENOENT)
198 max_events = DEFAULT_MAX_EVENTS;
199 else
200 SAFE_FILE_SCANF(SYSFS_MAX_EVENTS, "%d", &max_events);
201 tst_res(TINFO, "max_queued_events=%d", max_events);
202 }
203
cleanup(void)204 static void cleanup(void)
205 {
206 if (fd_notify > 0)
207 SAFE_CLOSE(fd_notify);
208 }
209
210 static struct tst_test test = {
211 .test = test_fanotify,
212 .tcnt = ARRAY_SIZE(tcases),
213 .setup = setup,
214 .cleanup = cleanup,
215 .needs_root = 1,
216 .mount_device = 1,
217 .mntpoint = MOUNT_PATH,
218 };
219 #else
220 TST_TEST_TCONF("system doesn't have required fanotify support");
221 #endif
222