avoid malloc of deps arrays for ldso and vdso
[musl.git] / src / time / clock_gettime.c
blob8fd1b8f58ae2e156a0995aa3b3613ea72521e3f8
1 #include <time.h>
2 #include <errno.h>
3 #include <stdint.h>
4 #include "syscall.h"
5 #include "atomic.h"
7 #ifdef VDSO_CGT_SYM
9 static void *volatile vdso_func;
11 static int cgt_init(clockid_t clk, struct timespec *ts)
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;
20 static void *volatile vdso_func = (void *)cgt_init;
22 #endif
24 int __clock_gettime(clockid_t clk, struct timespec *ts)
26 int r;
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. */
41 #endif
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;
50 r = -EINVAL;
52 return __syscall_ret(r);
55 weak_alias(__clock_gettime, clock_gettime);