| 1 |
|
| 2 | |
| 3 | * time function definitions for NOLIBC
|
| 4 | * Copyright (C) 2017-2022 Willy Tarreau <[email protected]>
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 | #include "nolibc.h"
|
| 9 |
|
| 10 | #ifndef _NOLIBC_TIME_H
|
| 11 | #define _NOLIBC_TIME_H
|
| 12 |
|
| 13 | #include "std.h"
|
| 14 | #include "arch.h"
|
| 15 | #include "types.h"
|
| 16 | #include "sys.h"
|
| 17 |
|
| 18 | #include <linux/signal.h>
|
| 19 | #include <linux/time.h>
|
| 20 |
|
| 21 | #define __nolibc_assert_time64_type(t) \
|
| 22 | __nolibc_static_assert(sizeof(t) == 8)
|
| 23 |
|
| 24 | #define __nolibc_assert_native_time64() \
|
| 25 | __nolibc_assert_time64_type(__kernel_old_time_t)
|
| 26 |
|
| 27 | |
| 28 | * int clock_getres(clockid_t clockid, struct timespec *res);
|
| 29 | * int clock_gettime(clockid_t clockid, struct timespec *tp);
|
| 30 | * int clock_settime(clockid_t clockid, const struct timespec *tp);
|
| 31 | * int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp,
|
| 32 | * struct timespec *rmtp)
|
| 33 |
|
| 34 |
|
| 35 | static __attribute__((unused))
|
| 36 | int _sys_clock_getres(clockid_t clockid, struct timespec *res)
|
| 37 | {
|
| 38 | #if defined(__NR_clock_getres_time64)
|
| 39 | __nolibc_assert_time64_type(res->tv_sec);
|
| 40 | return __nolibc_syscall2(__NR_clock_getres_time64, clockid, res);
|
| 41 | #else
|
| 42 | __nolibc_assert_native_time64();
|
| 43 | return __nolibc_syscall2(__NR_clock_getres, clockid, res);
|
| 44 | #endif
|
| 45 | }
|
| 46 |
|
| 47 | static __attribute__((unused))
|
| 48 | int clock_getres(clockid_t clockid, struct timespec *res)
|
| 49 | {
|
| 50 | return __sysret(_sys_clock_getres(clockid, res));
|
| 51 | }
|
| 52 |
|
| 53 | static __attribute__((unused))
|
| 54 | int _sys_clock_gettime(clockid_t clockid, struct timespec *tp)
|
| 55 | {
|
| 56 | #if defined(__NR_clock_gettime64)
|
| 57 | __nolibc_assert_time64_type(tp->tv_sec);
|
| 58 | return __nolibc_syscall2(__NR_clock_gettime64, clockid, tp);
|
| 59 | #else
|
| 60 | __nolibc_assert_native_time64();
|
| 61 | return __nolibc_syscall2(__NR_clock_gettime, clockid, tp);
|
| 62 | #endif
|
| 63 | }
|
| 64 |
|
| 65 | static __attribute__((unused))
|
| 66 | int clock_gettime(clockid_t clockid, struct timespec *tp)
|
| 67 | {
|
| 68 | return __sysret(_sys_clock_gettime(clockid, tp));
|
| 69 | }
|
| 70 |
|
| 71 | static __attribute__((unused))
|
| 72 | int _sys_clock_settime(clockid_t clockid, struct timespec *tp)
|
| 73 | {
|
| 74 | #if defined(__NR_clock_settime64)
|
| 75 | __nolibc_assert_time64_type(tp->tv_sec);
|
| 76 | return __nolibc_syscall2(__NR_clock_settime64, clockid, tp);
|
| 77 | #else
|
| 78 | __nolibc_assert_native_time64();
|
| 79 | return __nolibc_syscall2(__NR_clock_settime, clockid, tp);
|
| 80 | #endif
|
| 81 | }
|
| 82 |
|
| 83 | static __attribute__((unused))
|
| 84 | int clock_settime(clockid_t clockid, struct timespec *tp)
|
| 85 | {
|
| 86 | return __sysret(_sys_clock_settime(clockid, tp));
|
| 87 | }
|
| 88 |
|
| 89 | static __attribute__((unused))
|
| 90 | int _sys_clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp,
|
| 91 | struct timespec *rmtp)
|
| 92 | {
|
| 93 | #if defined(__NR_clock_nanosleep_time64)
|
| 94 | __nolibc_assert_time64_type(rqtp->tv_sec);
|
| 95 | return __nolibc_syscall4(__NR_clock_nanosleep_time64, clockid, flags, rqtp, rmtp);
|
| 96 | #else
|
| 97 | __nolibc_assert_native_time64();
|
| 98 | return __nolibc_syscall4(__NR_clock_nanosleep, clockid, flags, rqtp, rmtp);
|
| 99 | #endif
|
| 100 | }
|
| 101 |
|
| 102 | static __attribute__((unused))
|
| 103 | int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp,
|
| 104 | struct timespec *rmtp)
|
| 105 | {
|
| 106 |
|
| 107 | return -_sys_clock_nanosleep(clockid, flags, rqtp, rmtp);
|
| 108 | }
|
| 109 |
|
| 110 | static __inline__
|
| 111 | double difftime(time_t time1, time_t time2)
|
| 112 | {
|
| 113 | return time1 - time2;
|
| 114 | }
|
| 115 |
|
| 116 | static __inline__
|
| 117 | int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
| 118 | {
|
| 119 | return __sysret(_sys_clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp));
|
| 120 | }
|
| 121 |
|
| 122 |
|
| 123 | static __attribute__((unused))
|
| 124 | time_t time(time_t *tptr)
|
| 125 | {
|
| 126 | struct timeval tv;
|
| 127 |
|
| 128 |
|
| 129 | _sys_gettimeofday(&tv, NULL);
|
| 130 |
|
| 131 | if (tptr)
|
| 132 | *tptr = tv.tv_sec;
|
| 133 | return tv.tv_sec;
|
| 134 | }
|
| 135 |
|
| 136 |
|
| 137 | |
| 138 | * int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid);
|
| 139 | * int timer_gettime(timer_t timerid, struct itimerspec *curr_value);
|
| 140 | * int timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);
|
| 141 |
|
| 142 |
|
| 143 | static __attribute__((unused))
|
| 144 | int _sys_timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
|
| 145 | {
|
| 146 | return __nolibc_syscall3(__NR_timer_create, clockid, evp, timerid);
|
| 147 | }
|
| 148 |
|
| 149 | static __attribute__((unused))
|
| 150 | int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
|
| 151 | {
|
| 152 | return __sysret(_sys_timer_create(clockid, evp, timerid));
|
| 153 | }
|
| 154 |
|
| 155 | static __attribute__((unused))
|
| 156 | int _sys_timer_delete(timer_t timerid)
|
| 157 | {
|
| 158 | return __nolibc_syscall1(__NR_timer_delete, timerid);
|
| 159 | }
|
| 160 |
|
| 161 | static __attribute__((unused))
|
| 162 | int timer_delete(timer_t timerid)
|
| 163 | {
|
| 164 | return __sysret(_sys_timer_delete(timerid));
|
| 165 | }
|
| 166 |
|
| 167 | static __attribute__((unused))
|
| 168 | int _sys_timer_gettime(timer_t timerid, struct itimerspec *curr_value)
|
| 169 | {
|
| 170 | #if defined(__NR_timer_gettime64)
|
| 171 | __nolibc_assert_time64_type(curr_value->it_value.tv_sec);
|
| 172 | return __nolibc_syscall2(__NR_timer_gettime64, timerid, curr_value);
|
| 173 | #else
|
| 174 | __nolibc_assert_native_time64();
|
| 175 | return __nolibc_syscall2(__NR_timer_gettime, timerid, curr_value);
|
| 176 | #endif
|
| 177 | }
|
| 178 |
|
| 179 | static __attribute__((unused))
|
| 180 | int timer_gettime(timer_t timerid, struct itimerspec *curr_value)
|
| 181 | {
|
| 182 | return __sysret(_sys_timer_gettime(timerid, curr_value));
|
| 183 | }
|
| 184 |
|
| 185 | static __attribute__((unused))
|
| 186 | int _sys_timer_settime(timer_t timerid, int flags,
|
| 187 | const struct itimerspec *new_value, struct itimerspec *old_value)
|
| 188 | {
|
| 189 | #if defined(__NR_timer_settime64)
|
| 190 | __nolibc_assert_time64_type(new_value->it_value.tv_sec);
|
| 191 | return __nolibc_syscall4(__NR_timer_settime64, timerid, flags, new_value, old_value);
|
| 192 | #else
|
| 193 | __nolibc_assert_native_time64();
|
| 194 | return __nolibc_syscall4(__NR_timer_settime, timerid, flags, new_value, old_value);
|
| 195 | #endif
|
| 196 | }
|
| 197 |
|
| 198 | static __attribute__((unused))
|
| 199 | int timer_settime(timer_t timerid, int flags,
|
| 200 | const struct itimerspec *new_value, struct itimerspec *old_value)
|
| 201 | {
|
| 202 | return __sysret(_sys_timer_settime(timerid, flags, new_value, old_value));
|
| 203 | }
|
| 204 |
|
| 205 | #endif
|