Home | History | Annotate | Line # | Download | only in sys
t_timerfd.c revision 1.9
      1  1.9  riastrad /* $NetBSD: t_timerfd.c,v 1.9 2024/12/19 20:11:03 riastradh 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.9  riastrad __RCSID("$NetBSD: t_timerfd.c,v 1.9 2024/12/19 20:11:03 riastradh Exp $");
     33  1.2   thorpej 
     34  1.2   thorpej #include <sys/types.h>
     35  1.8  riastrad 
     36  1.2   thorpej #include <sys/event.h>
     37  1.4   thorpej #include <sys/ioctl.h>
     38  1.2   thorpej #include <sys/select.h>
     39  1.2   thorpej #include <sys/stat.h>
     40  1.2   thorpej #include <sys/syscall.h>
     41  1.2   thorpej #include <sys/timerfd.h>
     42  1.8  riastrad 
     43  1.2   thorpej #include <errno.h>
     44  1.2   thorpej #include <poll.h>
     45  1.2   thorpej #include <pthread.h>
     46  1.8  riastrad #include <stdio.h>
     47  1.2   thorpej #include <stdlib.h>
     48  1.2   thorpej #include <time.h>
     49  1.2   thorpej #include <unistd.h>
     50  1.2   thorpej 
     51  1.2   thorpej #include <atf-c.h>
     52  1.2   thorpej 
     53  1.8  riastrad #include "h_macros.h"
     54  1.3   hannken #include "isqemu.h"
     55  1.3   hannken 
     56  1.2   thorpej struct helper_context {
     57  1.2   thorpej 	int	fd;
     58  1.2   thorpej 
     59  1.2   thorpej 	pthread_barrier_t barrier;
     60  1.2   thorpej };
     61  1.2   thorpej 
     62  1.2   thorpej static void
     63  1.2   thorpej init_helper_context(struct helper_context * const ctx)
     64  1.2   thorpej {
     65  1.2   thorpej 
     66  1.2   thorpej 	memset(ctx, 0, sizeof(*ctx));
     67  1.2   thorpej 
     68  1.2   thorpej 	ATF_REQUIRE(pthread_barrier_init(&ctx->barrier, NULL, 2) == 0);
     69  1.2   thorpej }
     70  1.2   thorpej 
     71  1.2   thorpej static bool
     72  1.2   thorpej wait_barrier(struct helper_context * const ctx)
     73  1.2   thorpej {
     74  1.2   thorpej 	int rv = pthread_barrier_wait(&ctx->barrier);
     75  1.2   thorpej 
     76  1.2   thorpej 	return rv == 0 || rv == PTHREAD_BARRIER_SERIAL_THREAD;
     77  1.2   thorpej }
     78  1.2   thorpej 
     79  1.3   hannken static bool
     80  1.3   hannken check_value_against_bounds(uint64_t value, uint64_t lower, uint64_t upper)
     81  1.3   hannken {
     82  1.3   hannken 
     83  1.3   hannken 	/*
     84  1.3   hannken 	 * If running under QEMU make sure the upper bound is large
     85  1.3   hannken 	 * enough for the effect of kern/43997
     86  1.3   hannken 	 */
     87  1.3   hannken 	if (isQEMU()) {
     88  1.3   hannken 		upper *= 4;
     89  1.3   hannken 	}
     90  1.3   hannken 
     91  1.3   hannken 	if (value < lower || value > upper) {
     92  1.3   hannken 		printf("val %" PRIu64 " not in [ %" PRIu64 ", %" PRIu64 " ]\n",
     93  1.3   hannken 		    value, lower, upper);
     94  1.3   hannken 	}
     95  1.3   hannken 
     96  1.3   hannken 	return value >= lower && value <= upper;
     97  1.3   hannken }
     98  1.3   hannken 
     99  1.2   thorpej /*****************************************************************************/
    100  1.2   thorpej 
    101  1.2   thorpej static int
    102  1.2   thorpej timerfd_read(int fd, uint64_t *valp)
    103  1.2   thorpej {
    104  1.2   thorpej 	uint64_t val;
    105  1.2   thorpej 
    106  1.2   thorpej 	switch (read(fd, &val, sizeof(val))) {
    107  1.2   thorpej 	case -1:
    108  1.2   thorpej 		return -1;
    109  1.2   thorpej 
    110  1.2   thorpej 	case sizeof(val):
    111  1.2   thorpej 		*valp = val;
    112  1.2   thorpej 		return 0;
    113  1.2   thorpej 
    114  1.2   thorpej 	default:
    115  1.2   thorpej 		/* ?? Should never happen. */
    116  1.2   thorpej 		errno = EIO;
    117  1.2   thorpej 		return -1;
    118  1.2   thorpej 	}
    119  1.2   thorpej }
    120  1.2   thorpej 
    121  1.2   thorpej /*****************************************************************************/
    122  1.2   thorpej 
    123  1.2   thorpej ATF_TC(timerfd_create);
    124  1.2   thorpej ATF_TC_HEAD(timerfd_create, tc)
    125  1.2   thorpej {
    126  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates timerfd_create()");
    127  1.2   thorpej }
    128  1.2   thorpej ATF_TC_BODY(timerfd_create, tc)
    129  1.2   thorpej {
    130  1.2   thorpej 	int fd;
    131  1.2   thorpej 
    132  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
    133  1.2   thorpej 	(void) close(fd);
    134  1.2   thorpej 
    135  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    136  1.2   thorpej 	(void) close(fd);
    137  1.2   thorpej 
    138  1.2   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    139  1.2   thorpej 	    (fd = timerfd_create(CLOCK_VIRTUAL, 0)) == -1);
    140  1.2   thorpej 
    141  1.2   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    142  1.2   thorpej 	    (fd = timerfd_create(CLOCK_PROF, 0)) == -1);
    143  1.2   thorpej 
    144  1.2   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    145  1.2   thorpej 	    (fd = timerfd_create(CLOCK_REALTIME,
    146  1.2   thorpej 	    			    ~(TFD_CLOEXEC | TFD_NONBLOCK))) == -1);
    147  1.2   thorpej }
    148  1.2   thorpej 
    149  1.2   thorpej /*****************************************************************************/
    150  1.2   thorpej 
    151  1.8  riastrad ATF_TC(timerfd_write);
    152  1.8  riastrad ATF_TC_HEAD(timerfd_write, tc)
    153  1.8  riastrad {
    154  1.8  riastrad 	atf_tc_set_md_var(tc, "descr",
    155  1.8  riastrad 	    "validates rejection of writes to timerfds");
    156  1.8  riastrad }
    157  1.8  riastrad ATF_TC_BODY(timerfd_write, tc)
    158  1.8  riastrad {
    159  1.8  riastrad 	int fd;
    160  1.8  riastrad 	char c = 1;
    161  1.8  riastrad 
    162  1.8  riastrad 	RL(fd = timerfd_create(CLOCK_REALTIME, 0));
    163  1.8  riastrad 	ATF_CHECK_ERRNO(EBADF, write(fd, &c, 1) == -1);
    164  1.8  riastrad 	RL(close(fd));
    165  1.8  riastrad }
    166  1.8  riastrad 
    167  1.8  riastrad /*****************************************************************************/
    168  1.8  riastrad 
    169  1.2   thorpej ATF_TC(timerfd_bogusfd);
    170  1.2   thorpej ATF_TC_HEAD(timerfd_bogusfd, tc)
    171  1.2   thorpej {
    172  1.2   thorpej 	atf_tc_set_md_var(tc, "descr",
    173  1.2   thorpej 	    "validates rejection of bogus fds by timerfd_{get,set}time()");
    174  1.2   thorpej }
    175  1.2   thorpej ATF_TC_BODY(timerfd_bogusfd, tc)
    176  1.2   thorpej {
    177  1.2   thorpej 	struct itimerspec its = { 0 };
    178  1.2   thorpej 	int fd;
    179  1.2   thorpej 
    180  1.2   thorpej 	ATF_REQUIRE((fd = kqueue()) >= 0);	/* arbitrary fd type */
    181  1.2   thorpej 
    182  1.2   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    183  1.2   thorpej 	    timerfd_gettime(fd, &its) == -1);
    184  1.2   thorpej 
    185  1.2   thorpej 	its.it_value.tv_sec = 5;
    186  1.2   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    187  1.2   thorpej 	    timerfd_settime(fd, 0, &its, NULL) == -1);
    188  1.2   thorpej 
    189  1.2   thorpej 	(void) close(fd);
    190  1.2   thorpej }
    191  1.2   thorpej 
    192  1.2   thorpej /*****************************************************************************/
    193  1.2   thorpej 
    194  1.8  riastrad ATF_TC(timerfd_invalidtime);
    195  1.8  riastrad ATF_TC_HEAD(timerfd_invalidtime, tc)
    196  1.8  riastrad {
    197  1.8  riastrad 	atf_tc_set_md_var(tc, "descr",
    198  1.8  riastrad 	    "validates rejection of invalid itimerspec by timerfd_settime()");
    199  1.8  riastrad }
    200  1.8  riastrad ATF_TC_BODY(timerfd_invalidtime, tc)
    201  1.8  riastrad {
    202  1.8  riastrad 	const struct itimerspec einval_its[] = {
    203  1.8  riastrad 		[0] = { .it_value = {-1, 0} },
    204  1.8  riastrad 		[1] = { .it_value = {0, -1} },
    205  1.8  riastrad 		[2] = { .it_value = {0, 1000000001} },
    206  1.8  riastrad 		[3] = { .it_value = {1, 0}, .it_interval = {-1, 0} },
    207  1.8  riastrad 		[4] = { .it_value = {1, 0}, .it_interval = {0, -1} },
    208  1.8  riastrad 		[5] = { .it_value = {1, 0}, .it_interval = {0, 1000000001} },
    209  1.8  riastrad 	};
    210  1.8  riastrad 	struct timespec now;
    211  1.8  riastrad 	unsigned i;
    212  1.8  riastrad 	fd_set readfds;
    213  1.8  riastrad 	uint64_t val;
    214  1.8  riastrad 	int fd;
    215  1.8  riastrad 
    216  1.8  riastrad 	RL(clock_gettime(CLOCK_MONOTONIC, &now));
    217  1.8  riastrad 	RL(fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK));
    218  1.8  riastrad 
    219  1.8  riastrad 	atf_tc_expect_fail("PR kern/58914:"
    220  1.8  riastrad 	    " timerfd_settime(2) is missing itimespecfix");
    221  1.8  riastrad 	for (i = 0; i < __arraycount(einval_its); i++) {
    222  1.8  riastrad 		struct itimerspec its;
    223  1.8  riastrad 
    224  1.8  riastrad 		if (einval_its[i].it_interval.tv_sec < 0 ||
    225  1.8  riastrad 		    einval_its[i].it_interval.tv_nsec < 0 ||
    226  1.8  riastrad 		    einval_its[i].it_interval.tv_nsec >= 1000000000) {
    227  1.8  riastrad 			/* Avoid crashing the kernel for now, PR 58914. */
    228  1.8  riastrad 			fprintf(stderr, "skip %u\n", i);
    229  1.8  riastrad 			continue;
    230  1.8  riastrad 		}
    231  1.8  riastrad 
    232  1.8  riastrad 		fprintf(stderr, "case %u\n", i);
    233  1.8  riastrad 
    234  1.8  riastrad 		ATF_CHECK_ERRNO(EINVAL,
    235  1.8  riastrad 		    timerfd_settime(fd, 0, &einval_its[i], NULL) == -1);
    236  1.8  riastrad 
    237  1.8  riastrad 		/* Try the same with an absolute time near now. */
    238  1.8  riastrad 		its.it_value = einval_its[i].it_value;
    239  1.8  riastrad 		its.it_value.tv_sec += now.tv_sec + 60;
    240  1.8  riastrad 		ATF_CHECK_ERRNO(EINVAL,
    241  1.8  riastrad 		    timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, NULL) == -1);
    242  1.8  riastrad 	}
    243  1.8  riastrad 
    244  1.8  riastrad 	/* Wait up to 2sec to make sure no timer got set anyway. */
    245  1.8  riastrad 	FD_ZERO(&readfds);
    246  1.8  riastrad 	FD_SET(fd, &readfds);
    247  1.8  riastrad 	RL(select(fd + 1, &readfds, NULL, NULL, &(struct timeval){2, 0}));
    248  1.8  riastrad 	ATF_CHECK(!FD_ISSET(fd, &readfds));
    249  1.8  riastrad 	ATF_CHECK_ERRNO(EAGAIN, timerfd_read(fd, &val) == -1);
    250  1.8  riastrad 
    251  1.8  riastrad 	RL(close(fd));
    252  1.8  riastrad }
    253  1.8  riastrad 
    254  1.8  riastrad /*****************************************************************************/
    255  1.8  riastrad 
    256  1.8  riastrad ATF_TC(timerfd_past);
    257  1.8  riastrad ATF_TC_HEAD(timerfd_past, tc)
    258  1.8  riastrad {
    259  1.8  riastrad 	atf_tc_set_md_var(tc, "descr", "validates trigger on past time");
    260  1.8  riastrad }
    261  1.8  riastrad ATF_TC_BODY(timerfd_past, tc)
    262  1.8  riastrad {
    263  1.8  riastrad 	struct itimerspec its = {.it_value = {-1, 0}, .it_interval = {0, 0}};
    264  1.8  riastrad 	struct timespec then, now, delta;
    265  1.8  riastrad 	uint64_t val;
    266  1.8  riastrad 	int fd;
    267  1.8  riastrad 
    268  1.8  riastrad 	RL(fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK));
    269  1.8  riastrad 
    270  1.8  riastrad 	RL(clock_gettime(CLOCK_MONOTONIC, &then));
    271  1.8  riastrad 	timespecadd(&then, &its.it_value, &its.it_value);
    272  1.8  riastrad 	RL(timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, NULL));
    273  1.8  riastrad 
    274  1.8  riastrad 	/*
    275  1.8  riastrad 	 * Wait for one tick to pass.
    276  1.8  riastrad 	 *
    277  1.8  riastrad 	 * XXX Having to do this seems silly, but it matches Linux, so.
    278  1.8  riastrad 	 */
    279  1.8  riastrad 	RL(clock_nanosleep(CLOCK_MONOTONIC, 0, &(const struct timespec){0, 1},
    280  1.8  riastrad 		NULL));
    281  1.8  riastrad 
    282  1.8  riastrad 	RL(timerfd_read(fd, &val));
    283  1.8  riastrad 	RL(clock_gettime(CLOCK_MONOTONIC, &now));
    284  1.8  riastrad 	ATF_REQUIRE(check_value_against_bounds(val, 1, 1));
    285  1.8  riastrad 
    286  1.8  riastrad 	timespecsub(&now, &then, &delta);
    287  1.8  riastrad 	ATF_CHECK_MSG(check_value_against_bounds(delta.tv_sec, 0, 0),
    288  1.8  riastrad 	    "then=%jd.%09lu now=%jd.%09lu delta=%jd.%09lu",
    289  1.8  riastrad 	    (intmax_t)then.tv_sec, then.tv_nsec,
    290  1.8  riastrad 	    (intmax_t)now.tv_sec, now.tv_nsec,
    291  1.8  riastrad 	    (intmax_t)delta.tv_sec, delta.tv_nsec);
    292  1.8  riastrad 
    293  1.8  riastrad 	RL(close(fd));
    294  1.8  riastrad }
    295  1.8  riastrad 
    296  1.8  riastrad /*****************************************************************************/
    297  1.8  riastrad 
    298  1.2   thorpej ATF_TC(timerfd_block);
    299  1.2   thorpej ATF_TC_HEAD(timerfd_block, tc)
    300  1.2   thorpej {
    301  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates blocking behavior");
    302  1.2   thorpej }
    303  1.2   thorpej ATF_TC_BODY(timerfd_block, tc)
    304  1.2   thorpej {
    305  1.2   thorpej 	struct timespec then, now, delta;
    306  1.2   thorpej 	uint64_t val;
    307  1.2   thorpej 	int fd;
    308  1.2   thorpej 
    309  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    310  1.2   thorpej 
    311  1.9  riastrad 	struct itimerspec oits;
    312  1.2   thorpej 	const struct itimerspec its = {
    313  1.2   thorpej 		.it_value = { .tv_sec = 1, .tv_nsec = 0 },
    314  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    315  1.2   thorpej 	};
    316  1.2   thorpej 
    317  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    318  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    319  1.9  riastrad 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, &oits) == 0);
    320  1.9  riastrad 	atf_tc_expect_fail("PR kern/58917:"
    321  1.9  riastrad 	    " timer_settime and timerfd_settime return absolute time"
    322  1.9  riastrad 	    " of next event");
    323  1.9  riastrad 	ATF_CHECK_MSG(timespeccmp(&oits.it_value, &its.it_value, <=),
    324  1.9  riastrad 	    "timerfd_settime returned %jd.%09lu remaining,"
    325  1.9  riastrad 	    " expected at most %jd.%09lu",
    326  1.9  riastrad 	    (intmax_t)oits.it_value.tv_sec, oits.it_value.tv_nsec,
    327  1.9  riastrad 	    (intmax_t)its.it_value.tv_sec, its.it_value.tv_nsec);
    328  1.9  riastrad 	atf_tc_expect_pass();
    329  1.2   thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    330  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    331  1.3   hannken 	ATF_REQUIRE(check_value_against_bounds(val, 1, 1));
    332  1.2   thorpej 
    333  1.2   thorpej 	timespecsub(&now, &then, &delta);
    334  1.5  riastrad 	ATF_REQUIRE_MSG(check_value_against_bounds(delta.tv_sec, 1, 1),
    335  1.5  riastrad 	    "then=%jd.%09lu now=%jd.%09lu delta=%jd.%09lu",
    336  1.5  riastrad 	    (intmax_t)then.tv_sec, then.tv_nsec,
    337  1.5  riastrad 	    (intmax_t)now.tv_sec, now.tv_nsec,
    338  1.5  riastrad 	    (intmax_t)delta.tv_sec, delta.tv_nsec);
    339  1.2   thorpej 
    340  1.2   thorpej 	(void) close(fd);
    341  1.2   thorpej }
    342  1.2   thorpej 
    343  1.2   thorpej /*****************************************************************************/
    344  1.2   thorpej 
    345  1.2   thorpej ATF_TC(timerfd_repeating);
    346  1.2   thorpej ATF_TC_HEAD(timerfd_repeating, tc)
    347  1.2   thorpej {
    348  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates repeating timer behavior");
    349  1.2   thorpej }
    350  1.2   thorpej ATF_TC_BODY(timerfd_repeating, tc)
    351  1.2   thorpej {
    352  1.2   thorpej 	struct timespec then, now, delta;
    353  1.2   thorpej 	uint64_t val;
    354  1.2   thorpej 	int fd;
    355  1.2   thorpej 
    356  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC,
    357  1.2   thorpej 					    TFD_NONBLOCK)) >= 0);
    358  1.2   thorpej 
    359  1.2   thorpej 	const struct itimerspec its = {
    360  1.2   thorpej 		.it_value = { .tv_sec = 0, .tv_nsec = 200000000 },
    361  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 200000000 },
    362  1.2   thorpej 	};
    363  1.2   thorpej 
    364  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    365  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    366  1.2   thorpej 	ATF_REQUIRE(sleep(1) == 0);
    367  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    368  1.2   thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    369  1.3   hannken 	/* allow some slop */
    370  1.3   hannken 	ATF_REQUIRE(check_value_against_bounds(val, 3, 5));
    371  1.2   thorpej 
    372  1.2   thorpej 	timespecsub(&now, &then, &delta);
    373  1.5  riastrad 	ATF_REQUIRE_MSG(check_value_against_bounds(delta.tv_sec, 1, 1),
    374  1.5  riastrad 	    "then=%jd.%09lu now=%jd.%09lu delta=%jd.%09lu",
    375  1.5  riastrad 	    (intmax_t)then.tv_sec, then.tv_nsec,
    376  1.5  riastrad 	    (intmax_t)now.tv_sec, now.tv_nsec,
    377  1.5  riastrad 	    (intmax_t)delta.tv_sec, delta.tv_nsec);
    378  1.2   thorpej 
    379  1.2   thorpej 	(void) close(fd);
    380  1.2   thorpej }
    381  1.2   thorpej 
    382  1.2   thorpej /*****************************************************************************/
    383  1.2   thorpej 
    384  1.2   thorpej ATF_TC(timerfd_abstime);
    385  1.2   thorpej ATF_TC_HEAD(timerfd_abstime, tc)
    386  1.2   thorpej {
    387  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates specifying abstime");
    388  1.2   thorpej }
    389  1.2   thorpej ATF_TC_BODY(timerfd_abstime, tc)
    390  1.2   thorpej {
    391  1.2   thorpej 	struct timespec then, now, delta;
    392  1.2   thorpej 	uint64_t val;
    393  1.2   thorpej 	int fd;
    394  1.2   thorpej 
    395  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    396  1.2   thorpej 
    397  1.9  riastrad 	struct itimerspec oits, its = {
    398  1.2   thorpej 		.it_value = { .tv_sec = 0, .tv_nsec = 0 },
    399  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    400  1.2   thorpej 	};
    401  1.2   thorpej 
    402  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    403  1.9  riastrad 	delta = (struct timespec){1, 0};
    404  1.9  riastrad 	timespecadd(&then, &delta, &its.it_value);
    405  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, NULL) == 0);
    406  1.9  riastrad 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, &oits) == 0);
    407  1.9  riastrad 	timespecadd(&delta, (&(const struct timespec){2, 0}), /* tick slop */
    408  1.9  riastrad 	    &delta);
    409  1.9  riastrad 	atf_tc_expect_fail("PR kern/58917:"
    410  1.9  riastrad 	    " timer_settime and timerfd_settime return absolute time"
    411  1.9  riastrad 	    " of next event");
    412  1.9  riastrad 	ATF_CHECK_MSG(timespeccmp(&oits.it_value, &delta, <=),
    413  1.9  riastrad 	    "timerfd_settime returned %jd.%09lu remaining,"
    414  1.9  riastrad 	    " expected at most %jd.%09lu",
    415  1.9  riastrad 	    (intmax_t)oits.it_value.tv_sec, oits.it_value.tv_nsec,
    416  1.9  riastrad 	    (intmax_t)delta.tv_sec, delta.tv_nsec);
    417  1.9  riastrad 	atf_tc_expect_pass();
    418  1.2   thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    419  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    420  1.3   hannken 	ATF_REQUIRE(check_value_against_bounds(val, 1, 1));
    421  1.2   thorpej 
    422  1.2   thorpej 	timespecsub(&now, &then, &delta);
    423  1.5  riastrad 	ATF_REQUIRE_MSG(check_value_against_bounds(delta.tv_sec, 1, 1),
    424  1.5  riastrad 	    "then=%jd.%09lu now=%jd.%09lu delta=%jd.%09lu",
    425  1.5  riastrad 	    (intmax_t)then.tv_sec, then.tv_nsec,
    426  1.5  riastrad 	    (intmax_t)now.tv_sec, now.tv_nsec,
    427  1.5  riastrad 	    (intmax_t)delta.tv_sec, delta.tv_nsec);
    428  1.2   thorpej 
    429  1.2   thorpej 	(void) close(fd);
    430  1.2   thorpej }
    431  1.2   thorpej 
    432  1.2   thorpej /*****************************************************************************/
    433  1.2   thorpej 
    434  1.2   thorpej ATF_TC(timerfd_cancel_on_set_immed);
    435  1.2   thorpej ATF_TC_HEAD(timerfd_cancel_on_set_immed, tc)
    436  1.2   thorpej {
    437  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - immediate");
    438  1.2   thorpej 	atf_tc_set_md_var(tc, "require.user", "root");
    439  1.2   thorpej }
    440  1.2   thorpej ATF_TC_BODY(timerfd_cancel_on_set_immed, tc)
    441  1.2   thorpej {
    442  1.2   thorpej 	struct timespec now;
    443  1.2   thorpej 	uint64_t val;
    444  1.2   thorpej 	int fd;
    445  1.2   thorpej 
    446  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
    447  1.2   thorpej 
    448  1.2   thorpej 	const struct itimerspec its = {
    449  1.2   thorpej 		.it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
    450  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    451  1.2   thorpej 	};
    452  1.2   thorpej 
    453  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
    454  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
    455  1.2   thorpej 				    &its, NULL) == 0);
    456  1.2   thorpej 	ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
    457  1.2   thorpej 	ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
    458  1.2   thorpej 
    459  1.2   thorpej 	(void) close(fd);
    460  1.2   thorpej }
    461  1.2   thorpej 
    462  1.2   thorpej /*****************************************************************************/
    463  1.2   thorpej 
    464  1.2   thorpej static void *
    465  1.2   thorpej timerfd_cancel_on_set_block_helper(void * const v)
    466  1.2   thorpej {
    467  1.2   thorpej 	struct helper_context * const ctx = v;
    468  1.2   thorpej 	struct timespec now;
    469  1.2   thorpej 
    470  1.2   thorpej 	ATF_REQUIRE(wait_barrier(ctx));
    471  1.2   thorpej 
    472  1.2   thorpej 	ATF_REQUIRE(sleep(2) == 0);
    473  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
    474  1.2   thorpej 	ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
    475  1.2   thorpej 
    476  1.2   thorpej 	return NULL;
    477  1.2   thorpej }
    478  1.2   thorpej 
    479  1.2   thorpej ATF_TC(timerfd_cancel_on_set_block);
    480  1.2   thorpej ATF_TC_HEAD(timerfd_cancel_on_set_block, tc)
    481  1.2   thorpej {
    482  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - blocking");
    483  1.2   thorpej 	atf_tc_set_md_var(tc, "require.user", "root");
    484  1.2   thorpej }
    485  1.2   thorpej ATF_TC_BODY(timerfd_cancel_on_set_block, tc)
    486  1.2   thorpej {
    487  1.2   thorpej 	struct helper_context ctx;
    488  1.2   thorpej 	pthread_t helper;
    489  1.2   thorpej 	void *join_val;
    490  1.2   thorpej 	uint64_t val;
    491  1.2   thorpej 	int fd;
    492  1.2   thorpej 
    493  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
    494  1.2   thorpej 
    495  1.2   thorpej 	const struct itimerspec its = {
    496  1.2   thorpej 		.it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
    497  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    498  1.2   thorpej 	};
    499  1.2   thorpej 
    500  1.2   thorpej 	init_helper_context(&ctx);
    501  1.2   thorpej 
    502  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
    503  1.2   thorpej 				    &its, NULL) == 0);
    504  1.2   thorpej 	ATF_REQUIRE(pthread_create(&helper, NULL,
    505  1.2   thorpej 				timerfd_cancel_on_set_block_helper, &ctx) == 0);
    506  1.2   thorpej 	ATF_REQUIRE(wait_barrier(&ctx));
    507  1.2   thorpej 	ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
    508  1.2   thorpej 
    509  1.2   thorpej 	ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
    510  1.2   thorpej 
    511  1.2   thorpej 	(void) close(fd);
    512  1.2   thorpej }
    513  1.2   thorpej 
    514  1.2   thorpej /*****************************************************************************/
    515  1.2   thorpej 
    516  1.2   thorpej ATF_TC(timerfd_select_poll_kevent_immed);
    517  1.2   thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_immed, tc)
    518  1.2   thorpej {
    519  1.2   thorpej 	atf_tc_set_md_var(tc, "descr",
    520  1.2   thorpej 	    "validates select/poll/kevent behavior - immediate return");
    521  1.2   thorpej }
    522  1.2   thorpej ATF_TC_BODY(timerfd_select_poll_kevent_immed, tc)
    523  1.2   thorpej {
    524  1.2   thorpej 	const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
    525  1.2   thorpej 	struct itimerspec its;
    526  1.2   thorpej 	struct timeval tv;
    527  1.2   thorpej 	struct stat st;
    528  1.2   thorpej 	struct pollfd fds[1];
    529  1.2   thorpej 	uint64_t val;
    530  1.2   thorpej 	fd_set readfds, writefds, exceptfds;
    531  1.2   thorpej 	int fd;
    532  1.2   thorpej 	int kq;
    533  1.2   thorpej 	struct kevent kev[1];
    534  1.2   thorpej 
    535  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
    536  1.2   thorpej 
    537  1.2   thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
    538  1.2   thorpej 	EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
    539  1.2   thorpej 	ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
    540  1.7  riastrad 	EV_SET(&kev[0], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
    541  1.7  riastrad 	ATF_CHECK_ERRNO(EINVAL, kevent(kq, kev, 1, NULL, 0, &ts) == -1);
    542  1.2   thorpej 
    543  1.2   thorpej 	/*
    544  1.6  riastrad 	 * fd should not be ready for anything.  Pass all of the event
    545  1.6  riastrad 	 * bits; we should get back nothing.
    546  1.2   thorpej 	 */
    547  1.2   thorpej 	fds[0].fd = fd;
    548  1.2   thorpej 	fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
    549  1.2   thorpej 	    POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
    550  1.2   thorpej 	fds[0].revents = 0;
    551  1.7  riastrad 	ATF_REQUIRE(poll(fds, 1, 0) == 0);
    552  1.2   thorpej 
    553  1.2   thorpej 	/*
    554  1.6  riastrad 	 * As above; fd should not be set on return from the select()
    555  1.6  riastrad 	 * call.
    556  1.2   thorpej 	 */
    557  1.2   thorpej 	FD_ZERO(&readfds);
    558  1.2   thorpej 	FD_ZERO(&writefds);
    559  1.2   thorpej 	FD_ZERO(&exceptfds);
    560  1.2   thorpej 	tv.tv_sec = 0;
    561  1.2   thorpej 	tv.tv_usec = 0;
    562  1.2   thorpej 	FD_SET(fd, &readfds);
    563  1.2   thorpej 	FD_SET(fd, &writefds);
    564  1.2   thorpej 	FD_SET(fd, &exceptfds);
    565  1.7  riastrad 	ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 0);
    566  1.2   thorpej 	ATF_REQUIRE(!FD_ISSET(fd, &readfds));
    567  1.6  riastrad 	ATF_REQUIRE(!FD_ISSET(fd, &writefds));
    568  1.2   thorpej 	ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
    569  1.2   thorpej 
    570  1.2   thorpej 	/*
    571  1.2   thorpej 	 * Now set a one-shot half-second timer, wait for it to expire, and
    572  1.2   thorpej 	 * then check again.
    573  1.2   thorpej 	 */
    574  1.2   thorpej 	memset(&its, 0, sizeof(its));
    575  1.2   thorpej 	its.it_value.tv_sec = 0;
    576  1.2   thorpej 	its.it_value.tv_nsec = 500000000;
    577  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    578  1.2   thorpej 	ATF_REQUIRE(sleep(2) == 0);
    579  1.2   thorpej 
    580  1.2   thorpej 	/* Verify it actually fired via the stat() back-channel. */
    581  1.2   thorpej 	ATF_REQUIRE(fstat(fd, &st) == 0);
    582  1.2   thorpej 	ATF_REQUIRE(st.st_size == 1);
    583  1.2   thorpej 
    584  1.2   thorpej 	fds[0].fd = fd;
    585  1.2   thorpej 	fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
    586  1.2   thorpej 	    POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
    587  1.2   thorpej 	fds[0].revents = 0;
    588  1.2   thorpej 	ATF_REQUIRE(poll(fds, 1, 0) == 1);
    589  1.6  riastrad 	ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
    590  1.2   thorpej 
    591  1.2   thorpej 	FD_ZERO(&readfds);
    592  1.2   thorpej 	FD_ZERO(&writefds);
    593  1.2   thorpej 	FD_ZERO(&exceptfds);
    594  1.2   thorpej 	tv.tv_sec = 0;
    595  1.2   thorpej 	tv.tv_usec = 0;
    596  1.2   thorpej 	FD_SET(fd, &readfds);
    597  1.2   thorpej 	FD_SET(fd, &writefds);
    598  1.2   thorpej 	FD_SET(fd, &exceptfds);
    599  1.7  riastrad 	ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 1);
    600  1.2   thorpej 	ATF_REQUIRE(FD_ISSET(fd, &readfds));
    601  1.6  riastrad 	ATF_REQUIRE(!FD_ISSET(fd, &writefds));
    602  1.2   thorpej 	ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
    603  1.2   thorpej 
    604  1.2   thorpej 	/*
    605  1.2   thorpej 	 * Check that we get an EVFILT_READ event on fd.
    606  1.2   thorpej 	 */
    607  1.2   thorpej 	memset(kev, 0, sizeof(kev));
    608  1.2   thorpej 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, &ts) == 1);
    609  1.2   thorpej 	ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
    610  1.2   thorpej 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
    611  1.2   thorpej 	ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
    612  1.2   thorpej 	ATF_REQUIRE(kev[0].data == 1);
    613  1.2   thorpej 
    614  1.2   thorpej 	/*
    615  1.2   thorpej 	 * Read the timerfd to ensure we get the correct numnber of
    616  1.2   thorpej 	 * expirations.
    617  1.2   thorpej 	 */
    618  1.2   thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    619  1.2   thorpej 	ATF_REQUIRE(val == 1);
    620  1.2   thorpej 
    621  1.2   thorpej 	/* And ensure that we would block if we tried again. */
    622  1.2   thorpej 	ATF_REQUIRE_ERRNO(EAGAIN, timerfd_read(fd, &val) == -1);
    623  1.2   thorpej 
    624  1.2   thorpej 	(void) close(kq);
    625  1.2   thorpej 	(void) close(fd);
    626  1.2   thorpej }
    627  1.2   thorpej 
    628  1.2   thorpej /*****************************************************************************/
    629  1.2   thorpej 
    630  1.2   thorpej ATF_TC(timerfd_select_poll_kevent_block);
    631  1.2   thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_block, tc)
    632  1.2   thorpej {
    633  1.2   thorpej 	atf_tc_set_md_var(tc, "descr",
    634  1.2   thorpej 	    "validates select/poll/kevent behavior - blocking");
    635  1.2   thorpej }
    636  1.2   thorpej ATF_TC_BODY(timerfd_select_poll_kevent_block, tc)
    637  1.2   thorpej {
    638  1.2   thorpej 	const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
    639  1.2   thorpej 	struct timespec then, now;
    640  1.2   thorpej 	struct pollfd fds[1];
    641  1.2   thorpej 	fd_set readfds;
    642  1.2   thorpej 	int fd;
    643  1.2   thorpej 	int kq;
    644  1.2   thorpej 	struct kevent kev[1];
    645  1.2   thorpej 
    646  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
    647  1.2   thorpej 
    648  1.2   thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
    649  1.2   thorpej 	EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
    650  1.2   thorpej 	ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
    651  1.2   thorpej 
    652  1.2   thorpej 	/*
    653  1.2   thorpej 	 * For each of these tests, we do the following:
    654  1.2   thorpej 	 *
    655  1.2   thorpej 	 * - Get the current time.
    656  1.2   thorpej 	 * - Set a 1-second one-shot timer.
    657  1.2   thorpej 	 * - Block in the multiplexing call.
    658  1.2   thorpej 	 * - Get the current time and verify that the timer expiration
    659  1.2   thorpej 	 *   interval has passed.
    660  1.2   thorpej 	 */
    661  1.2   thorpej 
    662  1.2   thorpej 	const struct itimerspec its = {
    663  1.2   thorpej 		.it_value = { .tv_sec = 1, .tv_nsec = 0 },
    664  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    665  1.2   thorpej 	};
    666  1.2   thorpej 
    667  1.2   thorpej 	/* poll(2) */
    668  1.2   thorpej 	fds[0].fd = fd;
    669  1.2   thorpej 	fds[0].events = POLLIN | POLLRDNORM;
    670  1.2   thorpej 	fds[0].revents = 0;
    671  1.2   thorpej 
    672  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    673  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    674  1.2   thorpej 	ATF_REQUIRE(poll(fds, 1, INFTIM) == 1);
    675  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    676  1.2   thorpej 	ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
    677  1.2   thorpej 	ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
    678  1.2   thorpej 
    679  1.2   thorpej 	/* select(2) */
    680  1.2   thorpej 	FD_ZERO(&readfds);
    681  1.2   thorpej 	FD_SET(fd, &readfds);
    682  1.2   thorpej 
    683  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    684  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    685  1.2   thorpej 	ATF_REQUIRE(select(fd + 1, &readfds, NULL, NULL, NULL) == 1);
    686  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    687  1.2   thorpej 	ATF_REQUIRE(FD_ISSET(fd, &readfds));
    688  1.2   thorpej 	ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
    689  1.2   thorpej 
    690  1.2   thorpej 	/* kevent(2) */
    691  1.2   thorpej 	memset(kev, 0, sizeof(kev));
    692  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    693  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    694  1.2   thorpej 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1);
    695  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    696  1.2   thorpej 	ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
    697  1.2   thorpej 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
    698  1.2   thorpej 	ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
    699  1.2   thorpej 	ATF_REQUIRE(kev[0].data == 1);
    700  1.2   thorpej 
    701  1.2   thorpej 	(void) close(kq);
    702  1.2   thorpej 	(void) close(fd);
    703  1.2   thorpej }
    704  1.2   thorpej 
    705  1.2   thorpej /*****************************************************************************/
    706  1.2   thorpej 
    707  1.2   thorpej static void *
    708  1.2   thorpej timerfd_restart_helper(void * const v)
    709  1.2   thorpej {
    710  1.2   thorpej 	struct helper_context * const ctx = v;
    711  1.2   thorpej 
    712  1.2   thorpej 	ATF_REQUIRE(wait_barrier(ctx));
    713  1.2   thorpej 
    714  1.2   thorpej 	/*
    715  1.2   thorpej 	 * Wait 5 seconds (that should give the main thread time to
    716  1.2   thorpej 	 * block), and then close the descriptor.
    717  1.2   thorpej 	 */
    718  1.2   thorpej 	ATF_REQUIRE(sleep(5) == 0);
    719  1.2   thorpej 	ATF_REQUIRE(close(ctx->fd) == 0);
    720  1.2   thorpej 
    721  1.2   thorpej 	return NULL;
    722  1.2   thorpej }
    723  1.2   thorpej 
    724  1.2   thorpej ATF_TC(timerfd_restart);
    725  1.2   thorpej ATF_TC_HEAD(timerfd_restart, tc)
    726  1.2   thorpej {
    727  1.2   thorpej 	atf_tc_set_md_var(tc, "descr",
    728  1.2   thorpej 	    "exercises the 'restart' fileop code path");
    729  1.2   thorpej }
    730  1.2   thorpej ATF_TC_BODY(timerfd_restart, tc)
    731  1.2   thorpej {
    732  1.2   thorpej 	struct timespec then, now, delta;
    733  1.2   thorpej 	struct helper_context ctx;
    734  1.2   thorpej 	uint64_t val;
    735  1.2   thorpej 	pthread_t helper;
    736  1.2   thorpej 	void *join_val;
    737  1.2   thorpej 
    738  1.2   thorpej 	init_helper_context(&ctx);
    739  1.2   thorpej 
    740  1.2   thorpej 	ATF_REQUIRE((ctx.fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    741  1.2   thorpej 
    742  1.2   thorpej 	const struct itimerspec its = {
    743  1.2   thorpej 		.it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
    744  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    745  1.2   thorpej 	};
    746  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(ctx.fd, 0, &its, NULL) == 0);
    747  1.2   thorpej 
    748  1.2   thorpej 
    749  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    750  1.2   thorpej 	ATF_REQUIRE(pthread_create(&helper, NULL,
    751  1.2   thorpej 				   timerfd_restart_helper, &ctx) == 0);
    752  1.2   thorpej 
    753  1.2   thorpej 	/*
    754  1.2   thorpej 	 * Wait for the helper to be ready, and then immediately block
    755  1.2   thorpej 	 * in read().  The helper will close the file, and we should get
    756  1.2   thorpej 	 * EBADF after a few seconds.
    757  1.2   thorpej 	 */
    758  1.2   thorpej 	ATF_REQUIRE(wait_barrier(&ctx));
    759  1.2   thorpej 	ATF_REQUIRE_ERRNO(EBADF, timerfd_read(ctx.fd, &val) == -1);
    760  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    761  1.2   thorpej 
    762  1.2   thorpej 	timespecsub(&now, &then, &delta);
    763  1.2   thorpej 	ATF_REQUIRE(delta.tv_sec >= 5);
    764  1.2   thorpej 
    765  1.2   thorpej 	/* Reap the helper. */
    766  1.2   thorpej 	ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
    767  1.2   thorpej }
    768  1.2   thorpej 
    769  1.2   thorpej /*****************************************************************************/
    770  1.2   thorpej 
    771  1.4   thorpej ATF_TC(timerfd_fcntl);
    772  1.4   thorpej ATF_TC_HEAD(timerfd_fcntl, tc)
    773  1.4   thorpej {
    774  1.4   thorpej 	atf_tc_set_md_var(tc, "descr",
    775  1.4   thorpej 	    "validates fcntl behavior");
    776  1.4   thorpej }
    777  1.4   thorpej 
    778  1.4   thorpej ATF_TC_BODY(timerfd_fcntl, tc)
    779  1.4   thorpej {
    780  1.4   thorpej 	int tfd;
    781  1.4   thorpej 	int val;
    782  1.4   thorpej 
    783  1.4   thorpej 	ATF_REQUIRE((tfd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    784  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFL) & O_NONBLOCK) == 0);
    785  1.4   thorpej 	ATF_REQUIRE(fcntl(tfd, F_SETFL, O_NONBLOCK) == 0);
    786  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFL) & O_NONBLOCK) != 0);
    787  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) == 0);
    788  1.4   thorpej 
    789  1.4   thorpej 	/* If the timer hasn't fired, there is no readable data. */
    790  1.4   thorpej 	ATF_REQUIRE(ioctl(tfd, FIONREAD, &val) == 0);
    791  1.4   thorpej 	ATF_REQUIRE(val == 0);
    792  1.4   thorpej 
    793  1.4   thorpej 	ATF_REQUIRE_ERRNO(ENOTTY, ioctl(tfd, FIONWRITE, &val) == -1);
    794  1.4   thorpej 	ATF_REQUIRE_ERRNO(ENOTTY, ioctl(tfd, FIONSPACE, &val) == -1);
    795  1.4   thorpej 	(void)close(tfd);
    796  1.4   thorpej 
    797  1.4   thorpej 	ATF_REQUIRE((tfd = timerfd_create(CLOCK_MONOTONIC,
    798  1.4   thorpej 					  TFD_NONBLOCK | TFD_CLOEXEC)) >= 0);
    799  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFL) & ~O_ACCMODE) == O_NONBLOCK);
    800  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) != 0);
    801  1.4   thorpej 	ATF_REQUIRE(fcntl(tfd, F_SETFD, 0) == 0);
    802  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) == 0);
    803  1.4   thorpej 	ATF_REQUIRE(fcntl(tfd, F_SETFD, FD_CLOEXEC) == 0);
    804  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) != 0);
    805  1.4   thorpej 	(void)close(tfd);
    806  1.4   thorpej }
    807  1.4   thorpej 
    808  1.4   thorpej /*****************************************************************************/
    809  1.4   thorpej 
    810  1.2   thorpej ATF_TP_ADD_TCS(tp)
    811  1.2   thorpej {
    812  1.8  riastrad 
    813  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_create);
    814  1.8  riastrad 	ATF_TP_ADD_TC(tp, timerfd_write);
    815  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_bogusfd);
    816  1.8  riastrad 	ATF_TP_ADD_TC(tp, timerfd_invalidtime);
    817  1.8  riastrad 	ATF_TP_ADD_TC(tp, timerfd_past);
    818  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_block);
    819  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_repeating);
    820  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_abstime);
    821  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_block);
    822  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_immed);
    823  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_immed);
    824  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_block);
    825  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_restart);
    826  1.4   thorpej 	ATF_TP_ADD_TC(tp, timerfd_fcntl);
    827  1.2   thorpej 
    828  1.2   thorpej 	return atf_no_error();
    829  1.2   thorpej }
    830