1 /* SPDX-License-Identifier: GPL-2.0 */
10 #include "../kselftest.h"
13 # define CLONE_NEWTIME 0x00000080
16 static int config_posix_timers = true;
17 static int config_alarm_timers = true;
19 static inline void check_supported_timers(void)
23 if (timer_create(-1, 0, 0) == -1 && errno == ENOSYS)
24 config_posix_timers = false;
26 if (clock_gettime(CLOCK_BOOTTIME_ALARM, &ts) == -1 && errno == EINVAL)
27 config_alarm_timers = false;
30 static inline bool check_skip(int clockid)
32 if (!config_alarm_timers && clockid == CLOCK_BOOTTIME_ALARM) {
33 ksft_test_result_skip("CLOCK_BOOTTIME_ALARM isn't supported\n");
37 if (config_posix_timers)
41 /* Only these clocks are supported without CONFIG_POSIX_TIMERS. */
47 ksft_test_result_skip("Posix Clocks & timers are not supported\n");
54 static inline int unshare_timens(void)
56 if (unshare(CLONE_NEWTIME)) {
58 ksft_exit_skip("need to run as root\n");
59 return pr_perror("Can't unshare() timens");
64 static inline int _settime(clockid_t clk_id, time_t offset)
69 if (clk_id == CLOCK_MONOTONIC_COARSE || clk_id == CLOCK_MONOTONIC_RAW)
70 clk_id = CLOCK_MONOTONIC;
72 len = snprintf(buf, sizeof(buf), "%d %ld 0", clk_id, offset);
74 fd = open("/proc/self/timens_offsets", O_WRONLY);
76 return pr_perror("/proc/self/timens_offsets");
78 if (write(fd, buf, len) != len)
79 return pr_perror("/proc/self/timens_offsets");
86 static inline int _gettime(clockid_t clk_id, struct timespec *res, bool raw_syscall)
91 if (clock_gettime(clk_id, res)) {
92 pr_perror("clock_gettime(%d)", (int)clk_id);
98 err = syscall(SYS_clock_gettime, clk_id, res);
100 pr_perror("syscall(SYS_clock_gettime(%d))", (int)clk_id);
105 static inline void nscheck(void)
107 if (access("/proc/self/ns/time", F_OK) < 0)
108 ksft_exit_skip("Time namespaces are not supported\n");