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