| 1 | /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version. |
| 2 | Copyright (C) 2003-2021 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 | #include <time64-support.h> |
| 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 | int r; |
| 33 | |
| 34 | #ifndef __NR_clock_gettime64 |
| 35 | # define __NR_clock_gettime64 __NR_clock_gettime |
| 36 | #endif |
| 37 | |
| 38 | if (supports_time64 ()) |
| 39 | { |
| 40 | #ifdef HAVE_CLOCK_GETTIME64_VSYSCALL |
| 41 | r = INLINE_VSYSCALL (clock_gettime64, 2, clock_id, tp); |
| 42 | #else |
| 43 | r = INLINE_SYSCALL_CALL (clock_gettime64, clock_id, tp); |
| 44 | #endif |
| 45 | |
| 46 | if (r == 0 || errno != ENOSYS) |
| 47 | return r; |
| 48 | |
| 49 | mark_time64_unsupported (); |
| 50 | } |
| 51 | |
| 52 | #ifndef __ASSUME_TIME64_SYSCALLS |
| 53 | /* Fallback code that uses 32-bit support. */ |
| 54 | struct timespec tp32; |
| 55 | # ifdef HAVE_CLOCK_GETTIME_VSYSCALL |
| 56 | r = INLINE_VSYSCALL (clock_gettime, 2, clock_id, &tp32); |
| 57 | # else |
| 58 | r = INLINE_SYSCALL_CALL (clock_gettime, clock_id, &tp32); |
| 59 | # endif |
| 60 | if (r == 0) |
| 61 | *tp = valid_timespec_to_timespec64 (tp32); |
| 62 | #endif |
| 63 | |
| 64 | return r; |
| 65 | } |
| 66 | |
| 67 | #if __TIMESIZE != 64 |
| 68 | libc_hidden_def (__clock_gettime64) |
| 69 | |
| 70 | int |
| 71 | __clock_gettime (clockid_t clock_id, struct timespec *tp) |
| 72 | { |
| 73 | int ret; |
| 74 | struct __timespec64 tp64; |
| 75 | |
| 76 | ret = __clock_gettime64 (clock_id, &tp64); |
| 77 | |
| 78 | if (ret == 0) |
| 79 | { |
| 80 | if (! in_time_t_range (tp64.tv_sec)) |
| 81 | { |
| 82 | __set_errno (EOVERFLOW); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | *tp = valid_timespec64_to_timespec (tp64); |
| 87 | } |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | #endif |
| 92 | libc_hidden_def (__clock_gettime) |
| 93 | |
| 94 | versioned_symbol (libc, __clock_gettime, clock_gettime, GLIBC_2_17); |
| 95 | /* clock_gettime moved to libc in version 2.17; |
| 96 | old binaries may expect the symbol version it had in librt. */ |
| 97 | #if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17) |
| 98 | strong_alias (__clock_gettime, __clock_gettime_2); |
| 99 | compat_symbol (libc, __clock_gettime_2, clock_gettime, GLIBC_2_2); |
| 100 | #endif |
| 101 | |