Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __TIMENS_H__
0003 #define __TIMENS_H__
0004 
0005 #include <fcntl.h>
0006 #include <unistd.h>
0007 #include <stdlib.h>
0008 #include <stdbool.h>
0009 
0010 #include "../kselftest.h"
0011 
0012 #ifndef CLONE_NEWTIME
0013 # define CLONE_NEWTIME  0x00000080
0014 #endif
0015 
0016 static int config_posix_timers = true;
0017 static int config_alarm_timers = true;
0018 
0019 static inline void check_supported_timers(void)
0020 {
0021     struct timespec ts;
0022 
0023     if (timer_create(-1, 0, 0) == -1 && errno == ENOSYS)
0024         config_posix_timers = false;
0025 
0026     if (clock_gettime(CLOCK_BOOTTIME_ALARM, &ts) == -1 && errno == EINVAL)
0027         config_alarm_timers = false;
0028 }
0029 
0030 static inline bool check_skip(int clockid)
0031 {
0032     if (!config_alarm_timers && clockid == CLOCK_BOOTTIME_ALARM) {
0033         ksft_test_result_skip("CLOCK_BOOTTIME_ALARM isn't supported\n");
0034         return true;
0035     }
0036 
0037     if (config_posix_timers)
0038         return false;
0039 
0040     switch (clockid) {
0041     /* Only these clocks are supported without CONFIG_POSIX_TIMERS. */
0042     case CLOCK_BOOTTIME:
0043     case CLOCK_MONOTONIC:
0044     case CLOCK_REALTIME:
0045         return false;
0046     default:
0047         ksft_test_result_skip("Posix Clocks & timers are not supported\n");
0048         return true;
0049     }
0050 
0051     return false;
0052 }
0053 
0054 static inline int unshare_timens(void)
0055 {
0056     if (unshare(CLONE_NEWTIME)) {
0057         if (errno == EPERM)
0058             ksft_exit_skip("need to run as root\n");
0059         return pr_perror("Can't unshare() timens");
0060     }
0061     return 0;
0062 }
0063 
0064 static inline int _settime(clockid_t clk_id, time_t offset)
0065 {
0066     int fd, len;
0067     char buf[4096];
0068 
0069     if (clk_id == CLOCK_MONOTONIC_COARSE || clk_id == CLOCK_MONOTONIC_RAW)
0070         clk_id = CLOCK_MONOTONIC;
0071 
0072     len = snprintf(buf, sizeof(buf), "%d %ld 0", clk_id, offset);
0073 
0074     fd = open("/proc/self/timens_offsets", O_WRONLY);
0075     if (fd < 0)
0076         return pr_perror("/proc/self/timens_offsets");
0077 
0078     if (write(fd, buf, len) != len)
0079         return pr_perror("/proc/self/timens_offsets");
0080 
0081     close(fd);
0082 
0083     return 0;
0084 }
0085 
0086 static inline int _gettime(clockid_t clk_id, struct timespec *res, bool raw_syscall)
0087 {
0088     int err;
0089 
0090     if (!raw_syscall) {
0091         if (clock_gettime(clk_id, res)) {
0092             pr_perror("clock_gettime(%d)", (int)clk_id);
0093             return -1;
0094         }
0095         return 0;
0096     }
0097 
0098     err = syscall(SYS_clock_gettime, clk_id, res);
0099     if (err)
0100         pr_perror("syscall(SYS_clock_gettime(%d))", (int)clk_id);
0101 
0102     return err;
0103 }
0104 
0105 static inline void nscheck(void)
0106 {
0107     if (access("/proc/self/ns/time", F_OK) < 0)
0108         ksft_exit_skip("Time namespaces are not supported\n");
0109 }
0110 
0111 #endif