| 1 | /* Copyright (C) 2008-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <sys/times.h> |
| 20 | #include <sysdep.h> |
| 21 | |
| 22 | |
| 23 | clock_t |
| 24 | __times (struct tms *buf) |
| 25 | { |
| 26 | INTERNAL_SYSCALL_DECL (err); |
| 27 | clock_t ret = INTERNAL_SYSCALL (times, err, 1, buf); |
| 28 | if (INTERNAL_SYSCALL_ERROR_P (ret, err) |
| 29 | && __builtin_expect (INTERNAL_SYSCALL_ERRNO (ret, err) == EFAULT, 0) |
| 30 | && buf) |
| 31 | { |
| 32 | /* This might be an error or not. For architectures which have |
| 33 | no separate return value and error indicators we cannot |
| 34 | distinguish a return value of -1 from an error. Do it the |
| 35 | hard way. We crash applications which pass in an invalid |
| 36 | non-NULL BUF pointer. Linux allows BUF to be NULL. */ |
| 37 | #define touch(v) \ |
| 38 | do { \ |
| 39 | clock_t temp = v; \ |
| 40 | asm volatile ("" : "+r" (temp)); \ |
| 41 | v = temp; \ |
| 42 | } while (0) |
| 43 | touch (buf->tms_utime); |
| 44 | touch (buf->tms_stime); |
| 45 | touch (buf->tms_cutime); |
| 46 | touch (buf->tms_cstime); |
| 47 | |
| 48 | /* If we come here the memory is valid (or BUF is NULL, which is |
| 49 | a valid condition for the kernel syscall) and the kernel did not |
| 50 | return an EFAULT error. Return the value given by the kernel. */ |
| 51 | } |
| 52 | |
| 53 | /* Return value (clock_t) -1 signals an error, but if there wasn't any, |
| 54 | return the following value. */ |
| 55 | if (ret == (clock_t) -1) |
| 56 | return (clock_t) 0; |
| 57 | |
| 58 | return ret; |
| 59 | } |
| 60 | weak_alias (__times, times) |
| 61 | |