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 "uv-common.h"
24
25 #ifdef _WIN32
26 # include "win/internal.h"
27 #else
28 # include "unix/internal.h"
29 #endif
30
uv__random(void * buf,size_t buflen)31 static int uv__random(void* buf, size_t buflen) {
32 int rc;
33
34 #if defined(__PASE__)
35 rc = uv__random_readpath("/dev/urandom", buf, buflen);
36 #elif defined(_AIX) || defined(__QNX__)
37 rc = uv__random_readpath("/dev/random", buf, buflen);
38 #elif defined(__APPLE__) || defined(__OpenBSD__) || \
39 (defined(__ANDROID_API__) && __ANDROID_API__ >= 28)
40 rc = uv__random_getentropy(buf, buflen);
41 if (rc == UV_ENOSYS)
42 rc = uv__random_devurandom(buf, buflen);
43 #elif defined(__NetBSD__)
44 rc = uv__random_sysctl(buf, buflen);
45 #elif defined(__FreeBSD__) || defined(__linux__)
46 rc = uv__random_getrandom(buf, buflen);
47 if (rc == UV_ENOSYS)
48 rc = uv__random_devurandom(buf, buflen);
49 # if defined(__linux__)
50 switch (rc) {
51 case UV_EACCES:
52 case UV_EIO:
53 case UV_ELOOP:
54 case UV_EMFILE:
55 case UV_ENFILE:
56 case UV_ENOENT:
57 case UV_EPERM:
58 rc = uv__random_sysctl(buf, buflen);
59 break;
60 }
61 # endif
62 #elif defined(_WIN32)
63 uv__once_init();
64 rc = uv__random_rtlgenrandom(buf, buflen);
65 #else
66 rc = uv__random_devurandom(buf, buflen);
67 #endif
68
69 return rc;
70 }
71
72
73 #ifdef USE_FFRT
uv__random_work(struct uv__work * w,int qos)74 static void uv__random_work(struct uv__work* w, int qos) {
75 #else
76 static void uv__random_work(struct uv__work* w) {
77 #endif
78 uv_random_t* req;
79
80 req = container_of(w, uv_random_t, work_req);
81 req->status = uv__random(req->buf, req->buflen);
82 #ifdef USE_FFRT
83 uv__work_submit_to_eventloop((uv_req_t*)req, w, qos);
84 #endif
85 }
86
87
88 static void uv__random_done(struct uv__work* w, int status) {
89 uv_random_t* req;
90
91 req = container_of(w, uv_random_t, work_req);
92 uv__req_unregister(req->loop, req);
93
94 if (status == 0)
95 status = req->status;
96
97 req->cb(req, status, req->buf, req->buflen);
98 }
99
100
101 int uv_random(uv_loop_t* loop,
102 uv_random_t* req,
103 void *buf,
104 size_t buflen,
105 unsigned flags,
106 uv_random_cb cb) {
107 if (buflen > 0x7FFFFFFFu)
108 return UV_E2BIG;
109
110 if (flags != 0)
111 return UV_EINVAL;
112
113 if (cb == NULL)
114 return uv__random(buf, buflen);
115
116 uv__req_init(loop, req, UV_RANDOM);
117 req->loop = loop;
118 req->status = 0;
119 req->cb = cb;
120 req->buf = buf;
121 req->buflen = buflen;
122
123 uv__work_submit(loop,
124 #ifdef USE_FFRT
125 (uv_req_t*)req,
126 #endif
127 &req->work_req,
128 UV__WORK_CPU,
129 uv__random_work,
130 uv__random_done);
131
132 return 0;
133 }
134