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 #include <sys/event.h>
29 #include <sys/param.h>
30 #include <sys/select.h>
31 #include <sys/time.h>
32 #include <sys/timerfd.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 /* Time in ns that sleeps are allowed to take longer for in unit tests. */
48 #define TIMER_SLACK (90000000)
49
50 ATF_TC_WITHOUT_HEAD(timerfd__many_timers);
ATF_TC_BODY(timerfd__many_timers,tc)51 ATF_TC_BODY(timerfd__many_timers, tc)
52 {
53 int timer_fds[256];
54 int i;
55
56 for (i = 0; i < (int)nitems(timer_fds); ++i) {
57 timer_fds[i] = timerfd_create(CLOCK_MONOTONIC, /**/
58 TFD_CLOEXEC | TFD_NONBLOCK);
59 if (timer_fds[i] < 0 && errno == EMFILE) {
60 atf_tc_skip("timerfd_create: EMFILE");
61 }
62 ATF_REQUIRE_MSG(timer_fds[i] >= 0, "errno: %d", errno);
63 }
64 }
65
66 static uint64_t
wait_for_timerfd(int timerfd)67 wait_for_timerfd(int timerfd)
68 {
69 struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
70
71 ATF_REQUIRE(poll(&pfd, 1, -1) == 1);
72
73 uint64_t timeouts;
74 ssize_t r = read(timerfd, &timeouts, sizeof(timeouts));
75
76 ATF_REQUIRE_MSG(r == (ssize_t)sizeof(timeouts), "%d %d", (int)r, errno);
77 ATF_REQUIRE(timeouts > 0);
78 return timeouts;
79 }
80
81 ATF_TC_WITHOUT_HEAD(timerfd__simple_timer);
ATF_TC_BODY(timerfd__simple_timer,tc)82 ATF_TC_BODY(timerfd__simple_timer, tc)
83 {
84 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
85 TFD_CLOEXEC | TFD_NONBLOCK);
86
87 ATF_REQUIRE(timerfd >= 0);
88
89 struct itimerspec time = {
90 .it_value.tv_sec = 0,
91 .it_value.tv_nsec = 100000000,
92 };
93
94 struct timespec b, e;
95 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
96
97 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
98 (void)wait_for_timerfd(timerfd);
99
100 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
101 timespecsub(&e, &b, &e);
102
103 ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 100000000) || e.tv_sec > 0);
104 ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec < 100000000 + TIMER_SLACK);
105
106 ATF_REQUIRE(close(timerfd) == 0);
107 }
108
109 ATF_TC_WITHOUT_HEAD(timerfd__simple_periodic_timer);
ATF_TC_BODY(timerfd__simple_periodic_timer,tc)110 ATF_TC_BODY(timerfd__simple_periodic_timer, tc)
111 {
112 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
113 TFD_CLOEXEC | TFD_NONBLOCK);
114
115 ATF_REQUIRE(timerfd >= 0);
116
117 struct itimerspec time = {
118 .it_value.tv_sec = 0,
119 .it_value.tv_nsec = 200000000,
120 .it_interval.tv_sec = 0,
121 .it_interval.tv_nsec = 200000000,
122 };
123
124 struct timespec b, e;
125 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
126
127 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
128 uint64_t timeouts = wait_for_timerfd(timerfd);
129
130 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
131 timespecsub(&e, &b, &e);
132
133 ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 200000000) || e.tv_sec > 0);
134 ATF_REQUIRE(timeouts >= 1);
135 ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec < 200000000 + TIMER_SLACK);
136 ATF_REQUIRE(timeouts == 1);
137
138 usleep(400000);
139
140 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
141 (ssize_t)sizeof(timeouts));
142 ATF_REQUIRE(timeouts >= 2);
143 ATF_REQUIRE(timeouts == 2);
144
145 ATF_REQUIRE(close(timerfd) == 0);
146 }
147
148 ATF_TC_WITHOUT_HEAD(timerfd__complex_periodic_timer);
ATF_TC_BODY(timerfd__complex_periodic_timer,tc)149 ATF_TC_BODY(timerfd__complex_periodic_timer, tc)
150 {
151 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
152 TFD_CLOEXEC | TFD_NONBLOCK);
153
154 ATF_REQUIRE(timerfd >= 0);
155
156 struct itimerspec time = {
157 .it_value.tv_sec = 0,
158 .it_value.tv_nsec = 100000000,
159 .it_interval.tv_sec = 0,
160 .it_interval.tv_nsec = 200000001,
161 };
162
163 struct timespec b, e;
164 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
165
166 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
167 uint64_t timeouts = wait_for_timerfd(timerfd);
168
169 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
170 timespecsub(&e, &b, &e);
171
172 ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 100000000) || e.tv_sec > 0);
173 ATF_REQUIRE(timeouts >= 1);
174 ATF_REQUIRE_MSG(e.tv_sec == 0 && e.tv_nsec >= 100000000 &&
175 e.tv_nsec < 100000000 + TIMER_SLACK,
176 "%ld", (long)e.tv_nsec);
177 ATF_REQUIRE(timeouts == 1);
178
179 usleep(401000);
180
181 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
182 (ssize_t)sizeof(timeouts));
183 ATF_REQUIRE_MSG(timeouts >= 2, "%d", (int)timeouts);
184 ATF_REQUIRE_MSG(timeouts == 2, "%d", (int)timeouts);
185
186 ATF_REQUIRE(close(timerfd) == 0);
187 }
188
189 ATF_TC_WITHOUT_HEAD(timerfd__reset_periodic_timer);
ATF_TC_BODY(timerfd__reset_periodic_timer,tc)190 ATF_TC_BODY(timerfd__reset_periodic_timer, tc)
191 {
192 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
193 TFD_CLOEXEC | TFD_NONBLOCK);
194
195 ATF_REQUIRE(timerfd >= 0);
196
197 struct itimerspec time = {
198 .it_value.tv_sec = 0,
199 .it_value.tv_nsec = 100000000,
200 .it_interval.tv_sec = 0,
201 .it_interval.tv_nsec = 100000000,
202 };
203
204 struct timespec b, e;
205 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
206
207 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
208 (void)wait_for_timerfd(timerfd);
209
210 time = (struct itimerspec) {
211 .it_value.tv_sec = 0,
212 .it_value.tv_nsec = 50000000,
213 .it_interval.tv_sec = 0,
214 .it_interval.tv_nsec = 100000000,
215 };
216
217 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
218
219 uint64_t timeouts = wait_for_timerfd(timerfd);
220 ATF_REQUIRE(timeouts >= 1);
221 ATF_REQUIRE(timeouts == 1);
222
223 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
224 timespecsub(&e, &b, &e);
225 ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 150000000) || e.tv_sec > 0);
226 ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec >= 150000000 &&
227 e.tv_nsec < 150000000 + TIMER_SLACK * 2);
228
229 ATF_REQUIRE(close(timerfd) == 0);
230 }
231
232 ATF_TC_WITHOUT_HEAD(timerfd__reenable_periodic_timer);
ATF_TC_BODY(timerfd__reenable_periodic_timer,tc)233 ATF_TC_BODY(timerfd__reenable_periodic_timer, tc)
234 {
235 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
236 TFD_CLOEXEC | TFD_NONBLOCK);
237
238 ATF_REQUIRE(timerfd >= 0);
239
240 struct itimerspec time = {
241 .it_value.tv_sec = 0,
242 .it_value.tv_nsec = 100000000,
243 .it_interval.tv_sec = 0,
244 .it_interval.tv_nsec = 100000000,
245 };
246
247 struct timespec b, e;
248 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
249
250 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
251 uint64_t timeouts = wait_for_timerfd(timerfd);
252
253 ATF_REQUIRE(timeouts >= 1);
254 ATF_REQUIRE(timeouts == 1);
255
256 time = (struct itimerspec) {
257 .it_value.tv_sec = 0,
258 .it_value.tv_nsec = 0,
259 .it_interval.tv_sec = 0,
260 .it_interval.tv_nsec = 0,
261 };
262
263 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
264
265 struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
266 ATF_REQUIRE(poll(&pfd, 1, 250) == 0);
267
268 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
269 timespecsub(&e, &b, &e);
270
271 ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 350000000) || e.tv_sec > 0);
272 ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec >= 350000000 &&
273 e.tv_nsec < 350000000 + TIMER_SLACK * 2);
274
275 time = (struct itimerspec) {
276 .it_value.tv_sec = 1,
277 .it_value.tv_nsec = 0,
278 .it_interval.tv_sec = 1,
279 .it_interval.tv_nsec = 0,
280 };
281 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
282
283 ATF_REQUIRE(close(timerfd) == 0);
284 }
285
286 /*
287 * Adapted from sghctoma's example here:
288 * https://github.com/jiixyj/epoll-shim/issues/2
289 *
290 * The SIGUSR1 signal should not kill the process.
291 */
292 ATF_TC_WITHOUT_HEAD(timerfd__expire_five);
ATF_TC_BODY(timerfd__expire_five,tc)293 ATF_TC_BODY(timerfd__expire_five, tc)
294 {
295 int fd;
296 struct itimerspec value;
297 uint64_t total_exp = 0;
298
299 fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
300 ATF_REQUIRE(fd >= 0);
301
302 value.it_value.tv_sec = 3;
303 value.it_value.tv_nsec = 0;
304 value.it_interval.tv_sec = 1;
305 value.it_interval.tv_nsec = 0;
306
307 ATF_REQUIRE(timerfd_settime(fd, 0, &value, NULL) == 0);
308
309 sigset_t sigs;
310 sigemptyset(&sigs);
311 sigaddset(&sigs, SIGUSR1);
312 sigprocmask(SIG_BLOCK, &sigs, NULL);
313
314 kill(getpid(), SIGUSR1);
315
316 for (;;) {
317 uint64_t exp = wait_for_timerfd(fd);
318
319 printf("timer expired %u times\n", (unsigned)exp);
320
321 total_exp += exp;
322 if (total_exp >= 5) {
323 break;
324 }
325 }
326
327 ATF_REQUIRE(close(fd) == 0);
328 }
329
330 ATF_TC_WITHOUT_HEAD(timerfd__simple_gettime);
ATF_TC_BODY(timerfd__simple_gettime,tc)331 ATF_TC_BODY(timerfd__simple_gettime, tc)
332 {
333 struct itimerspec curr_value;
334
335 int fd = timerfd_create(CLOCK_MONOTONIC, 0);
336 ATF_REQUIRE(fd >= 0);
337
338 ATF_REQUIRE(timerfd_gettime(fd, &curr_value) == 0);
339
340 ATF_REQUIRE(curr_value.it_value.tv_sec == 0);
341 ATF_REQUIRE(curr_value.it_value.tv_nsec == 0);
342 ATF_REQUIRE(curr_value.it_interval.tv_sec == 0);
343 ATF_REQUIRE(curr_value.it_interval.tv_nsec == 0);
344
345 struct itimerspec time = {
346 .it_value.tv_sec = 0,
347 .it_value.tv_nsec = 100000000,
348 .it_interval.tv_sec = 0,
349 .it_interval.tv_nsec = 100000000,
350 };
351
352 curr_value = time;
353 ATF_REQUIRE(timerfd_settime(fd, 0, &time, &curr_value) == 0);
354 ATF_REQUIRE(curr_value.it_value.tv_sec == 0);
355 ATF_REQUIRE(curr_value.it_value.tv_nsec == 0);
356 ATF_REQUIRE(curr_value.it_interval.tv_sec == 0);
357 ATF_REQUIRE(curr_value.it_interval.tv_nsec == 0);
358
359 ATF_REQUIRE(close(fd) == 0);
360 }
361
362 ATF_TC_WITHOUT_HEAD(timerfd__simple_blocking_periodic_timer);
ATF_TC_BODY(timerfd__simple_blocking_periodic_timer,tc)363 ATF_TC_BODY(timerfd__simple_blocking_periodic_timer, tc)
364 {
365 int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
366
367 ATF_REQUIRE(timerfd >= 0);
368
369 struct itimerspec time = {
370 .it_value.tv_sec = 0,
371 .it_value.tv_nsec = 100000000,
372 .it_interval.tv_sec = 0,
373 .it_interval.tv_nsec = 100000000,
374 };
375
376 struct timespec b, e;
377 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
378
379 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
380
381 uint64_t timeouts = 0;
382 int num_loop_iterations = 0;
383
384 while (timeouts < 3) {
385 uint64_t timeouts_local;
386 ATF_REQUIRE(
387 read(timerfd, &timeouts_local, sizeof(timeouts_local)) ==
388 (ssize_t)sizeof(timeouts_local));
389 ATF_REQUIRE(timeouts_local > 0);
390
391 ++num_loop_iterations;
392 timeouts += timeouts_local;
393 }
394
395 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
396 timespecsub(&e, &b, &e);
397
398 ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 300000000) || e.tv_sec > 0);
399 ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec >= 300000000 &&
400 e.tv_nsec < 300000000 + TIMER_SLACK);
401
402 ATF_REQUIRE(num_loop_iterations <= 3);
403
404 ATF_REQUIRE(close(timerfd) == 0);
405 }
406
407 ATF_TC_WITHOUT_HEAD(timerfd__argument_checks);
ATF_TC_BODY(timerfd__argument_checks,tc)408 ATF_TC_BODY(timerfd__argument_checks, tc)
409 {
410 int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
411 ATF_REQUIRE(timerfd >= 0);
412
413 struct itimerspec time = {
414 .it_value.tv_sec = 0,
415 .it_value.tv_nsec = 100000000,
416 .it_interval.tv_sec = 0,
417 .it_interval.tv_nsec = 100000000,
418 };
419
420 ATF_REQUIRE_ERRNO(EFAULT, timerfd_settime(timerfd, 0, NULL, NULL) < 0);
421 ATF_REQUIRE_ERRNO(EFAULT, timerfd_settime(-2, 0, NULL, NULL) < 0);
422 ATF_REQUIRE_ERRNO(EBADF, timerfd_settime(-2, 0, &time, NULL) < 0);
423 ATF_REQUIRE_ERRNO(EFAULT, timerfd_settime(-2, 42, NULL, NULL) < 0);
424 ATF_REQUIRE_ERRNO(EINVAL, timerfd_settime(-2, 42, &time, NULL) < 0);
425 ATF_REQUIRE_ERRNO(EINVAL,
426 timerfd_settime(timerfd, 42, &time, NULL) < 0);
427
428 {
429 time = (struct itimerspec) {
430 .it_value.tv_sec = -1,
431 .it_value.tv_nsec = 100000000,
432 .it_interval.tv_sec = 0,
433 .it_interval.tv_nsec = 100000000,
434 };
435 ATF_REQUIRE_ERRNO(EINVAL,
436 timerfd_settime(timerfd, 0, &time, NULL) < 0);
437 }
438 {
439 time = (struct itimerspec) {
440 .it_value.tv_sec = 0,
441 .it_value.tv_nsec = -1,
442 .it_interval.tv_sec = 0,
443 .it_interval.tv_nsec = 100000000,
444 };
445 ATF_REQUIRE_ERRNO(EINVAL,
446 timerfd_settime(timerfd, 0, &time, NULL) < 0);
447 }
448 {
449 time = (struct itimerspec) {
450 .it_value.tv_sec = 0,
451 .it_value.tv_nsec = 100000000,
452 .it_interval.tv_sec = -1,
453 .it_interval.tv_nsec = 100000000,
454 };
455 ATF_REQUIRE_ERRNO(EINVAL,
456 timerfd_settime(timerfd, 0, &time, NULL) < 0);
457 }
458 {
459 time = (struct itimerspec) {
460 .it_value.tv_sec = 0,
461 .it_value.tv_nsec = 100000000,
462 .it_interval.tv_sec = 0,
463 .it_interval.tv_nsec = -1,
464 };
465 ATF_REQUIRE_ERRNO(EINVAL,
466 timerfd_settime(timerfd, 0, &time, NULL) < 0);
467 }
468 {
469 time = (struct itimerspec) {
470 .it_value.tv_sec = 0,
471 .it_value.tv_nsec = 1000000000,
472 .it_interval.tv_sec = 0,
473 .it_interval.tv_nsec = 100000000,
474 };
475 ATF_REQUIRE_ERRNO(EINVAL,
476 timerfd_settime(timerfd, 0, &time, NULL) < 0);
477 }
478 {
479 time = (struct itimerspec) {
480 .it_value.tv_sec = 0,
481 .it_value.tv_nsec = 100000000,
482 .it_interval.tv_sec = 0,
483 .it_interval.tv_nsec = 1000000000,
484 };
485 ATF_REQUIRE_ERRNO(EINVAL,
486 timerfd_settime(timerfd, 0, &time, NULL) < 0);
487 }
488
489 ATF_REQUIRE_ERRNO(EINVAL,
490 timerfd_create(CLOCK_MONOTONIC | 42, TFD_CLOEXEC));
491 ATF_REQUIRE_ERRNO(EINVAL,
492 timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | 42));
493
494 ATF_REQUIRE(close(timerfd) == 0);
495
496 struct itimerspec itimerspec;
497 ATF_REQUIRE_ERRNO(EBADF, timerfd_gettime(timerfd, &itimerspec) < 0);
498 ATF_REQUIRE_ERRNO(EINVAL,
499 timerfd_settime(timerfd, 0, &itimerspec, NULL) < 0);
500 }
501
502 ATF_TC_WITHOUT_HEAD(timerfd__upgrade_simple_to_complex);
ATF_TC_BODY(timerfd__upgrade_simple_to_complex,tc)503 ATF_TC_BODY(timerfd__upgrade_simple_to_complex, tc)
504 {
505 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
506 TFD_CLOEXEC | TFD_NONBLOCK);
507
508 ATF_REQUIRE(timerfd >= 0);
509
510 struct itimerspec time = {
511 .it_value.tv_sec = 0,
512 .it_value.tv_nsec = 100000000,
513 .it_interval.tv_sec = 0,
514 .it_interval.tv_nsec = 100000000,
515 };
516
517 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
518 (void)wait_for_timerfd(timerfd);
519
520 time = (struct itimerspec) {
521 .it_value.tv_sec = 0,
522 .it_value.tv_nsec = 50000000,
523 .it_interval.tv_sec = 0,
524 .it_interval.tv_nsec = 95000000,
525 };
526
527 struct timespec b, e;
528 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
529
530 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
531
532 uint64_t timeouts = wait_for_timerfd(timerfd);
533 ATF_REQUIRE(timeouts >= 1);
534 ATF_REQUIRE(timeouts == 1);
535
536 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
537 timespecsub(&e, &b, &e);
538 ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 50000000) || e.tv_sec > 0);
539 ATF_REQUIRE_MSG(e.tv_sec == 0 && e.tv_nsec < 50000000 + TIMER_SLACK,
540 "%ld", e.tv_nsec);
541
542 timeouts = wait_for_timerfd(timerfd);
543 ATF_REQUIRE(timeouts >= 1);
544 ATF_REQUIRE(timeouts == 1);
545
546 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
547 timespecsub(&e, &b, &e);
548 ATF_REQUIRE((e.tv_sec == 0 && e.tv_nsec >= 145000000) || e.tv_sec > 0);
549 ATF_REQUIRE(e.tv_sec == 0 && e.tv_nsec >= 145000000 &&
550 e.tv_nsec < 145000000 + TIMER_SLACK);
551
552 ATF_REQUIRE(close(timerfd) == 0);
553 }
554
555 ATF_TC_WITHOUT_HEAD(timerfd__absolute_timer);
ATF_TC_BODY(timerfd__absolute_timer,tc)556 ATF_TC_BODY(timerfd__absolute_timer, tc)
557 {
558 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
559 TFD_CLOEXEC | TFD_NONBLOCK);
560
561 ATF_REQUIRE(timerfd >= 0);
562
563 struct timespec b, e;
564 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
565
566 struct itimerspec time = {
567 .it_value = b,
568 .it_interval.tv_sec = 0,
569 .it_interval.tv_nsec = 0,
570 };
571
572 struct timespec ts_600ms = {
573 .tv_sec = 0,
574 .tv_nsec = 600000000,
575 };
576
577 timespecadd(&time.it_value, &ts_600ms, &time.it_value);
578
579 ATF_REQUIRE(timerfd_settime(timerfd, /**/
580 TFD_TIMER_ABSTIME, &time, NULL) == 0);
581
582 struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
583 ATF_REQUIRE(poll(&pfd, 1, -1) == 1);
584
585 // Don't read(2) here!
586
587 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0);
588 timespecsub(&e, &b, &e);
589 ATF_REQUIRE(e.tv_sec == 0 &&
590 /* Don't check for this because of spurious wakeups. */
591 /* e.tv_nsec >= 600000000 && */
592 e.tv_nsec < 600000000 + TIMER_SLACK);
593
594 struct itimerspec zeroed_its = {
595 .it_value.tv_sec = 0,
596 .it_value.tv_nsec = 0,
597 .it_interval.tv_sec = 0,
598 .it_interval.tv_nsec = 0,
599 };
600 ATF_REQUIRE(timerfd_settime(timerfd, 0, &zeroed_its, NULL) == 0);
601
602 uint64_t timeouts;
603 ATF_REQUIRE_ERRNO(EAGAIN,
604 read(timerfd, &timeouts, sizeof(timeouts)) < 0);
605
606 ATF_REQUIRE(poll(&pfd, 1, 0) == 0);
607
608 ATF_REQUIRE(close(timerfd) == 0);
609 }
610
611 ATF_TC_WITHOUT_HEAD(timerfd__absolute_timer_in_the_past);
ATF_TC_BODY(timerfd__absolute_timer_in_the_past,tc)612 ATF_TC_BODY(timerfd__absolute_timer_in_the_past, tc)
613 {
614 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
615 TFD_CLOEXEC | TFD_NONBLOCK);
616
617 ATF_REQUIRE(timerfd >= 0);
618
619 struct timespec b;
620 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
621
622 {
623 struct itimerspec time = {
624 .it_value = b,
625 .it_interval.tv_sec = 10,
626 .it_interval.tv_nsec = 0,
627 };
628 time.it_value.tv_sec -= 1;
629
630 ATF_REQUIRE(timerfd_settime(timerfd, /**/
631 TFD_TIMER_ABSTIME, &time, NULL) == 0);
632
633 struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
634 ATF_REQUIRE(poll(&pfd, 1, 1000) == 1);
635 }
636
637 {
638 struct itimerspec time = {
639 .it_value = b,
640 .it_interval.tv_sec = 0,
641 .it_interval.tv_nsec = 10000000,
642 };
643 time.it_value.tv_sec -= 1;
644
645 ATF_REQUIRE(timerfd_settime(timerfd, /**/
646 TFD_TIMER_ABSTIME, &time, NULL) == 0);
647
648 struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
649 ATF_REQUIRE(poll(&pfd, 1, -1) == 1);
650 }
651
652 uint64_t timeouts;
653 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
654 (ssize_t)sizeof(timeouts));
655
656 ATF_REQUIRE_MSG(timeouts >= 101, "%d", (int)timeouts);
657
658 ATF_REQUIRE(close(timerfd) == 0);
659 }
660
661 ATF_TC_WITHOUT_HEAD(timerfd__reset_absolute);
ATF_TC_BODY(timerfd__reset_absolute,tc)662 ATF_TC_BODY(timerfd__reset_absolute, tc)
663 {
664 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
665 TFD_CLOEXEC | TFD_NONBLOCK);
666
667 ATF_REQUIRE(timerfd >= 0);
668
669 struct timespec b;
670 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0);
671
672 {
673 struct itimerspec time = {
674 .it_value = b,
675 };
676 time.it_value.tv_sec += 10;
677
678 ATF_REQUIRE(timerfd_settime(timerfd, /**/
679 TFD_TIMER_ABSTIME, &time, NULL) == 0);
680
681 struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
682 ATF_REQUIRE(poll(&pfd, 1, 100) == 0);
683 }
684
685 {
686 struct itimerspec time = {
687 .it_value = b,
688 };
689 time.it_value.tv_nsec += 500000000;
690 if (time.it_value.tv_nsec >= 1000000000) {
691 time.it_value.tv_nsec -= 1000000000;
692 time.it_value.tv_sec += 1;
693 }
694
695 ATF_REQUIRE(timerfd_settime(timerfd, /**/
696 TFD_TIMER_ABSTIME, &time, NULL) == 0);
697
698 struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
699 ATF_REQUIRE(poll(&pfd, 1, 1000) == 1);
700 }
701
702 uint64_t timeouts;
703 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
704 (ssize_t)sizeof(timeouts));
705
706 ATF_REQUIRE_MSG(timeouts == 1, "%d", (int)timeouts);
707
708 ATF_REQUIRE(close(timerfd) == 0);
709 }
710
711 ATF_TC(timerfd__periodic_timer_performance);
ATF_TC_HEAD(timerfd__periodic_timer_performance,tc)712 ATF_TC_HEAD(timerfd__periodic_timer_performance, tc)
713 {
714 atf_tc_set_md_var(tc, "timeout", "1");
715 }
ATF_TC_BODY(timerfd__periodic_timer_performance,tc)716 ATF_TC_BODY(timerfd__periodic_timer_performance, tc)
717 {
718 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/
719 TFD_CLOEXEC | TFD_NONBLOCK);
720
721 ATF_REQUIRE(timerfd >= 0);
722
723 struct itimerspec time = {
724 .it_value.tv_sec = 0,
725 .it_value.tv_nsec = 1,
726 .it_interval.tv_sec = 0,
727 .it_interval.tv_nsec = 1,
728 };
729
730 ATF_REQUIRE(timerfd_settime(timerfd, 0, &time, NULL) == 0);
731
732 usleep(400000);
733
734 struct pollfd pfd = { .fd = timerfd, .events = POLLIN };
735 ATF_REQUIRE(poll(&pfd, 1, -1) == 1);
736
737 uint64_t timeouts;
738 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) ==
739 (ssize_t)sizeof(timeouts));
740 if (timeouts < 400000000)
741 atf_tc_expect_fail("https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=294053");
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);
ATF_TC_BODY(timerfd__argument_overflow,tc)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);
ATF_TC_HEAD(timerfd__short_evfilt_timer_timeout,tc)792 ATF_TC_HEAD(timerfd__short_evfilt_timer_timeout, tc)
793 {
794 atf_tc_set_md_var(tc, "timeout", "30");
795 }
ATF_TC_BODY(timerfd__short_evfilt_timer_timeout,tc)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);
ATF_TC_BODY(timerfd__unmodified_errno,tc)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);
ATF_TC_BODY(timerfd__reset_to_very_long,tc)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);
ATF_TC_BODY(timerfd__missed_events,tc)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
reset_time(void)987 reset_time(void)
988 {
989 (void)clock_settime(CLOCK_REALTIME, ¤t_time);
990 }
991
992 static void
clock_settime_or_skip_test(clockid_t clockid,struct timespec const * ts)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);
ATF_TC_BODY(timerfd_root__zero_read_on_abs_realtime,tc)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, ¤t_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);
ATF_TC_BODY(timerfd_root__read_on_abs_realtime_no_interval,tc)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, ¤t_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);
ATF_TC_BODY(timerfd_root__cancel_on_set,tc)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, ¤t_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, ¤t_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, ¤t_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, ¤t_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);
ATF_TC_BODY(timerfd_root__cancel_on_set_init,tc)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, ¤t_time) == 0);
1187 ATF_REQUIRE(atexit(reset_time) == 0);
1188
1189 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_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, ¤t_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 *
clock_change_thread(void * arg)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, ¤t_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, ¤t_time);
1229
1230 return NULL;
1231 }
1232
1233 ATF_TC(timerfd_root__clock_change_notification);
ATF_TC_HEAD(timerfd_root__clock_change_notification,tc)1234 ATF_TC_HEAD(timerfd_root__clock_change_notification, tc)
1235 {
1236 atf_tc_set_md_var(tc, "timeout", "10");
1237 }
ATF_TC_BODY(timerfd_root__clock_change_notification,tc)1238 ATF_TC_BODY(timerfd_root__clock_change_notification, tc)
1239 {
1240 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0);
1241 ATF_REQUIRE(atexit(reset_time) == 0);
1242
1243 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_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);
ATF_TC_BODY(timerfd_root__advance_time_no_cancel,tc)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, ¤t_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, ¤t_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
ATF_TP_ADD_TCS(tp)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