blob: 4b5bd7b29a9ce1830cecf87ed91aeb9374e2f686 [file] [log] [blame]
Paul Jakma57345092011-12-25 17:52:09 +01001/*
2 * This file is free software: you may copy, redistribute and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation, either version 2 of the License, or (at your
5 * option) any later version.
6 *
7 * This file is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *
15 * This file incorporates work covered by the following copyright and
16 * permission notice:
17 *
18
19Copyright 2007, 2008 by Grégoire Henry, Julien Cristau and Juliusz Chroboczek
20
21Permission is hereby granted, free of charge, to any person obtaining a copy
22of this software and associated documentation files (the "Software"), to deal
23in the Software without restriction, including without limitation the rights
24to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25copies of the Software, and to permit persons to whom the Software is
26furnished to do so, subject to the following conditions:
27
28The above copyright notice and this permission notice shall be included in
29all copies or substantial portions of the Software.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37THE SOFTWARE.
38*/
39
40#include <sys/time.h>
41#include <sys/param.h>
42#include <time.h>
43
44#include "babeld.h"
45
46#include "kernel_zebra.c"
47
48/* Like gettimeofday, but returns monotonic time. If POSIX clocks are not
49 available, falls back to gettimeofday but enforces monotonicity. */
50int
51gettime(struct timeval *tv)
52{
53 int rc;
54 static time_t offset = 0, previous = 0;
55
56#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC)
57 static int have_posix_clocks = -1;
58
59 if(UNLIKELY(have_posix_clocks < 0)) {
60 struct timespec ts;
61 rc = clock_gettime(CLOCK_MONOTONIC, &ts);
62 if(rc < 0) {
63 have_posix_clocks = 0;
64 } else {
65 have_posix_clocks = 1;
66 }
67 }
68
69 if(have_posix_clocks) {
70 struct timespec ts;
71 int rc;
72 rc = clock_gettime(CLOCK_MONOTONIC, &ts);
73 if(rc < 0)
74 return rc;
75 tv->tv_sec = ts.tv_sec;
76 tv->tv_usec = ts.tv_nsec / 1000;
77 return rc;
78 }
79#endif
80
81 rc = gettimeofday(tv, NULL);
82 if(rc < 0)
83 return rc;
84 tv->tv_sec += offset;
85 if(UNLIKELY(previous > tv->tv_sec)) {
86 offset += previous - tv->tv_sec;
87 tv->tv_sec = previous;
88 }
89 previous = tv->tv_sec;
90 return rc;
91}
92
93/* If /dev/urandom doesn't exist, this will fail with ENOENT, which the
94 caller will deal with gracefully. */
95
96int
97read_random_bytes(void *buf, size_t len)
98{
99 int fd;
100 int rc;
101
102 fd = open("/dev/urandom", O_RDONLY);
103 if(fd < 0) {
104 rc = -1;
105 } else {
106 rc = read(fd, buf, len);
107 if(rc < 0 || (unsigned) rc < len)
108 rc = -1;
109 close(fd);
110 }
111 return rc;
112}
113