11cb0ef41Sopenharmony_ci/* Copyright libuv contributors. All rights reserved. 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 41cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to 51cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the 61cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 71cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 81cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 91cb0ef41Sopenharmony_ci * 101cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 111cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software. 121cb0ef41Sopenharmony_ci * 131cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 141cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 151cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 161cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 171cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 181cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 191cb0ef41Sopenharmony_ci * IN THE SOFTWARE. 201cb0ef41Sopenharmony_ci */ 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci#include "uv.h" 231cb0ef41Sopenharmony_ci#include "internal.h" 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci#include <sys/stat.h> 261cb0ef41Sopenharmony_ci#include <unistd.h> 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cistatic uv_once_t once = UV_ONCE_INIT; 291cb0ef41Sopenharmony_cistatic int status; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciint uv__random_readpath(const char* path, void* buf, size_t buflen) { 331cb0ef41Sopenharmony_ci struct stat s; 341cb0ef41Sopenharmony_ci size_t pos; 351cb0ef41Sopenharmony_ci ssize_t n; 361cb0ef41Sopenharmony_ci int fd; 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci fd = uv__open_cloexec(path, O_RDONLY); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci if (fd < 0) 411cb0ef41Sopenharmony_ci return fd; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci if (fstat(fd, &s)) { 441cb0ef41Sopenharmony_ci uv__close(fd); 451cb0ef41Sopenharmony_ci return UV__ERR(errno); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci if (!S_ISCHR(s.st_mode)) { 491cb0ef41Sopenharmony_ci uv__close(fd); 501cb0ef41Sopenharmony_ci return UV_EIO; 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci for (pos = 0; pos != buflen; pos += n) { 541cb0ef41Sopenharmony_ci do 551cb0ef41Sopenharmony_ci n = read(fd, (char*) buf + pos, buflen - pos); 561cb0ef41Sopenharmony_ci while (n == -1 && errno == EINTR); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci if (n == -1) { 591cb0ef41Sopenharmony_ci uv__close(fd); 601cb0ef41Sopenharmony_ci return UV__ERR(errno); 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci if (n == 0) { 641cb0ef41Sopenharmony_ci uv__close(fd); 651cb0ef41Sopenharmony_ci return UV_EIO; 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci uv__close(fd); 701cb0ef41Sopenharmony_ci return 0; 711cb0ef41Sopenharmony_ci} 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_cistatic void uv__random_devurandom_init(void) { 751cb0ef41Sopenharmony_ci char c; 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci /* Linux's random(4) man page suggests applications should read at least 781cb0ef41Sopenharmony_ci * once from /dev/random before switching to /dev/urandom in order to seed 791cb0ef41Sopenharmony_ci * the system RNG. Reads from /dev/random can of course block indefinitely 801cb0ef41Sopenharmony_ci * until entropy is available but that's the point. 811cb0ef41Sopenharmony_ci */ 821cb0ef41Sopenharmony_ci status = uv__random_readpath("/dev/random", &c, 1); 831cb0ef41Sopenharmony_ci} 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ciint uv__random_devurandom(void* buf, size_t buflen) { 871cb0ef41Sopenharmony_ci uv_once(&once, uv__random_devurandom_init); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci if (status != 0) 901cb0ef41Sopenharmony_ci return status; 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci return uv__random_readpath("/dev/urandom", buf, buflen); 931cb0ef41Sopenharmony_ci} 94