]>
Commit | Line | Data |
---|---|---|
2f4f3bd4 | 1 | /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version. |
dff8da6b | 2 | Copyright (C) 2003-2024 Free Software Foundation, Inc. |
ad0e8eb0 UD |
3 | This file is part of the GNU C Library. |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
ad0e8eb0 UD |
18 | |
19 | #include <sysdep.h> | |
ec138c67 | 20 | #include <kernel-features.h> |
2f4f3bd4 | 21 | #include <errno.h> |
6ba85a6d | 22 | #include <time.h> |
2f4f3bd4 | 23 | #include "kernel-posix-cpu-timers.h" |
f534255e | 24 | #include <sysdep-vdso.h> |
7b5af2d8 ZW |
25 | #include <shlib-compat.h> |
26 | ||
b06f4c00 WD |
27 | /* Get current value of CLOCK and store it in TP. */ |
28 | int | |
ec138c67 AF |
29 | __clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp) |
30 | { | |
d0def09f | 31 | int r; |
d9310f33 AZ |
32 | |
33 | #ifndef __NR_clock_gettime64 | |
34 | # define __NR_clock_gettime64 __NR_clock_gettime | |
35 | #endif | |
36 | ||
d9310f33 | 37 | #ifdef HAVE_CLOCK_GETTIME64_VSYSCALL |
72e84d1d AZ |
38 | int (*vdso_time64) (clockid_t clock_id, struct __timespec64 *tp) |
39 | = GLRO(dl_vdso_clock_gettime64); | |
40 | if (vdso_time64 != NULL) | |
41 | { | |
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); | |
46 | } | |
d9310f33 AZ |
47 | #endif |
48 | ||
72e84d1d AZ |
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) | |
53 | { | |
54 | struct timespec tp32; | |
55 | r = INTERNAL_VSYSCALL_CALL (vdso_time, 2, clock_id, &tp32); | |
e0fc721c | 56 | if (r == 0 && tp32.tv_sec >= 0) |
72e84d1d AZ |
57 | { |
58 | *tp = valid_timespec_to_timespec64 (tp32); | |
59 | return 0; | |
60 | } | |
61 | else if (r != 0) | |
62 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (-r); | |
63 | ||
64 | /* Fallback to syscall if the 32-bit time_t vDSO returns overflows. */ | |
65 | } | |
66 | #endif | |
67 | ||
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); | |
d9310f33 AZ |
73 | |
74 | #ifndef __ASSUME_TIME64_SYSCALLS | |
d0def09f | 75 | /* Fallback code that uses 32-bit support. */ |
ec138c67 | 76 | struct timespec tp32; |
72e84d1d | 77 | r = INTERNAL_SYSCALL_CALL (clock_gettime, clock_id, &tp32); |
d0def09f | 78 | if (r == 0) |
72e84d1d AZ |
79 | { |
80 | *tp = valid_timespec_to_timespec64 (tp32); | |
81 | return 0; | |
82 | } | |
ec138c67 | 83 | #endif |
d9310f33 | 84 | |
72e84d1d | 85 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (-r); |
ec138c67 AF |
86 | } |
87 | ||
88 | #if __TIMESIZE != 64 | |
03343699 LM |
89 | libc_hidden_def (__clock_gettime64) |
90 | ||
ec138c67 | 91 | int |
b06f4c00 WD |
92 | __clock_gettime (clockid_t clock_id, struct timespec *tp) |
93 | { | |
ec138c67 AF |
94 | int ret; |
95 | struct __timespec64 tp64; | |
96 | ||
97 | ret = __clock_gettime64 (clock_id, &tp64); | |
98 | ||
99 | if (ret == 0) | |
100 | { | |
101 | if (! in_time_t_range (tp64.tv_sec)) | |
102 | { | |
103 | __set_errno (EOVERFLOW); | |
104 | return -1; | |
105 | } | |
106 | ||
107 | *tp = valid_timespec64_to_timespec (tp64); | |
108 | } | |
109 | ||
110 | return ret; | |
b06f4c00 | 111 | } |
ec138c67 | 112 | #endif |
b06f4c00 | 113 | libc_hidden_def (__clock_gettime) |
7b5af2d8 ZW |
114 | |
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 |