^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) #ifndef __TIMENS_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __TIMENS_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "../kselftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #ifndef CLONE_NEWTIME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) # define CLONE_NEWTIME 0x00000080
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static int config_posix_timers = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static int config_alarm_timers = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static inline void check_supported_timers(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct timespec ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (timer_create(-1, 0, 0) == -1 && errno == ENOSYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) config_posix_timers = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (clock_gettime(CLOCK_BOOTTIME_ALARM, &ts) == -1 && errno == EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) config_alarm_timers = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static inline bool check_skip(int clockid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!config_alarm_timers && clockid == CLOCK_BOOTTIME_ALARM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ksft_test_result_skip("CLOCK_BOOTTIME_ALARM isn't supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (config_posix_timers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) switch (clockid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Only these clocks are supported without CONFIG_POSIX_TIMERS. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) case CLOCK_BOOTTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) case CLOCK_MONOTONIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) case CLOCK_REALTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ksft_test_result_skip("Posix Clocks & timers are not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static inline int unshare_timens(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (unshare(CLONE_NEWTIME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (errno == EPERM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ksft_exit_skip("need to run as root\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return pr_perror("Can't unshare() timens");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static inline int _settime(clockid_t clk_id, time_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int fd, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) char buf[4096];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (clk_id == CLOCK_MONOTONIC_COARSE || clk_id == CLOCK_MONOTONIC_RAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) clk_id = CLOCK_MONOTONIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) len = snprintf(buf, sizeof(buf), "%d %ld 0", clk_id, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fd = open("/proc/self/timens_offsets", O_WRONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return pr_perror("/proc/self/timens_offsets");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (write(fd, buf, len) != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return pr_perror("/proc/self/timens_offsets");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static inline int _gettime(clockid_t clk_id, struct timespec *res, bool raw_syscall)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!raw_syscall) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (clock_gettime(clk_id, res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) pr_perror("clock_gettime(%d)", (int)clk_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) err = syscall(SYS_clock_gettime, clk_id, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pr_perror("syscall(SYS_clock_gettime(%d))", (int)clk_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static inline void nscheck(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (access("/proc/self/ns/time", F_OK) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ksft_exit_skip("Time namespaces are not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #endif