Mercurial
comparison third_party/libuv/src/unix/random-sysctl-linux.c @ 160:948de3f54cea
[ThirdParty] Added libuv
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 14 Jan 2026 19:39:52 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 159:05cf9467a1c3 | 160:948de3f54cea |
|---|---|
| 1 /* Copyright libuv contributors. All rights reserved. | |
| 2 * | |
| 3 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 4 * of this software and associated documentation files (the "Software"), to | |
| 5 * deal in the Software without restriction, including without limitation the | |
| 6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
| 7 * sell copies of the Software, and to permit persons to whom the Software is | |
| 8 * furnished to do so, subject to the following conditions: | |
| 9 * | |
| 10 * The above copyright notice and this permission notice shall be included in | |
| 11 * all copies or substantial portions of the Software. | |
| 12 * | |
| 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
| 19 * IN THE SOFTWARE. | |
| 20 */ | |
| 21 | |
| 22 #include "uv.h" | |
| 23 #include "internal.h" | |
| 24 | |
| 25 #include <errno.h> | |
| 26 #include <string.h> | |
| 27 | |
| 28 #include <syscall.h> | |
| 29 #include <unistd.h> | |
| 30 | |
| 31 | |
| 32 struct uv__sysctl_args { | |
| 33 int* name; | |
| 34 int nlen; | |
| 35 void* oldval; | |
| 36 size_t* oldlenp; | |
| 37 void* newval; | |
| 38 size_t newlen; | |
| 39 unsigned long unused[4]; | |
| 40 }; | |
| 41 | |
| 42 | |
| 43 int uv__random_sysctl(void* buf, size_t buflen) { | |
| 44 static int name[] = {1 /*CTL_KERN*/, 40 /*KERN_RANDOM*/, 6 /*RANDOM_UUID*/}; | |
| 45 struct uv__sysctl_args args; | |
| 46 char uuid[16]; | |
| 47 char* p; | |
| 48 char* pe; | |
| 49 size_t n; | |
| 50 | |
| 51 p = buf; | |
| 52 pe = p + buflen; | |
| 53 | |
| 54 while (p < pe) { | |
| 55 memset(&args, 0, sizeof(args)); | |
| 56 | |
| 57 args.name = name; | |
| 58 args.nlen = ARRAY_SIZE(name); | |
| 59 args.oldval = uuid; | |
| 60 args.oldlenp = &n; | |
| 61 n = sizeof(uuid); | |
| 62 | |
| 63 /* Emits a deprecation warning with some kernels but that seems like | |
| 64 * an okay trade-off for the fallback of the fallback: this function is | |
| 65 * only called when neither getrandom(2) nor /dev/urandom are available. | |
| 66 * Fails with ENOSYS on kernels configured without CONFIG_SYSCTL_SYSCALL. | |
| 67 * At least arm64 never had a _sysctl system call and therefore doesn't | |
| 68 * have a SYS__sysctl define either. | |
| 69 */ | |
| 70 #ifdef SYS__sysctl | |
| 71 if (syscall(SYS__sysctl, &args) == -1) | |
| 72 return UV__ERR(errno); | |
| 73 #else | |
| 74 { | |
| 75 (void) &args; | |
| 76 return UV_ENOSYS; | |
| 77 } | |
| 78 #endif | |
| 79 | |
| 80 if (n != sizeof(uuid)) | |
| 81 return UV_EIO; /* Can't happen. */ | |
| 82 | |
| 83 /* uuid[] is now a type 4 UUID. Bytes 6 and 8 (counting from zero) contain | |
| 84 * 4 and 5 bits of entropy, respectively. For ease of use, we skip those | |
| 85 * and only use 14 of the 16 bytes. | |
| 86 */ | |
| 87 uuid[6] = uuid[14]; | |
| 88 uuid[8] = uuid[15]; | |
| 89 | |
| 90 n = pe - p; | |
| 91 if (n > 14) | |
| 92 n = 14; | |
| 93 | |
| 94 memcpy(p, uuid, n); | |
| 95 p += n; | |
| 96 } | |
| 97 | |
| 98 return 0; | |
| 99 } |