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); 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 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 712 ATF_TC_HEAD(timerfd__periodic_timer_performance, tc) 713 { 714 atf_tc_set_md_var(tc, "timeout", "1"); 715 } 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 atf_tc_expect_fail("https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=294053"); 741 ATF_REQUIRE_MSG(timeouts >= 400000000, "%ld", (long)timeouts); 742 743 ATF_REQUIRE(close(timerfd) == 0); 744 } 745 746 ATF_TC_WITHOUT_HEAD(timerfd__argument_overflow); 747 ATF_TC_BODY(timerfd__argument_overflow, tc) 748 { 749 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/ 750 TFD_CLOEXEC | TFD_NONBLOCK); 751 ATF_REQUIRE(timerfd >= 0); 752 { 753 struct itimerspec time = { 754 .it_value.tv_sec = 0, 755 .it_value.tv_nsec = 1, 756 }; 757 758 ATF_REQUIRE(timerfd_settime(timerfd, /**/ 759 TFD_TIMER_ABSTIME, &time, NULL) == 0); 760 761 struct pollfd pfd = { .fd = timerfd, .events = POLLIN }; 762 ATF_REQUIRE(poll(&pfd, 1, -1) == 1); 763 764 uint64_t timeouts; 765 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) == 766 (ssize_t)sizeof(timeouts)); 767 ATF_REQUIRE(timeouts == 1); 768 769 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) < 0); 770 } 771 { 772 struct itimerspec time = { 773 .it_value.tv_sec = LONG_MAX, 774 .it_value.tv_nsec = 999999999, 775 }; 776 777 ATF_REQUIRE(timerfd_settime(timerfd, /**/ 778 TFD_TIMER_ABSTIME, &time, NULL) == 0); 779 780 struct pollfd pfd = { .fd = timerfd, .events = POLLIN }; 781 ATF_REQUIRE(poll(&pfd, 1, 500) == 0); 782 783 uint64_t timeouts; 784 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) < 0); 785 } 786 787 ATF_REQUIRE(close(timerfd) == 0); 788 } 789 790 ATF_TC(timerfd__short_evfilt_timer_timeout); 791 ATF_TC_HEAD(timerfd__short_evfilt_timer_timeout, tc) 792 { 793 atf_tc_set_md_var(tc, "timeout", "30"); 794 } 795 ATF_TC_BODY(timerfd__short_evfilt_timer_timeout, tc) 796 { 797 int kq = kqueue(); 798 ATF_REQUIRE(kq >= 0); 799 800 bool returns_early = false; 801 802 for (int l = 0; l < 10; ++l) { 803 for (int i = 1; i <= 17; ++i) { 804 struct kevent kev; 805 EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, i, 806 0); 807 808 struct timespec b; 809 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0); 810 811 ATF_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0); 812 813 ATF_REQUIRE(kevent(kq, NULL, 0, &kev, 1, NULL) == 1); 814 815 struct timespec e; 816 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0); 817 818 struct timespec diff; 819 timespecsub(&e, &b, &diff); 820 821 if (diff.tv_sec != 0 || diff.tv_nsec < i * 1000000) { 822 fprintf(stderr, 823 "expected: %lldns, got: %lldns\n", 824 (long long)(i * 1000000LL), 825 (long long)diff.tv_nsec); 826 returns_early = true; 827 goto check; 828 } 829 } 830 } 831 832 check: 833 ATF_REQUIRE(!returns_early); 834 835 ATF_REQUIRE(close(kq) == 0); 836 837 /* 838 * timerfd's should never return early, regardless of how 839 * EVFILT_TIMER behaves. 840 */ 841 842 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/ 843 TFD_CLOEXEC | TFD_NONBLOCK); 844 845 ATF_REQUIRE(timerfd >= 0); 846 847 for (int l = 0; l < 10; ++l) { 848 for (int i = 1; i <= 17; ++i) { 849 struct itimerspec time = { 850 .it_value.tv_sec = 0, 851 .it_value.tv_nsec = i * 1000000, 852 }; 853 854 struct timespec b; 855 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &b) == 0); 856 857 ATF_REQUIRE( 858 timerfd_settime(timerfd, 0, &time, NULL) == 0); 859 (void)wait_for_timerfd(timerfd); 860 861 struct timespec e; 862 ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &e) == 0); 863 864 struct timespec diff; 865 timespecsub(&e, &b, &diff); 866 867 ATF_REQUIRE( 868 diff.tv_sec == 0 && diff.tv_nsec >= i * 1000000); 869 fprintf(stderr, "%dms, waited %lldns\n", i, 870 (long long)diff.tv_nsec); 871 } 872 } 873 874 ATF_REQUIRE(close(timerfd) == 0); 875 } 876 877 ATF_TC_WITHOUT_HEAD(timerfd__unmodified_errno); 878 ATF_TC_BODY(timerfd__unmodified_errno, tc) 879 { 880 ATF_REQUIRE(errno == 0); 881 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/ 882 TFD_CLOEXEC | TFD_NONBLOCK); 883 ATF_REQUIRE(timerfd >= 0); 884 ATF_REQUIRE(errno == 0); 885 886 ATF_REQUIRE(timerfd_settime(timerfd, 0, 887 &(struct itimerspec) { 888 .it_value.tv_sec = 0, 889 .it_value.tv_nsec = 100000000, 890 }, 891 NULL) == 0); 892 ATF_REQUIRE(errno == 0); 893 (void)wait_for_timerfd(timerfd); 894 ATF_REQUIRE(errno == 0); 895 896 ATF_REQUIRE(timerfd_settime(timerfd, 0, 897 &(struct itimerspec) { 898 .it_value.tv_sec = 0, 899 .it_value.tv_nsec = 0, 900 }, 901 NULL) == 0); 902 ATF_REQUIRE(errno == 0); 903 904 ATF_REQUIRE(timerfd_settime(timerfd, 0, 905 &(struct itimerspec) { 906 .it_value.tv_sec = 0, 907 .it_value.tv_nsec = 0, 908 }, 909 NULL) == 0); 910 ATF_REQUIRE(errno == 0); 911 912 ATF_REQUIRE(close(timerfd) == 0); 913 ATF_REQUIRE(errno == 0); 914 } 915 916 ATF_TC_WITHOUT_HEAD(timerfd__reset_to_very_long); 917 ATF_TC_BODY(timerfd__reset_to_very_long, tc) 918 { 919 ATF_REQUIRE(errno == 0); 920 int timerfd = timerfd_create(CLOCK_MONOTONIC, /**/ 921 TFD_CLOEXEC | TFD_NONBLOCK); 922 ATF_REQUIRE(timerfd >= 0); 923 ATF_REQUIRE(errno == 0); 924 925 ATF_REQUIRE(timerfd_settime(timerfd, 0, 926 &(struct itimerspec) { 927 .it_value.tv_sec = 0, 928 .it_value.tv_nsec = 100000000, 929 }, 930 NULL) == 0); 931 ATF_REQUIRE(errno == 0); 932 933 ATF_REQUIRE(timerfd_settime(timerfd, 0, 934 &(struct itimerspec) { 935 .it_value.tv_sec = 630720000, 936 .it_value.tv_nsec = 0, 937 }, 938 NULL) == 0); 939 ATF_REQUIRE(errno == 0); 940 941 struct pollfd pfd = { .fd = timerfd, .events = POLLIN }; 942 ATF_REQUIRE(poll(&pfd, 1, 500) == 0); 943 uint64_t timeouts; 944 ssize_t r = read(timerfd, &timeouts, sizeof(timeouts)); 945 ATF_REQUIRE_ERRNO(EAGAIN, r < 0); 946 947 ATF_REQUIRE(close(timerfd) == 0); 948 ATF_REQUIRE(errno == EAGAIN); 949 } 950 951 ATF_TC_WITHOUT_HEAD(timerfd__missed_events); 952 ATF_TC_BODY(timerfd__missed_events, tc) 953 { 954 struct itimerspec its = { }; 955 uint64_t timeouts; 956 int timerfd; 957 958 timerfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC); 959 ATF_REQUIRE(timerfd >= 0); 960 961 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &its.it_value) == 0); 962 its.it_value.tv_sec -= 1000; 963 its.it_interval.tv_sec = 1; 964 965 ATF_REQUIRE(timerfd_settime(timerfd, TFD_TIMER_ABSTIME, &its, 966 NULL) == 0); 967 968 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) == 969 sizeof(timeouts)); 970 ATF_REQUIRE_MSG(timeouts == 1001, "%ld", (long)timeouts); 971 972 ATF_REQUIRE(read(timerfd, &timeouts, sizeof(timeouts)) == 973 sizeof(timeouts)); 974 ATF_REQUIRE_MSG(timeouts == 1, "%ld", (long)timeouts); 975 976 ATF_REQUIRE(close(timerfd) == 0); 977 } 978 979 /* 980 * Tests requiring root (clock_settime on CLOCK_REALTIME). 981 * Tests gracefully skip if not running as root. 982 */ 983 984 static struct timespec current_time; 985 static void 986 reset_time(void) 987 { 988 (void)clock_settime(CLOCK_REALTIME, ¤t_time); 989 } 990 991 static void 992 clock_settime_or_skip_test(clockid_t clockid, struct timespec const *ts) 993 { 994 int r = clock_settime(clockid, ts); 995 if (r < 0 && errno == EPERM) { 996 atf_tc_skip("root required"); 997 } 998 ATF_REQUIRE(r == 0); 999 } 1000 1001 ATF_TC_WITHOUT_HEAD(timerfd_root__zero_read_on_abs_realtime); 1002 ATF_TC_BODY(timerfd_root__zero_read_on_abs_realtime, tc) 1003 { 1004 int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC); 1005 ATF_REQUIRE(tfd >= 0); 1006 1007 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0); 1008 ATF_REQUIRE(atexit(reset_time) == 0); 1009 1010 ATF_REQUIRE(timerfd_settime(tfd, TFD_TIMER_ABSTIME, 1011 &(struct itimerspec) { 1012 .it_value = current_time, 1013 .it_interval.tv_sec = 1, 1014 .it_interval.tv_nsec = 0, 1015 }, 1016 NULL) == 0); 1017 1018 ATF_REQUIRE( 1019 poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, -1) == 1); 1020 1021 clock_settime_or_skip_test(CLOCK_REALTIME, 1022 &(struct timespec) { 1023 .tv_sec = current_time.tv_sec - 1, 1024 .tv_nsec = current_time.tv_nsec, 1025 }); 1026 1027 uint64_t exp; 1028 ssize_t r = read(tfd, &exp, sizeof(exp)); 1029 ATF_REQUIRE_MSG(r == 0, "r: %d, errno: %d", (int)r, errno); 1030 1031 { 1032 int r = fcntl(tfd, F_GETFL); 1033 ATF_REQUIRE(r >= 0); 1034 r = fcntl(tfd, F_SETFL, r | O_NONBLOCK); 1035 ATF_REQUIRE(r >= 0); 1036 } 1037 1038 r = read(tfd, &exp, sizeof(exp)); 1039 ATF_REQUIRE_ERRNO(EAGAIN, r < 0); 1040 1041 current_time.tv_sec += 1; 1042 ATF_REQUIRE(poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, 1043 1800) == 1); 1044 r = read(tfd, &exp, sizeof(exp)); 1045 ATF_REQUIRE(r == (ssize_t)sizeof(exp)); 1046 ATF_REQUIRE(exp == 1); 1047 1048 ATF_REQUIRE(close(tfd) == 0); 1049 } 1050 1051 ATF_TC_WITHOUT_HEAD(timerfd_root__read_on_abs_realtime_no_interval); 1052 ATF_TC_BODY(timerfd_root__read_on_abs_realtime_no_interval, tc) 1053 { 1054 int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC); 1055 ATF_REQUIRE(tfd >= 0); 1056 1057 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0); 1058 ATF_REQUIRE(atexit(reset_time) == 0); 1059 1060 ATF_REQUIRE(timerfd_settime(tfd, TFD_TIMER_ABSTIME, 1061 &(struct itimerspec) { 1062 .it_value = current_time, 1063 .it_interval.tv_sec = 0, 1064 .it_interval.tv_nsec = 0, 1065 }, 1066 NULL) == 0); 1067 1068 ATF_REQUIRE( 1069 poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, -1) == 1); 1070 1071 clock_settime_or_skip_test(CLOCK_REALTIME, 1072 &(struct timespec) { 1073 .tv_sec = current_time.tv_sec - 1, 1074 .tv_nsec = current_time.tv_nsec, 1075 }); 1076 1077 uint64_t exp; 1078 ssize_t r = read(tfd, &exp, sizeof(exp)); 1079 ATF_REQUIRE(r == (ssize_t)sizeof(exp)); 1080 ATF_REQUIRE(exp == 1); 1081 1082 ATF_REQUIRE(close(tfd) == 0); 1083 } 1084 1085 ATF_TC_WITHOUT_HEAD(timerfd_root__cancel_on_set); 1086 ATF_TC_BODY(timerfd_root__cancel_on_set, tc) 1087 { 1088 int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC); 1089 ATF_REQUIRE(tfd >= 0); 1090 1091 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0); 1092 ATF_REQUIRE(atexit(reset_time) == 0); 1093 1094 ATF_REQUIRE( 1095 timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, 1096 &(struct itimerspec) { 1097 .it_value.tv_sec = current_time.tv_sec + 10, 1098 .it_value.tv_nsec = current_time.tv_nsec, 1099 .it_interval.tv_sec = 0, 1100 .it_interval.tv_nsec = 0, 1101 }, 1102 NULL) == 0); 1103 1104 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_time); 1105 1106 ATF_REQUIRE( 1107 poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, -1) == 1); 1108 1109 { 1110 int r = timerfd_settime(tfd, 1111 TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, 1112 &(struct itimerspec) { 1113 .it_value.tv_sec = current_time.tv_sec, 1114 .it_value.tv_nsec = current_time.tv_nsec, 1115 .it_interval.tv_sec = 0, 1116 .it_interval.tv_nsec = 0, 1117 }, 1118 NULL); 1119 ATF_REQUIRE_ERRNO(ECANCELED, r < 0); 1120 } 1121 1122 ATF_REQUIRE(poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, 1123 800) == 1); 1124 1125 uint64_t exp; 1126 ssize_t r; 1127 1128 r = read(tfd, &exp, sizeof(exp)); 1129 ATF_REQUIRE(r == (ssize_t)sizeof(exp)); 1130 ATF_REQUIRE(exp == 1); 1131 1132 ATF_REQUIRE( 1133 timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, 1134 &(struct itimerspec) { 1135 .it_value.tv_sec = current_time.tv_sec + 1, 1136 .it_value.tv_nsec = current_time.tv_nsec, 1137 .it_interval.tv_sec = 1, 1138 .it_interval.tv_nsec = 0, 1139 }, 1140 NULL) == 0); 1141 1142 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_time); 1143 1144 ATF_REQUIRE( 1145 poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, -1) == 1); 1146 1147 r = read(tfd, &exp, sizeof(exp)); 1148 ATF_REQUIRE_ERRNO(ECANCELED, r < 0); 1149 1150 r = read(tfd, &exp, sizeof(exp)); 1151 current_time.tv_sec += 1; 1152 ATF_REQUIRE_MSG(r == (ssize_t)sizeof(exp), "%d %d", (int)r, errno); 1153 ATF_REQUIRE(exp == 1); 1154 1155 ATF_REQUIRE( 1156 timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, 1157 &(struct itimerspec) { 1158 .it_value.tv_sec = current_time.tv_sec + 1, 1159 .it_value.tv_nsec = current_time.tv_nsec, 1160 .it_interval.tv_sec = 1, 1161 .it_interval.tv_nsec = 0, 1162 }, 1163 NULL) == 0); 1164 1165 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_time); 1166 current_time.tv_sec += 2; 1167 ATF_REQUIRE(nanosleep(&(struct timespec) { .tv_sec = 2 }, NULL) == 0); 1168 1169 r = read(tfd, &exp, sizeof(exp)); 1170 ATF_REQUIRE_ERRNO(ECANCELED, r < 0); 1171 1172 r = poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1, 3000); 1173 ATF_REQUIRE(r == 0); 1174 current_time.tv_sec += 3; 1175 1176 ATF_REQUIRE(close(tfd) == 0); 1177 } 1178 1179 ATF_TC_WITHOUT_HEAD(timerfd_root__cancel_on_set_init); 1180 ATF_TC_BODY(timerfd_root__cancel_on_set_init, tc) 1181 { 1182 int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC); 1183 ATF_REQUIRE(tfd >= 0); 1184 1185 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0); 1186 ATF_REQUIRE(atexit(reset_time) == 0); 1187 1188 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_time); 1189 1190 ATF_REQUIRE( 1191 timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, 1192 &(struct itimerspec) { 1193 .it_value.tv_sec = current_time.tv_sec + 10, 1194 .it_value.tv_nsec = current_time.tv_nsec, 1195 .it_interval.tv_sec = 0, 1196 .it_interval.tv_nsec = 0, 1197 }, 1198 NULL) == 0); 1199 1200 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_time); 1201 1202 int r = timerfd_settime(tfd, 1203 TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, 1204 &(struct itimerspec) { 1205 .it_value.tv_sec = current_time.tv_sec + 10, 1206 .it_value.tv_nsec = current_time.tv_nsec, 1207 .it_interval.tv_sec = 0, 1208 .it_interval.tv_nsec = 0, 1209 }, 1210 NULL); 1211 ATF_REQUIRE_ERRNO(ECANCELED, r < 0); 1212 ATF_REQUIRE(close(tfd) == 0); 1213 } 1214 1215 static void * 1216 clock_change_thread(void *arg) 1217 { 1218 (void)arg; 1219 1220 fprintf(stderr, "clock change\n"); 1221 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_time); 1222 1223 current_time.tv_sec += 2; 1224 ATF_REQUIRE(nanosleep(&(struct timespec) { .tv_sec = 2 }, NULL) == 0); 1225 1226 fprintf(stderr, "clock change\n"); 1227 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_time); 1228 1229 return NULL; 1230 } 1231 1232 ATF_TC(timerfd_root__clock_change_notification); 1233 ATF_TC_HEAD(timerfd_root__clock_change_notification, tc) 1234 { 1235 atf_tc_set_md_var(tc, "timeout", "10"); 1236 } 1237 ATF_TC_BODY(timerfd_root__clock_change_notification, tc) 1238 { 1239 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0); 1240 ATF_REQUIRE(atexit(reset_time) == 0); 1241 1242 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_time); 1243 1244 #define TIME_T_MAX (time_t)((UINTMAX_C(1) << ((sizeof(time_t) << 3) - 1)) - 1) 1245 struct itimerspec its = { 1246 .it_value.tv_sec = TIME_T_MAX, 1247 }; 1248 #undef TIME_T_MAX 1249 1250 int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC); 1251 ATF_REQUIRE(tfd >= 0); 1252 1253 ATF_REQUIRE( 1254 timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, 1255 &its, NULL) == 0); 1256 1257 pthread_t clock_changer; 1258 ATF_REQUIRE(pthread_create(&clock_changer, NULL, /**/ 1259 clock_change_thread, NULL) == 0); 1260 1261 uint64_t exp; 1262 ssize_t r; 1263 1264 r = read(tfd, &exp, sizeof(exp)); 1265 ATF_REQUIRE_ERRNO(ECANCELED, r < 0); 1266 fprintf(stderr, "clock change detected\n"); 1267 1268 r = read(tfd, &exp, sizeof(exp)); 1269 ATF_REQUIRE_ERRNO(ECANCELED, r < 0); 1270 fprintf(stderr, "clock change detected\n"); 1271 1272 ATF_REQUIRE(pthread_join(clock_changer, NULL) == 0); 1273 1274 ATF_REQUIRE(close(tfd) == 0); 1275 } 1276 1277 ATF_TC_WITHOUT_HEAD(timerfd_root__advance_time_no_cancel); 1278 ATF_TC_BODY(timerfd_root__advance_time_no_cancel, tc) 1279 { 1280 int tfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC); 1281 ATF_REQUIRE(tfd >= 0); 1282 1283 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, ¤t_time) == 0); 1284 ATF_REQUIRE(atexit(reset_time) == 0); 1285 1286 ATF_REQUIRE(timerfd_settime(tfd, TFD_TIMER_ABSTIME, 1287 &(struct itimerspec) { 1288 .it_value.tv_sec = current_time.tv_sec + 10, 1289 .it_value.tv_nsec = current_time.tv_nsec, 1290 .it_interval.tv_sec = 0, 1291 .it_interval.tv_nsec = 0, 1292 }, 1293 NULL) == 0); 1294 1295 current_time.tv_sec += 9; 1296 clock_settime_or_skip_test(CLOCK_REALTIME, ¤t_time); 1297 current_time.tv_sec -= 8; 1298 1299 { 1300 int r = poll(&(struct pollfd) { .fd = tfd, .events = POLLIN }, 1301 1, 1800); 1302 ATF_REQUIRE(r == 1); 1303 } 1304 1305 uint64_t exp; 1306 ssize_t r; 1307 1308 r = read(tfd, &exp, sizeof(exp)); 1309 ATF_REQUIRE(r == (ssize_t)sizeof(exp)); 1310 ATF_REQUIRE(exp == 1); 1311 1312 ATF_REQUIRE(close(tfd) == 0); 1313 } 1314 1315 ATF_TP_ADD_TCS(tp) 1316 { 1317 ATF_TP_ADD_TC(tp, timerfd__many_timers); 1318 ATF_TP_ADD_TC(tp, timerfd__simple_timer); 1319 ATF_TP_ADD_TC(tp, timerfd__simple_periodic_timer); 1320 ATF_TP_ADD_TC(tp, timerfd__complex_periodic_timer); 1321 ATF_TP_ADD_TC(tp, timerfd__reset_periodic_timer); 1322 ATF_TP_ADD_TC(tp, timerfd__reenable_periodic_timer); 1323 ATF_TP_ADD_TC(tp, timerfd__expire_five); 1324 ATF_TP_ADD_TC(tp, timerfd__simple_gettime); 1325 ATF_TP_ADD_TC(tp, timerfd__simple_blocking_periodic_timer); 1326 ATF_TP_ADD_TC(tp, timerfd__argument_checks); 1327 ATF_TP_ADD_TC(tp, timerfd__upgrade_simple_to_complex); 1328 ATF_TP_ADD_TC(tp, timerfd__absolute_timer); 1329 ATF_TP_ADD_TC(tp, timerfd__absolute_timer_in_the_past); 1330 ATF_TP_ADD_TC(tp, timerfd__reset_absolute); 1331 ATF_TP_ADD_TC(tp, timerfd__periodic_timer_performance); 1332 ATF_TP_ADD_TC(tp, timerfd__argument_overflow); 1333 ATF_TP_ADD_TC(tp, timerfd__short_evfilt_timer_timeout); 1334 ATF_TP_ADD_TC(tp, timerfd__unmodified_errno); 1335 ATF_TP_ADD_TC(tp, timerfd__reset_to_very_long); 1336 ATF_TP_ADD_TC(tp, timerfd__missed_events); 1337 1338 ATF_TP_ADD_TC(tp, timerfd_root__zero_read_on_abs_realtime); 1339 ATF_TP_ADD_TC(tp, timerfd_root__read_on_abs_realtime_no_interval); 1340 ATF_TP_ADD_TC(tp, timerfd_root__cancel_on_set); 1341 ATF_TP_ADD_TC(tp, timerfd_root__cancel_on_set_init); 1342 ATF_TP_ADD_TC(tp, timerfd_root__clock_change_notification); 1343 ATF_TP_ADD_TC(tp, timerfd_root__advance_time_no_cancel); 1344 1345 return atf_no_error(); 1346 } 1347