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#ifdef __linux__ 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci#include "linux-syscalls.h" 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci#define uv__random_getrandom_init() 0 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci#else /* !__linux__ */ 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci#include <stddef.h> 341cb0ef41Sopenharmony_ci#include <dlfcn.h> 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_citypedef ssize_t (*uv__getrandom_cb)(void *, size_t, unsigned); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_cistatic uv__getrandom_cb uv__getrandom; 391cb0ef41Sopenharmony_cistatic uv_once_t once = UV_ONCE_INIT; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cistatic void uv__random_getrandom_init_once(void) { 421cb0ef41Sopenharmony_ci uv__getrandom = (uv__getrandom_cb) dlsym(RTLD_DEFAULT, "getrandom"); 431cb0ef41Sopenharmony_ci} 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_cistatic int uv__random_getrandom_init(void) { 461cb0ef41Sopenharmony_ci uv_once(&once, uv__random_getrandom_init_once); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci if (uv__getrandom == NULL) 491cb0ef41Sopenharmony_ci return UV_ENOSYS; 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci return 0; 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci#endif /* !__linux__ */ 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ciint uv__random_getrandom(void* buf, size_t buflen) { 571cb0ef41Sopenharmony_ci ssize_t n; 581cb0ef41Sopenharmony_ci size_t pos; 591cb0ef41Sopenharmony_ci int rc; 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci rc = uv__random_getrandom_init(); 621cb0ef41Sopenharmony_ci if (rc != 0) 631cb0ef41Sopenharmony_ci return rc; 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci for (pos = 0; pos != buflen; pos += n) { 661cb0ef41Sopenharmony_ci do { 671cb0ef41Sopenharmony_ci n = buflen - pos; 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci /* Most getrandom() implementations promise that reads <= 256 bytes 701cb0ef41Sopenharmony_ci * will always succeed and won't be interrupted by signals. 711cb0ef41Sopenharmony_ci * It's therefore useful to split it up in smaller reads because 721cb0ef41Sopenharmony_ci * one big read may, in theory, continuously fail with EINTR. 731cb0ef41Sopenharmony_ci */ 741cb0ef41Sopenharmony_ci if (n > 256) 751cb0ef41Sopenharmony_ci n = 256; 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci n = uv__getrandom((char *) buf + pos, n, 0); 781cb0ef41Sopenharmony_ci } while (n == -1 && errno == EINTR); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci if (n == -1) 811cb0ef41Sopenharmony_ci return UV__ERR(errno); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci if (n == 0) 841cb0ef41Sopenharmony_ci return UV_EIO; 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci return 0; 881cb0ef41Sopenharmony_ci} 89