timer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <sched.h>
  4. #include <sys/syscall.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <time.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <signal.h>
  13. #include "log.h"
  14. #include "timens.h"
  15. int run_test(int clockid, struct timespec now)
  16. {
  17. struct itimerspec new_value;
  18. long long elapsed;
  19. timer_t fd;
  20. int i;
  21. if (check_skip(clockid))
  22. return 0;
  23. for (i = 0; i < 2; i++) {
  24. struct sigevent sevp = {.sigev_notify = SIGEV_NONE};
  25. int flags = 0;
  26. new_value.it_value.tv_sec = 3600;
  27. new_value.it_value.tv_nsec = 0;
  28. new_value.it_interval.tv_sec = 1;
  29. new_value.it_interval.tv_nsec = 0;
  30. if (i == 1) {
  31. new_value.it_value.tv_sec += now.tv_sec;
  32. new_value.it_value.tv_nsec += now.tv_nsec;
  33. }
  34. if (timer_create(clockid, &sevp, &fd) == -1) {
  35. if (errno == ENOSYS) {
  36. ksft_test_result_skip("Posix Clocks & timers are supported\n");
  37. return 0;
  38. }
  39. return pr_perror("timerfd_create");
  40. }
  41. if (i == 1)
  42. flags |= TIMER_ABSTIME;
  43. if (timer_settime(fd, flags, &new_value, NULL) == -1)
  44. return pr_perror("timerfd_settime");
  45. if (timer_gettime(fd, &new_value) == -1)
  46. return pr_perror("timerfd_gettime");
  47. elapsed = new_value.it_value.tv_sec;
  48. if (abs(elapsed - 3600) > 60) {
  49. ksft_test_result_fail("clockid: %d elapsed: %lld\n",
  50. clockid, elapsed);
  51. return 1;
  52. }
  53. }
  54. ksft_test_result_pass("clockid=%d\n", clockid);
  55. return 0;
  56. }
  57. int main(int argc, char *argv[])
  58. {
  59. int ret, status, len, fd;
  60. char buf[4096];
  61. pid_t pid;
  62. struct timespec btime_now, mtime_now;
  63. nscheck();
  64. check_supported_timers();
  65. ksft_set_plan(3);
  66. clock_gettime(CLOCK_MONOTONIC, &mtime_now);
  67. clock_gettime(CLOCK_BOOTTIME, &btime_now);
  68. if (unshare_timens())
  69. return 1;
  70. len = snprintf(buf, sizeof(buf), "%d %d 0\n%d %d 0",
  71. CLOCK_MONOTONIC, 70 * 24 * 3600,
  72. CLOCK_BOOTTIME, 9 * 24 * 3600);
  73. fd = open("/proc/self/timens_offsets", O_WRONLY);
  74. if (fd < 0)
  75. return pr_perror("/proc/self/timens_offsets");
  76. if (write(fd, buf, len) != len)
  77. return pr_perror("/proc/self/timens_offsets");
  78. close(fd);
  79. mtime_now.tv_sec += 70 * 24 * 3600;
  80. btime_now.tv_sec += 9 * 24 * 3600;
  81. pid = fork();
  82. if (pid < 0)
  83. return pr_perror("Unable to fork");
  84. if (pid == 0) {
  85. ret = 0;
  86. ret |= run_test(CLOCK_BOOTTIME, btime_now);
  87. ret |= run_test(CLOCK_MONOTONIC, mtime_now);
  88. ret |= run_test(CLOCK_BOOTTIME_ALARM, btime_now);
  89. if (ret)
  90. ksft_exit_fail();
  91. ksft_exit_pass();
  92. return ret;
  93. }
  94. if (waitpid(pid, &status, 0) != pid)
  95. return pr_perror("Unable to wait the child process");
  96. if (WIFEXITED(status))
  97. return WEXITSTATUS(status);
  98. return 1;
  99. }