Blame sysdeps/unix/sysv/linux/times.c

Packit 6c4009
/* Copyright (C) 2008-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <sys/times.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
clock_t
Packit 6c4009
__times (struct tms *buf)
Packit 6c4009
{
Packit 6c4009
  INTERNAL_SYSCALL_DECL (err);
Packit 6c4009
  clock_t ret = INTERNAL_SYSCALL (times, err, 1, buf);
Packit 6c4009
  if (INTERNAL_SYSCALL_ERROR_P (ret, err)
Packit 6c4009
      && __builtin_expect (INTERNAL_SYSCALL_ERRNO (ret, err) == EFAULT, 0)
Packit 6c4009
      && buf)
Packit 6c4009
    {
Packit 6c4009
      /* This might be an error or not.  For architectures which have no
Packit 6c4009
	 separate return value and error indicators we cannot
Packit 6c4009
	 distinguish a return value of e.g. (clock_t) -14 from -EFAULT.
Packit 6c4009
	 Therefore the only course of action is to dereference the user
Packit 6c4009
	 -supplied structure on a return of (clock_t) -14.  This will crash
Packit 6c4009
	 applications which pass in an invalid non-NULL BUF pointer.
Packit 6c4009
	 Note that Linux allows BUF to be NULL in which case we skip this.  */
Packit 6c4009
#define touch(v) \
Packit 6c4009
      do {								      \
Packit 6c4009
	clock_t temp = v;						      \
Packit 6c4009
	asm volatile ("" : "+r" (temp));				      \
Packit 6c4009
	v = temp;							      \
Packit 6c4009
      } while (0)
Packit 6c4009
      touch (buf->tms_utime);
Packit 6c4009
      touch (buf->tms_stime);
Packit 6c4009
      touch (buf->tms_cutime);
Packit 6c4009
      touch (buf->tms_cstime);
Packit 6c4009
Packit 6c4009
      /* If we come here the memory is valid and the kernel did not
Packit 6c4009
	 return an EFAULT error, but rather e.g. (clock_t) -14.
Packit 6c4009
	 Return the value given by the kernel.  */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* On Linux this function never fails except with EFAULT.
Packit 6c4009
     POSIX says that returning a value (clock_t) -1 indicates an error,
Packit 6c4009
     but on Linux this is simply one of the valid clock values after
Packit 6c4009
     clock_t wraps.  Therefore when we would return (clock_t) -1, we
Packit 6c4009
     instead return (clock_t) 0, and loose a tick of accuracy (having
Packit 6c4009
     returned 0 for two consecutive calls even though the clock
Packit 6c4009
     advanced).  */
Packit 6c4009
  if (ret == (clock_t) -1)
Packit 6c4009
    return (clock_t) 0;
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
weak_alias (__times, times)