glibc/sysdeps/unix/sysv/linux/clock_gettime.c
Adhemerval Zanella d0def09ff6 linux: Fix vDSO macros build with time64 interfaces
As indicated on libc-help [1] the ec138c67cb commit broke 32-bit
builds when configured with --enable-kernel=5.1 or higher.  The
scenario 10 from [2] might also occur in this configuration and
INLINE_VSYSCALL will try to use the vDSO symbol and
HAVE_CLOCK_GETTIME64_VSYSCALL does not set HAVE_VSYSCALL prior its
usage.

Also, there is no easy way to just enable the code to use one
vDSO symbol since the macro INLINE_VSYSCALL is redefined if
HAVE_VSYSCALL is set.

Instead of adding more pre-processor handling and making the code
even more convoluted, this patch removes the requirement of defining
HAVE_VSYSCALL before including sysdep-vdso.h to enable vDSO usage.

The INLINE_VSYSCALL is now expected to be issued inside a
HAVE_*_VSYSCALL check, since it will try to use the internal vDSO
pointers.

Both clock_getres and clock_gettime vDSO code for time64_t were
removed since there is no vDSO setup code for the symbol (an
architecture can not set HAVE_CLOCK_GETTIME64_VSYSCALL).

Checked on i686-linux-gnu (default and with --enable-kernel=5.1),
x86_64-linux-gnu, aarch64-linux-gnu, and powerpc64le-linux-gnu.
I also checked against a build to mips64-linux-gnu and
sparc64-linux-gnu.

[1] https://sourceware.org/ml/libc-help/2019-12/msg00014.html
[2] https://sourceware.org/ml/libc-alpha/2019-12/msg00142.html

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
2020-01-03 10:02:05 -03:00

96 lines
2.8 KiB
C

/* clock_gettime -- Get current time from a POSIX clockid_t. Linux version.
Copyright (C) 2003-2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <sysdep.h>
#include <kernel-features.h>
#include <errno.h>
#include <time.h>
#include "kernel-posix-cpu-timers.h"
#include <sysdep-vdso.h>
#include <shlib-compat.h>
/* Get current value of CLOCK and store it in TP. */
int
__clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp)
{
#ifdef __ASSUME_TIME64_SYSCALLS
/* 64 bit ABIs or Newer 32-bit ABIs that only support 64-bit time_t. */
# ifdef __NR_clock_gettime64
return INLINE_SYSCALL_CALL (clock_gettime64, clock_id, tp);
# else
# ifdef HAVE_CLOCK_GETTIME_VSYSCALL
return INLINE_VSYSCALL (clock_gettime, 2, clock_id, tp);
# else
return INLINE_SYSCALL_CALL (clock_gettime, clock_id, tp);
# endif
# endif
#else
int r;
/* Old 32-bit ABI with possible 64-bit time_t support. */
# ifdef __NR_clock_gettime64
r = INLINE_SYSCALL_CALL (clock_gettime64, clock_id, tp);
if (r == 0 || errno != ENOSYS)
return r;
# endif
/* Fallback code that uses 32-bit support. */
struct timespec tp32;
# ifdef HAVE_CLOCK_GETTIME_VSYSCALL
r = INLINE_VSYSCALL (clock_gettime, 2, clock_id, &tp32);
# else
r = INLINE_SYSCALL_CALL (clock_gettime, clock_id, &tp32);
# endif
if (r == 0)
*tp = valid_timespec_to_timespec64 (tp32);
return r;
#endif
}
#if __TIMESIZE != 64
int
__clock_gettime (clockid_t clock_id, struct timespec *tp)
{
int ret;
struct __timespec64 tp64;
ret = __clock_gettime64 (clock_id, &tp64);
if (ret == 0)
{
if (! in_time_t_range (tp64.tv_sec))
{
__set_errno (EOVERFLOW);
return -1;
}
*tp = valid_timespec64_to_timespec (tp64);
}
return ret;
}
#endif
libc_hidden_def (__clock_gettime)
versioned_symbol (libc, __clock_gettime, clock_gettime, GLIBC_2_17);
/* clock_gettime moved to libc in version 2.17;
old binaries may expect the symbol version it had in librt. */
#if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17)
strong_alias (__clock_gettime, __clock_gettime_2);
compat_symbol (libc, __clock_gettime_2, clock_gettime, GLIBC_2_2);
#endif