1/* Computing deadlines for timeouts.
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <net-internal.h>
20
21#include <assert.h>
22#include <limits.h>
23#include <stdio.h>
24#include <stdint.h>
25#include <time.h>
26
27struct deadline_current_time
28__deadline_current_time (void)
29{
30 struct deadline_current_time result;
31 if (__clock_gettime (CLOCK_MONOTONIC, &result.current) != 0)
32 {
33 struct timeval current_tv;
34 if (__gettimeofday (&current_tv, NULL) == 0)
35 __libc_fatal ("Fatal error: gettimeofday system call failed\n");
36 result.current.tv_sec = current_tv.tv_sec;
37 result.current.tv_nsec = current_tv.tv_usec * 1000;
38 }
39 assert (result.current.tv_sec >= 0);
40 return result;
41}
42
43/* A special deadline value for which __deadline_is_infinite is
44 true. */
45static inline struct deadline
46infinite_deadline (void)
47{
48 return (struct deadline) { { -1, -1 } };
49}
50
51struct deadline
52__deadline_from_timeval (struct deadline_current_time current,
53 struct timeval tv)
54{
55 assert (__is_timeval_valid_timeout (tv));
56
57 /* Compute second-based deadline. Perform the addition in
58 uintmax_t, which is unsigned, to simply overflow detection. */
59 uintmax_t sec = current.current.tv_sec;
60 sec += tv.tv_sec;
61 if (sec < (uintmax_t) tv.tv_sec)
62 return infinite_deadline ();
63
64 /* Compute nanosecond deadline. */
65 int nsec = current.current.tv_nsec + tv.tv_usec * 1000;
66 if (nsec >= 1000 * 1000 * 1000)
67 {
68 /* Carry nanosecond overflow to seconds. */
69 nsec -= 1000 * 1000 * 1000;
70 if (sec + 1 < sec)
71 return infinite_deadline ();
72 ++sec;
73 }
74 /* This uses a GCC extension, otherwise these casts for detecting
75 overflow would not be defined. */
76 if ((time_t) sec < 0 || sec != (uintmax_t) (time_t) sec)
77 return infinite_deadline ();
78
79 return (struct deadline) { { sec, nsec } };
80}
81
82int
83__deadline_to_ms (struct deadline_current_time current,
84 struct deadline deadline)
85{
86 if (__deadline_is_infinite (deadline))
87 return INT_MAX;
88
89 if (current.current.tv_sec > deadline.absolute.tv_sec
90 || (current.current.tv_sec == deadline.absolute.tv_sec
91 && current.current.tv_nsec >= deadline.absolute.tv_nsec))
92 return 0;
93 time_t sec = deadline.absolute.tv_sec - current.current.tv_sec;
94 if (sec >= INT_MAX)
95 /* This value will overflow below. */
96 return INT_MAX;
97 int nsec = deadline.absolute.tv_nsec - current.current.tv_nsec;
98 if (nsec < 0)
99 {
100 /* Borrow from the seconds field. */
101 assert (sec > 0);
102 --sec;
103 nsec += 1000 * 1000 * 1000;
104 }
105
106 /* Prepare for rounding up to milliseconds. */
107 nsec += 999999;
108 if (nsec > 1000 * 1000 * 1000)
109 {
110 assert (sec < INT_MAX);
111 ++sec;
112 nsec -= 1000 * 1000 * 1000;
113 }
114
115 unsigned int msec = nsec / (1000 * 1000);
116 if (sec > INT_MAX / 1000)
117 return INT_MAX;
118 msec += sec * 1000;
119 if (msec > INT_MAX)
120 return INT_MAX;
121 return msec;
122}
123