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