]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/getitimer.c
65ffe9386e964507f3ba2010f6ab77f0d79999cd
[thirdparty/glibc.git] / sysdeps / mach / hurd / getitimer.c
1 /* Copyright (C) 1994-2018 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 <stddef.h>
19 #include <errno.h>
20 #include <sys/time.h>
21 #include <hurd.h>
22
23 /* XXX Temporary cheezoid implementation; see __setitmr.c. */
24
25 /* These are defined in __setitmr.c. */
26 extern spin_lock_t _hurd_itimer_lock;
27 extern struct itimerval _hurd_itimerval;
28 extern struct timeval _hurd_itimer_started;
29
30 static inline void
31 subtract_timeval (struct timeval *from, const struct timeval *subtract)
32 {
33 from->tv_usec -= subtract->tv_usec;
34 from->tv_sec -= subtract->tv_sec;
35 while (from->tv_usec < 0)
36 {
37 --from->tv_sec;
38 from->tv_usec += 1000000;
39 }
40 }
41
42 /* Set *VALUE to the current setting of timer WHICH.
43 Return 0 on success, -1 on errors. */
44 int
45 __getitimer (enum __itimer_which which, struct itimerval *value)
46 {
47 struct itimerval val;
48 struct timeval elapsed;
49
50 switch (which)
51 {
52 default:
53 return __hurd_fail (EINVAL);
54
55 case ITIMER_VIRTUAL:
56 case ITIMER_PROF:
57 return __hurd_fail (ENOSYS);
58
59 case ITIMER_REAL:
60 break;
61 }
62
63 /* Get the time now. */
64 if (__gettimeofday (&elapsed, NULL) < 0)
65 return -1;
66
67 /* Extract the current timer setting; and the time it was set, so we can
68 calculate the time elapsed so far. */
69 HURD_CRITICAL_BEGIN;
70 __spin_lock (&_hurd_itimer_lock);
71 val = _hurd_itimerval;
72 subtract_timeval (&elapsed, &_hurd_itimer_started);
73 __spin_unlock (&_hurd_itimer_lock);
74 HURD_CRITICAL_END;
75
76 if ((val.it_value.tv_sec | val.it_value.tv_usec) != 0)
77 {
78 /* There is a pending alarm set. VAL indicates the interval it was
79 set for, relative to the time recorded in _hurd_itimer_started.
80 Now compensate for the time elapsed since to get the user's
81 conception of the current value of the timer (as if the value
82 stored decreased every microsecond). */
83 if (timercmp (&val.it_value, &elapsed, <))
84 {
85 /* Hmm. The timer should have just gone off, but has not been
86 reset. This is a possible timing glitch. The alarm will signal
87 soon, so fabricate a value for how soon. */
88 val.it_value.tv_sec = 0;
89 val.it_value.tv_usec = 10; /* Random. */
90 }
91 else
92 /* Subtract the time elapsed since the timer was set
93 from the current timer value the user sees. */
94 subtract_timeval (&val.it_value, &elapsed);
95 }
96
97 *value = val;
98 return 0;
99 }
100
101 weak_alias (__getitimer, getitimer)