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