1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2014 SUSE Linux. All Rights Reserved.
4 *
5 * Started by Jan Kara <jack@suse.cz>
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 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 sprintf(symlnk, "/proc/self/fd/%d", event->fd);
73 len = readlink(symlnk, fdpath, sizeof(fdpath));
74 if (len < 0)
75 len = 0;
76 fdpath[len] = 0;
77 filename = basename(fdpath);
78 if (len > FNAME_PREFIX_LEN && atoi(filename + FNAME_PREFIX_LEN) != i) {
79 tst_res(TFAIL, "Got event #%d out of order filename=%s", i, filename);
80 } else if (i == 0) {
81 tst_res(TINFO, "Got event #%d filename=%s", i, filename);
82 }
83 }
84
generate_events(int open_flags,int num_files)85 static void generate_events(int open_flags, int num_files)
86 {
87 long long elapsed_ms;
88 int i;
89
90 tst_timer_start(CLOCK_MONOTONIC);
91
92 for (i = 0; i < num_files; i++) {
93 sprintf(fname, PATH_PREFIX "%d", i);
94 fd = SAFE_OPEN(fname, open_flags, 0644);
95 SAFE_CLOSE(fd);
96 }
97
98 tst_timer_stop();
99
100 elapsed_ms = tst_timer_elapsed_ms();
101
102 tst_res(TINFO, "%s %d files in %llims",
103 (open_flags & O_CREAT) ? "Created" : "Opened", i, elapsed_ms);
104 }
105
test_fanotify(unsigned int n)106 static void test_fanotify(unsigned int n)
107 {
108 struct tcase *tc = &tcases[n];
109 int len, nevents = 0, got_overflow = 0;
110 int num_files = max_events + 1;
111 int expect_overflow = !(tc->init_flags & FAN_UNLIMITED_QUEUE);
112
113 tst_res(TINFO, "Test #%d: %s", n, tc->tname);
114
115 fd_notify = SAFE_FANOTIFY_INIT(tc->init_flags | FAN_NONBLOCK, O_RDONLY);
116
117 SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_MOUNT | FAN_MARK_ADD, FAN_OPEN,
118 AT_FDCWD, MOUNT_PATH);
119
120 /*
121 * Generate events on unique files so they won't be merged
122 */
123 generate_events(O_RDWR | O_CREAT, num_files);
124
125 /*
126 * Generate more events on the same files that me be merged
127 */
128 generate_events(O_RDONLY, num_files);
129
130 while (1) {
131 /*
132 * get list on events
133 */
134 len = read(fd_notify, &event, sizeof(event));
135 if (len < 0) {
136 if (errno != EAGAIN) {
137 tst_brk(TBROK | TERRNO,
138 "read of notification event failed");
139 }
140 if (!got_overflow)
141 tst_res(expect_overflow ? TFAIL : TPASS, "Overflow event not generated!\n");
142 break;
143 }
144 if (event.fd != FAN_NOFD) {
145 /*
146 * Verify that events generated on unique files
147 * are received by the same order they were generated.
148 */
149 if (nevents < num_files)
150 event_res(&event, nevents);
151 close(event.fd);
152 }
153 nevents++;
154
155 /*
156 * check events
157 */
158 if (event.mask != FAN_OPEN &&
159 event.mask != FAN_Q_OVERFLOW) {
160 tst_res(TFAIL,
161 "got event: mask=%llx (expected %llx) pid=%u fd=%d",
162 (unsigned long long)event.mask,
163 (unsigned long long)FAN_OPEN,
164 (unsigned)event.pid, event.fd);
165 break;
166 }
167 if (event.mask == FAN_Q_OVERFLOW) {
168 if (got_overflow || event.fd != FAN_NOFD) {
169 tst_res(TFAIL,
170 "%s overflow event: mask=%llx pid=%u fd=%d",
171 got_overflow ? "unexpected" : "invalid",
172 (unsigned long long)event.mask,
173 (unsigned)event.pid,
174 event.fd);
175 break;
176 }
177 tst_res(expect_overflow ? TPASS : TFAIL,
178 "Got an overflow event: pid=%u fd=%d",
179 (unsigned)event.pid, event.fd);
180 got_overflow = 1;
181 }
182 }
183 tst_res(TINFO, "Got %d events", nevents);
184 SAFE_CLOSE(fd_notify);
185 }
186
setup(void)187 static void setup(void)
188 {
189 int fd;
190
191 /* Check for kernel fanotify support */
192 fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY);
193 SAFE_CLOSE(fd);
194
195 /* In older kernels this limit is fixed in kernel */
196 if (access(SYSFS_MAX_EVENTS, F_OK) && errno == ENOENT)
197 max_events = DEFAULT_MAX_EVENTS;
198 else
199 SAFE_FILE_SCANF(SYSFS_MAX_EVENTS, "%d", &max_events);
200 tst_res(TINFO, "max_queued_events=%d", max_events);
201 }
202
cleanup(void)203 static void cleanup(void)
204 {
205 if (fd_notify > 0)
206 SAFE_CLOSE(fd_notify);
207 }
208
209 static struct tst_test test = {
210 .test = test_fanotify,
211 .tcnt = ARRAY_SIZE(tcases),
212 .setup = setup,
213 .cleanup = cleanup,
214 .needs_root = 1,
215 .mount_device = 1,
216 .mntpoint = MOUNT_PATH,
217 };
218 #else
219 TST_TEST_TCONF("system doesn't have required fanotify support");
220 #endif
221