1 // SPDX-License-Identifier: GPL-2.0
6 #include <asm/unistd.h>
7 #include <linux/time_types.h>
12 #include <sys/epoll.h>
13 #include <sys/eventfd.h>
14 #include "../../kselftest_harness.h"
16 #define EVENTFD_TEST_ITERATIONS 100000UL
23 static int error_set(struct error *err, int code, const char *fmt, ...)
28 if (code == 0 || !err || err->code != 0)
33 r = vsnprintf(err->msg, sizeof(err->msg), fmt, args);
34 assert((size_t)r < sizeof(err->msg));
40 static inline int sys_eventfd2(unsigned int count, int flags)
42 return syscall(__NR_eventfd2, count, flags);
45 TEST(eventfd_check_flag_rdwr)
49 fd = sys_eventfd2(0, 0);
52 flags = fcntl(fd, F_GETFL);
53 // since the kernel automatically added O_RDWR.
54 EXPECT_EQ(flags, O_RDWR);
59 TEST(eventfd_check_flag_cloexec)
63 fd = sys_eventfd2(0, EFD_CLOEXEC);
66 flags = fcntl(fd, F_GETFD);
68 EXPECT_EQ(flags, FD_CLOEXEC);
73 TEST(eventfd_check_flag_nonblock)
77 fd = sys_eventfd2(0, EFD_NONBLOCK);
80 flags = fcntl(fd, F_GETFL);
82 EXPECT_EQ(flags & EFD_NONBLOCK, EFD_NONBLOCK);
83 EXPECT_EQ(flags & O_RDWR, O_RDWR);
88 TEST(eventfd_chek_flag_cloexec_and_nonblock)
92 fd = sys_eventfd2(0, EFD_CLOEXEC|EFD_NONBLOCK);
95 flags = fcntl(fd, F_GETFL);
97 EXPECT_EQ(flags & EFD_NONBLOCK, EFD_NONBLOCK);
98 EXPECT_EQ(flags & O_RDWR, O_RDWR);
100 flags = fcntl(fd, F_GETFD);
101 ASSERT_GT(flags, -1);
102 EXPECT_EQ(flags, FD_CLOEXEC);
107 static inline void trim_newline(char *str)
109 char *pos = strrchr(str, '\n');
115 static int verify_fdinfo(int fd, struct error *err, const char *prefix,
116 size_t prefix_len, const char *expect, ...)
118 char buffer[512] = {0, };
119 char path[512] = {0, };
127 va_start(args, expect);
128 r = vsnprintf(buffer, sizeof(buffer), expect, args);
129 assert((size_t)r < sizeof(buffer));
132 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
133 f = fopen(path, "re");
135 return error_set(err, -1, "fdinfo open failed for %d", fd);
137 while (getline(&line, &n, f) != -1) {
140 if (strncmp(line, prefix, prefix_len))
145 val = line + prefix_len;
146 r = strcmp(val, buffer);
149 trim_newline(buffer);
150 error_set(err, -1, "%s '%s' != '%s'",
151 prefix, val, buffer);
160 return error_set(err, -1, "%s not found for fd %d",
166 TEST(eventfd_check_flag_semaphore)
168 struct error err = {0};
171 fd = sys_eventfd2(0, EFD_SEMAPHORE);
174 ret = fcntl(fd, F_GETFL);
176 EXPECT_EQ(ret & O_RDWR, O_RDWR);
178 // The semaphore could only be obtained from fdinfo.
179 ret = verify_fdinfo(fd, &err, "eventfd-semaphore: ", 19, "1\n");
181 ksft_print_msg("eventfd-semaphore check failed, msg: %s\n",
189 * A write(2) fails with the error EINVAL if the size of the supplied buffer
190 * is less than 8 bytes, or if an attempt is made to write the value
191 * 0xffffffffffffffff.
193 TEST(eventfd_check_write)
199 fd = sys_eventfd2(0, 0);
202 size = write(fd, &value, sizeof(int));
204 EXPECT_EQ(errno, EINVAL);
206 size = write(fd, &value, sizeof(value));
207 EXPECT_EQ(size, sizeof(value));
209 value = (uint64_t)-1;
210 size = write(fd, &value, sizeof(value));
212 EXPECT_EQ(errno, EINVAL);
218 * A read(2) fails with the error EINVAL if the size of the supplied buffer is
221 TEST(eventfd_check_read)
227 fd = sys_eventfd2(1, 0);
230 size = read(fd, &value, sizeof(int));
232 EXPECT_EQ(errno, EINVAL);
234 size = read(fd, &value, sizeof(value));
235 EXPECT_EQ(size, sizeof(value));
243 * If EFD_SEMAPHORE was not specified and the eventfd counter has a nonzero
244 * value, then a read(2) returns 8 bytes containing that value, and the
245 * counter's value is reset to zero.
246 * If the eventfd counter is zero at the time of the call to read(2), then the
247 * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
249 TEST(eventfd_check_read_with_nonsemaphore)
256 fd = sys_eventfd2(0, EFD_NONBLOCK);
260 for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) {
261 size = write(fd, &value, sizeof(value));
262 EXPECT_EQ(size, sizeof(value));
265 size = read(fd, &value, sizeof(value));
266 EXPECT_EQ(size, sizeof(uint64_t));
267 EXPECT_EQ(value, EVENTFD_TEST_ITERATIONS);
269 size = read(fd, &value, sizeof(value));
271 EXPECT_EQ(errno, EAGAIN);
277 * If EFD_SEMAPHORE was specified and the eventfd counter has a nonzero value,
278 * then a read(2) returns 8 bytes containing the value 1, and the counter's
279 * value is decremented by 1.
280 * If the eventfd counter is zero at the time of the call to read(2), then the
281 * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
283 TEST(eventfd_check_read_with_semaphore)
290 fd = sys_eventfd2(0, EFD_SEMAPHORE|EFD_NONBLOCK);
294 for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) {
295 size = write(fd, &value, sizeof(value));
296 EXPECT_EQ(size, sizeof(value));
299 for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) {
300 size = read(fd, &value, sizeof(value));
301 EXPECT_EQ(size, sizeof(value));
305 size = read(fd, &value, sizeof(value));
307 EXPECT_EQ(errno, EAGAIN);