Commit c9f8d6c7 authored by Jason Madden's avatar Jason Madden

The random* files are needed to build non-embedded.

parent 7d50d054
......@@ -81,9 +81,7 @@ Updating libuv
rm -rf libuv/test/*.[ch] libuv/test/test.gyp # must leave the fixtures/ dir
rm -rf libuv/tools
rm -f libuv/android-configure*
rm -f libuv/src/unix/haiku.c # We don't support Haiku
rm -f libuv/src/unix/random-*.c # We don't use the uv_random interfaces
rm -f libuv/src/random.c
At this point there might be new files in libuv that need added to git
and the build process. Evaluate those and add them to git and to
......
/* Copyright libuv contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "uv-common.h"
#ifdef _WIN32
# include "win/internal.h"
#else
# include "unix/internal.h"
#endif
static int uv__random(void* buf, size_t buflen) {
int rc;
#if defined(__PASE__)
rc = uv__random_readpath("/dev/urandom", buf, buflen);
#elif defined(_AIX)
rc = uv__random_readpath("/dev/random", buf, buflen);
#elif defined(__APPLE__) || defined(__OpenBSD__) || \
(defined(__ANDROID_API__) && __ANDROID_API__ >= 28)
rc = uv__random_getentropy(buf, buflen);
if (rc == UV_ENOSYS)
rc = uv__random_devurandom(buf, buflen);
#elif defined(__NetBSD__)
rc = uv__random_sysctl(buf, buflen);
#elif defined(__FreeBSD__) || defined(__linux__)
rc = uv__random_getrandom(buf, buflen);
if (rc == UV_ENOSYS)
rc = uv__random_devurandom(buf, buflen);
# if defined(__linux__)
switch (rc) {
case UV_EACCES:
case UV_EIO:
case UV_ELOOP:
case UV_EMFILE:
case UV_ENFILE:
case UV_ENOENT:
case UV_EPERM:
rc = uv__random_sysctl(buf, buflen);
break;
}
# endif
#elif defined(_WIN32)
uv__once_init();
rc = uv__random_rtlgenrandom(buf, buflen);
#else
rc = uv__random_devurandom(buf, buflen);
#endif
return rc;
}
static void uv__random_work(struct uv__work* w) {
uv_random_t* req;
req = container_of(w, uv_random_t, work_req);
req->status = uv__random(req->buf, req->buflen);
}
static void uv__random_done(struct uv__work* w, int status) {
uv_random_t* req;
req = container_of(w, uv_random_t, work_req);
uv__req_unregister(req->loop, req);
if (status == 0)
status = req->status;
req->cb(req, status, req->buf, req->buflen);
}
int uv_random(uv_loop_t* loop,
uv_random_t* req,
void *buf,
size_t buflen,
unsigned flags,
uv_random_cb cb) {
if (buflen > 0x7FFFFFFFu)
return UV_E2BIG;
if (flags != 0)
return UV_EINVAL;
if (cb == NULL)
return uv__random(buf, buflen);
uv__req_init(loop, req, UV_RANDOM);
req->loop = loop;
req->status = 0;
req->cb = cb;
req->buf = buf;
req->buflen = buflen;
uv__work_submit(loop,
&req->work_req,
UV__WORK_CPU,
uv__random_work,
uv__random_done);
return 0;
}
/* Copyright libuv contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "internal.h"
#include <sys/stat.h>
#include <unistd.h>
static uv_once_t once = UV_ONCE_INIT;
static int status;
int uv__random_readpath(const char* path, void* buf, size_t buflen) {
struct stat s;
size_t pos;
ssize_t n;
int fd;
fd = uv__open_cloexec(path, O_RDONLY);
if (fd == -1)
return UV__ERR(errno);
if (fstat(fd, &s)) {
uv__close(fd);
return UV__ERR(errno);
}
if (!S_ISCHR(s.st_mode)) {
uv__close(fd);
return UV_EIO;
}
for (pos = 0; pos != buflen; pos += n) {
do
n = read(fd, (char*) buf + pos, buflen - pos);
while (n == -1 && errno == EINTR);
if (n == -1) {
uv__close(fd);
return UV__ERR(errno);
}
if (n == 0) {
uv__close(fd);
return UV_EIO;
}
}
uv__close(fd);
return 0;
}
static void uv__random_devurandom_init(void) {
char c;
/* Linux's random(4) man page suggests applications should read at least
* once from /dev/random before switching to /dev/urandom in order to seed
* the system RNG. Reads from /dev/random can of course block indefinitely
* until entropy is available but that's the point.
*/
status = uv__random_readpath("/dev/random", &c, 1);
}
int uv__random_devurandom(void* buf, size_t buflen) {
uv_once(&once, uv__random_devurandom_init);
if (status != 0)
return status;
return uv__random_readpath("/dev/urandom", buf, buflen);
}
/* Copyright libuv contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "internal.h"
#include <stddef.h>
#include <dlfcn.h>
typedef int (*uv__getentropy_cb)(void *, size_t);
static uv__getentropy_cb uv__getentropy;
static uv_once_t once = UV_ONCE_INIT;
static void uv__random_getentropy_init(void) {
uv__getentropy = (uv__getentropy_cb) dlsym(RTLD_DEFAULT, "getentropy");
}
int uv__random_getentropy(void* buf, size_t buflen) {
size_t pos;
size_t stride;
uv_once(&once, uv__random_getentropy_init);
if (uv__getentropy == NULL)
return UV_ENOSYS;
/* getentropy() returns an error for requests > 256 bytes. */
for (pos = 0, stride = 256; pos + stride < buflen; pos += stride)
if (uv__getentropy((char *) buf + pos, stride))
return UV__ERR(errno);
if (uv__getentropy((char *) buf + pos, buflen - pos))
return UV__ERR(errno);
return 0;
}
/* Copyright libuv contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "internal.h"
#ifdef __linux__
#include "linux-syscalls.h"
#define uv__random_getrandom_init() 0
#else /* !__linux__ */
#include <stddef.h>
#include <dlfcn.h>
typedef ssize_t (*uv__getrandom_cb)(void *, size_t, unsigned);
static uv__getrandom_cb uv__getrandom;
static uv_once_t once = UV_ONCE_INIT;
static void uv__random_getrandom_init_once(void) {
uv__getrandom = (uv__getrandom_cb) dlsym(RTLD_DEFAULT, "getrandom");
}
static int uv__random_getrandom_init(void) {
uv_once(&once, uv__random_getrandom_init_once);
if (uv__getrandom == NULL)
return UV_ENOSYS;
return 0;
}
#endif /* !__linux__ */
int uv__random_getrandom(void* buf, size_t buflen) {
ssize_t n;
size_t pos;
int rc;
rc = uv__random_getrandom_init();
if (rc != 0)
return rc;
for (pos = 0; pos != buflen; pos += n) {
do {
n = buflen - pos;
/* Most getrandom() implementations promise that reads <= 256 bytes
* will always succeed and won't be interrupted by signals.
* It's therefore useful to split it up in smaller reads because
* one big read may, in theory, continuously fail with EINTR.
*/
if (n > 256)
n = 256;
n = uv__getrandom((char *) buf + pos, n, 0);
} while (n == -1 && errno == EINTR);
if (n == -1)
return UV__ERR(errno);
if (n == 0)
return UV_EIO;
}
return 0;
}
/* Copyright libuv contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "internal.h"
#include <errno.h>
#include <string.h>
#include <syscall.h>
#include <unistd.h>
struct uv__sysctl_args {
int* name;
int nlen;
void* oldval;
size_t* oldlenp;
void* newval;
size_t newlen;
unsigned long unused[4];
};
int uv__random_sysctl(void* buf, size_t buflen) {
static int name[] = {1 /*CTL_KERN*/, 40 /*KERN_RANDOM*/, 6 /*RANDOM_UUID*/};
struct uv__sysctl_args args;
char uuid[16];
char* p;
char* pe;
size_t n;
p = buf;
pe = p + buflen;
while (p < pe) {
memset(&args, 0, sizeof(args));
args.name = name;
args.nlen = ARRAY_SIZE(name);
args.oldval = uuid;
args.oldlenp = &n;
n = sizeof(uuid);
/* Emits a deprecation warning with some kernels but that seems like
* an okay trade-off for the fallback of the fallback: this function is
* only called when neither getrandom(2) nor /dev/urandom are available.
* Fails with ENOSYS on kernels configured without CONFIG_SYSCTL_SYSCALL.
* At least arm64 never had a _sysctl system call and therefore doesn't
* have a SYS__sysctl define either.
*/
#ifdef SYS__sysctl
if (syscall(SYS__sysctl, &args) == -1)
return UV__ERR(errno);
#else
{
(void) &args;
return UV_ENOSYS;
}
#endif
if (n != sizeof(uuid))
return UV_EIO; /* Can't happen. */
/* uuid[] is now a type 4 UUID. Bytes 6 and 8 (counting from zero) contain
* 4 and 5 bits of entropy, respectively. For ease of use, we skip those
* and only use 14 of the 16 bytes.
*/
uuid[6] = uuid[14];
uuid[8] = uuid[15];
n = pe - p;
if (n > 14)
n = 14;
memcpy(p, uuid, n);
p += n;
}
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment