Rizin
unix-like reverse engineering framework and cli tools
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 
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 }
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset fstat
Definition: sflib.h:107
#define UV__ERR(x)
Definition: errno.h:29
voidpf void * buf
Definition: ioapi.h:138
int n
Definition: mipsasm.c:19
int uv__random_readpath(const char *path, void *buf, size_t buflen)
static int status
static uv_once_t once
static void uv__random_devurandom_init(void)
int uv__random_devurandom(void *buf, size_t buflen)
static RzSocket * s
Definition: rtr.c:28
#define EINTR
Definition: sftypes.h:114
#define O_RDONLY
Definition: sftypes.h:486
int ssize_t
Definition: sftypes.h:39
#define c(i)
Definition: sha256.c:43
Definition: sftypes.h:80
int pos
Definition: main.c:11
int uv__open_cloexec(const char *path, int flags)
Definition: core.c:1003
int uv__close(int fd)
Definition: core.c:569
ut64 buflen
Definition: core.c:76
pthread_once_t uv_once_t
Definition: unix.h:135
#define UV_ONCE_INIT
Definition: unix.h:133
UV_EXTERN void uv_once(uv_once_t *guard, void(*callback)(void))
Definition: thread.c:419
static const z80_opcode fd[]
Definition: z80_tab.h:997
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115