internally, define plain syscalls, if missing, as their time64 variants
[musl] / src / time / clock_gettime.c
1 #include <time.h>
2 #include <errno.h>
3 #include <stdint.h>
4 #include "syscall.h"
5 #include "atomic.h"
6
7 #ifdef VDSO_CGT_SYM
8
9 static void *volatile vdso_func;
10
11 static int cgt_init(clockid_t clk, struct timespec *ts)
12 {
13         void *p = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM);
14         int (*f)(clockid_t, struct timespec *) =
15                 (int (*)(clockid_t, struct timespec *))p;
16         a_cas_p(&vdso_func, (void *)cgt_init, p);
17         return f ? f(clk, ts) : -ENOSYS;
18 }
19
20 static void *volatile vdso_func = (void *)cgt_init;
21
22 #endif
23
24 int __clock_gettime(clockid_t clk, struct timespec *ts)
25 {
26         int r;
27
28 #ifdef VDSO_CGT_SYM
29         int (*f)(clockid_t, struct timespec *) =
30                 (int (*)(clockid_t, struct timespec *))vdso_func;
31         if (f) {
32                 r = f(clk, ts);
33                 if (!r) return r;
34                 if (r == -EINVAL) return __syscall_ret(r);
35                 /* Fall through on errors other than EINVAL. Some buggy
36                  * vdso implementations return ENOSYS for clocks they
37                  * can't handle, rather than making the syscall. This
38                  * also handles the case where cgt_init fails to find
39                  * a vdso function to use. */
40         }
41 #endif
42
43         r = __syscall(SYS_clock_gettime, clk, ts);
44         if (r == -ENOSYS) {
45                 if (clk == CLOCK_REALTIME) {
46                         __syscall(SYS_gettimeofday, ts, 0);
47                         ts->tv_nsec = (int)ts->tv_nsec * 1000;
48                         return 0;
49                 }
50                 r = -EINVAL;
51         }
52         return __syscall_ret(r);
53 }
54
55 weak_alias(__clock_gettime, clock_gettime);