t_eventfd.c revision 1.2 1 1.2 thorpej /* $NetBSD: t_eventfd.c,v 1.2 2021/09/19 15:51:28 thorpej Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.2 thorpej * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.2 thorpej * modification, are permitted provided that the following conditions
9 1.2 thorpej * are met:
10 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.2 thorpej *
16 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.2 thorpej */
28 1.2 thorpej
29 1.2 thorpej #include <sys/cdefs.h>
30 1.2 thorpej __COPYRIGHT("@(#) Copyright (c) 2020\
31 1.2 thorpej The NetBSD Foundation, inc. All rights reserved.");
32 1.2 thorpej __RCSID("$NetBSD: t_eventfd.c,v 1.2 2021/09/19 15:51:28 thorpej Exp $");
33 1.2 thorpej
34 1.2 thorpej #include <sys/types.h>
35 1.2 thorpej #include <sys/event.h>
36 1.2 thorpej #include <sys/eventfd.h>
37 1.2 thorpej #include <sys/select.h>
38 1.2 thorpej #include <sys/stat.h>
39 1.2 thorpej #include <sys/syscall.h>
40 1.2 thorpej #include <errno.h>
41 1.2 thorpej #include <poll.h>
42 1.2 thorpej #include <pthread.h>
43 1.2 thorpej #include <stdlib.h>
44 1.2 thorpej #include <stdio.h>
45 1.2 thorpej #include <time.h>
46 1.2 thorpej #include <unistd.h>
47 1.2 thorpej
48 1.2 thorpej #include <atf-c.h>
49 1.2 thorpej
50 1.2 thorpej struct helper_context {
51 1.2 thorpej int efd;
52 1.2 thorpej
53 1.2 thorpej pthread_mutex_t mutex;
54 1.2 thorpej pthread_cond_t cond;
55 1.2 thorpej pthread_barrier_t barrier;
56 1.2 thorpej int state;
57 1.2 thorpej };
58 1.2 thorpej
59 1.2 thorpej static void
60 1.2 thorpej init_helper_context(struct helper_context * const ctx)
61 1.2 thorpej {
62 1.2 thorpej pthread_condattr_t condattr;
63 1.2 thorpej
64 1.2 thorpej memset(ctx, 0, sizeof(*ctx));
65 1.2 thorpej
66 1.2 thorpej ATF_REQUIRE(pthread_mutex_init(&ctx->mutex, NULL) == 0);
67 1.2 thorpej
68 1.2 thorpej ATF_REQUIRE(pthread_condattr_init(&condattr) == 0);
69 1.2 thorpej ATF_REQUIRE(pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) == 0);
70 1.2 thorpej ATF_REQUIRE(pthread_cond_init(&ctx->cond, &condattr) == 0);
71 1.2 thorpej ATF_REQUIRE(pthread_condattr_destroy(&condattr) == 0);
72 1.2 thorpej
73 1.2 thorpej ATF_REQUIRE(pthread_barrier_init(&ctx->barrier, NULL, 2) == 0);
74 1.2 thorpej }
75 1.2 thorpej
76 1.2 thorpej static void
77 1.2 thorpej set_state(struct helper_context * const ctx, int const new)
78 1.2 thorpej {
79 1.2 thorpej pthread_mutex_lock(&ctx->mutex);
80 1.2 thorpej ctx->state = new;
81 1.2 thorpej pthread_cond_signal(&ctx->cond);
82 1.2 thorpej pthread_mutex_unlock(&ctx->mutex);
83 1.2 thorpej }
84 1.2 thorpej
85 1.2 thorpej static int
86 1.2 thorpej get_state(struct helper_context * const ctx)
87 1.2 thorpej {
88 1.2 thorpej int rv;
89 1.2 thorpej
90 1.2 thorpej pthread_mutex_lock(&ctx->mutex);
91 1.2 thorpej rv = ctx->state;
92 1.2 thorpej pthread_mutex_unlock(&ctx->mutex);
93 1.2 thorpej
94 1.2 thorpej return rv;
95 1.2 thorpej }
96 1.2 thorpej
97 1.2 thorpej static bool
98 1.2 thorpej wait_state(struct helper_context * const ctx, int const val)
99 1.2 thorpej {
100 1.2 thorpej struct timespec deadline;
101 1.2 thorpej int error;
102 1.2 thorpej bool rv;
103 1.2 thorpej
104 1.2 thorpej pthread_mutex_lock(&ctx->mutex);
105 1.2 thorpej
106 1.2 thorpej ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &deadline) == 0);
107 1.2 thorpej deadline.tv_sec += 5;
108 1.2 thorpej
109 1.2 thorpej while (ctx->state != val) {
110 1.2 thorpej error = pthread_cond_timedwait(&ctx->cond, &ctx->mutex,
111 1.2 thorpej &deadline);
112 1.2 thorpej if (error) {
113 1.2 thorpej break;
114 1.2 thorpej }
115 1.2 thorpej }
116 1.2 thorpej rv = ctx->state == val;
117 1.2 thorpej
118 1.2 thorpej pthread_mutex_unlock(&ctx->mutex);
119 1.2 thorpej
120 1.2 thorpej return rv;
121 1.2 thorpej }
122 1.2 thorpej
123 1.2 thorpej static bool
124 1.2 thorpej wait_barrier(struct helper_context * const ctx)
125 1.2 thorpej {
126 1.2 thorpej int rv = pthread_barrier_wait(&ctx->barrier);
127 1.2 thorpej
128 1.2 thorpej return rv == 0 || rv == PTHREAD_BARRIER_SERIAL_THREAD;
129 1.2 thorpej }
130 1.2 thorpej
131 1.2 thorpej /*****************************************************************************/
132 1.2 thorpej
133 1.2 thorpej static void *
134 1.2 thorpej eventfd_normal_helper(void * const v)
135 1.2 thorpej {
136 1.2 thorpej struct helper_context * const ctx = v;
137 1.2 thorpej eventfd_t efd_value;
138 1.2 thorpej
139 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
140 1.2 thorpej
141 1.2 thorpej /* Read the value. This will reset it to zero. */
142 1.2 thorpej ATF_REQUIRE(get_state(ctx) == 666);
143 1.2 thorpej ATF_REQUIRE(eventfd_read(ctx->efd, &efd_value) == 0);
144 1.2 thorpej
145 1.2 thorpej /* Assert the value. */
146 1.2 thorpej ATF_REQUIRE(efd_value == 0xcafebabe);
147 1.2 thorpej
148 1.2 thorpej set_state(ctx, 0);
149 1.2 thorpej
150 1.2 thorpej /* Wait for the main thread to prep the next test. */
151 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
152 1.2 thorpej
153 1.2 thorpej /* Read the value. */
154 1.2 thorpej ATF_REQUIRE(eventfd_read(ctx->efd, &efd_value) == 0);
155 1.2 thorpej
156 1.2 thorpej /* Assert the value. */
157 1.2 thorpej ATF_REQUIRE(efd_value == 0xbeefcafe);
158 1.2 thorpej
159 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
160 1.2 thorpej
161 1.2 thorpej return NULL;
162 1.2 thorpej }
163 1.2 thorpej
164 1.2 thorpej ATF_TC(eventfd_normal);
165 1.2 thorpej ATF_TC_HEAD(eventfd_normal, tc)
166 1.2 thorpej {
167 1.2 thorpej atf_tc_set_md_var(tc, "descr",
168 1.2 thorpej "validates basic normal eventfd operation");
169 1.2 thorpej }
170 1.2 thorpej ATF_TC_BODY(eventfd_normal, tc)
171 1.2 thorpej {
172 1.2 thorpej struct helper_context ctx;
173 1.2 thorpej pthread_t helper;
174 1.2 thorpej void *join_val;
175 1.2 thorpej
176 1.2 thorpej init_helper_context(&ctx);
177 1.2 thorpej
178 1.2 thorpej ATF_REQUIRE((ctx.efd = eventfd(0, 0)) >= 0);
179 1.2 thorpej
180 1.2 thorpej ATF_REQUIRE(pthread_create(&helper, NULL,
181 1.2 thorpej eventfd_normal_helper, &ctx) == 0);
182 1.2 thorpej
183 1.2 thorpej /*
184 1.2 thorpej * Wait for the helper to block in read(). Give it some time
185 1.2 thorpej * so that if the read fails or returns immediately, we'll
186 1.2 thorpej * notice.
187 1.2 thorpej */
188 1.2 thorpej set_state(&ctx, 666);
189 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
190 1.2 thorpej sleep(2);
191 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 666);
192 1.2 thorpej
193 1.2 thorpej /* Write a distinct value; helper will assert it. */
194 1.2 thorpej ATF_REQUIRE(eventfd_write(ctx.efd, 0xcafebabe) == 0);
195 1.2 thorpej
196 1.2 thorpej /* Wait for helper to read the value. */
197 1.2 thorpej ATF_REQUIRE(wait_state(&ctx, 0));
198 1.2 thorpej
199 1.2 thorpej /* Helper is now blocked in a barrier. */
200 1.2 thorpej
201 1.2 thorpej /* Test additive property of the efd value. */
202 1.2 thorpej ATF_REQUIRE(eventfd_write(ctx.efd, 0x0000cafe) == 0);
203 1.2 thorpej ATF_REQUIRE(eventfd_write(ctx.efd, 0xbeef0000) == 0);
204 1.2 thorpej
205 1.2 thorpej /* Satisfy the barrier; helper will read value and assert 0xbeefcafe. */
206 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
207 1.2 thorpej
208 1.2 thorpej /* And wait for it to finish. */
209 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
210 1.2 thorpej
211 1.2 thorpej /* Reap the helper. */
212 1.2 thorpej ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
213 1.2 thorpej
214 1.2 thorpej (void) close(ctx.efd);
215 1.2 thorpej }
216 1.2 thorpej
217 1.2 thorpej /*****************************************************************************/
218 1.2 thorpej
219 1.2 thorpej ATF_TC(eventfd_semaphore);
220 1.2 thorpej ATF_TC_HEAD(eventfd_semaphore, tc)
221 1.2 thorpej {
222 1.2 thorpej atf_tc_set_md_var(tc, "descr",
223 1.2 thorpej "validates semaphore and non-blocking eventfd operation");
224 1.2 thorpej }
225 1.2 thorpej ATF_TC_BODY(eventfd_semaphore, tc)
226 1.2 thorpej {
227 1.2 thorpej eventfd_t efd_value;
228 1.2 thorpej int efd;
229 1.2 thorpej
230 1.2 thorpej ATF_REQUIRE((efd = eventfd(3, EFD_SEMAPHORE | EFD_NONBLOCK)) >= 0);
231 1.2 thorpej
232 1.2 thorpej /* 3 reads should succeed without blocking. */
233 1.2 thorpej ATF_REQUIRE(eventfd_read(efd, &efd_value) == 0);
234 1.2 thorpej ATF_REQUIRE(efd_value == 1);
235 1.2 thorpej
236 1.2 thorpej ATF_REQUIRE(eventfd_read(efd, &efd_value) == 0);
237 1.2 thorpej ATF_REQUIRE(efd_value == 1);
238 1.2 thorpej
239 1.2 thorpej ATF_REQUIRE(eventfd_read(efd, &efd_value) == 0);
240 1.2 thorpej ATF_REQUIRE(efd_value == 1);
241 1.2 thorpej
242 1.2 thorpej /* This one should block. */
243 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
244 1.2 thorpej eventfd_read(efd, &efd_value) == -1);
245 1.2 thorpej
246 1.2 thorpej /* Add 1 to the semaphore. */
247 1.2 thorpej ATF_REQUIRE(eventfd_write(efd, 1) == 0);
248 1.2 thorpej
249 1.2 thorpej /* One more read allowed. */
250 1.2 thorpej ATF_REQUIRE(eventfd_read(efd, &efd_value) == 0);
251 1.2 thorpej ATF_REQUIRE(efd_value == 1);
252 1.2 thorpej
253 1.2 thorpej /* And this one again should block. */
254 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
255 1.2 thorpej eventfd_read(efd, &efd_value) == -1);
256 1.2 thorpej
257 1.2 thorpej (void) close(efd);
258 1.2 thorpej }
259 1.2 thorpej
260 1.2 thorpej /*****************************************************************************/
261 1.2 thorpej
262 1.2 thorpej ATF_TC(eventfd_select_poll_kevent_immed);
263 1.2 thorpej ATF_TC_HEAD(eventfd_select_poll_kevent_immed, tc)
264 1.2 thorpej {
265 1.2 thorpej atf_tc_set_md_var(tc, "descr",
266 1.2 thorpej "validates select/poll/kevent behavior - immediate return");
267 1.2 thorpej }
268 1.2 thorpej ATF_TC_BODY(eventfd_select_poll_kevent_immed, tc)
269 1.2 thorpej {
270 1.2 thorpej const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
271 1.2 thorpej struct timeval tv;
272 1.2 thorpej struct pollfd fds[1];
273 1.2 thorpej fd_set readfds, writefds, exceptfds;
274 1.2 thorpej int efd;
275 1.2 thorpej int kq;
276 1.2 thorpej struct kevent kev[2];
277 1.2 thorpej
278 1.2 thorpej ATF_REQUIRE((efd = eventfd(0, EFD_NONBLOCK)) >= 0);
279 1.2 thorpej
280 1.2 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
281 1.2 thorpej EV_SET(&kev[0], efd, EVFILT_READ, EV_ADD, 0, 0, NULL);
282 1.2 thorpej EV_SET(&kev[1], efd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
283 1.2 thorpej ATF_REQUIRE(kevent(kq, kev, 2, NULL, 0, &ts) == 0);
284 1.2 thorpej
285 1.2 thorpej /*
286 1.2 thorpej * efd should be writable but not readable. Pass all of the
287 1.2 thorpej * event bits; we should only get back POLLOUT | POLLWRNORM.
288 1.2 thorpej */
289 1.2 thorpej fds[0].fd = efd;
290 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
291 1.2 thorpej POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
292 1.2 thorpej fds[0].revents = 0;
293 1.2 thorpej ATF_REQUIRE(poll(fds, 1, 0) == 1);
294 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLOUT | POLLWRNORM));
295 1.2 thorpej
296 1.2 thorpej /*
297 1.2 thorpej * As above; efd should only be set in writefds upon return
298 1.2 thorpej * from the select() call.
299 1.2 thorpej */
300 1.2 thorpej FD_ZERO(&readfds);
301 1.2 thorpej FD_ZERO(&writefds);
302 1.2 thorpej FD_ZERO(&exceptfds);
303 1.2 thorpej tv.tv_sec = 0;
304 1.2 thorpej tv.tv_usec = 0;
305 1.2 thorpej FD_SET(efd, &readfds);
306 1.2 thorpej FD_SET(efd, &writefds);
307 1.2 thorpej FD_SET(efd, &exceptfds);
308 1.2 thorpej ATF_REQUIRE(select(efd + 1, &readfds, &writefds, &exceptfds, &tv) == 1);
309 1.2 thorpej ATF_REQUIRE(!FD_ISSET(efd, &readfds));
310 1.2 thorpej ATF_REQUIRE(FD_ISSET(efd, &writefds));
311 1.2 thorpej ATF_REQUIRE(!FD_ISSET(efd, &exceptfds));
312 1.2 thorpej
313 1.2 thorpej /*
314 1.2 thorpej * Check that we get an EVFILT_WRITE event (and only that event)
315 1.2 thorpej * on efd.
316 1.2 thorpej */
317 1.2 thorpej memset(kev, 0, sizeof(kev));
318 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 2, &ts) == 1);
319 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)efd);
320 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
321 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
322 1.2 thorpej ATF_REQUIRE(kev[0].data == 0);
323 1.2 thorpej
324 1.2 thorpej /*
325 1.2 thorpej * Write the maximum value into the eventfd. This should result
326 1.2 thorpej * in the eventfd becoming readable but NOT writable.
327 1.2 thorpej */
328 1.2 thorpej ATF_REQUIRE(eventfd_write(efd, UINT64_MAX - 1) == 0);
329 1.2 thorpej
330 1.2 thorpej fds[0].fd = efd;
331 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
332 1.2 thorpej POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
333 1.2 thorpej fds[0].revents = 0;
334 1.2 thorpej ATF_REQUIRE(poll(fds, 1, 0) == 1);
335 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
336 1.2 thorpej
337 1.2 thorpej FD_ZERO(&readfds);
338 1.2 thorpej FD_ZERO(&writefds);
339 1.2 thorpej FD_ZERO(&exceptfds);
340 1.2 thorpej tv.tv_sec = 0;
341 1.2 thorpej tv.tv_usec = 0;
342 1.2 thorpej FD_SET(efd, &readfds);
343 1.2 thorpej FD_SET(efd, &writefds);
344 1.2 thorpej FD_SET(efd, &exceptfds);
345 1.2 thorpej ATF_REQUIRE(select(efd + 1, &readfds, &writefds, &exceptfds, &tv) == 1);
346 1.2 thorpej ATF_REQUIRE(FD_ISSET(efd, &readfds));
347 1.2 thorpej ATF_REQUIRE(!FD_ISSET(efd, &writefds));
348 1.2 thorpej ATF_REQUIRE(!FD_ISSET(efd, &exceptfds));
349 1.2 thorpej
350 1.2 thorpej /*
351 1.2 thorpej * Check that we get an EVFILT_READ event (and only that event)
352 1.2 thorpej * on efd.
353 1.2 thorpej */
354 1.2 thorpej memset(kev, 0, sizeof(kev));
355 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 2, &ts) == 1);
356 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)efd);
357 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_READ);
358 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
359 1.2 thorpej ATF_REQUIRE(kev[0].data == (int64_t)(UINT64_MAX - 1));
360 1.2 thorpej
361 1.2 thorpej (void) close(kq);
362 1.2 thorpej (void) close(efd);
363 1.2 thorpej }
364 1.2 thorpej
365 1.2 thorpej /*****************************************************************************/
366 1.2 thorpej
367 1.2 thorpej static void *
368 1.2 thorpej eventfd_select_poll_kevent_block_helper(void * const v)
369 1.2 thorpej {
370 1.2 thorpej struct helper_context * const ctx = v;
371 1.2 thorpej struct pollfd fds[1];
372 1.2 thorpej fd_set selfds;
373 1.2 thorpej eventfd_t efd_value;
374 1.2 thorpej int kq;
375 1.2 thorpej struct kevent kev[1];
376 1.2 thorpej
377 1.2 thorpej fds[0].fd = ctx->efd;
378 1.2 thorpej fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
379 1.2 thorpej fds[0].revents = 0;
380 1.2 thorpej
381 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
382 1.2 thorpej eventfd_read(ctx->efd, &efd_value) == -1);
383 1.2 thorpej
384 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
385 1.2 thorpej
386 1.2 thorpej ATF_REQUIRE(get_state(ctx) == 666);
387 1.2 thorpej ATF_REQUIRE(poll(fds, 1, INFTIM) == 1);
388 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
389 1.2 thorpej set_state(ctx, 0);
390 1.2 thorpej
391 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
392 1.2 thorpej
393 1.2 thorpej /*
394 1.2 thorpej * The maximum value was written to the eventfd, so we
395 1.2 thorpej * should block waiting for writability.
396 1.2 thorpej */
397 1.2 thorpej fds[0].fd = ctx->efd;
398 1.2 thorpej fds[0].events = POLLOUT | POLLWRNORM;
399 1.2 thorpej fds[0].revents = 0;
400 1.2 thorpej
401 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
402 1.2 thorpej eventfd_write(ctx->efd, UINT64_MAX - 1) == -1);
403 1.2 thorpej
404 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
405 1.2 thorpej
406 1.2 thorpej ATF_REQUIRE(get_state(ctx) == 666);
407 1.2 thorpej ATF_REQUIRE(poll(fds, 1, INFTIM) == 1);
408 1.2 thorpej ATF_REQUIRE(fds[0].revents == (POLLOUT | POLLWRNORM));
409 1.2 thorpej set_state(ctx, 0);
410 1.2 thorpej
411 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
412 1.2 thorpej
413 1.2 thorpej /*
414 1.2 thorpej * Now, the same dance again, with select().
415 1.2 thorpej */
416 1.2 thorpej
417 1.2 thorpej FD_ZERO(&selfds);
418 1.2 thorpej FD_SET(ctx->efd, &selfds);
419 1.2 thorpej
420 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
421 1.2 thorpej eventfd_read(ctx->efd, &efd_value) == -1);
422 1.2 thorpej
423 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
424 1.2 thorpej
425 1.2 thorpej ATF_REQUIRE(get_state(ctx) == 666);
426 1.2 thorpej ATF_REQUIRE(select(ctx->efd + 1, &selfds, NULL, NULL, NULL) == 1);
427 1.2 thorpej ATF_REQUIRE(FD_ISSET(ctx->efd, &selfds));
428 1.2 thorpej set_state(ctx, 0);
429 1.2 thorpej
430 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
431 1.2 thorpej
432 1.2 thorpej FD_ZERO(&selfds);
433 1.2 thorpej FD_SET(ctx->efd, &selfds);
434 1.2 thorpej
435 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
436 1.2 thorpej eventfd_write(ctx->efd, UINT64_MAX - 1) == -1);
437 1.2 thorpej
438 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
439 1.2 thorpej
440 1.2 thorpej ATF_REQUIRE(get_state(ctx) == 666);
441 1.2 thorpej ATF_REQUIRE(select(ctx->efd + 1, NULL, &selfds, NULL, NULL) == 1);
442 1.2 thorpej ATF_REQUIRE(FD_ISSET(ctx->efd, &selfds));
443 1.2 thorpej set_state(ctx, 0);
444 1.2 thorpej
445 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
446 1.2 thorpej
447 1.2 thorpej /*
448 1.2 thorpej * Now, the same dance again, with kevent().
449 1.2 thorpej */
450 1.2 thorpej ATF_REQUIRE((kq = kqueue()) >= 0);
451 1.2 thorpej
452 1.2 thorpej EV_SET(&kev[0], ctx->efd, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, NULL);
453 1.2 thorpej ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0);
454 1.2 thorpej
455 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
456 1.2 thorpej eventfd_read(ctx->efd, &efd_value) == -1);
457 1.2 thorpej
458 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
459 1.2 thorpej
460 1.2 thorpej ATF_REQUIRE(get_state(ctx) == 666);
461 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1);
462 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)ctx->efd);
463 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_READ);
464 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
465 1.2 thorpej ATF_REQUIRE(kev[0].data == (int64_t)(UINT64_MAX - 1));
466 1.2 thorpej set_state(ctx, 0);
467 1.2 thorpej
468 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
469 1.2 thorpej
470 1.2 thorpej EV_SET(&kev[0], ctx->efd, EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0,
471 1.2 thorpej NULL);
472 1.2 thorpej ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0);
473 1.2 thorpej
474 1.2 thorpej ATF_REQUIRE_ERRNO(EAGAIN,
475 1.2 thorpej eventfd_write(ctx->efd, UINT64_MAX - 1) == -1);
476 1.2 thorpej
477 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
478 1.2 thorpej
479 1.2 thorpej ATF_REQUIRE(get_state(ctx) == 666);
480 1.2 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1);
481 1.2 thorpej ATF_REQUIRE(kev[0].ident == (uintptr_t)ctx->efd);
482 1.2 thorpej ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
483 1.2 thorpej ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
484 1.2 thorpej ATF_REQUIRE(kev[0].data == 0);
485 1.2 thorpej set_state(ctx, 0);
486 1.2 thorpej
487 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
488 1.2 thorpej
489 1.2 thorpej (void) close(kq);
490 1.2 thorpej
491 1.2 thorpej return NULL;
492 1.2 thorpej }
493 1.2 thorpej
494 1.2 thorpej ATF_TC(eventfd_select_poll_kevent_block);
495 1.2 thorpej ATF_TC_HEAD(eventfd_select_poll_kevent_block, tc)
496 1.2 thorpej {
497 1.2 thorpej atf_tc_set_md_var(tc, "descr",
498 1.2 thorpej "validates select/poll/kevent behavior - return after blocking");
499 1.2 thorpej }
500 1.2 thorpej ATF_TC_BODY(eventfd_select_poll_kevent_block, tc)
501 1.2 thorpej {
502 1.2 thorpej struct helper_context ctx;
503 1.2 thorpej pthread_t helper;
504 1.2 thorpej eventfd_t efd_value;
505 1.2 thorpej void *join_val;
506 1.2 thorpej
507 1.2 thorpej init_helper_context(&ctx);
508 1.2 thorpej
509 1.2 thorpej ATF_REQUIRE((ctx.efd = eventfd(0, EFD_NONBLOCK)) >= 0);
510 1.2 thorpej
511 1.2 thorpej ATF_REQUIRE(pthread_create(&helper, NULL,
512 1.2 thorpej eventfd_select_poll_kevent_block_helper,
513 1.2 thorpej &ctx) == 0);
514 1.2 thorpej
515 1.2 thorpej /*
516 1.2 thorpej * Wait for the helper to block in poll(). Give it some time
517 1.2 thorpej * so that if the poll returns immediately, we'll notice.
518 1.2 thorpej */
519 1.2 thorpej set_state(&ctx, 666);
520 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
521 1.2 thorpej sleep(2);
522 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 666);
523 1.2 thorpej
524 1.2 thorpej /*
525 1.2 thorpej * Write the max value to the eventfd so that it becomes readable
526 1.2 thorpej * and unblocks the helper waiting in poll().
527 1.2 thorpej */
528 1.2 thorpej ATF_REQUIRE(eventfd_write(ctx.efd, UINT64_MAX - 1) == 0);
529 1.2 thorpej
530 1.2 thorpej /*
531 1.2 thorpej * Ensure the helper woke from the poll() call.
532 1.2 thorpej */
533 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
534 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 0);
535 1.2 thorpej
536 1.2 thorpej /*
537 1.2 thorpej * Wait for the helper to block in poll(), this time waiting
538 1.2 thorpej * for writability.
539 1.2 thorpej */
540 1.2 thorpej set_state(&ctx, 666);
541 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
542 1.2 thorpej sleep(2);
543 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 666);
544 1.2 thorpej
545 1.2 thorpej /*
546 1.2 thorpej * Now read the value, which will reset the eventfd to 0 and
547 1.2 thorpej * unblock the poll() call.
548 1.2 thorpej */
549 1.2 thorpej ATF_REQUIRE(eventfd_read(ctx.efd, &efd_value) == 0);
550 1.2 thorpej ATF_REQUIRE(efd_value == UINT64_MAX - 1);
551 1.2 thorpej
552 1.2 thorpej /*
553 1.2 thorpej * Ensure that the helper woke from the poll() call.
554 1.2 thorpej */
555 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
556 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 0);
557 1.2 thorpej
558 1.2 thorpej /*
559 1.2 thorpej * Wait for the helper to block in select(), waiting for readability.
560 1.2 thorpej */
561 1.2 thorpej set_state(&ctx, 666);
562 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
563 1.2 thorpej sleep(2);
564 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 666);
565 1.2 thorpej
566 1.2 thorpej /*
567 1.2 thorpej * Write the max value to the eventfd so that it becomes readable
568 1.2 thorpej * and unblocks the helper waiting in select().
569 1.2 thorpej */
570 1.2 thorpej efd_value = UINT64_MAX - 1;
571 1.2 thorpej ATF_REQUIRE(eventfd_write(ctx.efd, UINT64_MAX - 1) == 0);
572 1.2 thorpej
573 1.2 thorpej /*
574 1.2 thorpej * Ensure the helper woke from the select() call.
575 1.2 thorpej */
576 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
577 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 0);
578 1.2 thorpej
579 1.2 thorpej /*
580 1.2 thorpej * Wait for the helper to block in select(), this time waiting
581 1.2 thorpej * for writability.
582 1.2 thorpej */
583 1.2 thorpej set_state(&ctx, 666);
584 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
585 1.2 thorpej sleep(2);
586 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 666);
587 1.2 thorpej
588 1.2 thorpej /*
589 1.2 thorpej * Now read the value, which will reset the eventfd to 0 and
590 1.2 thorpej * unblock the select() call.
591 1.2 thorpej */
592 1.2 thorpej ATF_REQUIRE(eventfd_read(ctx.efd, &efd_value) == 0);
593 1.2 thorpej ATF_REQUIRE(efd_value == UINT64_MAX - 1);
594 1.2 thorpej
595 1.2 thorpej /*
596 1.2 thorpej * Ensure that the helper woke from the select() call.
597 1.2 thorpej */
598 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
599 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 0);
600 1.2 thorpej
601 1.2 thorpej /*
602 1.2 thorpej * Wait for the helper to block in kevent(), waiting for readability.
603 1.2 thorpej */
604 1.2 thorpej set_state(&ctx, 666);
605 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
606 1.2 thorpej sleep(2);
607 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 666);
608 1.2 thorpej
609 1.2 thorpej /*
610 1.2 thorpej * Write the max value to the eventfd so that it becomes readable
611 1.2 thorpej * and unblocks the helper waiting in kevent().
612 1.2 thorpej */
613 1.2 thorpej efd_value = UINT64_MAX - 1;
614 1.2 thorpej ATF_REQUIRE(eventfd_write(ctx.efd, UINT64_MAX - 1) == 0);
615 1.2 thorpej
616 1.2 thorpej /*
617 1.2 thorpej * Ensure the helper woke from the kevent() call.
618 1.2 thorpej */
619 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
620 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 0);
621 1.2 thorpej
622 1.2 thorpej /*
623 1.2 thorpej * Wait for the helper to block in kevent(), this time waiting
624 1.2 thorpej * for writability.
625 1.2 thorpej */
626 1.2 thorpej set_state(&ctx, 666);
627 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
628 1.2 thorpej sleep(2);
629 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 666);
630 1.2 thorpej
631 1.2 thorpej /*
632 1.2 thorpej * Now read the value, which will reset the eventfd to 0 and
633 1.2 thorpej * unblock the select() call.
634 1.2 thorpej */
635 1.2 thorpej ATF_REQUIRE(eventfd_read(ctx.efd, &efd_value) == 0);
636 1.2 thorpej ATF_REQUIRE(efd_value == UINT64_MAX - 1);
637 1.2 thorpej
638 1.2 thorpej /*
639 1.2 thorpej * Ensure that the helper woke from the kevent() call.
640 1.2 thorpej */
641 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
642 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 0);
643 1.2 thorpej
644 1.2 thorpej /* Reap the helper. */
645 1.2 thorpej ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
646 1.2 thorpej
647 1.2 thorpej (void) close(ctx.efd);
648 1.2 thorpej }
649 1.2 thorpej
650 1.2 thorpej /*****************************************************************************/
651 1.2 thorpej
652 1.2 thorpej static void *
653 1.2 thorpej eventfd_restart_helper(void * const v)
654 1.2 thorpej {
655 1.2 thorpej struct helper_context * const ctx = v;
656 1.2 thorpej eventfd_t efd_value;
657 1.2 thorpej
658 1.2 thorpej /*
659 1.2 thorpej * Issue a single read to ensure that the descriptor is valid.
660 1.2 thorpej * Thius will not block because it was created with an initial
661 1.2 thorpej * count of 1.
662 1.2 thorpej */
663 1.2 thorpej ATF_REQUIRE(eventfd_read(ctx->efd, &efd_value) == 0);
664 1.2 thorpej ATF_REQUIRE(efd_value == 1);
665 1.2 thorpej
666 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
667 1.2 thorpej
668 1.2 thorpej /*
669 1.2 thorpej * Block in read. The main thread will close the descriptor,
670 1.2 thorpej * which should unblock us and result in EBADF.
671 1.2 thorpej */
672 1.2 thorpej ATF_REQUIRE(get_state(ctx) == 666);
673 1.2 thorpej ATF_REQUIRE_ERRNO(EBADF, eventfd_read(ctx->efd, &efd_value) == -1);
674 1.2 thorpej set_state(ctx, 0);
675 1.2 thorpej
676 1.2 thorpej ATF_REQUIRE(wait_barrier(ctx));
677 1.2 thorpej
678 1.2 thorpej return NULL;
679 1.2 thorpej }
680 1.2 thorpej
681 1.2 thorpej ATF_TC(eventfd_restart);
682 1.2 thorpej ATF_TC_HEAD(eventfd_restart, tc)
683 1.2 thorpej {
684 1.2 thorpej atf_tc_set_md_var(tc, "descr",
685 1.2 thorpej "exercises the 'restart' fileop code path");
686 1.2 thorpej }
687 1.2 thorpej ATF_TC_BODY(eventfd_restart, tc)
688 1.2 thorpej {
689 1.2 thorpej struct helper_context ctx;
690 1.2 thorpej pthread_t helper;
691 1.2 thorpej void *join_val;
692 1.2 thorpej
693 1.2 thorpej init_helper_context(&ctx);
694 1.2 thorpej
695 1.2 thorpej ATF_REQUIRE((ctx.efd = eventfd(1, 0)) >= 0);
696 1.2 thorpej
697 1.2 thorpej ATF_REQUIRE(pthread_create(&helper, NULL,
698 1.2 thorpej eventfd_restart_helper, &ctx) == 0);
699 1.2 thorpej
700 1.2 thorpej /*
701 1.2 thorpej * Wait for the helper to block in read(). Give it some time
702 1.2 thorpej * so that if the poll returns immediately, we'll notice.
703 1.2 thorpej */
704 1.2 thorpej set_state(&ctx, 666);
705 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
706 1.2 thorpej sleep(2);
707 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 666);
708 1.2 thorpej
709 1.2 thorpej /*
710 1.2 thorpej * Close the descriptor. This should unblock the reader,
711 1.2 thorpej * and cause it to receive EBADF.
712 1.2 thorpej */
713 1.2 thorpej ATF_REQUIRE(close(ctx.efd) == 0);
714 1.2 thorpej
715 1.2 thorpej /*
716 1.2 thorpej * Ensure that the helper woke from the read() call.
717 1.2 thorpej */
718 1.2 thorpej ATF_REQUIRE(wait_barrier(&ctx));
719 1.2 thorpej ATF_REQUIRE(get_state(&ctx) == 0);
720 1.2 thorpej
721 1.2 thorpej /* Reap the helper. */
722 1.2 thorpej ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
723 1.2 thorpej }
724 1.2 thorpej
725 1.2 thorpej /*****************************************************************************/
726 1.2 thorpej
727 1.2 thorpej ATF_TC(eventfd_badflags);
728 1.2 thorpej ATF_TC_HEAD(eventfd_badflags, tc)
729 1.2 thorpej {
730 1.2 thorpej atf_tc_set_md_var(tc, "descr",
731 1.2 thorpej "validates behavior when eventfd() called with bad flags");
732 1.2 thorpej }
733 1.2 thorpej ATF_TC_BODY(eventfd_badflags, tc)
734 1.2 thorpej {
735 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
736 1.2 thorpej eventfd(0, ~(EFD_SEMAPHORE | EFD_CLOEXEC | EFD_NONBLOCK)) == -1);
737 1.2 thorpej }
738 1.2 thorpej
739 1.2 thorpej /*****************************************************************************/
740 1.2 thorpej
741 1.2 thorpej ATF_TC(eventfd_bufsize);
742 1.2 thorpej ATF_TC_HEAD(eventfd_bufsize, tc)
743 1.2 thorpej {
744 1.2 thorpej atf_tc_set_md_var(tc, "descr",
745 1.2 thorpej "validates expected buffer size behavior");
746 1.2 thorpej }
747 1.2 thorpej ATF_TC_BODY(eventfd_bufsize, tc)
748 1.2 thorpej {
749 1.2 thorpej eventfd_t efd_value[2];
750 1.2 thorpej int efd;
751 1.2 thorpej
752 1.2 thorpej ATF_REQUIRE((efd = eventfd(1, EFD_NONBLOCK)) >= 0);
753 1.2 thorpej
754 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
755 1.2 thorpej read(efd, efd_value, sizeof(efd_value[0]) - 1) == -1);
756 1.2 thorpej
757 1.2 thorpej efd_value[0] = 0xdeadbeef;
758 1.2 thorpej efd_value[1] = 0xdeadbeef;
759 1.2 thorpej ATF_REQUIRE(read(efd, efd_value, sizeof(efd_value)) ==
760 1.2 thorpej sizeof(efd_value[0]));
761 1.2 thorpej ATF_REQUIRE(efd_value[0] == 1);
762 1.2 thorpej ATF_REQUIRE(efd_value[1] == 0xdeadbeef);
763 1.2 thorpej
764 1.2 thorpej ATF_REQUIRE_ERRNO(EINVAL,
765 1.2 thorpej write(efd, efd_value, sizeof(efd_value[0]) - 1) == -1);
766 1.2 thorpej ATF_REQUIRE(write(efd, efd_value, sizeof(efd_value)) ==
767 1.2 thorpej sizeof(efd_value[0]));
768 1.2 thorpej
769 1.2 thorpej ATF_REQUIRE(read(efd, efd_value, sizeof(efd_value)) ==
770 1.2 thorpej sizeof(efd_value[0]));
771 1.2 thorpej ATF_REQUIRE(efd_value[0] == 1);
772 1.2 thorpej ATF_REQUIRE(efd_value[1] == 0xdeadbeef);
773 1.2 thorpej
774 1.2 thorpej (void) close(efd);
775 1.2 thorpej }
776 1.2 thorpej
777 1.2 thorpej /*****************************************************************************/
778 1.2 thorpej
779 1.2 thorpej ATF_TP_ADD_TCS(tp)
780 1.2 thorpej {
781 1.2 thorpej ATF_TP_ADD_TC(tp, eventfd_normal);
782 1.2 thorpej ATF_TP_ADD_TC(tp, eventfd_semaphore);
783 1.2 thorpej ATF_TP_ADD_TC(tp, eventfd_badflags);
784 1.2 thorpej ATF_TP_ADD_TC(tp, eventfd_bufsize);
785 1.2 thorpej ATF_TP_ADD_TC(tp, eventfd_select_poll_kevent_immed);
786 1.2 thorpej ATF_TP_ADD_TC(tp, eventfd_select_poll_kevent_block);
787 1.2 thorpej ATF_TP_ADD_TC(tp, eventfd_restart);
788 1.2 thorpej
789 1.2 thorpej return atf_no_error();
790 1.2 thorpej }
791