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 <errno.h>
261cb0ef41Sopenharmony_ci#include <string.h>
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci#include <syscall.h>
291cb0ef41Sopenharmony_ci#include <unistd.h>
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cistruct uv__sysctl_args {
331cb0ef41Sopenharmony_ci  int* name;
341cb0ef41Sopenharmony_ci  int nlen;
351cb0ef41Sopenharmony_ci  void* oldval;
361cb0ef41Sopenharmony_ci  size_t* oldlenp;
371cb0ef41Sopenharmony_ci  void* newval;
381cb0ef41Sopenharmony_ci  size_t newlen;
391cb0ef41Sopenharmony_ci  unsigned long unused[4];
401cb0ef41Sopenharmony_ci};
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciint uv__random_sysctl(void* buf, size_t buflen) {
441cb0ef41Sopenharmony_ci  static int name[] = {1 /*CTL_KERN*/, 40 /*KERN_RANDOM*/, 6 /*RANDOM_UUID*/};
451cb0ef41Sopenharmony_ci  struct uv__sysctl_args args;
461cb0ef41Sopenharmony_ci  char uuid[16];
471cb0ef41Sopenharmony_ci  char* p;
481cb0ef41Sopenharmony_ci  char* pe;
491cb0ef41Sopenharmony_ci  size_t n;
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  p = buf;
521cb0ef41Sopenharmony_ci  pe = p + buflen;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  while (p < pe) {
551cb0ef41Sopenharmony_ci    memset(&args, 0, sizeof(args));
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    args.name = name;
581cb0ef41Sopenharmony_ci    args.nlen = ARRAY_SIZE(name);
591cb0ef41Sopenharmony_ci    args.oldval = uuid;
601cb0ef41Sopenharmony_ci    args.oldlenp = &n;
611cb0ef41Sopenharmony_ci    n = sizeof(uuid);
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci    /* Emits a deprecation warning with some kernels but that seems like
641cb0ef41Sopenharmony_ci     * an okay trade-off for the fallback of the fallback: this function is
651cb0ef41Sopenharmony_ci     * only called when neither getrandom(2) nor /dev/urandom are available.
661cb0ef41Sopenharmony_ci     * Fails with ENOSYS on kernels configured without CONFIG_SYSCTL_SYSCALL.
671cb0ef41Sopenharmony_ci     * At least arm64 never had a _sysctl system call and therefore doesn't
681cb0ef41Sopenharmony_ci     * have a SYS__sysctl define either.
691cb0ef41Sopenharmony_ci     */
701cb0ef41Sopenharmony_ci#ifdef SYS__sysctl
711cb0ef41Sopenharmony_ci    if (syscall(SYS__sysctl, &args) == -1)
721cb0ef41Sopenharmony_ci      return UV__ERR(errno);
731cb0ef41Sopenharmony_ci#else
741cb0ef41Sopenharmony_ci    {
751cb0ef41Sopenharmony_ci      (void) &args;
761cb0ef41Sopenharmony_ci      return UV_ENOSYS;
771cb0ef41Sopenharmony_ci    }
781cb0ef41Sopenharmony_ci#endif
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    if (n != sizeof(uuid))
811cb0ef41Sopenharmony_ci      return UV_EIO;  /* Can't happen. */
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci    /* uuid[] is now a type 4 UUID. Bytes 6 and 8 (counting from zero) contain
841cb0ef41Sopenharmony_ci     * 4 and 5 bits of entropy, respectively. For ease of use, we skip those
851cb0ef41Sopenharmony_ci     * and only use 14 of the 16 bytes.
861cb0ef41Sopenharmony_ci     */
871cb0ef41Sopenharmony_ci    uuid[6] = uuid[14];
881cb0ef41Sopenharmony_ci    uuid[8] = uuid[15];
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci    n = pe - p;
911cb0ef41Sopenharmony_ci    if (n > 14)
921cb0ef41Sopenharmony_ci      n = 14;
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci    memcpy(p, uuid, n);
951cb0ef41Sopenharmony_ci    p += n;
961cb0ef41Sopenharmony_ci  }
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  return 0;
991cb0ef41Sopenharmony_ci}
100