| 1 | /* Copyright (C) 2008-2019 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 no |
| 33 | separate return value and error indicators we cannot |
| 34 | distinguish a return value of e.g. (clock_t) -14 from -EFAULT. |
| 35 | Therefore the only course of action is to dereference the user |
| 36 | -supplied structure on a return of (clock_t) -14. This will crash |
| 37 | applications which pass in an invalid non-NULL BUF pointer. |
| 38 | Note that Linux allows BUF to be NULL in which case we skip this. */ |
| 39 | #define touch(v) \ |
| 40 | do { \ |
| 41 | clock_t temp = v; \ |
| 42 | asm volatile ("" : "+r" (temp)); \ |
| 43 | v = temp; \ |
| 44 | } while (0) |
| 45 | touch (buf->tms_utime); |
| 46 | touch (buf->tms_stime); |
| 47 | touch (buf->tms_cutime); |
| 48 | touch (buf->tms_cstime); |
| 49 | |
| 50 | /* If we come here the memory is valid and the kernel did not |
| 51 | return an EFAULT error, but rather e.g. (clock_t) -14. |
| 52 | Return the value given by the kernel. */ |
| 53 | } |
| 54 | |
| 55 | /* On Linux this function never fails except with EFAULT. |
| 56 | POSIX says that returning a value (clock_t) -1 indicates an error, |
| 57 | but on Linux this is simply one of the valid clock values after |
| 58 | clock_t wraps. Therefore when we would return (clock_t) -1, we |
| 59 | instead return (clock_t) 0, and loose a tick of accuracy (having |
| 60 | returned 0 for two consecutive calls even though the clock |
| 61 | advanced). */ |
| 62 | if (ret == (clock_t) -1) |
| 63 | return (clock_t) 0; |
| 64 | |
| 65 | return ret; |
| 66 | } |
| 67 | weak_alias (__times, times) |
| 68 | |