This is used in various selftests and will be handy when integrating
those with nolibc.

Signed-off-by: Thomas Weißschuh <thomas.weisssc...@linutronix.de>
Acked-by: Willy Tarreau <w...@1wt.eu>
---
 tools/include/nolibc/time.h                  | 92 ++++++++++++++++++++++++++++
 tools/include/nolibc/types.h                 |  2 +
 tools/testing/selftests/nolibc/nolibc-test.c |  5 ++
 3 files changed, 99 insertions(+)

diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h
index 
9502f9aaf62110ec8ee113fcf08dd47de40e924c..7e0b7eac0b7c31910a48ebb9755d5004e5da1a69
 100644
--- a/tools/include/nolibc/time.h
+++ b/tools/include/nolibc/time.h
@@ -15,6 +15,98 @@
 #include "types.h"
 #include "sys.h"
 
+#include <linux/time.h>
+
+static __inline__
+void __nolibc_timespec_user_to_kernel(const struct timespec *ts, struct 
__kernel_timespec *kts)
+{
+       kts->tv_sec = ts->tv_sec;
+       kts->tv_nsec = ts->tv_nsec;
+}
+
+static __inline__
+void __nolibc_timespec_kernel_to_user(const struct __kernel_timespec *kts, 
struct timespec *ts)
+{
+       ts->tv_sec = kts->tv_sec;
+       ts->tv_nsec = kts->tv_nsec;
+}
+
+/*
+ * int clock_getres(clockid_t clockid, struct timespec *res);
+ * int clock_gettime(clockid_t clockid, struct timespec *tp);
+ * int clock_settime(clockid_t clockid, const struct timespec *tp);
+ */
+
+static __attribute__((unused))
+int sys_clock_getres(clockid_t clockid, struct timespec *res)
+{
+#if defined(__NR_clock_getres)
+       return my_syscall2(__NR_clock_getres, clockid, res);
+#elif defined(__NR_clock_getres_time64)
+       struct __kernel_timespec kres;
+       int ret;
+
+       ret = my_syscall2(__NR_clock_getres_time64, clockid, &kres);
+       if (res)
+               __nolibc_timespec_kernel_to_user(&kres, res);
+       return ret;
+#else
+       return __nolibc_enosys(__func__, clockid, res);
+#endif
+}
+
+static __attribute__((unused))
+int clock_getres(clockid_t clockid, struct timespec *res)
+{
+       return __sysret(sys_clock_getres(clockid, res));
+}
+
+static __attribute__((unused))
+int sys_clock_gettime(clockid_t clockid, struct timespec *tp)
+{
+#if defined(__NR_clock_gettime)
+       return my_syscall2(__NR_clock_gettime, clockid, tp);
+#elif defined(__NR_clock_gettime64)
+       struct __kernel_timespec ktp;
+       int ret;
+
+       ret = my_syscall2(__NR_clock_gettime64, clockid, &ktp);
+       if (tp)
+               __nolibc_timespec_kernel_to_user(&ktp, tp);
+       return ret;
+#else
+       return __nolibc_enosys(__func__, clockid, tp);
+#endif
+}
+
+static __attribute__((unused))
+int clock_gettime(clockid_t clockid, struct timespec *tp)
+{
+       return __sysret(sys_clock_gettime(clockid, tp));
+}
+
+static __attribute__((unused))
+int sys_clock_settime(clockid_t clockid, struct timespec *tp)
+{
+#if defined(__NR_clock_settime)
+       return my_syscall2(__NR_clock_settime, clockid, tp);
+#elif defined(__NR_clock_settime64)
+       struct __kernel_timespec ktp;
+
+       __nolibc_timespec_user_to_kernel(tp, &ktp);
+       return my_syscall2(__NR_clock_settime64, clockid, &ktp);
+#else
+       return __nolibc_enosys(__func__, clockid, tp);
+#endif
+}
+
+static __attribute__((unused))
+int clock_settime(clockid_t clockid, struct timespec *tp)
+{
+       return __sysret(sys_clock_settime(clockid, tp));
+}
+
+
 static __attribute__((unused))
 time_t time(time_t *tptr)
 {
diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h
index 
fe97953d16572db3e4c18cdc8921c6a991d64f94..8eb48d98667f5fe65bb8a974f613fd1fd17ae11e
 100644
--- a/tools/include/nolibc/types.h
+++ b/tools/include/nolibc/types.h
@@ -201,6 +201,8 @@ struct stat {
        union { time_t st_ctime; struct timespec st_ctim; }; /* time of last 
status change */
 };
 
+typedef __kernel_clockid_t clockid_t;
+
 /* WARNING, it only deals with the 4096 first majors and 256 first minors */
 #define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor) & 
0xff)))
 #define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff))
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c 
b/tools/testing/selftests/nolibc/nolibc-test.c
index 
10db118b8b111ec17f713d57240f193c7b18c70f..9b8aa41a547b1735014c5a7541e426f4e08c7f90
 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -38,6 +38,7 @@
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdint.h>
+#include <time.h>
 #include <unistd.h>
 #include <limits.h>
 #include <ctype.h>
@@ -1081,6 +1082,7 @@ int run_syscall(int min, int max)
 {
        struct timeval tv;
        struct timezone tz;
+       struct timespec ts;
        struct stat stat_buf;
        int euid0;
        int proc;
@@ -1114,6 +1116,9 @@ int run_syscall(int min, int max)
                switch (test + __LINE__ + 1) {
                CASE_TEST(access);            EXPECT_SYSZR(proc, 
access("/proc/self", R_OK)); break;
                CASE_TEST(access_bad);        EXPECT_SYSER(proc, 
access("/proc/self", W_OK), -1, EPERM); break;
+               CASE_TEST(clock_getres);      EXPECT_SYSZR(1, 
clock_getres(CLOCK_MONOTONIC, &ts)); break;
+               CASE_TEST(clock_gettime);     EXPECT_SYSZR(1, 
clock_gettime(CLOCK_MONOTONIC, &ts)); break;
+               CASE_TEST(clock_settime);     EXPECT_SYSER(1, 
clock_settime(CLOCK_MONOTONIC, &ts), -1, EINVAL); break;
                CASE_TEST(getpid);            EXPECT_SYSNE(1, getpid(), -1); 
break;
                CASE_TEST(getppid);           EXPECT_SYSNE(1, getppid(), -1); 
break;
                CASE_TEST(gettid);            EXPECT_SYSNE(has_gettid, 
gettid(), -1); break;

-- 
2.49.0


Reply via email to