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