1  /* clock_gettime -- Get current time from a POSIX clockid_t.  Linux version.
       2     Copyright (C) 2003-2023 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 <shlib-compat.h>
      26  
      27  /* Get current value of CLOCK and store it in TP.  */
      28  int
      29  __clock_gettime64 (clockid_t clock_id, struct __timespec64 *tp)
      30  {
      31    int r;
      32  
      33  #ifndef __NR_clock_gettime64
      34  # define __NR_clock_gettime64 __NR_clock_gettime
      35  #endif
      36  
      37  #ifdef HAVE_CLOCK_GETTIME64_VSYSCALL
      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      }
      47  #endif
      48  
      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);
      56        if (r == 0 && tp32.tv_sec >= 0)
      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);
      73  
      74  #ifndef __ASSUME_TIME64_SYSCALLS
      75    /* Fallback code that uses 32-bit support.  */
      76    struct timespec tp32;
      77    r = INTERNAL_SYSCALL_CALL (clock_gettime, clock_id, &tp32);
      78    if (r == 0)
      79      {
      80        *tp = valid_timespec_to_timespec64 (tp32);
      81        return 0;
      82      }
      83  #endif
      84  
      85    return INLINE_SYSCALL_ERROR_RETURN_VALUE (-r);
      86  }
      87  
      88  #if __TIMESIZE != 64
      89  libc_hidden_def (__clock_gettime64)
      90  
      91  int
      92  __clock_gettime (clockid_t clock_id, struct timespec *tp)
      93  {
      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;
     111  }
     112  #endif
     113  libc_hidden_def (__clock_gettime)
     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