x86: In ld.so, diagnose missing APX support in APX-only builds
[glibc.git] / sysdeps / unix / sysv / linux / clock_gettime.c
blobf10a289f791b00b9d333750f6b861b14d674800a
1 /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version.
2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <sysdep.h>
20 #include <kernel-features.h>
21 #include <errno.h>
22 #include <time.h>
23 #include "kernel-posix-cpu-timers.h"
24 #include <sysdep-vdso.h>
25 #include <shlib-compat.h>
27 /* Get current value of CLOCK and store it in TP. */
28 int
29 __clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp)
31 int r;
33 #ifndef __NR_clock_gettime64
34 # define __NR_clock_gettime64 __NR_clock_gettime
35 #endif
37 #ifdef HAVE_CLOCK_GETTIME64_VSYSCALL
38 int (*vdso_time64) (clockid_t clock_id, struct __timespec64 *tp)
39 = GLRO(dl_vdso_clock_gettime64);
40 if (vdso_time64 != NULL)
42 r = INTERNAL_VSYSCALL_CALL (vdso_time64, 2, clock_id, tp);
43 if (r == 0)
44 return 0;
45 return INLINE_SYSCALL_ERROR_RETURN_VALUE (-r);
47 #endif
49 #ifdef HAVE_CLOCK_GETTIME_VSYSCALL
50 int (*vdso_time) (clockid_t clock_id, struct timespec *tp)
51 = GLRO(dl_vdso_clock_gettime);
52 if (vdso_time != NULL)
54 struct timespec tp32;
55 r = INTERNAL_VSYSCALL_CALL (vdso_time, 2, clock_id, &tp32);
56 if (r == 0 && tp32.tv_sec >= 0)
58 *tp = valid_timespec_to_timespec64 (tp32);
59 return 0;
61 else if (r != 0)
62 return INLINE_SYSCALL_ERROR_RETURN_VALUE (-r);
64 /* Fallback to syscall if the 32-bit time_t vDSO returns overflows. */
66 #endif
68 r = INTERNAL_SYSCALL_CALL (clock_gettime64, clock_id, tp);
69 if (r == 0)
70 return 0;
71 if (r != -ENOSYS)
72 return INLINE_SYSCALL_ERROR_RETURN_VALUE (-r);
74 #ifndef __ASSUME_TIME64_SYSCALLS
75 /* Fallback code that uses 32-bit support. */
76 struct timespec tp32;
77 r = INTERNAL_SYSCALL_CALL (clock_gettime, clock_id, &tp32);
78 if (r == 0)
80 *tp = valid_timespec_to_timespec64 (tp32);
81 return 0;
83 #endif
85 return INLINE_SYSCALL_ERROR_RETURN_VALUE (-r);
88 #if __TIMESIZE != 64
89 libc_hidden_def (__clock_gettime64)
91 int
92 __clock_gettime (clockid_t clock_id, struct timespec *tp)
94 int ret;
95 struct __timespec64 tp64;
97 ret = __clock_gettime64 (clock_id, &tp64);
99 if (ret == 0)
101 if (! in_time_t_range (tp64.tv_sec))
103 __set_errno (EOVERFLOW);
104 return -1;
107 *tp = valid_timespec64_to_timespec (tp64);
110 return ret;
112 #endif
113 libc_hidden_def (__clock_gettime)
115 versioned_symbol (libc, __clock_gettime, clock_gettime, GLIBC_2_17);
116 /* clock_gettime moved to libc in version 2.17;
117 old binaries may expect the symbol version it had in librt. */
118 #if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17)
119 strong_alias (__clock_gettime, __clock_gettime_2);
120 compat_symbol (libc, __clock_gettime_2, clock_gettime, GLIBC_2_2);
121 #endif