~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/filesystems/eventfd/eventfd_test.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 #define _GNU_SOURCE
  4 #include <errno.h>
  5 #include <fcntl.h>
  6 #include <asm/unistd.h>
  7 #include <linux/time_types.h>
  8 #include <unistd.h>
  9 #include <assert.h>
 10 #include <signal.h>
 11 #include <pthread.h>
 12 #include <sys/epoll.h>
 13 #include <sys/eventfd.h>
 14 #include "../../kselftest_harness.h"
 15 
 16 #define EVENTFD_TEST_ITERATIONS 100000UL
 17 
 18 struct error {
 19         int  code;
 20         char msg[512];
 21 };
 22 
 23 static int error_set(struct error *err, int code, const char *fmt, ...)
 24 {
 25         va_list args;
 26         int r;
 27 
 28         if (code == 0 || !err || err->code != 0)
 29                 return code;
 30 
 31         err->code = code;
 32         va_start(args, fmt);
 33         r = vsnprintf(err->msg, sizeof(err->msg), fmt, args);
 34         assert((size_t)r < sizeof(err->msg));
 35         va_end(args);
 36 
 37         return code;
 38 }
 39 
 40 static inline int sys_eventfd2(unsigned int count, int flags)
 41 {
 42         return syscall(__NR_eventfd2, count, flags);
 43 }
 44 
 45 TEST(eventfd_check_flag_rdwr)
 46 {
 47         int fd, flags;
 48 
 49         fd = sys_eventfd2(0, 0);
 50         ASSERT_GE(fd, 0);
 51 
 52         flags = fcntl(fd, F_GETFL);
 53         // since the kernel automatically added O_RDWR.
 54         EXPECT_EQ(flags, O_RDWR);
 55 
 56         close(fd);
 57 }
 58 
 59 TEST(eventfd_check_flag_cloexec)
 60 {
 61         int fd, flags;
 62 
 63         fd = sys_eventfd2(0, EFD_CLOEXEC);
 64         ASSERT_GE(fd, 0);
 65 
 66         flags = fcntl(fd, F_GETFD);
 67         ASSERT_GT(flags, -1);
 68         EXPECT_EQ(flags, FD_CLOEXEC);
 69 
 70         close(fd);
 71 }
 72 
 73 TEST(eventfd_check_flag_nonblock)
 74 {
 75         int fd, flags;
 76 
 77         fd = sys_eventfd2(0, EFD_NONBLOCK);
 78         ASSERT_GE(fd, 0);
 79 
 80         flags = fcntl(fd, F_GETFL);
 81         ASSERT_GT(flags, -1);
 82         EXPECT_EQ(flags & EFD_NONBLOCK, EFD_NONBLOCK);
 83         EXPECT_EQ(flags & O_RDWR, O_RDWR);
 84 
 85         close(fd);
 86 }
 87 
 88 TEST(eventfd_chek_flag_cloexec_and_nonblock)
 89 {
 90         int fd, flags;
 91 
 92         fd = sys_eventfd2(0, EFD_CLOEXEC|EFD_NONBLOCK);
 93         ASSERT_GE(fd, 0);
 94 
 95         flags = fcntl(fd, F_GETFL);
 96         ASSERT_GT(flags, -1);
 97         EXPECT_EQ(flags & EFD_NONBLOCK, EFD_NONBLOCK);
 98         EXPECT_EQ(flags & O_RDWR, O_RDWR);
 99 
100         flags = fcntl(fd, F_GETFD);
101         ASSERT_GT(flags, -1);
102         EXPECT_EQ(flags, FD_CLOEXEC);
103 
104         close(fd);
105 }
106 
107 static inline void trim_newline(char *str)
108 {
109         char *pos = strrchr(str, '\n');
110 
111         if (pos)
112                 *pos = '\0';
113 }
114 
115 static int verify_fdinfo(int fd, struct error *err, const char *prefix,
116                 size_t prefix_len, const char *expect, ...)
117 {
118         char buffer[512] = {0, };
119         char path[512] = {0, };
120         va_list args;
121         FILE *f;
122         char *line = NULL;
123         size_t n = 0;
124         int found = 0;
125         int r;
126 
127         va_start(args, expect);
128         r = vsnprintf(buffer, sizeof(buffer), expect, args);
129         assert((size_t)r < sizeof(buffer));
130         va_end(args);
131 
132         snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
133         f = fopen(path, "re");
134         if (!f)
135                 return error_set(err, -1, "fdinfo open failed for %d", fd);
136 
137         while (getline(&line, &n, f) != -1) {
138                 char *val;
139 
140                 if (strncmp(line, prefix, prefix_len))
141                         continue;
142 
143                 found = 1;
144 
145                 val = line + prefix_len;
146                 r = strcmp(val, buffer);
147                 if (r != 0) {
148                         trim_newline(line);
149                         trim_newline(buffer);
150                         error_set(err, -1, "%s '%s' != '%s'",
151                                   prefix, val, buffer);
152                 }
153                 break;
154         }
155 
156         free(line);
157         fclose(f);
158 
159         if (found == 0)
160                 return error_set(err, -1, "%s not found for fd %d",
161                                  prefix, fd);
162 
163         return 0;
164 }
165 
166 TEST(eventfd_check_flag_semaphore)
167 {
168         struct error err = {0};
169         int fd, ret;
170 
171         fd = sys_eventfd2(0, EFD_SEMAPHORE);
172         ASSERT_GE(fd, 0);
173 
174         ret = fcntl(fd, F_GETFL);
175         ASSERT_GT(ret, -1);
176         EXPECT_EQ(ret & O_RDWR, O_RDWR);
177 
178         // The semaphore could only be obtained from fdinfo.
179         ret = verify_fdinfo(fd, &err, "eventfd-semaphore: ", 19, "1\n");
180         if (ret != 0)
181                 ksft_print_msg("eventfd-semaphore check failed, msg: %s\n",
182                                 err.msg);
183         EXPECT_EQ(ret, 0);
184 
185         close(fd);
186 }
187 
188 /*
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.
192  */
193 TEST(eventfd_check_write)
194 {
195         uint64_t value = 1;
196         ssize_t size;
197         int fd;
198 
199         fd = sys_eventfd2(0, 0);
200         ASSERT_GE(fd, 0);
201 
202         size = write(fd, &value, sizeof(int));
203         EXPECT_EQ(size, -1);
204         EXPECT_EQ(errno, EINVAL);
205 
206         size = write(fd, &value, sizeof(value));
207         EXPECT_EQ(size, sizeof(value));
208 
209         value = (uint64_t)-1;
210         size = write(fd, &value, sizeof(value));
211         EXPECT_EQ(size, -1);
212         EXPECT_EQ(errno, EINVAL);
213 
214         close(fd);
215 }
216 
217 /*
218  * A read(2) fails with the error EINVAL if the size of the supplied buffer is
219  * less than 8 bytes.
220  */
221 TEST(eventfd_check_read)
222 {
223         uint64_t value;
224         ssize_t size;
225         int fd;
226 
227         fd = sys_eventfd2(1, 0);
228         ASSERT_GE(fd, 0);
229 
230         size = read(fd, &value, sizeof(int));
231         EXPECT_EQ(size, -1);
232         EXPECT_EQ(errno, EINVAL);
233 
234         size = read(fd, &value, sizeof(value));
235         EXPECT_EQ(size, sizeof(value));
236         EXPECT_EQ(value, 1);
237 
238         close(fd);
239 }
240 
241 
242 /*
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.
248  */
249 TEST(eventfd_check_read_with_nonsemaphore)
250 {
251         uint64_t value;
252         ssize_t size;
253         int fd;
254         int i;
255 
256         fd = sys_eventfd2(0, EFD_NONBLOCK);
257         ASSERT_GE(fd, 0);
258 
259         value = 1;
260         for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) {
261                 size = write(fd, &value, sizeof(value));
262                 EXPECT_EQ(size, sizeof(value));
263         }
264 
265         size = read(fd, &value, sizeof(value));
266         EXPECT_EQ(size, sizeof(uint64_t));
267         EXPECT_EQ(value, EVENTFD_TEST_ITERATIONS);
268 
269         size = read(fd, &value, sizeof(value));
270         EXPECT_EQ(size, -1);
271         EXPECT_EQ(errno, EAGAIN);
272 
273         close(fd);
274 }
275 
276 /*
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.
282  */
283 TEST(eventfd_check_read_with_semaphore)
284 {
285         uint64_t value;
286         ssize_t size;
287         int fd;
288         int i;
289 
290         fd = sys_eventfd2(0, EFD_SEMAPHORE|EFD_NONBLOCK);
291         ASSERT_GE(fd, 0);
292 
293         value = 1;
294         for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) {
295                 size = write(fd, &value, sizeof(value));
296                 EXPECT_EQ(size, sizeof(value));
297         }
298 
299         for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) {
300                 size = read(fd, &value, sizeof(value));
301                 EXPECT_EQ(size, sizeof(value));
302                 EXPECT_EQ(value, 1);
303         }
304 
305         size = read(fd, &value, sizeof(value));
306         EXPECT_EQ(size, -1);
307         EXPECT_EQ(errno, EAGAIN);
308 
309         close(fd);
310 }
311 
312 TEST_HARNESS_MAIN
313 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php