Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / timens / timens.h
blobd4fc52d4714627d3b8d4a9de9526d4944e9e6d8e
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __TIMENS_H__
3 #define __TIMENS_H__
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
10 #include "../kselftest.h"
12 #ifndef CLONE_NEWTIME
13 # define CLONE_NEWTIME 0x00000080
14 #endif
16 static int config_posix_timers = true;
17 static int config_alarm_timers = true;
19 static inline void check_supported_timers(void)
21 struct timespec ts;
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");
34 return true;
37 if (config_posix_timers)
38 return false;
40 switch (clockid) {
41 /* Only these clocks are supported without CONFIG_POSIX_TIMERS. */
42 case CLOCK_BOOTTIME:
43 case CLOCK_MONOTONIC:
44 case CLOCK_REALTIME:
45 return false;
46 default:
47 ksft_test_result_skip("Posix Clocks & timers are not supported\n");
48 return true;
51 return false;
54 static inline int unshare_timens(void)
56 if (unshare(CLONE_NEWTIME)) {
57 if (errno == EPERM)
58 ksft_exit_skip("need to run as root\n");
59 return pr_perror("Can't unshare() timens");
61 return 0;
64 static inline int _settime(clockid_t clk_id, time_t offset)
66 int fd, len;
67 char buf[4096];
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);
75 if (fd < 0)
76 return pr_perror("/proc/self/timens_offsets");
78 if (write(fd, buf, len) != len)
79 return pr_perror("/proc/self/timens_offsets");
81 close(fd);
83 return 0;
86 static inline int _gettime(clockid_t clk_id, struct timespec *res, bool raw_syscall)
88 int err;
90 if (!raw_syscall) {
91 if (clock_gettime(clk_id, res)) {
92 pr_perror("clock_gettime(%d)", (int)clk_id);
93 return -1;
95 return 0;
98 err = syscall(SYS_clock_gettime, clk_id, res);
99 if (err)
100 pr_perror("syscall(SYS_clock_gettime(%d))", (int)clk_id);
102 return err;
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");
111 #endif