random-devurandom.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 <sys/stat.h>
26 #include <unistd.h>
27 
29 static int status;
30 
31 
32 int uv__random_readpath(const char* path, void* buf, size_t buflen) {
33  struct stat s;
34  size_t pos;
35  ssize_t n;
36  int fd;
37 
38  fd = uv__open_cloexec(path, O_RDONLY);
39 
40  if (fd < 0)
41  return fd;
42 
43  if (fstat(fd, &s)) {
44  uv__close(fd);
45  return UV__ERR(errno);
46  }
47 
48  if (!S_ISCHR(s.st_mode)) {
49  uv__close(fd);
50  return UV_EIO;
51  }
52 
53  for (pos = 0; pos != buflen; pos += n) {
54  do
55  n = read(fd, (char*) buf + pos, buflen - pos);
56  while (n == -1 && errno == EINTR);
57 
58  if (n == -1) {
59  uv__close(fd);
60  return UV__ERR(errno);
61  }
62 
63  if (n == 0) {
64  uv__close(fd);
65  return UV_EIO;
66  }
67  }
68 
69  uv__close(fd);
70  return 0;
71 }
72 
73 
74 static void uv__random_devurandom_init(void) {
75  char c;
76 
77  /* Linux's random(4) man page suggests applications should read at least
78  * once from /dev/random before switching to /dev/urandom in order to seed
79  * the system RNG. Reads from /dev/random can of course block indefinitely
80  * until entropy is available but that's the point.
81  */
82  status = uv__random_readpath("/dev/random", &c, 1);
83 }
84 
85 
86 int uv__random_devurandom(void* buf, size_t buflen) {
88 
89  if (status != 0)
90  return status;
91 
92  return uv__random_readpath("/dev/urandom", buf, buflen);
93 }
uv__random_devurandom_init
static void uv__random_devurandom_init(void)
Definition: random-devurandom.c:74
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
uv__random_devurandom
int uv__random_devurandom(void *buf, size_t buflen)
Definition: random-devurandom.c:86
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv__random_readpath
int uv__random_readpath(const char *path, void *buf, size_t buflen)
Definition: random-devurandom.c:32
uv__open_cloexec
int uv__open_cloexec(const char *path, int flags)
Definition: unix/core.c:991
check_documentation.path
path
Definition: check_documentation.py:57
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
ssize_t
intptr_t ssize_t
Definition: win.h:27
uv_once
UV_EXTERN void uv_once(uv_once_t *guard, void(*callback)(void))
Definition: libuv/src/unix/thread.c:418
uv_once_t
pthread_once_t uv_once_t
Definition: unix.h:133
UV__ERR
#define UV__ERR(x)
Definition: errno.h:29
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
once
static uv_once_t once
Definition: random-devurandom.c:28
uv.h
internal.h
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
UV_ONCE_INIT
#define UV_ONCE_INIT
Definition: unix.h:131
stat
#define stat
Definition: test-fs.c:50
status
static int status
Definition: random-devurandom.c:29
uv__close
int uv__close(int fd)
Definition: unix/core.c:557


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