1/* Copyright libuv contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22#include "uv.h"
23#include "internal.h"
24
25#include <sys/stat.h>
26#include <unistd.h>
27
28static uv_once_t once = UV_ONCE_INIT;
29static int status;
30
31
32int uv__random_readpath(const char* path, void* buf, size_t buflen) {
33 struct stat s;
34 size_t pos;
35 ssize_t n;
36 int fd;
37
38 fd = uv__open_cloexec(path, O_RDONLY);
39
40 if (fd < 0)
41 return fd;
42
43 if (fstat(fd, &s)) {
44 uv__close(fd);
45 return UV__ERR(errno);
46 }
47
48 if (!S_ISCHR(s.st_mode)) {
49 uv__close(fd);
50 return UV_EIO;
51 }
52
53 for (pos = 0; pos != buflen; pos += n) {
54 do
55 n = read(fd, (char*) buf + pos, buflen - pos);
56 while (n == -1 && errno == EINTR);
57
58 if (n == -1) {
59 uv__close(fd);
60 return UV__ERR(errno);
61 }
62
63 if (n == 0) {
64 uv__close(fd);
65 return UV_EIO;
66 }
67 }
68
69 uv__close(fd);
70 return 0;
71}
72
73
74static void uv__random_devurandom_init(void) {
75 char c;
76
77 /* Linux's random(4) man page suggests applications should read at least
78 * once from /dev/random before switching to /dev/urandom in order to seed
79 * the system RNG. Reads from /dev/random can of course block indefinitely
80 * until entropy is available but that's the point.
81 */
82 status = uv__random_readpath("/dev/random", &c, 1);
83}
84
85
86int uv__random_devurandom(void* buf, size_t buflen) {
87 uv_once(&once, uv__random_devurandom_init);
88
89 if (status != 0)
90 return status;
91
92 return uv__random_readpath("/dev/urandom", buf, buflen);
93}
94