xref: /freebsd/tests/sys/kern/timerfd.c (revision c25976f0a9a3a102ce47b45c19b2c93e8069433b)
1 /*-
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright (c) 2016 Jan Kokemüller
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include <atf-c.h>
26 
27 #include <sys/types.h>
28 
29 #include <sys/event.h>
30 #include <sys/param.h>
31 #include <sys/select.h>
32 #include <sys/time.h>
33 
34 #include <errno.h>
35 #include <signal.h>
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 
41 #include <err.h>
42 #include <poll.h>
43 #include <pthread.h>
44 #include <time.h>
45 #include <unistd.h>
46 
47 #include <sys/timerfd.h>
48 
49 /* Time in ns that sleeps are allowed to take longer for in unit tests. */
50 #define TIMER_SLACK (90000000)
51 
52 ATF_TC_WITHOUT_HEAD(timerfd__many_timers);
53 ATF_TC_BODY(timerfd__many_timers, tc)
54 {
55 	int timer_fds[256];
56 	int i;
57 
58 	for (i = 0; i < (int)nitems(timer_fds); ++i) {
59 		timer_fds[i] = timerfd_create(CLOCK_MONOTONIC, /**/
60 		    TFD_CLOEXEC | TFD_NONBLOCK);
61 		if (timer_fds[i] < 0 && errno == EMFILE) {
62 			atf_tc_skip("timerfd_create: EMFILE");
63 		}
64 		ATF_REQUIRE_MSG(timer_fds[i] >= 0, "errno: %d", errno);
65 	}
66 }
67 
68 static uint64_t
69 wait_for_timerfd(int timerfd)
70 {
71 	struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
72 
73 	ATF_REQUIRE(poll(&pfd, 1, -1) == 1);
74 
75 	uint64_t timeouts;
76 	ssize_t r = read(timerfd, &timeouts, sizeof(timeouts));
77 
78 	ATF_REQUIRE_MSG(r == (ssize_t)sizeof(timeouts), "%d %d", (int)r, errno);
79 	ATF_REQUIRE(timeouts > 0);
80 	return timeouts;
81 }
82 
83 ATF_TC_WITHOUT_HEAD(timerfd__simple_timer);
84 ATF_TC_BODY(timerfd__simple_timer, tc)
85 {
86 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
87 	    TFD_CLOEXEC | TFD_NONBLOCK);
88 
89 	ATF_REQUIRE(timerfd >= 0);
90 
91 	struct itimerspec time = {
92 		.it_value.tv_sec = 0,
93 		.it_value.tv_nsec = 100000000,
94 	};
95 
96 	struct timespec b, e;
97 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
98 
99 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
100 	(void)wait_for_timerfd(timerfd);
101 
102 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
103 	timespecsub(&e, &b, &e);
104 
105 	ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 100000000) || e.tv_sec > 0);
106 	ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec < 100000000 + TIMER_SLACK);
107 
108 	ATF_REQUIRE(close(timerfd) == 0);
109 }
110 
111 ATF_TC_WITHOUT_HEAD(timerfd__simple_periodic_timer);
112 ATF_TC_BODY(timerfd__simple_periodic_timer, tc)
113 {
114 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
115 	    TFD_CLOEXEC | TFD_NONBLOCK);
116 
117 	ATF_REQUIRE(timerfd >= 0);
118 
119 	struct itimerspec time = {
120 		.it_value.tv_sec = 0,
121 		.it_value.tv_nsec = 200000000,
122 		.it_interval.tv_sec = 0,
123 		.it_interval.tv_nsec = 200000000,
124 	};
125 
126 	struct timespec b, e;
127 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
128 
129 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
130 	uint64_t timeouts = wait_for_timerfd(timerfd);
131 
132 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
133 	timespecsub(&e, &b, &e);
134 
135 	ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 200000000) || e.tv_sec > 0);
136 	ATF_REQUIRE(timeouts >= 1);
137 	ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec < 200000000 + TIMER_SLACK);
138 	ATF_REQUIRE(timeouts == 1);
139 
140 	usleep(400000);
141 
142 	ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
143 	    (ssize_t)sizeof(timeouts));
144 	ATF_REQUIRE(timeouts >= 2);
145 	ATF_REQUIRE(timeouts == 2);
146 
147 	ATF_REQUIRE(close(timerfd) == 0);
148 }
149 
150 ATF_TC_WITHOUT_HEAD(timerfd__complex_periodic_timer);
151 ATF_TC_BODY(timerfd__complex_periodic_timer, tc)
152 {
153 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
154 	    TFD_CLOEXEC | TFD_NONBLOCK);
155 
156 	ATF_REQUIRE(timerfd >= 0);
157 
158 	struct itimerspec time = {
159 		.it_value.tv_sec = 0,
160 		.it_value.tv_nsec = 100000000,
161 		.it_interval.tv_sec = 0,
162 		.it_interval.tv_nsec = 200000001,
163 	};
164 
165 	struct timespec b, e;
166 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
167 
168 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
169 	uint64_t timeouts = wait_for_timerfd(timerfd);
170 
171 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
172 	timespecsub(&e, &b, &e);
173 
174 	ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 100000000) || e.tv_sec > 0);
175 	ATF_REQUIRE(timeouts >= 1);
176 	ATF_REQUIRE_MSG(e.tv_sec == 0 && e.tv_nsec >= 100000000 &&
177 		e.tv_nsec < 100000000 + TIMER_SLACK,
178 	    "%ld", (long)e.tv_nsec);
179 	ATF_REQUIRE(timeouts == 1);
180 
181 	usleep(401000);
182 
183 	ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
184 	    (ssize_t)sizeof(timeouts));
185 	ATF_REQUIRE_MSG(timeouts >= 2, "%d", (int)timeouts);
186 	ATF_REQUIRE_MSG(timeouts == 2, "%d", (int)timeouts);
187 
188 	ATF_REQUIRE(close(timerfd) == 0);
189 }
190 
191 ATF_TC_WITHOUT_HEAD(timerfd__reset_periodic_timer);
192 ATF_TC_BODY(timerfd__reset_periodic_timer, tc)
193 {
194 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
195 	    TFD_CLOEXEC | TFD_NONBLOCK);
196 
197 	ATF_REQUIRE(timerfd >= 0);
198 
199 	struct itimerspec time = {
200 		.it_value.tv_sec = 0,
201 		.it_value.tv_nsec = 100000000,
202 		.it_interval.tv_sec = 0,
203 		.it_interval.tv_nsec = 100000000,
204 	};
205 
206 	struct timespec b, e;
207 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
208 
209 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
210 	(void)wait_for_timerfd(timerfd);
211 
212 	time = (struct itimerspec) {
213 		.it_value.tv_sec = 0,
214 		.it_value.tv_nsec = 50000000,
215 		.it_interval.tv_sec = 0,
216 		.it_interval.tv_nsec = 100000000,
217 	};
218 
219 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
220 
221 	uint64_t timeouts = wait_for_timerfd(timerfd);
222 	ATF_REQUIRE(timeouts >= 1);
223 	ATF_REQUIRE(timeouts == 1);
224 
225 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
226 	timespecsub(&e, &b, &e);
227 	ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 150000000) || e.tv_sec > 0);
228 	ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec >= 150000000 &&
229 	    e.tv_nsec < 150000000 + TIMER_SLACK * 2);
230 
231 	ATF_REQUIRE(close(timerfd) == 0);
232 }
233 
234 ATF_TC_WITHOUT_HEAD(timerfd__reenable_periodic_timer);
235 ATF_TC_BODY(timerfd__reenable_periodic_timer, tc)
236 {
237 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
238 	    TFD_CLOEXEC | TFD_NONBLOCK);
239 
240 	ATF_REQUIRE(timerfd >= 0);
241 
242 	struct itimerspec time = {
243 		.it_value.tv_sec = 0,
244 		.it_value.tv_nsec = 100000000,
245 		.it_interval.tv_sec = 0,
246 		.it_interval.tv_nsec = 100000000,
247 	};
248 
249 	struct timespec b, e;
250 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
251 
252 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
253 	uint64_t timeouts = wait_for_timerfd(timerfd);
254 
255 	ATF_REQUIRE(timeouts >= 1);
256 	ATF_REQUIRE(timeouts == 1);
257 
258 	time = (struct itimerspec) {
259 		.it_value.tv_sec = 0,
260 		.it_value.tv_nsec = 0,
261 		.it_interval.tv_sec = 0,
262 		.it_interval.tv_nsec = 0,
263 	};
264 
265 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
266 
267 	struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
268 	ATF_REQUIRE(poll(&pfd, 1, 250) == 0);
269 
270 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
271 	timespecsub(&e, &b, &e);
272 
273 	ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 350000000) || e.tv_sec > 0);
274 	ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec >= 350000000 &&
275 	    e.tv_nsec < 350000000 + TIMER_SLACK * 2);
276 
277 	time = (struct itimerspec) {
278 		.it_value.tv_sec = 1,
279 		.it_value.tv_nsec = 0,
280 		.it_interval.tv_sec = 1,
281 		.it_interval.tv_nsec = 0,
282 	};
283 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
284 
285 	ATF_REQUIRE(close(timerfd) == 0);
286 }
287 
288 /*
289  * Adapted from sghctoma's example here:
290  * https://github.com/jiixyj/epoll-shim/issues/2
291  *
292  * The SIGUSR1 signal should not kill the process.
293  */
294 ATF_TC_WITHOUT_HEAD(timerfd__expire_five);
295 ATF_TC_BODY(timerfd__expire_five, tc)
296 {
297 	int fd;
298 	struct itimerspec value;
299 	uint64_t total_exp = 0;
300 
301 	fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
302 	ATF_REQUIRE(fd >= 0);
303 
304 	value.it_value.tv_sec = 3;
305 	value.it_value.tv_nsec = 0;
306 	value.it_interval.tv_sec = 1;
307 	value.it_interval.tv_nsec = 0;
308 
309 	ATF_REQUIRE(timerfd_settime(fd, 0, &value, NULL) == 0);
310 
311 	sigset_t sigs;
312 	sigemptyset(&sigs);
313 	sigaddset(&sigs, SIGUSR1);
314 	sigprocmask(SIG_BLOCK, &sigs, NULL);
315 
316 	kill(getpid(), SIGUSR1);
317 
318 	for (;;) {
319 		uint64_t exp = wait_for_timerfd(fd);
320 
321 		printf("timer expired %u times\n", (unsigned)exp);
322 
323 		total_exp += exp;
324 		if (total_exp >= 5) {
325 			break;
326 		}
327 	}
328 
329 	ATF_REQUIRE(close(fd) == 0);
330 }
331 
332 ATF_TC_WITHOUT_HEAD(timerfd__simple_gettime);
333 ATF_TC_BODY(timerfd__simple_gettime, tc)
334 {
335 	struct itimerspec curr_value;
336 
337 	int fd = timerfd_create(CLOCK_MONOTONIC, 0);
338 	ATF_REQUIRE(fd >= 0);
339 
340 	ATF_REQUIRE(timerfd_gettime(fd, &curr_value) == 0);
341 
342 	ATF_REQUIRE(curr_value.it_value.tv_sec == 0);
343 	ATF_REQUIRE(curr_value.it_value.tv_nsec == 0);
344 	ATF_REQUIRE(curr_value.it_interval.tv_sec == 0);
345 	ATF_REQUIRE(curr_value.it_interval.tv_nsec == 0);
346 
347 	struct itimerspec time = {
348 		.it_value.tv_sec = 0,
349 		.it_value.tv_nsec = 100000000,
350 		.it_interval.tv_sec = 0,
351 		.it_interval.tv_nsec = 100000000,
352 	};
353 
354 	curr_value = time;
355 	ATF_REQUIRE(timerfd_settime(fd, 0, &time, &curr_value) == 0);
356 	ATF_REQUIRE(curr_value.it_value.tv_sec == 0);
357 	ATF_REQUIRE(curr_value.it_value.tv_nsec == 0);
358 	ATF_REQUIRE(curr_value.it_interval.tv_sec == 0);
359 	ATF_REQUIRE(curr_value.it_interval.tv_nsec == 0);
360 
361 	ATF_REQUIRE(close(fd) == 0);
362 }
363 
364 ATF_TC_WITHOUT_HEAD(timerfd__simple_blocking_periodic_timer);
365 ATF_TC_BODY(timerfd__simple_blocking_periodic_timer, tc)
366 {
367 	int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
368 
369 	ATF_REQUIRE(timerfd >= 0);
370 
371 	struct itimerspec time = {
372 		.it_value.tv_sec = 0,
373 		.it_value.tv_nsec = 100000000,
374 		.it_interval.tv_sec = 0,
375 		.it_interval.tv_nsec = 100000000,
376 	};
377 
378 	struct timespec b, e;
379 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
380 
381 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
382 
383 	uint64_t timeouts = 0;
384 	int num_loop_iterations = 0;
385 
386 	while (timeouts < 3) {
387 		uint64_t timeouts_local;
388 		ATF_REQUIRE(
389 		    read(timerfd, &timeouts_local, sizeof(timeouts_local)) ==
390 		    (ssize_t)sizeof(timeouts_local));
391 		ATF_REQUIRE(timeouts_local > 0);
392 
393 		++num_loop_iterations;
394 		timeouts += timeouts_local;
395 	}
396 
397 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
398 	timespecsub(&e, &b, &e);
399 
400 	ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 300000000) || e.tv_sec > 0);
401 	ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec >= 300000000 &&
402 	    e.tv_nsec < 300000000 + TIMER_SLACK);
403 
404 	ATF_REQUIRE(num_loop_iterations <= 3);
405 
406 	ATF_REQUIRE(close(timerfd) == 0);
407 }
408 
409 ATF_TC_WITHOUT_HEAD(timerfd__argument_checks);
410 ATF_TC_BODY(timerfd__argument_checks, tc)
411 {
412 	int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
413 	ATF_REQUIRE(timerfd >= 0);
414 
415 	struct itimerspec time = {
416 		.it_value.tv_sec = 0,
417 		.it_value.tv_nsec = 100000000,
418 		.it_interval.tv_sec = 0,
419 		.it_interval.tv_nsec = 100000000,
420 	};
421 
422 	ATF_REQUIRE_ERRNO(EFAULT, timerfd_settime(timerfd, 0, NULL, NULL) < 0);
423 	ATF_REQUIRE_ERRNO(EFAULT, timerfd_settime(-2, 0, NULL, NULL) < 0);
424 	ATF_REQUIRE_ERRNO(EBADF, timerfd_settime(-2, 0, &time, NULL) < 0);
425 	ATF_REQUIRE_ERRNO(EFAULT, timerfd_settime(-2, 42, NULL, NULL) < 0);
426 	ATF_REQUIRE_ERRNO(EINVAL, timerfd_settime(-2, 42, &time, NULL) < 0);
427 	ATF_REQUIRE_ERRNO(EINVAL,
428 	    timerfd_settime(timerfd, 42, &time, NULL) < 0);
429 
430 	{
431 		time = (struct itimerspec) {
432 			.it_value.tv_sec = -1,
433 			.it_value.tv_nsec = 100000000,
434 			.it_interval.tv_sec = 0,
435 			.it_interval.tv_nsec = 100000000,
436 		};
437 		ATF_REQUIRE_ERRNO(EINVAL,
438 		    timerfd_settime(timerfd, 0, &time, NULL) < 0);
439 	}
440 	{
441 		time = (struct itimerspec) {
442 			.it_value.tv_sec = 0,
443 			.it_value.tv_nsec = -1,
444 			.it_interval.tv_sec = 0,
445 			.it_interval.tv_nsec = 100000000,
446 		};
447 		ATF_REQUIRE_ERRNO(EINVAL,
448 		    timerfd_settime(timerfd, 0, &time, NULL) < 0);
449 	}
450 	{
451 		time = (struct itimerspec) {
452 			.it_value.tv_sec = 0,
453 			.it_value.tv_nsec = 100000000,
454 			.it_interval.tv_sec = -1,
455 			.it_interval.tv_nsec = 100000000,
456 		};
457 		ATF_REQUIRE_ERRNO(EINVAL,
458 		    timerfd_settime(timerfd, 0, &time, NULL) < 0);
459 	}
460 	{
461 		time = (struct itimerspec) {
462 			.it_value.tv_sec = 0,
463 			.it_value.tv_nsec = 100000000,
464 			.it_interval.tv_sec = 0,
465 			.it_interval.tv_nsec = -1,
466 		};
467 		ATF_REQUIRE_ERRNO(EINVAL,
468 		    timerfd_settime(timerfd, 0, &time, NULL) < 0);
469 	}
470 	{
471 		time = (struct itimerspec) {
472 			.it_value.tv_sec = 0,
473 			.it_value.tv_nsec = 1000000000,
474 			.it_interval.tv_sec = 0,
475 			.it_interval.tv_nsec = 100000000,
476 		};
477 		ATF_REQUIRE_ERRNO(EINVAL,
478 		    timerfd_settime(timerfd, 0, &time, NULL) < 0);
479 	}
480 	{
481 		time = (struct itimerspec) {
482 			.it_value.tv_sec = 0,
483 			.it_value.tv_nsec = 100000000,
484 			.it_interval.tv_sec = 0,
485 			.it_interval.tv_nsec = 1000000000,
486 		};
487 		ATF_REQUIRE_ERRNO(EINVAL,
488 		    timerfd_settime(timerfd, 0, &time, NULL) < 0);
489 	}
490 
491 	ATF_REQUIRE_ERRNO(EINVAL,
492 	    timerfd_create(CLOCK_MONOTONIC | 42, TFD_CLOEXEC));
493 	ATF_REQUIRE_ERRNO(EINVAL,
494 	    timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | 42));
495 
496 	ATF_REQUIRE(close(timerfd) == 0);
497 
498 	struct itimerspec itimerspec;
499 	ATF_REQUIRE_ERRNO(EBADF, timerfd_gettime(timerfd, &itimerspec) < 0);
500 	ATF_REQUIRE_ERRNO(EINVAL,
501 	    timerfd_settime(timerfd, 0, &itimerspec, NULL) < 0);
502 }
503 
504 ATF_TC_WITHOUT_HEAD(timerfd__upgrade_simple_to_complex);
505 ATF_TC_BODY(timerfd__upgrade_simple_to_complex, tc)
506 {
507 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
508 	    TFD_CLOEXEC | TFD_NONBLOCK);
509 
510 	ATF_REQUIRE(timerfd >= 0);
511 
512 	struct itimerspec time = {
513 		.it_value.tv_sec = 0,
514 		.it_value.tv_nsec = 100000000,
515 		.it_interval.tv_sec = 0,
516 		.it_interval.tv_nsec = 100000000,
517 	};
518 
519 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
520 	(void)wait_for_timerfd(timerfd);
521 
522 	time = (struct itimerspec) {
523 		.it_value.tv_sec = 0,
524 		.it_value.tv_nsec = 50000000,
525 		.it_interval.tv_sec = 0,
526 		.it_interval.tv_nsec = 95000000,
527 	};
528 
529 	struct timespec b, e;
530 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
531 
532 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
533 
534 	uint64_t timeouts = wait_for_timerfd(timerfd);
535 	ATF_REQUIRE(timeouts >= 1);
536 	ATF_REQUIRE(timeouts == 1);
537 
538 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
539 	timespecsub(&e, &b, &e);
540 	ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 50000000) || e.tv_sec > 0);
541 	ATF_REQUIRE_MSG(e.tv_sec == 0 && e.tv_nsec < 50000000 + TIMER_SLACK,
542 	    "%ld", e.tv_nsec);
543 
544 	timeouts = wait_for_timerfd(timerfd);
545 	ATF_REQUIRE(timeouts >= 1);
546 	ATF_REQUIRE(timeouts == 1);
547 
548 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
549 	timespecsub(&e, &b, &e);
550 	ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 145000000) || e.tv_sec > 0);
551 	ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec >= 145000000 &&
552 	    e.tv_nsec < 145000000 + TIMER_SLACK);
553 
554 	ATF_REQUIRE(close(timerfd) == 0);
555 }
556 
557 ATF_TC_WITHOUT_HEAD(timerfd__absolute_timer);
558 ATF_TC_BODY(timerfd__absolute_timer, tc)
559 {
560 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
561 	    TFD_CLOEXEC | TFD_NONBLOCK);
562 
563 	ATF_REQUIRE(timerfd >= 0);
564 
565 	struct timespec b, e;
566 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
567 
568 	struct itimerspec time = {
569 		.it_value = b,
570 		.it_interval.tv_sec = 0,
571 		.it_interval.tv_nsec = 0,
572 	};
573 
574 	struct timespec ts_600ms = {
575 		.tv_sec = 0,
576 		.tv_nsec = 600000000,
577 	};
578 
579 	timespecadd(&time.it_value, &ts_600ms, &time.it_value);
580 
581 	ATF_REQUIRE(timerfd_settime(timerfd, /**/
582 			TFD_TIMER_ABSTIME, &time, NULL) == 0);
583 
584 	struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
585 	ATF_REQUIRE(poll(&pfd, 1, -1) == 1);
586 
587 	// Don't read(2) here!
588 
589 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
590 	timespecsub(&e, &b, &e);
591 	ATF_REQUIRE(e.tv_sec == 0 &&
592 	    /* Don't check for this because of spurious wakeups. */
593 	    /* e.tv_nsec >= 600000000 && */
594 	    e.tv_nsec < 600000000 + TIMER_SLACK);
595 
596 	struct itimerspec zeroed_its = {
597 		.it_value.tv_sec = 0,
598 		.it_value.tv_nsec = 0,
599 		.it_interval.tv_sec = 0,
600 		.it_interval.tv_nsec = 0,
601 	};
602 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &zeroed_its, NULL) == 0);
603 
604 	uint64_t timeouts;
605 	ATF_REQUIRE_ERRNO(EAGAIN,
606 	    read(timerfd, &timeouts, sizeof(timeouts)) < 0);
607 
608 	ATF_REQUIRE(poll(&pfd, 1, 0) == 0);
609 
610 	ATF_REQUIRE(close(timerfd) == 0);
611 }
612 
613 ATF_TC_WITHOUT_HEAD(timerfd__absolute_timer_in_the_past);
614 ATF_TC_BODY(timerfd__absolute_timer_in_the_past, tc)
615 {
616 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
617 	    TFD_CLOEXEC | TFD_NONBLOCK);
618 
619 	ATF_REQUIRE(timerfd >= 0);
620 
621 	struct timespec b;
622 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
623 
624 	{
625 		struct itimerspec time = {
626 			.it_value = b,
627 			.it_interval.tv_sec = 10,
628 			.it_interval.tv_nsec = 0,
629 		};
630 		time.it_value.tv_sec -= 1;
631 
632 		ATF_REQUIRE(timerfd_settime(timerfd, /**/
633 				TFD_TIMER_ABSTIME, &time, NULL) == 0);
634 
635 		struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
636 		ATF_REQUIRE(poll(&pfd, 1, 1000) == 1);
637 	}
638 
639 	{
640 		struct itimerspec time = {
641 			.it_value = b,
642 			.it_interval.tv_sec = 0,
643 			.it_interval.tv_nsec = 10000000,
644 		};
645 		time.it_value.tv_sec -= 1;
646 
647 		ATF_REQUIRE(timerfd_settime(timerfd, /**/
648 				TFD_TIMER_ABSTIME, &time, NULL) == 0);
649 
650 		struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
651 		ATF_REQUIRE(poll(&pfd, 1, -1) == 1);
652 	}
653 
654 	uint64_t timeouts;
655 	ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
656 	    (ssize_t)sizeof(timeouts));
657 
658 	ATF_REQUIRE_MSG(timeouts >= 101, "%d", (int)timeouts);
659 
660 	ATF_REQUIRE(close(timerfd) == 0);
661 }
662 
663 ATF_TC_WITHOUT_HEAD(timerfd__reset_absolute);
664 ATF_TC_BODY(timerfd__reset_absolute, tc)
665 {
666 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
667 	    TFD_CLOEXEC | TFD_NONBLOCK);
668 
669 	ATF_REQUIRE(timerfd >= 0);
670 
671 	struct timespec b;
672 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
673 
674 	{
675 		struct itimerspec time = {
676 			.it_value = b,
677 		};
678 		time.it_value.tv_sec += 10;
679 
680 		ATF_REQUIRE(timerfd_settime(timerfd, /**/
681 				TFD_TIMER_ABSTIME, &time, NULL) == 0);
682 
683 		struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
684 		ATF_REQUIRE(poll(&pfd, 1, 100) == 0);
685 	}
686 
687 	{
688 		struct itimerspec time = {
689 			.it_value = b,
690 		};
691 		time.it_value.tv_nsec += 500000000;
692 		if (time.it_value.tv_nsec >= 1000000000) {
693 			time.it_value.tv_nsec -= 1000000000;
694 			time.it_value.tv_sec += 1;
695 		}
696 
697 		ATF_REQUIRE(timerfd_settime(timerfd, /**/
698 				TFD_TIMER_ABSTIME, &time, NULL) == 0);
699 
700 		struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
701 		ATF_REQUIRE(poll(&pfd, 1, 1000) == 1);
702 	}
703 
704 	uint64_t timeouts;
705 	ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
706 	    (ssize_t)sizeof(timeouts));
707 
708 	ATF_REQUIRE_MSG(timeouts == 1, "%d", (int)timeouts);
709 
710 	ATF_REQUIRE(close(timerfd) == 0);
711 }
712 
713 ATF_TC(timerfd__periodic_timer_performance);
714 ATF_TC_HEAD(timerfd__periodic_timer_performance, tc)
715 {
716 	atf_tc_set_md_var(tc, "timeout", "1");
717 }
718 ATF_TC_BODY(timerfd__periodic_timer_performance, tc)
719 {
720 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
721 	    TFD_CLOEXEC | TFD_NONBLOCK);
722 
723 	ATF_REQUIRE(timerfd >= 0);
724 
725 	struct itimerspec time = {
726 		.it_value.tv_sec = 0,
727 		.it_value.tv_nsec = 1,
728 		.it_interval.tv_sec = 0,
729 		.it_interval.tv_nsec = 1,
730 	};
731 
732 	ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
733 
734 	usleep(400000);
735 
736 	struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
737 	ATF_REQUIRE(poll(&pfd, 1, -1) == 1);
738 
739 	uint64_t timeouts;
740 	ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
741 	    (ssize_t)sizeof(timeouts));
742 	ATF_REQUIRE_MSG(timeouts >= 400000000, "%ld", (long)timeouts);
743 
744 	ATF_REQUIRE(close(timerfd) == 0);
745 }
746 
747 ATF_TC_WITHOUT_HEAD(timerfd__argument_overflow);
748 ATF_TC_BODY(timerfd__argument_overflow, tc)
749 {
750 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
751 	    TFD_CLOEXEC | TFD_NONBLOCK);
752 	ATF_REQUIRE(timerfd >= 0);
753 	{
754 		struct itimerspec time = {
755 			.it_value.tv_sec = 0,
756 			.it_value.tv_nsec = 1,
757 		};
758 
759 		ATF_REQUIRE(timerfd_settime(timerfd, /**/
760 				TFD_TIMER_ABSTIME, &time, NULL) == 0);
761 
762 		struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
763 		ATF_REQUIRE(poll(&pfd, 1, -1) == 1);
764 
765 		uint64_t timeouts;
766 		ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
767 		    (ssize_t)sizeof(timeouts));
768 		ATF_REQUIRE(timeouts == 1);
769 
770 		ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) < 0);
771 	}
772 	{
773 		struct itimerspec time = {
774 			.it_value.tv_sec = LONG_MAX,
775 			.it_value.tv_nsec = 999999999,
776 		};
777 
778 		ATF_REQUIRE(timerfd_settime(timerfd, /**/
779 				TFD_TIMER_ABSTIME, &time, NULL) == 0);
780 
781 		struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
782 		ATF_REQUIRE(poll(&pfd, 1, 500) == 0);
783 
784 		uint64_t timeouts;
785 		ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) < 0);
786 	}
787 
788 	ATF_REQUIRE(close(timerfd) == 0);
789 }
790 
791 ATF_TC(timerfd__short_evfilt_timer_timeout);
792 ATF_TC_HEAD(timerfd__short_evfilt_timer_timeout, tc)
793 {
794 	atf_tc_set_md_var(tc, "timeout", "30");
795 }
796 ATF_TC_BODY(timerfd__short_evfilt_timer_timeout, tc)
797 {
798 	int kq = kqueue();
799 	ATF_REQUIRE(kq >= 0);
800 
801 	bool returns_early = false;
802 
803 	for (int l = 0; l < 10; ++l) {
804 		for (int i = 1; i <= 17; ++i) {
805 			struct kevent kev;
806 			EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, i,
807 			    0);
808 
809 			struct timespec b;
810 			ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
811 
812 			ATF_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
813 
814 			ATF_REQUIRE(kevent(kq, NULL, 0, &kev, 1, NULL) == 1);
815 
816 			struct timespec e;
817 			ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
818 
819 			struct timespec diff;
820 			timespecsub(&e, &b, &diff);
821 
822 			if (diff.tv_sec != 0 || diff.tv_nsec < i * 1000000) {
823 				fprintf(stderr,
824 				    "expected: %lldns, got: %lldns\n",
825 				    (long long)(i * 1000000LL),
826 				    (long long)diff.tv_nsec);
827 				returns_early = true;
828 				goto check;
829 			}
830 		}
831 	}
832 
833 check:
834 	ATF_REQUIRE(!returns_early);
835 
836 	ATF_REQUIRE(close(kq) == 0);
837 
838 	/*
839 	 * timerfd's should never return early, regardless of how
840 	 * EVFILT_TIMER behaves.
841 	 */
842 
843 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
844 	    TFD_CLOEXEC | TFD_NONBLOCK);
845 
846 	ATF_REQUIRE(timerfd >= 0);
847 
848 	for (int l = 0; l < 10; ++l) {
849 		for (int i = 1; i <= 17; ++i) {
850 			struct itimerspec time = {
851 				.it_value.tv_sec = 0,
852 				.it_value.tv_nsec = i * 1000000,
853 			};
854 
855 			struct timespec b;
856 			ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
857 
858 			ATF_REQUIRE(
859 			    timerfd_settime(timerfd, 0, &time, NULL) == 0);
860 			(void)wait_for_timerfd(timerfd);
861 
862 			struct timespec e;
863 			ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
864 
865 			struct timespec diff;
866 			timespecsub(&e, &b, &diff);
867 
868 			ATF_REQUIRE(
869 			    diff.tv_sec == 0 && diff.tv_nsec >= i * 1000000);
870 			fprintf(stderr, "%dms, waited %lldns\n", i,
871 			    (long long)diff.tv_nsec);
872 		}
873 	}
874 
875 	ATF_REQUIRE(close(timerfd) == 0);
876 }
877 
878 ATF_TC_WITHOUT_HEAD(timerfd__unmodified_errno);
879 ATF_TC_BODY(timerfd__unmodified_errno, tc)
880 {
881 	ATF_REQUIRE(errno == 0);
882 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
883 	    TFD_CLOEXEC | TFD_NONBLOCK);
884 	ATF_REQUIRE(timerfd >= 0);
885 	ATF_REQUIRE(errno == 0);
886 
887 	ATF_REQUIRE(timerfd_settime(timerfd, 0,
888 			&(struct itimerspec) {
889 			    .it_value.tv_sec = 0,
890 			    .it_value.tv_nsec = 100000000,
891 			},
892 			NULL) == 0);
893 	ATF_REQUIRE(errno == 0);
894 	(void)wait_for_timerfd(timerfd);
895 	ATF_REQUIRE(errno == 0);
896 
897 	ATF_REQUIRE(timerfd_settime(timerfd, 0,
898 			&(struct itimerspec) {
899 			    .it_value.tv_sec = 0,
900 			    .it_value.tv_nsec = 0,
901 			},
902 			NULL) == 0);
903 	ATF_REQUIRE(errno == 0);
904 
905 	ATF_REQUIRE(timerfd_settime(timerfd, 0,
906 			&(struct itimerspec) {
907 			    .it_value.tv_sec = 0,
908 			    .it_value.tv_nsec = 0,
909 			},
910 			NULL) == 0);
911 	ATF_REQUIRE(errno == 0);
912 
913 	ATF_REQUIRE(close(timerfd) == 0);
914 	ATF_REQUIRE(errno == 0);
915 }
916 
917 ATF_TC_WITHOUT_HEAD(timerfd__reset_to_very_long);
918 ATF_TC_BODY(timerfd__reset_to_very_long, tc)
919 {
920 	ATF_REQUIRE(errno == 0);
921 	int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
922 	    TFD_CLOEXEC | TFD_NONBLOCK);
923 	ATF_REQUIRE(timerfd >= 0);
924 	ATF_REQUIRE(errno == 0);
925 
926 	ATF_REQUIRE(timerfd_settime(timerfd, 0,
927 			&(struct itimerspec) {
928 			    .it_value.tv_sec = 0,
929 			    .it_value.tv_nsec = 100000000,
930 			},
931 			NULL) == 0);
932 	ATF_REQUIRE(errno == 0);
933 
934 	ATF_REQUIRE(timerfd_settime(timerfd, 0,
935 			&(struct itimerspec) {
936 			    .it_value.tv_sec = 630720000,
937 			    .it_value.tv_nsec = 0,
938 			},
939 			NULL) == 0);
940 	ATF_REQUIRE(errno == 0);
941 
942 	struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
943 	ATF_REQUIRE(poll(&pfd, 1, 500) == 0);
944 	uint64_t timeouts;
945 	ssize_t r = read(timerfd, &timeouts, sizeof(timeouts));
946 	ATF_REQUIRE_ERRNO(EAGAIN, r < 0);
947 
948 	ATF_REQUIRE(close(timerfd) == 0);
949 	ATF_REQUIRE(errno == EAGAIN);
950 }
951 
952 ATF_TC_WITHOUT_HEAD(timerfd__missed_events);
953 ATF_TC_BODY(timerfd__missed_events, tc)
954 {
955 	struct itimerspec its = { };
956 	uint64_t timeouts;
957 	int timerfd;
958 
959 	timerfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
960 	ATF_REQUIRE(timerfd >= 0);
961 
962 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &its.it_value) == 0);
963 	its.it_value.tv_sec -= 1000;
964 	its.it_interval.tv_sec = 1;
965 
966 	ATF_REQUIRE(timerfd_settime(timerfd, TFD_TIMER_ABSTIME, &its,
967 	    NULL) == 0);
968 
969 	ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
970 	    sizeof(timeouts));
971 	ATF_REQUIRE_MSG(timeouts == 1001, "%ld", (long)timeouts);
972 
973 	ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
974 	    sizeof(timeouts));
975 	ATF_REQUIRE_MSG(timeouts == 1, "%ld", (long)timeouts);
976 
977 	ATF_REQUIRE(close(timerfd) == 0);
978 }
979 
980 /*
981  * Tests requiring root (clock_settime on CLOCK_REALTIME).
982  * Tests gracefully skip if not running as root.
983  */
984 
985 static struct timespec current_time;
986 static void
987 reset_time(void)
988 {
989 	(void)clock_settime(CLOCK_REALTIME, &current_time);
990 }
991 
992 static void
993 clock_settime_or_skip_test(clockid_t clockid, struct timespec const *ts)
994 {
995 	int r = clock_settime(clockid, ts);
996 	if (r < 0 && errno == EPERM) {
997 		atf_tc_skip("root required");
998 	}
999 	ATF_REQUIRE(r == 0);
1000 }
1001 
1002 ATF_TC_WITHOUT_HEAD(timerfd_root__zero_read_on_abs_realtime);
1003 ATF_TC_BODY(timerfd_root__zero_read_on_abs_realtime, tc)
1004 {
1005 	int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
1006 	ATF_REQUIRE(tfd >= 0);
1007 
1008 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &current_time) == 0);
1009 	ATF_REQUIRE(atexit(reset_time) == 0);
1010 
1011 	ATF_REQUIRE(timerfd_settime(tfd, TFD_TIMER_ABSTIME,
1012 			&(struct itimerspec) {
1013 			    .it_value = current_time,
1014 			    .it_interval.tv_sec = 1,
1015 			    .it_interval.tv_nsec = 0,
1016 			},
1017 			NULL) == 0);
1018 
1019 	ATF_REQUIRE(
1020 	    poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, -1) == 1);
1021 
1022 	clock_settime_or_skip_test(CLOCK_REALTIME,
1023 	    &(struct timespec) {
1024 		.tv_sec = current_time.tv_sec - 1,
1025 		.tv_nsec = current_time.tv_nsec,
1026 	    });
1027 
1028 	uint64_t exp;
1029 	ssize_t r = read(tfd, &exp, sizeof(exp));
1030 	ATF_REQUIRE_MSG(r == 0, "r: %d, errno: %d", (int)r, errno);
1031 
1032 	{
1033 		int r = fcntl(tfd, F_GETFL);
1034 		ATF_REQUIRE(r >= 0);
1035 		r = fcntl(tfd, F_SETFL, r | O_NONBLOCK);
1036 		ATF_REQUIRE(r >= 0);
1037 	}
1038 
1039 	r = read(tfd, &exp, sizeof(exp));
1040 	ATF_REQUIRE_ERRNO(EAGAIN, r < 0);
1041 
1042 	current_time.tv_sec += 1;
1043 	ATF_REQUIRE(poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1,
1044 			1800) == 1);
1045 	r = read(tfd, &exp, sizeof(exp));
1046 	ATF_REQUIRE(r == (ssize_t)sizeof(exp));
1047 	ATF_REQUIRE(exp == 1);
1048 
1049 	ATF_REQUIRE(close(tfd) == 0);
1050 }
1051 
1052 ATF_TC_WITHOUT_HEAD(timerfd_root__read_on_abs_realtime_no_interval);
1053 ATF_TC_BODY(timerfd_root__read_on_abs_realtime_no_interval, tc)
1054 {
1055 	int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
1056 	ATF_REQUIRE(tfd >= 0);
1057 
1058 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &current_time) == 0);
1059 	ATF_REQUIRE(atexit(reset_time) == 0);
1060 
1061 	ATF_REQUIRE(timerfd_settime(tfd, TFD_TIMER_ABSTIME,
1062 			&(struct itimerspec) {
1063 			    .it_value = current_time,
1064 			    .it_interval.tv_sec = 0,
1065 			    .it_interval.tv_nsec = 0,
1066 			},
1067 			NULL) == 0);
1068 
1069 	ATF_REQUIRE(
1070 	    poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, -1) == 1);
1071 
1072 	clock_settime_or_skip_test(CLOCK_REALTIME,
1073 	    &(struct timespec) {
1074 		.tv_sec = current_time.tv_sec - 1,
1075 		.tv_nsec = current_time.tv_nsec,
1076 	    });
1077 
1078 	uint64_t exp;
1079 	ssize_t r = read(tfd, &exp, sizeof(exp));
1080 	ATF_REQUIRE(r == (ssize_t)sizeof(exp));
1081 	ATF_REQUIRE(exp == 1);
1082 
1083 	ATF_REQUIRE(close(tfd) == 0);
1084 }
1085 
1086 ATF_TC_WITHOUT_HEAD(timerfd_root__cancel_on_set);
1087 ATF_TC_BODY(timerfd_root__cancel_on_set, tc)
1088 {
1089 	int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
1090 	ATF_REQUIRE(tfd >= 0);
1091 
1092 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &current_time) == 0);
1093 	ATF_REQUIRE(atexit(reset_time) == 0);
1094 
1095 	ATF_REQUIRE(
1096 	    timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET,
1097 		&(struct itimerspec) {
1098 		    .it_value.tv_sec = current_time.tv_sec + 10,
1099 		    .it_value.tv_nsec = current_time.tv_nsec,
1100 		    .it_interval.tv_sec = 0,
1101 		    .it_interval.tv_nsec = 0,
1102 		},
1103 		NULL) == 0);
1104 
1105 	clock_settime_or_skip_test(CLOCK_REALTIME, &current_time);
1106 
1107 	ATF_REQUIRE(
1108 	    poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, -1) == 1);
1109 
1110 	{
1111 		int r = timerfd_settime(tfd,
1112 		    TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET,
1113 		    &(struct itimerspec) {
1114 			.it_value.tv_sec = current_time.tv_sec,
1115 			.it_value.tv_nsec = current_time.tv_nsec,
1116 			.it_interval.tv_sec = 0,
1117 			.it_interval.tv_nsec = 0,
1118 		    },
1119 		    NULL);
1120 		ATF_REQUIRE_ERRNO(ECANCELED, r < 0);
1121 	}
1122 
1123 	ATF_REQUIRE(poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1,
1124 			800) == 1);
1125 
1126 	uint64_t exp;
1127 	ssize_t r;
1128 
1129 	r = read(tfd, &exp, sizeof(exp));
1130 	ATF_REQUIRE(r == (ssize_t)sizeof(exp));
1131 	ATF_REQUIRE(exp == 1);
1132 
1133 	ATF_REQUIRE(
1134 	    timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET,
1135 		&(struct itimerspec) {
1136 		    .it_value.tv_sec = current_time.tv_sec + 1,
1137 		    .it_value.tv_nsec = current_time.tv_nsec,
1138 		    .it_interval.tv_sec = 1,
1139 		    .it_interval.tv_nsec = 0,
1140 		},
1141 		NULL) == 0);
1142 
1143 	clock_settime_or_skip_test(CLOCK_REALTIME, &current_time);
1144 
1145 	ATF_REQUIRE(
1146 	    poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, -1) == 1);
1147 
1148 	r = read(tfd, &exp, sizeof(exp));
1149 	ATF_REQUIRE_ERRNO(ECANCELED, r < 0);
1150 
1151 	r = read(tfd, &exp, sizeof(exp));
1152 	current_time.tv_sec += 1;
1153 	ATF_REQUIRE_MSG(r == (ssize_t)sizeof(exp), "%d %d", (int)r, errno);
1154 	ATF_REQUIRE(exp == 1);
1155 
1156 	ATF_REQUIRE(
1157 	    timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET,
1158 		&(struct itimerspec) {
1159 		    .it_value.tv_sec = current_time.tv_sec + 1,
1160 		    .it_value.tv_nsec = current_time.tv_nsec,
1161 		    .it_interval.tv_sec = 1,
1162 		    .it_interval.tv_nsec = 0,
1163 		},
1164 		NULL) == 0);
1165 
1166 	clock_settime_or_skip_test(CLOCK_REALTIME, &current_time);
1167 	current_time.tv_sec += 2;
1168 	ATF_REQUIRE(nanosleep(&(struct timespec) { .tv_sec = 2 }, NULL) == 0);
1169 
1170 	r = read(tfd, &exp, sizeof(exp));
1171 	ATF_REQUIRE_ERRNO(ECANCELED, r < 0);
1172 
1173 	r = poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, 3000);
1174 	ATF_REQUIRE(r == 0);
1175 	current_time.tv_sec += 3;
1176 
1177 	ATF_REQUIRE(close(tfd) == 0);
1178 }
1179 
1180 ATF_TC_WITHOUT_HEAD(timerfd_root__cancel_on_set_init);
1181 ATF_TC_BODY(timerfd_root__cancel_on_set_init, tc)
1182 {
1183 	int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
1184 	ATF_REQUIRE(tfd >= 0);
1185 
1186 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &current_time) == 0);
1187 	ATF_REQUIRE(atexit(reset_time) == 0);
1188 
1189 	clock_settime_or_skip_test(CLOCK_REALTIME, &current_time);
1190 
1191 	ATF_REQUIRE(
1192 	    timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET,
1193 		&(struct itimerspec) {
1194 		    .it_value.tv_sec = current_time.tv_sec + 10,
1195 		    .it_value.tv_nsec = current_time.tv_nsec,
1196 		    .it_interval.tv_sec = 0,
1197 		    .it_interval.tv_nsec = 0,
1198 		},
1199 		NULL) == 0);
1200 
1201 	clock_settime_or_skip_test(CLOCK_REALTIME, &current_time);
1202 
1203 	int r = timerfd_settime(tfd,
1204 	    TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET,
1205 	    &(struct itimerspec) {
1206 		.it_value.tv_sec = current_time.tv_sec + 10,
1207 		.it_value.tv_nsec = current_time.tv_nsec,
1208 		.it_interval.tv_sec = 0,
1209 		.it_interval.tv_nsec = 0,
1210 	    },
1211 	    NULL);
1212 	ATF_REQUIRE_ERRNO(ECANCELED, r < 0);
1213 	ATF_REQUIRE(close(tfd) == 0);
1214 }
1215 
1216 static void *
1217 clock_change_thread(void *arg)
1218 {
1219 	(void)arg;
1220 
1221 	fprintf(stderr, "clock change\n");
1222 	clock_settime_or_skip_test(CLOCK_REALTIME, &current_time);
1223 
1224 	current_time.tv_sec += 2;
1225 	ATF_REQUIRE(nanosleep(&(struct timespec) { .tv_sec = 2 }, NULL) == 0);
1226 
1227 	fprintf(stderr, "clock change\n");
1228 	clock_settime_or_skip_test(CLOCK_REALTIME, &current_time);
1229 
1230 	return NULL;
1231 }
1232 
1233 ATF_TC(timerfd_root__clock_change_notification);
1234 ATF_TC_HEAD(timerfd_root__clock_change_notification, tc)
1235 {
1236 	atf_tc_set_md_var(tc, "timeout", "10");
1237 }
1238 ATF_TC_BODY(timerfd_root__clock_change_notification, tc)
1239 {
1240 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &current_time) == 0);
1241 	ATF_REQUIRE(atexit(reset_time) == 0);
1242 
1243 	clock_settime_or_skip_test(CLOCK_REALTIME, &current_time);
1244 
1245 #define TIME_T_MAX (time_t)((UINTMAX_C(1) << ((sizeof(time_t) << 3) - 1)) - 1)
1246 	struct itimerspec its = {
1247 		.it_value.tv_sec = TIME_T_MAX,
1248 	};
1249 #undef TIME_T_MAX
1250 
1251 	int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
1252 	ATF_REQUIRE(tfd >= 0);
1253 
1254 	ATF_REQUIRE(
1255 	    timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET,
1256 		&its, NULL) == 0);
1257 
1258 	pthread_t clock_changer;
1259 	ATF_REQUIRE(pthread_create(&clock_changer, NULL, /**/
1260 			clock_change_thread, NULL) == 0);
1261 
1262 	uint64_t exp;
1263 	ssize_t r;
1264 
1265 	r = read(tfd, &exp, sizeof(exp));
1266 	ATF_REQUIRE_ERRNO(ECANCELED, r < 0);
1267 	fprintf(stderr, "clock change detected\n");
1268 
1269 	r = read(tfd, &exp, sizeof(exp));
1270 	ATF_REQUIRE_ERRNO(ECANCELED, r < 0);
1271 	fprintf(stderr, "clock change detected\n");
1272 
1273 	ATF_REQUIRE(pthread_join(clock_changer, NULL) == 0);
1274 
1275 	ATF_REQUIRE(close(tfd) == 0);
1276 }
1277 
1278 ATF_TC_WITHOUT_HEAD(timerfd_root__advance_time_no_cancel);
1279 ATF_TC_BODY(timerfd_root__advance_time_no_cancel, tc)
1280 {
1281 	int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
1282 	ATF_REQUIRE(tfd >= 0);
1283 
1284 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &current_time) == 0);
1285 	ATF_REQUIRE(atexit(reset_time) == 0);
1286 
1287 	ATF_REQUIRE(timerfd_settime(tfd, TFD_TIMER_ABSTIME,
1288 			&(struct itimerspec) {
1289 			    .it_value.tv_sec = current_time.tv_sec + 10,
1290 			    .it_value.tv_nsec = current_time.tv_nsec,
1291 			    .it_interval.tv_sec = 0,
1292 			    .it_interval.tv_nsec = 0,
1293 			},
1294 			NULL) == 0);
1295 
1296 	current_time.tv_sec += 9;
1297 	clock_settime_or_skip_test(CLOCK_REALTIME, &current_time);
1298 	current_time.tv_sec -= 8;
1299 
1300 	{
1301 		int r = poll(&(struct pollfd) { .fd = tfd, .events = POLLIN },
1302 		    1, 1800);
1303 		ATF_REQUIRE(r == 1);
1304 	}
1305 
1306 	uint64_t exp;
1307 	ssize_t r;
1308 
1309 	r = read(tfd, &exp, sizeof(exp));
1310 	ATF_REQUIRE(r == (ssize_t)sizeof(exp));
1311 	ATF_REQUIRE(exp == 1);
1312 
1313 	ATF_REQUIRE(close(tfd) == 0);
1314 }
1315 
1316 ATF_TP_ADD_TCS(tp)
1317 {
1318 	ATF_TP_ADD_TC(tp, timerfd__many_timers);
1319 	ATF_TP_ADD_TC(tp, timerfd__simple_timer);
1320 	ATF_TP_ADD_TC(tp, timerfd__simple_periodic_timer);
1321 	ATF_TP_ADD_TC(tp, timerfd__complex_periodic_timer);
1322 	ATF_TP_ADD_TC(tp, timerfd__reset_periodic_timer);
1323 	ATF_TP_ADD_TC(tp, timerfd__reenable_periodic_timer);
1324 	ATF_TP_ADD_TC(tp, timerfd__expire_five);
1325 	ATF_TP_ADD_TC(tp, timerfd__simple_gettime);
1326 	ATF_TP_ADD_TC(tp, timerfd__simple_blocking_periodic_timer);
1327 	ATF_TP_ADD_TC(tp, timerfd__argument_checks);
1328 	ATF_TP_ADD_TC(tp, timerfd__upgrade_simple_to_complex);
1329 	ATF_TP_ADD_TC(tp, timerfd__absolute_timer);
1330 	ATF_TP_ADD_TC(tp, timerfd__absolute_timer_in_the_past);
1331 	ATF_TP_ADD_TC(tp, timerfd__reset_absolute);
1332 	ATF_TP_ADD_TC(tp, timerfd__periodic_timer_performance);
1333 	ATF_TP_ADD_TC(tp, timerfd__argument_overflow);
1334 	ATF_TP_ADD_TC(tp, timerfd__short_evfilt_timer_timeout);
1335 	ATF_TP_ADD_TC(tp, timerfd__unmodified_errno);
1336 	ATF_TP_ADD_TC(tp, timerfd__reset_to_very_long);
1337 	ATF_TP_ADD_TC(tp, timerfd__missed_events);
1338 
1339 	ATF_TP_ADD_TC(tp, timerfd_root__zero_read_on_abs_realtime);
1340 	ATF_TP_ADD_TC(tp, timerfd_root__read_on_abs_realtime_no_interval);
1341 	ATF_TP_ADD_TC(tp, timerfd_root__cancel_on_set);
1342 	ATF_TP_ADD_TC(tp, timerfd_root__cancel_on_set_init);
1343 	ATF_TP_ADD_TC(tp, timerfd_root__clock_change_notification);
1344 	ATF_TP_ADD_TC(tp, timerfd_root__advance_time_no_cancel);
1345 
1346 	return atf_no_error();
1347 }
1348