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