| 1 | /* gettimeofday - set time. Linux version. |
| 2 | Copyright (C) 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 License as |
| 7 | published by the Free Software Foundation; either version 2.1 of the |
| 8 | 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 | /* Optimize the function call by setting the PLT directly to vDSO symbol. */ |
| 20 | #ifdef USE_IFUNC_GETTIMEOFDAY |
| 21 | # include <time.h> |
| 22 | # include <string.h> |
| 23 | # include <sysdep.h> |
| 24 | # include <sysdep-vdso.h> |
| 25 | |
| 26 | # ifdef SHARED |
| 27 | # include <dl-vdso.h> |
| 28 | # include <libc-vdso.h> |
| 29 | |
| 30 | static int |
| 31 | __gettimeofday_syscall (struct timeval *restrict tv, void *restrict tz) |
| 32 | { |
| 33 | if (__glibc_unlikely (tz != 0)) |
| 34 | memset (tz, 0, sizeof *tz); |
| 35 | return INLINE_SYSCALL_CALL (gettimeofday, tv, tz); |
| 36 | } |
| 37 | |
| 38 | # undef INIT_ARCH |
| 39 | # define INIT_ARCH() \ |
| 40 | void *vdso_gettimeofday = dl_vdso_vsym (HAVE_GETTIMEOFDAY_VSYSCALL) |
| 41 | libc_ifunc (__gettimeofday, |
| 42 | vdso_gettimeofday ? VDSO_IFUNC_RET (vdso_gettimeofday) |
| 43 | : (void *) __gettimeofday_syscall) |
| 44 | |
| 45 | # else |
| 46 | int |
| 47 | __gettimeofday (struct timeval *restrict tv, void *restrict tz) |
| 48 | { |
| 49 | if (__glibc_unlikely (tz != 0)) |
| 50 | memset (tz, 0, sizeof *tz); |
| 51 | |
| 52 | return INLINE_VSYSCALL (gettimeofday, 2, tv, tz); |
| 53 | } |
| 54 | # endif |
| 55 | weak_alias (__gettimeofday, gettimeofday) |
| 56 | #else /* USE_IFUNC_GETTIMEOFDAY */ |
| 57 | /* Conversion of gettimeofday function to support 64 bit time on archs |
| 58 | with __WORDSIZE == 32 and __TIMESIZE == 32/64 */ |
| 59 | #include <errno.h> |
| 60 | |
| 61 | int |
| 62 | __gettimeofday64 (struct __timeval64 *restrict tv, void *restrict tz) |
| 63 | { |
| 64 | if (__glibc_unlikely (tz != 0)) |
| 65 | memset (tz, 0, sizeof (struct timezone)); |
| 66 | |
| 67 | struct __timespec64 ts64; |
| 68 | if (__clock_gettime64 (CLOCK_REALTIME, &ts64)) |
| 69 | return -1; |
| 70 | |
| 71 | *tv = timespec64_to_timeval64 (ts64); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | # if __TIMESIZE != 64 |
| 76 | libc_hidden_def (__gettimeofday64) |
| 77 | |
| 78 | int |
| 79 | __gettimeofday (struct timeval *restrict tv, void *restrict tz) |
| 80 | { |
| 81 | struct __timeval64 tv64; |
| 82 | if (__gettimeofday64 (&tv64, tz)) |
| 83 | return -1; |
| 84 | |
| 85 | if (! in_time_t_range (tv64.tv_sec)) |
| 86 | { |
| 87 | __set_errno (EOVERFLOW); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | *tv = valid_timeval64_to_timeval (tv64); |
| 92 | return 0; |
| 93 | } |
| 94 | # endif |
| 95 | weak_alias (__gettimeofday, gettimeofday) |
| 96 | #endif |
| 97 | |