| 1 | /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version. |
| 2 | Copyright (C) 2003-2020 Free Software Foundation, Inc. |
| 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 |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 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 | |
| 26 | #include <shlib-compat.h> |
| 27 | |
| 28 | /* Get current value of CLOCK and store it in TP. */ |
| 29 | int |
| 30 | __clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp) |
| 31 | { |
| 32 | #ifdef __ASSUME_TIME64_SYSCALLS |
| 33 | /* 64 bit ABIs or newer 32-bit ABIs that only support 64-bit time_t. */ |
| 34 | # ifndef __NR_clock_gettime64 |
| 35 | # define __NR_clock_gettime64 __NR_clock_gettime |
| 36 | # endif |
| 37 | # ifdef HAVE_CLOCK_GETTIME64_VSYSCALL |
| 38 | return INLINE_VSYSCALL (clock_gettime64, 2, clock_id, tp); |
| 39 | # else |
| 40 | return INLINE_SYSCALL_CALL (clock_gettime64, clock_id, tp); |
| 41 | # endif |
| 42 | #else |
| 43 | int r; |
| 44 | /* Old 32-bit ABI with possible 64-bit time_t support. */ |
| 45 | # ifdef __NR_clock_gettime64 |
| 46 | /* Avoid issue a __NR_clock_gettime64 syscall on kernels that do not |
| 47 | support 64-bit time_t. */ |
| 48 | static int time64_support = 1; |
| 49 | if (atomic_load_relaxed (&time64_support) != 0) |
| 50 | { |
| 51 | # ifdef HAVE_CLOCK_GETTIME64_VSYSCALL |
| 52 | r = INLINE_VSYSCALL (clock_gettime64, 2, clock_id, tp); |
| 53 | # else |
| 54 | r = INLINE_SYSCALL_CALL (clock_gettime64, clock_id, tp); |
| 55 | # endif |
| 56 | if (r == 0 || errno != ENOSYS) |
| 57 | return r; |
| 58 | |
| 59 | atomic_store_relaxed (&time64_support, 0); |
| 60 | } |
| 61 | # endif |
| 62 | /* Fallback code that uses 32-bit support. */ |
| 63 | struct timespec tp32; |
| 64 | # ifdef HAVE_CLOCK_GETTIME_VSYSCALL |
| 65 | r = INLINE_VSYSCALL (clock_gettime, 2, clock_id, &tp32); |
| 66 | # else |
| 67 | r = INLINE_SYSCALL_CALL (clock_gettime, clock_id, &tp32); |
| 68 | # endif |
| 69 | if (r == 0) |
| 70 | *tp = valid_timespec_to_timespec64 (tp32); |
| 71 | return r; |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | #if __TIMESIZE != 64 |
| 76 | libc_hidden_def (__clock_gettime64) |
| 77 | |
| 78 | int |
| 79 | __clock_gettime (clockid_t clock_id, struct timespec *tp) |
| 80 | { |
| 81 | int ret; |
| 82 | struct __timespec64 tp64; |
| 83 | |
| 84 | ret = __clock_gettime64 (clock_id, &tp64); |
| 85 | |
| 86 | if (ret == 0) |
| 87 | { |
| 88 | if (! in_time_t_range (tp64.tv_sec)) |
| 89 | { |
| 90 | __set_errno (EOVERFLOW); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | *tp = valid_timespec64_to_timespec (tp64); |
| 95 | } |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | #endif |
| 100 | libc_hidden_def (__clock_gettime) |
| 101 | |
| 102 | versioned_symbol (libc, __clock_gettime, clock_gettime, GLIBC_2_17); |
| 103 | /* clock_gettime moved to libc in version 2.17; |
| 104 | old binaries may expect the symbol version it had in librt. */ |
| 105 | #if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17) |
| 106 | strong_alias (__clock_gettime, __clock_gettime_2); |
| 107 | compat_symbol (libc, __clock_gettime_2, clock_gettime, GLIBC_2_2); |
| 108 | #endif |
| 109 | |