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