Home | History | Annotate | Line # | Download | only in sys
t_timerfd.c revision 1.8
      1  1.8  riastrad /* $NetBSD: t_timerfd.c,v 1.8 2024/12/19 20:10:49 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.8  riastrad __RCSID("$NetBSD: t_timerfd.c,v 1.8 2024/12/19 20:10:49 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.2   thorpej 	const struct itimerspec its = {
    312  1.2   thorpej 		.it_value = { .tv_sec = 1, .tv_nsec = 0 },
    313  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    314  1.2   thorpej 	};
    315  1.2   thorpej 
    316  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    317  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    318  1.2   thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    319  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    320  1.3   hannken 	ATF_REQUIRE(check_value_against_bounds(val, 1, 1));
    321  1.2   thorpej 
    322  1.2   thorpej 	timespecsub(&now, &then, &delta);
    323  1.5  riastrad 	ATF_REQUIRE_MSG(check_value_against_bounds(delta.tv_sec, 1, 1),
    324  1.5  riastrad 	    "then=%jd.%09lu now=%jd.%09lu delta=%jd.%09lu",
    325  1.5  riastrad 	    (intmax_t)then.tv_sec, then.tv_nsec,
    326  1.5  riastrad 	    (intmax_t)now.tv_sec, now.tv_nsec,
    327  1.5  riastrad 	    (intmax_t)delta.tv_sec, delta.tv_nsec);
    328  1.2   thorpej 
    329  1.2   thorpej 	(void) close(fd);
    330  1.2   thorpej }
    331  1.2   thorpej 
    332  1.2   thorpej /*****************************************************************************/
    333  1.2   thorpej 
    334  1.2   thorpej ATF_TC(timerfd_repeating);
    335  1.2   thorpej ATF_TC_HEAD(timerfd_repeating, tc)
    336  1.2   thorpej {
    337  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates repeating timer behavior");
    338  1.2   thorpej }
    339  1.2   thorpej ATF_TC_BODY(timerfd_repeating, tc)
    340  1.2   thorpej {
    341  1.2   thorpej 	struct timespec then, now, delta;
    342  1.2   thorpej 	uint64_t val;
    343  1.2   thorpej 	int fd;
    344  1.2   thorpej 
    345  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC,
    346  1.2   thorpej 					    TFD_NONBLOCK)) >= 0);
    347  1.2   thorpej 
    348  1.2   thorpej 	const struct itimerspec its = {
    349  1.2   thorpej 		.it_value = { .tv_sec = 0, .tv_nsec = 200000000 },
    350  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 200000000 },
    351  1.2   thorpej 	};
    352  1.2   thorpej 
    353  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    354  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    355  1.2   thorpej 	ATF_REQUIRE(sleep(1) == 0);
    356  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    357  1.2   thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    358  1.3   hannken 	/* allow some slop */
    359  1.3   hannken 	ATF_REQUIRE(check_value_against_bounds(val, 3, 5));
    360  1.2   thorpej 
    361  1.2   thorpej 	timespecsub(&now, &then, &delta);
    362  1.5  riastrad 	ATF_REQUIRE_MSG(check_value_against_bounds(delta.tv_sec, 1, 1),
    363  1.5  riastrad 	    "then=%jd.%09lu now=%jd.%09lu delta=%jd.%09lu",
    364  1.5  riastrad 	    (intmax_t)then.tv_sec, then.tv_nsec,
    365  1.5  riastrad 	    (intmax_t)now.tv_sec, now.tv_nsec,
    366  1.5  riastrad 	    (intmax_t)delta.tv_sec, delta.tv_nsec);
    367  1.2   thorpej 
    368  1.2   thorpej 	(void) close(fd);
    369  1.2   thorpej }
    370  1.2   thorpej 
    371  1.2   thorpej /*****************************************************************************/
    372  1.2   thorpej 
    373  1.2   thorpej ATF_TC(timerfd_abstime);
    374  1.2   thorpej ATF_TC_HEAD(timerfd_abstime, tc)
    375  1.2   thorpej {
    376  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates specifying abstime");
    377  1.2   thorpej }
    378  1.2   thorpej ATF_TC_BODY(timerfd_abstime, tc)
    379  1.2   thorpej {
    380  1.2   thorpej 	struct timespec then, now, delta;
    381  1.2   thorpej 	uint64_t val;
    382  1.2   thorpej 	int fd;
    383  1.2   thorpej 
    384  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    385  1.2   thorpej 
    386  1.2   thorpej 	struct itimerspec its = {
    387  1.2   thorpej 		.it_value = { .tv_sec = 0, .tv_nsec = 0 },
    388  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    389  1.2   thorpej 	};
    390  1.2   thorpej 
    391  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    392  1.2   thorpej 	its.it_value = then;
    393  1.2   thorpej 	its.it_value.tv_sec += 1;
    394  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, NULL) == 0);
    395  1.2   thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    396  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    397  1.3   hannken 	ATF_REQUIRE(check_value_against_bounds(val, 1, 1));
    398  1.2   thorpej 
    399  1.2   thorpej 	timespecsub(&now, &then, &delta);
    400  1.5  riastrad 	ATF_REQUIRE_MSG(check_value_against_bounds(delta.tv_sec, 1, 1),
    401  1.5  riastrad 	    "then=%jd.%09lu now=%jd.%09lu delta=%jd.%09lu",
    402  1.5  riastrad 	    (intmax_t)then.tv_sec, then.tv_nsec,
    403  1.5  riastrad 	    (intmax_t)now.tv_sec, now.tv_nsec,
    404  1.5  riastrad 	    (intmax_t)delta.tv_sec, delta.tv_nsec);
    405  1.2   thorpej 
    406  1.2   thorpej 	(void) close(fd);
    407  1.2   thorpej }
    408  1.2   thorpej 
    409  1.2   thorpej /*****************************************************************************/
    410  1.2   thorpej 
    411  1.2   thorpej ATF_TC(timerfd_cancel_on_set_immed);
    412  1.2   thorpej ATF_TC_HEAD(timerfd_cancel_on_set_immed, tc)
    413  1.2   thorpej {
    414  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - immediate");
    415  1.2   thorpej 	atf_tc_set_md_var(tc, "require.user", "root");
    416  1.2   thorpej }
    417  1.2   thorpej ATF_TC_BODY(timerfd_cancel_on_set_immed, tc)
    418  1.2   thorpej {
    419  1.2   thorpej 	struct timespec now;
    420  1.2   thorpej 	uint64_t val;
    421  1.2   thorpej 	int fd;
    422  1.2   thorpej 
    423  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
    424  1.2   thorpej 
    425  1.2   thorpej 	const struct itimerspec its = {
    426  1.2   thorpej 		.it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
    427  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    428  1.2   thorpej 	};
    429  1.2   thorpej 
    430  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
    431  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
    432  1.2   thorpej 				    &its, NULL) == 0);
    433  1.2   thorpej 	ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
    434  1.2   thorpej 	ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
    435  1.2   thorpej 
    436  1.2   thorpej 	(void) close(fd);
    437  1.2   thorpej }
    438  1.2   thorpej 
    439  1.2   thorpej /*****************************************************************************/
    440  1.2   thorpej 
    441  1.2   thorpej static void *
    442  1.2   thorpej timerfd_cancel_on_set_block_helper(void * const v)
    443  1.2   thorpej {
    444  1.2   thorpej 	struct helper_context * const ctx = v;
    445  1.2   thorpej 	struct timespec now;
    446  1.2   thorpej 
    447  1.2   thorpej 	ATF_REQUIRE(wait_barrier(ctx));
    448  1.2   thorpej 
    449  1.2   thorpej 	ATF_REQUIRE(sleep(2) == 0);
    450  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &now) == 0);
    451  1.2   thorpej 	ATF_REQUIRE(clock_settime(CLOCK_REALTIME, &now) == 0);
    452  1.2   thorpej 
    453  1.2   thorpej 	return NULL;
    454  1.2   thorpej }
    455  1.2   thorpej 
    456  1.2   thorpej ATF_TC(timerfd_cancel_on_set_block);
    457  1.2   thorpej ATF_TC_HEAD(timerfd_cancel_on_set_block, tc)
    458  1.2   thorpej {
    459  1.2   thorpej 	atf_tc_set_md_var(tc, "descr", "validates cancel-on-set - blocking");
    460  1.2   thorpej 	atf_tc_set_md_var(tc, "require.user", "root");
    461  1.2   thorpej }
    462  1.2   thorpej ATF_TC_BODY(timerfd_cancel_on_set_block, tc)
    463  1.2   thorpej {
    464  1.2   thorpej 	struct helper_context ctx;
    465  1.2   thorpej 	pthread_t helper;
    466  1.2   thorpej 	void *join_val;
    467  1.2   thorpej 	uint64_t val;
    468  1.2   thorpej 	int fd;
    469  1.2   thorpej 
    470  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_REALTIME, 0)) >= 0);
    471  1.2   thorpej 
    472  1.2   thorpej 	const struct itimerspec its = {
    473  1.2   thorpej 		.it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
    474  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    475  1.2   thorpej 	};
    476  1.2   thorpej 
    477  1.2   thorpej 	init_helper_context(&ctx);
    478  1.2   thorpej 
    479  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, TFD_TIMER_CANCEL_ON_SET,
    480  1.2   thorpej 				    &its, NULL) == 0);
    481  1.2   thorpej 	ATF_REQUIRE(pthread_create(&helper, NULL,
    482  1.2   thorpej 				timerfd_cancel_on_set_block_helper, &ctx) == 0);
    483  1.2   thorpej 	ATF_REQUIRE(wait_barrier(&ctx));
    484  1.2   thorpej 	ATF_REQUIRE_ERRNO(ECANCELED, timerfd_read(fd, &val) == -1);
    485  1.2   thorpej 
    486  1.2   thorpej 	ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
    487  1.2   thorpej 
    488  1.2   thorpej 	(void) close(fd);
    489  1.2   thorpej }
    490  1.2   thorpej 
    491  1.2   thorpej /*****************************************************************************/
    492  1.2   thorpej 
    493  1.2   thorpej ATF_TC(timerfd_select_poll_kevent_immed);
    494  1.2   thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_immed, tc)
    495  1.2   thorpej {
    496  1.2   thorpej 	atf_tc_set_md_var(tc, "descr",
    497  1.2   thorpej 	    "validates select/poll/kevent behavior - immediate return");
    498  1.2   thorpej }
    499  1.2   thorpej ATF_TC_BODY(timerfd_select_poll_kevent_immed, tc)
    500  1.2   thorpej {
    501  1.2   thorpej 	const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
    502  1.2   thorpej 	struct itimerspec its;
    503  1.2   thorpej 	struct timeval tv;
    504  1.2   thorpej 	struct stat st;
    505  1.2   thorpej 	struct pollfd fds[1];
    506  1.2   thorpej 	uint64_t val;
    507  1.2   thorpej 	fd_set readfds, writefds, exceptfds;
    508  1.2   thorpej 	int fd;
    509  1.2   thorpej 	int kq;
    510  1.2   thorpej 	struct kevent kev[1];
    511  1.2   thorpej 
    512  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
    513  1.2   thorpej 
    514  1.2   thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
    515  1.2   thorpej 	EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
    516  1.2   thorpej 	ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
    517  1.7  riastrad 	EV_SET(&kev[0], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
    518  1.7  riastrad 	ATF_CHECK_ERRNO(EINVAL, kevent(kq, kev, 1, NULL, 0, &ts) == -1);
    519  1.2   thorpej 
    520  1.2   thorpej 	/*
    521  1.6  riastrad 	 * fd should not be ready for anything.  Pass all of the event
    522  1.6  riastrad 	 * bits; we should get back nothing.
    523  1.2   thorpej 	 */
    524  1.2   thorpej 	fds[0].fd = fd;
    525  1.2   thorpej 	fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
    526  1.2   thorpej 	    POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
    527  1.2   thorpej 	fds[0].revents = 0;
    528  1.7  riastrad 	ATF_REQUIRE(poll(fds, 1, 0) == 0);
    529  1.2   thorpej 
    530  1.2   thorpej 	/*
    531  1.6  riastrad 	 * As above; fd should not be set on return from the select()
    532  1.6  riastrad 	 * call.
    533  1.2   thorpej 	 */
    534  1.2   thorpej 	FD_ZERO(&readfds);
    535  1.2   thorpej 	FD_ZERO(&writefds);
    536  1.2   thorpej 	FD_ZERO(&exceptfds);
    537  1.2   thorpej 	tv.tv_sec = 0;
    538  1.2   thorpej 	tv.tv_usec = 0;
    539  1.2   thorpej 	FD_SET(fd, &readfds);
    540  1.2   thorpej 	FD_SET(fd, &writefds);
    541  1.2   thorpej 	FD_SET(fd, &exceptfds);
    542  1.7  riastrad 	ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 0);
    543  1.2   thorpej 	ATF_REQUIRE(!FD_ISSET(fd, &readfds));
    544  1.6  riastrad 	ATF_REQUIRE(!FD_ISSET(fd, &writefds));
    545  1.2   thorpej 	ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
    546  1.2   thorpej 
    547  1.2   thorpej 	/*
    548  1.2   thorpej 	 * Now set a one-shot half-second timer, wait for it to expire, and
    549  1.2   thorpej 	 * then check again.
    550  1.2   thorpej 	 */
    551  1.2   thorpej 	memset(&its, 0, sizeof(its));
    552  1.2   thorpej 	its.it_value.tv_sec = 0;
    553  1.2   thorpej 	its.it_value.tv_nsec = 500000000;
    554  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    555  1.2   thorpej 	ATF_REQUIRE(sleep(2) == 0);
    556  1.2   thorpej 
    557  1.2   thorpej 	/* Verify it actually fired via the stat() back-channel. */
    558  1.2   thorpej 	ATF_REQUIRE(fstat(fd, &st) == 0);
    559  1.2   thorpej 	ATF_REQUIRE(st.st_size == 1);
    560  1.2   thorpej 
    561  1.2   thorpej 	fds[0].fd = fd;
    562  1.2   thorpej 	fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI |
    563  1.2   thorpej 	    POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP;
    564  1.2   thorpej 	fds[0].revents = 0;
    565  1.2   thorpej 	ATF_REQUIRE(poll(fds, 1, 0) == 1);
    566  1.6  riastrad 	ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
    567  1.2   thorpej 
    568  1.2   thorpej 	FD_ZERO(&readfds);
    569  1.2   thorpej 	FD_ZERO(&writefds);
    570  1.2   thorpej 	FD_ZERO(&exceptfds);
    571  1.2   thorpej 	tv.tv_sec = 0;
    572  1.2   thorpej 	tv.tv_usec = 0;
    573  1.2   thorpej 	FD_SET(fd, &readfds);
    574  1.2   thorpej 	FD_SET(fd, &writefds);
    575  1.2   thorpej 	FD_SET(fd, &exceptfds);
    576  1.7  riastrad 	ATF_REQUIRE(select(fd + 1, &readfds, &writefds, &exceptfds, &tv) == 1);
    577  1.2   thorpej 	ATF_REQUIRE(FD_ISSET(fd, &readfds));
    578  1.6  riastrad 	ATF_REQUIRE(!FD_ISSET(fd, &writefds));
    579  1.2   thorpej 	ATF_REQUIRE(!FD_ISSET(fd, &exceptfds));
    580  1.2   thorpej 
    581  1.2   thorpej 	/*
    582  1.2   thorpej 	 * Check that we get an EVFILT_READ event on fd.
    583  1.2   thorpej 	 */
    584  1.2   thorpej 	memset(kev, 0, sizeof(kev));
    585  1.2   thorpej 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, &ts) == 1);
    586  1.2   thorpej 	ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
    587  1.2   thorpej 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
    588  1.2   thorpej 	ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
    589  1.2   thorpej 	ATF_REQUIRE(kev[0].data == 1);
    590  1.2   thorpej 
    591  1.2   thorpej 	/*
    592  1.2   thorpej 	 * Read the timerfd to ensure we get the correct numnber of
    593  1.2   thorpej 	 * expirations.
    594  1.2   thorpej 	 */
    595  1.2   thorpej 	ATF_REQUIRE(timerfd_read(fd, &val) == 0);
    596  1.2   thorpej 	ATF_REQUIRE(val == 1);
    597  1.2   thorpej 
    598  1.2   thorpej 	/* And ensure that we would block if we tried again. */
    599  1.2   thorpej 	ATF_REQUIRE_ERRNO(EAGAIN, timerfd_read(fd, &val) == -1);
    600  1.2   thorpej 
    601  1.2   thorpej 	(void) close(kq);
    602  1.2   thorpej 	(void) close(fd);
    603  1.2   thorpej }
    604  1.2   thorpej 
    605  1.2   thorpej /*****************************************************************************/
    606  1.2   thorpej 
    607  1.2   thorpej ATF_TC(timerfd_select_poll_kevent_block);
    608  1.2   thorpej ATF_TC_HEAD(timerfd_select_poll_kevent_block, tc)
    609  1.2   thorpej {
    610  1.2   thorpej 	atf_tc_set_md_var(tc, "descr",
    611  1.2   thorpej 	    "validates select/poll/kevent behavior - blocking");
    612  1.2   thorpej }
    613  1.2   thorpej ATF_TC_BODY(timerfd_select_poll_kevent_block, tc)
    614  1.2   thorpej {
    615  1.2   thorpej 	const struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
    616  1.2   thorpej 	struct timespec then, now;
    617  1.2   thorpej 	struct pollfd fds[1];
    618  1.2   thorpej 	fd_set readfds;
    619  1.2   thorpej 	int fd;
    620  1.2   thorpej 	int kq;
    621  1.2   thorpej 	struct kevent kev[1];
    622  1.2   thorpej 
    623  1.2   thorpej 	ATF_REQUIRE((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) >= 0);
    624  1.2   thorpej 
    625  1.2   thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
    626  1.2   thorpej 	EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
    627  1.2   thorpej 	ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, &ts) == 0);
    628  1.2   thorpej 
    629  1.2   thorpej 	/*
    630  1.2   thorpej 	 * For each of these tests, we do the following:
    631  1.2   thorpej 	 *
    632  1.2   thorpej 	 * - Get the current time.
    633  1.2   thorpej 	 * - Set a 1-second one-shot timer.
    634  1.2   thorpej 	 * - Block in the multiplexing call.
    635  1.2   thorpej 	 * - Get the current time and verify that the timer expiration
    636  1.2   thorpej 	 *   interval has passed.
    637  1.2   thorpej 	 */
    638  1.2   thorpej 
    639  1.2   thorpej 	const struct itimerspec its = {
    640  1.2   thorpej 		.it_value = { .tv_sec = 1, .tv_nsec = 0 },
    641  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    642  1.2   thorpej 	};
    643  1.2   thorpej 
    644  1.2   thorpej 	/* poll(2) */
    645  1.2   thorpej 	fds[0].fd = fd;
    646  1.2   thorpej 	fds[0].events = POLLIN | POLLRDNORM;
    647  1.2   thorpej 	fds[0].revents = 0;
    648  1.2   thorpej 
    649  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    650  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    651  1.2   thorpej 	ATF_REQUIRE(poll(fds, 1, INFTIM) == 1);
    652  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    653  1.2   thorpej 	ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM));
    654  1.2   thorpej 	ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
    655  1.2   thorpej 
    656  1.2   thorpej 	/* select(2) */
    657  1.2   thorpej 	FD_ZERO(&readfds);
    658  1.2   thorpej 	FD_SET(fd, &readfds);
    659  1.2   thorpej 
    660  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    661  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    662  1.2   thorpej 	ATF_REQUIRE(select(fd + 1, &readfds, NULL, NULL, NULL) == 1);
    663  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    664  1.2   thorpej 	ATF_REQUIRE(FD_ISSET(fd, &readfds));
    665  1.2   thorpej 	ATF_REQUIRE(now.tv_sec - then.tv_sec >= 1);
    666  1.2   thorpej 
    667  1.2   thorpej 	/* kevent(2) */
    668  1.2   thorpej 	memset(kev, 0, sizeof(kev));
    669  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    670  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(fd, 0, &its, NULL) == 0);
    671  1.2   thorpej 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1);
    672  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    673  1.2   thorpej 	ATF_REQUIRE(kev[0].ident == (uintptr_t)fd);
    674  1.2   thorpej 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
    675  1.2   thorpej 	ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0);
    676  1.2   thorpej 	ATF_REQUIRE(kev[0].data == 1);
    677  1.2   thorpej 
    678  1.2   thorpej 	(void) close(kq);
    679  1.2   thorpej 	(void) close(fd);
    680  1.2   thorpej }
    681  1.2   thorpej 
    682  1.2   thorpej /*****************************************************************************/
    683  1.2   thorpej 
    684  1.2   thorpej static void *
    685  1.2   thorpej timerfd_restart_helper(void * const v)
    686  1.2   thorpej {
    687  1.2   thorpej 	struct helper_context * const ctx = v;
    688  1.2   thorpej 
    689  1.2   thorpej 	ATF_REQUIRE(wait_barrier(ctx));
    690  1.2   thorpej 
    691  1.2   thorpej 	/*
    692  1.2   thorpej 	 * Wait 5 seconds (that should give the main thread time to
    693  1.2   thorpej 	 * block), and then close the descriptor.
    694  1.2   thorpej 	 */
    695  1.2   thorpej 	ATF_REQUIRE(sleep(5) == 0);
    696  1.2   thorpej 	ATF_REQUIRE(close(ctx->fd) == 0);
    697  1.2   thorpej 
    698  1.2   thorpej 	return NULL;
    699  1.2   thorpej }
    700  1.2   thorpej 
    701  1.2   thorpej ATF_TC(timerfd_restart);
    702  1.2   thorpej ATF_TC_HEAD(timerfd_restart, tc)
    703  1.2   thorpej {
    704  1.2   thorpej 	atf_tc_set_md_var(tc, "descr",
    705  1.2   thorpej 	    "exercises the 'restart' fileop code path");
    706  1.2   thorpej }
    707  1.2   thorpej ATF_TC_BODY(timerfd_restart, tc)
    708  1.2   thorpej {
    709  1.2   thorpej 	struct timespec then, now, delta;
    710  1.2   thorpej 	struct helper_context ctx;
    711  1.2   thorpej 	uint64_t val;
    712  1.2   thorpej 	pthread_t helper;
    713  1.2   thorpej 	void *join_val;
    714  1.2   thorpej 
    715  1.2   thorpej 	init_helper_context(&ctx);
    716  1.2   thorpej 
    717  1.2   thorpej 	ATF_REQUIRE((ctx.fd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    718  1.2   thorpej 
    719  1.2   thorpej 	const struct itimerspec its = {
    720  1.2   thorpej 		.it_value = { .tv_sec = 60 * 60, .tv_nsec = 0 },
    721  1.2   thorpej 		.it_interval = { .tv_sec = 0, .tv_nsec = 0 },
    722  1.2   thorpej 	};
    723  1.2   thorpej 	ATF_REQUIRE(timerfd_settime(ctx.fd, 0, &its, NULL) == 0);
    724  1.2   thorpej 
    725  1.2   thorpej 
    726  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &then) == 0);
    727  1.2   thorpej 	ATF_REQUIRE(pthread_create(&helper, NULL,
    728  1.2   thorpej 				   timerfd_restart_helper, &ctx) == 0);
    729  1.2   thorpej 
    730  1.2   thorpej 	/*
    731  1.2   thorpej 	 * Wait for the helper to be ready, and then immediately block
    732  1.2   thorpej 	 * in read().  The helper will close the file, and we should get
    733  1.2   thorpej 	 * EBADF after a few seconds.
    734  1.2   thorpej 	 */
    735  1.2   thorpej 	ATF_REQUIRE(wait_barrier(&ctx));
    736  1.2   thorpej 	ATF_REQUIRE_ERRNO(EBADF, timerfd_read(ctx.fd, &val) == -1);
    737  1.2   thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
    738  1.2   thorpej 
    739  1.2   thorpej 	timespecsub(&now, &then, &delta);
    740  1.2   thorpej 	ATF_REQUIRE(delta.tv_sec >= 5);
    741  1.2   thorpej 
    742  1.2   thorpej 	/* Reap the helper. */
    743  1.2   thorpej 	ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
    744  1.2   thorpej }
    745  1.2   thorpej 
    746  1.2   thorpej /*****************************************************************************/
    747  1.2   thorpej 
    748  1.4   thorpej ATF_TC(timerfd_fcntl);
    749  1.4   thorpej ATF_TC_HEAD(timerfd_fcntl, tc)
    750  1.4   thorpej {
    751  1.4   thorpej 	atf_tc_set_md_var(tc, "descr",
    752  1.4   thorpej 	    "validates fcntl behavior");
    753  1.4   thorpej }
    754  1.4   thorpej 
    755  1.4   thorpej ATF_TC_BODY(timerfd_fcntl, tc)
    756  1.4   thorpej {
    757  1.4   thorpej 	int tfd;
    758  1.4   thorpej 	int val;
    759  1.4   thorpej 
    760  1.4   thorpej 	ATF_REQUIRE((tfd = timerfd_create(CLOCK_MONOTONIC, 0)) >= 0);
    761  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFL) & O_NONBLOCK) == 0);
    762  1.4   thorpej 	ATF_REQUIRE(fcntl(tfd, F_SETFL, O_NONBLOCK) == 0);
    763  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFL) & O_NONBLOCK) != 0);
    764  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) == 0);
    765  1.4   thorpej 
    766  1.4   thorpej 	/* If the timer hasn't fired, there is no readable data. */
    767  1.4   thorpej 	ATF_REQUIRE(ioctl(tfd, FIONREAD, &val) == 0);
    768  1.4   thorpej 	ATF_REQUIRE(val == 0);
    769  1.4   thorpej 
    770  1.4   thorpej 	ATF_REQUIRE_ERRNO(ENOTTY, ioctl(tfd, FIONWRITE, &val) == -1);
    771  1.4   thorpej 	ATF_REQUIRE_ERRNO(ENOTTY, ioctl(tfd, FIONSPACE, &val) == -1);
    772  1.4   thorpej 	(void)close(tfd);
    773  1.4   thorpej 
    774  1.4   thorpej 	ATF_REQUIRE((tfd = timerfd_create(CLOCK_MONOTONIC,
    775  1.4   thorpej 					  TFD_NONBLOCK | TFD_CLOEXEC)) >= 0);
    776  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFL) & ~O_ACCMODE) == O_NONBLOCK);
    777  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) != 0);
    778  1.4   thorpej 	ATF_REQUIRE(fcntl(tfd, F_SETFD, 0) == 0);
    779  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) == 0);
    780  1.4   thorpej 	ATF_REQUIRE(fcntl(tfd, F_SETFD, FD_CLOEXEC) == 0);
    781  1.4   thorpej 	ATF_REQUIRE((fcntl(tfd, F_GETFD) & FD_CLOEXEC) != 0);
    782  1.4   thorpej 	(void)close(tfd);
    783  1.4   thorpej }
    784  1.4   thorpej 
    785  1.4   thorpej /*****************************************************************************/
    786  1.4   thorpej 
    787  1.2   thorpej ATF_TP_ADD_TCS(tp)
    788  1.2   thorpej {
    789  1.8  riastrad 
    790  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_create);
    791  1.8  riastrad 	ATF_TP_ADD_TC(tp, timerfd_write);
    792  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_bogusfd);
    793  1.8  riastrad 	ATF_TP_ADD_TC(tp, timerfd_invalidtime);
    794  1.8  riastrad 	ATF_TP_ADD_TC(tp, timerfd_past);
    795  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_block);
    796  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_repeating);
    797  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_abstime);
    798  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_block);
    799  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_cancel_on_set_immed);
    800  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_immed);
    801  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_select_poll_kevent_block);
    802  1.2   thorpej 	ATF_TP_ADD_TC(tp, timerfd_restart);
    803  1.4   thorpej 	ATF_TP_ADD_TC(tp, timerfd_fcntl);
    804  1.2   thorpej 
    805  1.2   thorpej 	return atf_no_error();
    806  1.2   thorpej }
    807