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