random-sysctl-linux.c
Go to the documentation of this file.
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 
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 }
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
memset
return memset(p, 0, total)
uv__sysctl_args::oldlenp
size_t * oldlenp
Definition: random-sysctl-linux.c:36
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv__sysctl_args::name
int * name
Definition: random-sysctl-linux.c:33
uv__sysctl_args::nlen
int nlen
Definition: random-sysctl-linux.c:34
uv__sysctl_args::unused
unsigned long unused[4]
Definition: random-sysctl-linux.c:39
setup.name
name
Definition: setup.py:542
xds_manager.p
p
Definition: xds_manager.py:60
uv__sysctl_args::oldval
void * oldval
Definition: random-sysctl-linux.c:35
uv__sysctl_args
Definition: random-sysctl-linux.c:32
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
uv__sysctl_args::newval
void * newval
Definition: random-sysctl-linux.c:37
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
uv__random_sysctl
int uv__random_sysctl(void *buf, size_t buflen)
Definition: random-sysctl-linux.c:43
syscall
const char * syscall
Definition: third_party/libuv/src/win/internal.h:270
UV__ERR
#define UV__ERR(x)
Definition: errno.h:29
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
uv.h
internal.h
uv__sysctl_args::newlen
size_t newlen
Definition: random-sysctl-linux.c:38
errno.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:59