Home | History | Annotate | Line # | Download | only in sys
t_timerfd.c revision 1.4
      1  1.4  thorpej /* $NetBSD: t_timerfd.c,v 1.4 2022/02/20 15:21:14 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.4  thorpej __RCSID("$NetBSD: t_timerfd.c,v 1.4 2022/02/20 15:21:14 thorpej Exp $");
     33  1.2  thorpej 
     34  1.2  thorpej #include <sys/types.h>
     35  1.2  thorpej #include <sys/event.h>
     36  1.4  thorpej #include <sys/ioctl.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 <sys/timerfd.h>
     41  1.2  thorpej #include <errno.h>
     42  1.2  thorpej #include <poll.h>
     43  1.2  thorpej #include <pthread.h>
     44  1.2  thorpej #include <stdlib.h>
     45  1.2  thorpej #include <stdio.h>
     46  1.2  thorpej #include <time.h>
     47  1.2  thorpej #include <unistd.h>
     48  1.2  thorpej 
     49  1.2  thorpej #include <atf-c.h>
     50  1.2  thorpej 
     51  1.3  hannken #include "isqemu.h"
     52  1.3  hannken 
     53  1.2  thorpej struct helper_context {
     54  1.2  thorpej 	int	fd;
     55  1.2  thorpej 
     56  1.2  thorpej 	pthread_barrier_t barrier;
     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 
     63  1.2  thorpej 	memset(ctx, 0, sizeof(*ctx));
     64  1.2  thorpej 
     65  1.2  thorpej 	ATF_REQUIRE(pthread_barrier_init(&ctx->barrier, NULL, 2) == 0);
     66  1.2  thorpej }
     67  1.2  thorpej 
     68  1.2  thorpej static bool
     69  1.2  thorpej wait_barrier(struct helper_context * const ctx)
     70  1.2  thorpej {
     71  1.2  thorpej 	int rv = pthread_barrier_wait(&ctx->barrier);
     72  1.2  thorpej 
     73  1.2  thorpej 	return rv == 0 || rv == PTHREAD_BARRIER_SERIAL_THREAD;
     74  1.2  thorpej }
     75  1.2  thorpej 
     76  1.3  hannken static bool
     77  1.3  hannken check_value_against_bounds(uint64_t value, uint64_t lower, uint64_t upper)
     78  1.3  hannken {
     79  1.3  hannken 
     80  1.3  hannken 	/*
     81  1.3  hannken 	 * If running under QEMU make sure the upper bound is large
     82  1.3  hannken 	 * enough for the effect of kern/43997
     83  1.3  hannken 	 */
     84  1.3  hannken 	if (isQEMU()) {
     85  1.3  hannken 		upper *= 4;
     86  1.3  hannken 	}
     87  1.3  hannken 
     88  1.3  hannken 	if (value < lower || value > upper) {
     89  1.3  hannken 		printf("val %" PRIu64 " not in [ %" PRIu64 ", %" PRIu64 " ]\n",
     90  1.3  hannken 		    value, lower, upper);
     91  1.3  hannken 	}
     92  1.3  hannken 
     93  1.3  hannken 	return value >= lower && value <= upper;
     94  1.3  hannken }
     95  1.3  hannken 
     96  1.2  thorpej /*****************************************************************************/
     97  1.2  thorpej 
     98  1.2  thorpej static int
     99  1.2  thorpej timerfd_read(int fd, uint64_t *valp)
    100  1.2  thorpej {
    101  1.2  thorpej 	uint64_t val;
    102  1.2  thorpej 
    103  1.2  thorpej 	switch (read(fd, &val, sizeof(val))) {
    104  1.2  thorpej 	case -1:
    105  1.2  thorpej 		return -1;
    106  1.2  thorpej 
    107  1.2  thorpej 	case sizeof(val):
    108  1.2  thorpej 		*valp = val;
    109  1.2  thorpej 		return 0;
    110  1.2  thorpej 
    111  1.2  thorpej 	default:
    112  1.2  thorpej 		/* ?? Should never happen. */
    113  1.2  thorpej 		errno = EIO;
    114  1.2  thorpej 		return -1;
    115  1.2  thorpej 	}
    116  1.2  thorpej }
    117  1.2  thorpej 
    118  1.2  thorpej /*****************************************************************************/
    119  1.2  thorpej 
    120  1.2  thorpej ATF_TC(timerfd_create);
    121  1.2  thorpej ATF_TC_HEAD(timerfd_create, tc)
    122  1.2  thorpej {
    123  1.2  thorpej 	atf_tc_set_md_var(tc, "descr", "validates timerfd_create()");
    124  1.2  thorpej }
    125  1.2  thorpej ATF_TC_BODY(timerfd_create, tc)
    126  1.2  thorpej {
    127  1.2  thorpej 	int fd;
    128  1.2  thorpej 
    129  1.2  thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
    130  1.2  thorpej 	(void) close(fd);
    131  1.2  thorpej 
    132  1.2  thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    133  1.2  thorpej 	(void) close(fd);
    134  1.2  thorpej 
    135  1.2  thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    136  1.2  thorpej 	    (fd = timerfd_create(CLOCK_VIRTUAL, 0)) == -1);
    137  1.2  thorpej 
    138  1.2  thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    139  1.2  thorpej 	    (fd = timerfd_create(CLOCK_PROF, 0)) == -1);
    140  1.2  thorpej 
    141  1.2  thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    142  1.2  thorpej 	    (fd = timerfd_create(CLOCK_REALTIME,
    143  1.2  thorpej 	    			    ~(TFD_CLOEXEC | TFD_NONBLOCK))) == -1);
    144  1.2  thorpej }
    145  1.2  thorpej 
    146  1.2  thorpej /*****************************************************************************/
    147  1.2  thorpej 
    148  1.2  thorpej ATF_TC(timerfd_bogusfd);
    149  1.2  thorpej ATF_TC_HEAD(timerfd_bogusfd, tc)
    150  1.2  thorpej {
    151  1.2  thorpej 	atf_tc_set_md_var(tc, "descr",
    152  1.2  thorpej 	    "validates rejection of bogus fds by timerfd_{get,set}time()");
    153  1.2  thorpej }
    154  1.2  thorpej ATF_TC_BODY(timerfd_bogusfd, tc)
    155  1.2  thorpej {
    156  1.2  thorpej 	struct itimerspec its = { 0 };
    157  1.2  thorpej 	int fd;
    158  1.2  thorpej 
    159  1.2  thorpej 	ATF_REQUIRE((fd = kqueue()) >= 0);	/* arbitrary fd type */
    160  1.2  thorpej 
    161  1.2  thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    162  1.2  thorpej 	    timerfd_gettime(fd, &its) == -1);
    163  1.2  thorpej 
    164  1.2  thorpej 	its.it_value.tv_sec = 5;
    165  1.2  thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    166  1.2  thorpej 	    timerfd_settime(fd, 0, &its, NULL) == -1);
    167  1.2  thorpej 
    168  1.2  thorpej 	(void) close(fd);
    169  1.2  thorpej }
    170  1.2  thorpej 
    171  1.2  thorpej /*****************************************************************************/
    172  1.2  thorpej 
    173  1.2  thorpej ATF_TC(timerfd_block);
    174  1.2  thorpej ATF_TC_HEAD(timerfd_block, tc)
    175  1.2  thorpej {
    176  1.2  thorpej 	atf_tc_set_md_var(tc, "descr", "validates blocking behavior");
    177  1.2  thorpej }
    178  1.2  thorpej ATF_TC_BODY(timerfd_block, tc)
    179  1.2  thorpej {
    180  1.2  thorpej 	struct timespec then, now, delta;
    181  1.2  thorpej 	uint64_t val;
    182  1.2  thorpej 	int fd;
    183  1.2  thorpej 
    184  1.2  thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    185  1.2  thorpej 
    186  1.2  thorpej 	const struct itimerspec its = {
    187  1.2  thorpej 		.it_value = { .tv_sec = 1, .tv_nsec = 0 },
    188  1.2  thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    189  1.2  thorpej 	};
    190  1.2  thorpej 
    191  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    192  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    193  1.2  thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    194  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    195  1.3  hannken 	ATF_REQUIRE(check_value_against_bounds(val, 1, 1));
    196  1.2  thorpej 
    197  1.2  thorpej 	timespecsub(&now, &then, &delta);
    198  1.3  hannken 	ATF_REQUIRE(check_value_against_bounds(delta.tv_sec, 1, 1));
    199  1.2  thorpej 
    200  1.2  thorpej 	(void) close(fd);
    201  1.2  thorpej }
    202  1.2  thorpej 
    203  1.2  thorpej /*****************************************************************************/
    204  1.2  thorpej 
    205  1.2  thorpej ATF_TC(timerfd_repeating);
    206  1.2  thorpej ATF_TC_HEAD(timerfd_repeating, tc)
    207  1.2  thorpej {
    208  1.2  thorpej 	atf_tc_set_md_var(tc, "descr", "validates repeating timer behavior");
    209  1.2  thorpej }
    210  1.2  thorpej ATF_TC_BODY(timerfd_repeating, tc)
    211  1.2  thorpej {
    212  1.2  thorpej 	struct timespec then, now, delta;
    213  1.2  thorpej 	uint64_t val;
    214  1.2  thorpej 	int fd;
    215  1.2  thorpej 
    216  1.2  thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC,
    217  1.2  thorpej 					    TFD_NONBLOCK)) >= 0);
    218  1.2  thorpej 
    219  1.2  thorpej 	const struct itimerspec its = {
    220  1.2  thorpej 		.it_value = { .tv_sec = 0, .tv_nsec = 200000000 },
    221  1.2  thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 200000000 },
    222  1.2  thorpej 	};
    223  1.2  thorpej 
    224  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    225  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    226  1.2  thorpej 	ATF_REQUIRE(sleep(1) == 0);
    227  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    228  1.2  thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    229  1.3  hannken 	/* allow some slop */
    230  1.3  hannken 	ATF_REQUIRE(check_value_against_bounds(val, 3, 5));
    231  1.2  thorpej 
    232  1.2  thorpej 	timespecsub(&now, &then, &delta);
    233  1.3  hannken 	ATF_REQUIRE(check_value_against_bounds(delta.tv_sec, 1, 1));
    234  1.2  thorpej 
    235  1.2  thorpej 	(void) close(fd);
    236  1.2  thorpej }
    237  1.2  thorpej 
    238  1.2  thorpej /*****************************************************************************/
    239  1.2  thorpej 
    240  1.2  thorpej ATF_TC(timerfd_abstime);
    241  1.2  thorpej ATF_TC_HEAD(timerfd_abstime, tc)
    242  1.2  thorpej {
    243  1.2  thorpej 	atf_tc_set_md_var(tc, "descr", "validates specifying abstime");
    244  1.2  thorpej }
    245  1.2  thorpej ATF_TC_BODY(timerfd_abstime, tc)
    246  1.2  thorpej {
    247  1.2  thorpej 	struct timespec then, now, delta;
    248  1.2  thorpej 	uint64_t val;
    249  1.2  thorpej 	int fd;
    250  1.2  thorpej 
    251  1.2  thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    252  1.2  thorpej 
    253  1.2  thorpej 	struct itimerspec its = {
    254  1.2  thorpej 		.it_value = { .tv_sec = 0, .tv_nsec = 0 },
    255  1.2  thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    256  1.2  thorpej 	};
    257  1.2  thorpej 
    258  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    259  1.2  thorpej 	its.it_value = then;
    260  1.2  thorpej 	its.it_value.tv_sec += 1;
    261  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, NULL) == 0);
    262  1.2  thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    263  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    264  1.3  hannken 	ATF_REQUIRE(check_value_against_bounds(val, 1, 1));
    265  1.2  thorpej 
    266  1.2  thorpej 	timespecsub(&now, &then, &delta);
    267  1.3  hannken 	ATF_REQUIRE(check_value_against_bounds(delta.tv_sec, 1, 1));
    268  1.2  thorpej 
    269  1.2  thorpej 	(void) close(fd);
    270  1.2  thorpej }
    271  1.2  thorpej 
    272  1.2  thorpej /*****************************************************************************/
    273  1.2  thorpej 
    274  1.2  thorpej ATF_TC(timerfd_cancel_on_set_immed);
    275  1.2  thorpej ATF_TC_HEAD(timerfd_cancel_on_set_immed, tc)
    276  1.2  thorpej {
    277  1.2  thorpej 	atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - immediate");
    278  1.2  thorpej 	atf_tc_set_md_var(tc, "require.user", "root");
    279  1.2  thorpej }
    280  1.2  thorpej ATF_TC_BODY(timerfd_cancel_on_set_immed, tc)
    281  1.2  thorpej {
    282  1.2  thorpej 	struct timespec now;
    283  1.2  thorpej 	uint64_t val;
    284  1.2  thorpej 	int fd;
    285  1.2  thorpej 
    286  1.2  thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
    287  1.2  thorpej 
    288  1.2  thorpej 	const struct itimerspec its = {
    289  1.2  thorpej 		.it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
    290  1.2  thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    291  1.2  thorpej 	};
    292  1.2  thorpej 
    293  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
    294  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
    295  1.2  thorpej 				    &its, NULL) == 0);
    296  1.2  thorpej 	ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
    297  1.2  thorpej 	ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
    298  1.2  thorpej 
    299  1.2  thorpej 	(void) close(fd);
    300  1.2  thorpej }
    301  1.2  thorpej 
    302  1.2  thorpej /*****************************************************************************/
    303  1.2  thorpej 
    304  1.2  thorpej static void *
    305  1.2  thorpej timerfd_cancel_on_set_block_helper(void * const v)
    306  1.2  thorpej {
    307  1.2  thorpej 	struct helper_context * const ctx = v;
    308  1.2  thorpej 	struct timespec now;
    309  1.2  thorpej 
    310  1.2  thorpej 	ATF_REQUIRE(wait_barrier(ctx));
    311  1.2  thorpej 
    312  1.2  thorpej 	ATF_REQUIRE(sleep(2) == 0);
    313  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
    314  1.2  thorpej 	ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
    315  1.2  thorpej 
    316  1.2  thorpej 	return NULL;
    317  1.2  thorpej }
    318  1.2  thorpej 
    319  1.2  thorpej ATF_TC(timerfd_cancel_on_set_block);
    320  1.2  thorpej ATF_TC_HEAD(timerfd_cancel_on_set_block, tc)
    321  1.2  thorpej {
    322  1.2  thorpej 	atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - blocking");
    323  1.2  thorpej 	atf_tc_set_md_var(tc, "require.user", "root");
    324  1.2  thorpej }
    325  1.2  thorpej ATF_TC_BODY(timerfd_cancel_on_set_block, tc)
    326  1.2  thorpej {
    327  1.2  thorpej 	struct helper_context ctx;
    328  1.2  thorpej 	pthread_t helper;
    329  1.2  thorpej 	void *join_val;
    330  1.2  thorpej 	uint64_t val;
    331  1.2  thorpej 	int fd;
    332  1.2  thorpej 
    333  1.2  thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
    334  1.2  thorpej 
    335  1.2  thorpej 	const struct itimerspec its = {
    336  1.2  thorpej 		.it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
    337  1.2  thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    338  1.2  thorpej 	};
    339  1.2  thorpej 
    340  1.2  thorpej 	init_helper_context(&ctx);
    341  1.2  thorpej 
    342  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
    343  1.2  thorpej 				    &its, NULL) == 0);
    344  1.2  thorpej 	ATF_REQUIRE(pthread_create(&helper, NULL,
    345  1.2  thorpej 				timerfd_cancel_on_set_block_helper, &ctx) == 0);
    346  1.2  thorpej 	ATF_REQUIRE(wait_barrier(&ctx));
    347  1.2  thorpej 	ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
    348  1.2  thorpej 
    349  1.2  thorpej 	ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
    350  1.2  thorpej 
    351  1.2  thorpej 	(void) close(fd);
    352  1.2  thorpej }
    353  1.2  thorpej 
    354  1.2  thorpej /*****************************************************************************/
    355  1.2  thorpej 
    356  1.2  thorpej ATF_TC(timerfd_select_poll_kevent_immed);
    357  1.2  thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_immed, tc)
    358  1.2  thorpej {
    359  1.2  thorpej 	atf_tc_set_md_var(tc, "descr",
    360  1.2  thorpej 	    "validates select/poll/kevent behavior - immediate return");
    361  1.2  thorpej }
    362  1.2  thorpej ATF_TC_BODY(timerfd_select_poll_kevent_immed, tc)
    363  1.2  thorpej {
    364  1.2  thorpej 	const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
    365  1.2  thorpej 	struct itimerspec its;
    366  1.2  thorpej 	struct timeval tv;
    367  1.2  thorpej 	struct stat st;
    368  1.2  thorpej 	struct pollfd fds[1];
    369  1.2  thorpej 	uint64_t val;
    370  1.2  thorpej 	fd_set readfds, writefds, exceptfds;
    371  1.2  thorpej 	int fd;
    372  1.2  thorpej 	int kq;
    373  1.2  thorpej 	struct kevent kev[1];
    374  1.2  thorpej 
    375  1.2  thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
    376  1.2  thorpej 
    377  1.2  thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
    378  1.2  thorpej 	EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
    379  1.2  thorpej 	ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
    380  1.2  thorpej 
    381  1.2  thorpej 	/*
    382  1.2  thorpej 	 * fd should be writable but not readable.  Pass all of the
    383  1.2  thorpej 	 * event bits; we should only get back POLLOUT | POLLWRNORM.
    384  1.2  thorpej 	 * (It's writable only in so far as we'll get an error if we try.)
    385  1.2  thorpej 	 */
    386  1.2  thorpej 	fds[0].fd = fd;
    387  1.2  thorpej 	fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
    388  1.2  thorpej 	    POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
    389  1.2  thorpej 	fds[0].revents = 0;
    390  1.2  thorpej 	ATF_REQUIRE(poll(fds, 1, 0) == 1);
    391  1.2  thorpej 	ATF_REQUIRE(fds[0].revents == (POLLOUT | POLLWRNORM));
    392  1.2  thorpej 
    393  1.2  thorpej 	/*
    394  1.2  thorpej 	 * As above; fd should only be set in writefds upon return
    395  1.2  thorpej 	 * from the select() call.
    396  1.2  thorpej 	 */
    397  1.2  thorpej 	FD_ZERO(&readfds);
    398  1.2  thorpej 	FD_ZERO(&writefds);
    399  1.2  thorpej 	FD_ZERO(&exceptfds);
    400  1.2  thorpej 	tv.tv_sec = 0;
    401  1.2  thorpej 	tv.tv_usec = 0;
    402  1.2  thorpej 	FD_SET(fd, &readfds);
    403  1.2  thorpej 	FD_SET(fd, &writefds);
    404  1.2  thorpej 	FD_SET(fd, &exceptfds);
    405  1.2  thorpej 	ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 1);
    406  1.2  thorpej 	ATF_REQUIRE(!FD_ISSET(fd, &readfds));
    407  1.2  thorpej 	ATF_REQUIRE(FD_ISSET(fd, &writefds));
    408  1.2  thorpej 	ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
    409  1.2  thorpej 
    410  1.2  thorpej 	/*
    411  1.2  thorpej 	 * Now set a one-shot half-second timer, wait for it to expire, and
    412  1.2  thorpej 	 * then check again.
    413  1.2  thorpej 	 */
    414  1.2  thorpej 	memset(&its, 0, sizeof(its));
    415  1.2  thorpej 	its.it_value.tv_sec = 0;
    416  1.2  thorpej 	its.it_value.tv_nsec = 500000000;
    417  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    418  1.2  thorpej 	ATF_REQUIRE(sleep(2) == 0);
    419  1.2  thorpej 
    420  1.2  thorpej 	/* Verify it actually fired via the stat() back-channel. */
    421  1.2  thorpej 	ATF_REQUIRE(fstat(fd, &st) == 0);
    422  1.2  thorpej 	ATF_REQUIRE(st.st_size == 1);
    423  1.2  thorpej 
    424  1.2  thorpej 	fds[0].fd = fd;
    425  1.2  thorpej 	fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
    426  1.2  thorpej 	    POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
    427  1.2  thorpej 	fds[0].revents = 0;
    428  1.2  thorpej 	ATF_REQUIRE(poll(fds, 1, 0) == 1);
    429  1.2  thorpej 	ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM |
    430  1.2  thorpej 				       POLLOUT | POLLWRNORM));
    431  1.2  thorpej 
    432  1.2  thorpej 	FD_ZERO(&readfds);
    433  1.2  thorpej 	FD_ZERO(&writefds);
    434  1.2  thorpej 	FD_ZERO(&exceptfds);
    435  1.2  thorpej 	tv.tv_sec = 0;
    436  1.2  thorpej 	tv.tv_usec = 0;
    437  1.2  thorpej 	FD_SET(fd, &readfds);
    438  1.2  thorpej 	FD_SET(fd, &writefds);
    439  1.2  thorpej 	FD_SET(fd, &exceptfds);
    440  1.2  thorpej 	ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 2);
    441  1.2  thorpej 	ATF_REQUIRE(FD_ISSET(fd, &readfds));
    442  1.2  thorpej 	ATF_REQUIRE(FD_ISSET(fd, &writefds));
    443  1.2  thorpej 	ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
    444  1.2  thorpej 
    445  1.2  thorpej 	/*
    446  1.2  thorpej 	 * Check that we get an EVFILT_READ event on fd.
    447  1.2  thorpej 	 */
    448  1.2  thorpej 	memset(kev, 0, sizeof(kev));
    449  1.2  thorpej 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, &ts) == 1);
    450  1.2  thorpej 	ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
    451  1.2  thorpej 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
    452  1.2  thorpej 	ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
    453  1.2  thorpej 	ATF_REQUIRE(kev[0].data == 1);
    454  1.2  thorpej 
    455  1.2  thorpej 	/*
    456  1.2  thorpej 	 * Read the timerfd to ensure we get the correct numnber of
    457  1.2  thorpej 	 * expirations.
    458  1.2  thorpej 	 */
    459  1.2  thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    460  1.2  thorpej 	ATF_REQUIRE(val == 1);
    461  1.2  thorpej 
    462  1.2  thorpej 	/* And ensure that we would block if we tried again. */
    463  1.2  thorpej 	ATF_REQUIRE_ERRNO(EAGAIN, timerfd_read(fd, &val) == -1);
    464  1.2  thorpej 
    465  1.2  thorpej 	(void) close(kq);
    466  1.2  thorpej 	(void) close(fd);
    467  1.2  thorpej }
    468  1.2  thorpej 
    469  1.2  thorpej /*****************************************************************************/
    470  1.2  thorpej 
    471  1.2  thorpej ATF_TC(timerfd_select_poll_kevent_block);
    472  1.2  thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_block, tc)
    473  1.2  thorpej {
    474  1.2  thorpej 	atf_tc_set_md_var(tc, "descr",
    475  1.2  thorpej 	    "validates select/poll/kevent behavior - blocking");
    476  1.2  thorpej }
    477  1.2  thorpej ATF_TC_BODY(timerfd_select_poll_kevent_block, tc)
    478  1.2  thorpej {
    479  1.2  thorpej 	const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
    480  1.2  thorpej 	struct timespec then, now;
    481  1.2  thorpej 	struct pollfd fds[1];
    482  1.2  thorpej 	fd_set readfds;
    483  1.2  thorpej 	int fd;
    484  1.2  thorpej 	int kq;
    485  1.2  thorpej 	struct kevent kev[1];
    486  1.2  thorpej 
    487  1.2  thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
    488  1.2  thorpej 
    489  1.2  thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
    490  1.2  thorpej 	EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
    491  1.2  thorpej 	ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
    492  1.2  thorpej 
    493  1.2  thorpej 	/*
    494  1.2  thorpej 	 * For each of these tests, we do the following:
    495  1.2  thorpej 	 *
    496  1.2  thorpej 	 * - Get the current time.
    497  1.2  thorpej 	 * - Set a 1-second one-shot timer.
    498  1.2  thorpej 	 * - Block in the multiplexing call.
    499  1.2  thorpej 	 * - Get the current time and verify that the timer expiration
    500  1.2  thorpej 	 *   interval has passed.
    501  1.2  thorpej 	 */
    502  1.2  thorpej 
    503  1.2  thorpej 	const struct itimerspec its = {
    504  1.2  thorpej 		.it_value = { .tv_sec = 1, .tv_nsec = 0 },
    505  1.2  thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    506  1.2  thorpej 	};
    507  1.2  thorpej 
    508  1.2  thorpej 	/* poll(2) */
    509  1.2  thorpej 	fds[0].fd = fd;
    510  1.2  thorpej 	fds[0].events = POLLIN | POLLRDNORM;
    511  1.2  thorpej 	fds[0].revents = 0;
    512  1.2  thorpej 
    513  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    514  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    515  1.2  thorpej 	ATF_REQUIRE(poll(fds, 1, INFTIM) == 1);
    516  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    517  1.2  thorpej 	ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
    518  1.2  thorpej 	ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
    519  1.2  thorpej 
    520  1.2  thorpej 	/* select(2) */
    521  1.2  thorpej 	FD_ZERO(&readfds);
    522  1.2  thorpej 	FD_SET(fd, &readfds);
    523  1.2  thorpej 
    524  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    525  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    526  1.2  thorpej 	ATF_REQUIRE(select(fd + 1, &readfds, NULL, NULL, NULL) == 1);
    527  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    528  1.2  thorpej 	ATF_REQUIRE(FD_ISSET(fd, &readfds));
    529  1.2  thorpej 	ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
    530  1.2  thorpej 
    531  1.2  thorpej 	/* kevent(2) */
    532  1.2  thorpej 	memset(kev, 0, sizeof(kev));
    533  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    534  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    535  1.2  thorpej 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1);
    536  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    537  1.2  thorpej 	ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
    538  1.2  thorpej 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
    539  1.2  thorpej 	ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
    540  1.2  thorpej 	ATF_REQUIRE(kev[0].data == 1);
    541  1.2  thorpej 
    542  1.2  thorpej 	(void) close(kq);
    543  1.2  thorpej 	(void) close(fd);
    544  1.2  thorpej }
    545  1.2  thorpej 
    546  1.2  thorpej /*****************************************************************************/
    547  1.2  thorpej 
    548  1.2  thorpej static void *
    549  1.2  thorpej timerfd_restart_helper(void * const v)
    550  1.2  thorpej {
    551  1.2  thorpej 	struct helper_context * const ctx = v;
    552  1.2  thorpej 
    553  1.2  thorpej 	ATF_REQUIRE(wait_barrier(ctx));
    554  1.2  thorpej 
    555  1.2  thorpej 	/*
    556  1.2  thorpej 	 * Wait 5 seconds (that should give the main thread time to
    557  1.2  thorpej 	 * block), and then close the descriptor.
    558  1.2  thorpej 	 */
    559  1.2  thorpej 	ATF_REQUIRE(sleep(5) == 0);
    560  1.2  thorpej 	ATF_REQUIRE(close(ctx->fd) == 0);
    561  1.2  thorpej 
    562  1.2  thorpej 	return NULL;
    563  1.2  thorpej }
    564  1.2  thorpej 
    565  1.2  thorpej ATF_TC(timerfd_restart);
    566  1.2  thorpej ATF_TC_HEAD(timerfd_restart, tc)
    567  1.2  thorpej {
    568  1.2  thorpej 	atf_tc_set_md_var(tc, "descr",
    569  1.2  thorpej 	    "exercises the 'restart' fileop code path");
    570  1.2  thorpej }
    571  1.2  thorpej ATF_TC_BODY(timerfd_restart, tc)
    572  1.2  thorpej {
    573  1.2  thorpej 	struct timespec then, now, delta;
    574  1.2  thorpej 	struct helper_context ctx;
    575  1.2  thorpej 	uint64_t val;
    576  1.2  thorpej 	pthread_t helper;
    577  1.2  thorpej 	void *join_val;
    578  1.2  thorpej 
    579  1.2  thorpej 	init_helper_context(&ctx);
    580  1.2  thorpej 
    581  1.2  thorpej 	ATF_REQUIRE((ctx.fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    582  1.2  thorpej 
    583  1.2  thorpej 	const struct itimerspec its = {
    584  1.2  thorpej 		.it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
    585  1.2  thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    586  1.2  thorpej 	};
    587  1.2  thorpej 	ATF_REQUIRE(timerfd_settime(ctx.fd, 0, &its, NULL) == 0);
    588  1.2  thorpej 
    589  1.2  thorpej 
    590  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    591  1.2  thorpej 	ATF_REQUIRE(pthread_create(&helper, NULL,
    592  1.2  thorpej 				   timerfd_restart_helper, &ctx) == 0);
    593  1.2  thorpej 
    594  1.2  thorpej 	/*
    595  1.2  thorpej 	 * Wait for the helper to be ready, and then immediately block
    596  1.2  thorpej 	 * in read().  The helper will close the file, and we should get
    597  1.2  thorpej 	 * EBADF after a few seconds.
    598  1.2  thorpej 	 */
    599  1.2  thorpej 	ATF_REQUIRE(wait_barrier(&ctx));
    600  1.2  thorpej 	ATF_REQUIRE_ERRNO(EBADF, timerfd_read(ctx.fd, &val) == -1);
    601  1.2  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    602  1.2  thorpej 
    603  1.2  thorpej 	timespecsub(&now, &then, &delta);
    604  1.2  thorpej 	ATF_REQUIRE(delta.tv_sec >= 5);
    605  1.2  thorpej 
    606  1.2  thorpej 	/* Reap the helper. */
    607  1.2  thorpej 	ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
    608  1.2  thorpej }
    609  1.2  thorpej 
    610  1.2  thorpej /*****************************************************************************/
    611  1.2  thorpej 
    612  1.4  thorpej ATF_TC(timerfd_fcntl);
    613  1.4  thorpej ATF_TC_HEAD(timerfd_fcntl, tc)
    614  1.4  thorpej {
    615  1.4  thorpej 	atf_tc_set_md_var(tc, "descr",
    616  1.4  thorpej 	    "validates fcntl behavior");
    617  1.4  thorpej }
    618  1.4  thorpej 
    619  1.4  thorpej ATF_TC_BODY(timerfd_fcntl, tc)
    620  1.4  thorpej {
    621  1.4  thorpej 	int tfd;
    622  1.4  thorpej 	int val;
    623  1.4  thorpej 
    624  1.4  thorpej 	ATF_REQUIRE((tfd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    625  1.4  thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFL) & O_NONBLOCK) == 0);
    626  1.4  thorpej 	ATF_REQUIRE(fcntl(tfd, F_SETFL, O_NONBLOCK) == 0);
    627  1.4  thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFL) & O_NONBLOCK) != 0);
    628  1.4  thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) == 0);
    629  1.4  thorpej 
    630  1.4  thorpej 	/* If the timer hasn't fired, there is no readable data. */
    631  1.4  thorpej 	ATF_REQUIRE(ioctl(tfd, FIONREAD, &val) == 0);
    632  1.4  thorpej 	ATF_REQUIRE(val == 0);
    633  1.4  thorpej 
    634  1.4  thorpej 	ATF_REQUIRE_ERRNO(ENOTTY, ioctl(tfd, FIONWRITE, &val) == -1);
    635  1.4  thorpej 	ATF_REQUIRE_ERRNO(ENOTTY, ioctl(tfd, FIONSPACE, &val) == -1);
    636  1.4  thorpej 	(void)close(tfd);
    637  1.4  thorpej 
    638  1.4  thorpej 	ATF_REQUIRE((tfd = timerfd_create(CLOCK_MONOTONIC,
    639  1.4  thorpej 					  TFD_NONBLOCK | TFD_CLOEXEC)) >= 0);
    640  1.4  thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFL) & ~O_ACCMODE) == O_NONBLOCK);
    641  1.4  thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) != 0);
    642  1.4  thorpej 	ATF_REQUIRE(fcntl(tfd, F_SETFD, 0) == 0);
    643  1.4  thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) == 0);
    644  1.4  thorpej 	ATF_REQUIRE(fcntl(tfd, F_SETFD, FD_CLOEXEC) == 0);
    645  1.4  thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) != 0);
    646  1.4  thorpej 	(void)close(tfd);
    647  1.4  thorpej }
    648  1.4  thorpej 
    649  1.4  thorpej /*****************************************************************************/
    650  1.4  thorpej 
    651  1.2  thorpej ATF_TP_ADD_TCS(tp)
    652  1.2  thorpej {
    653  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_create);
    654  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_bogusfd);
    655  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_block);
    656  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_repeating);
    657  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_abstime);
    658  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_block);
    659  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_immed);
    660  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_immed);
    661  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_block);
    662  1.2  thorpej 	ATF_TP_ADD_TC(tp, timerfd_restart);
    663  1.4  thorpej 	ATF_TP_ADD_TC(tp, timerfd_fcntl);
    664  1.2  thorpej 
    665  1.2  thorpej 	return atf_no_error();
    666  1.2  thorpej }
    667