forked from cloudius-systems/osv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.c
51 lines (44 loc) · 1.32 KB
/
random.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright (C) 2018 Waldemar Kozaczuk
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/random.h>
#define RANDOM_PATH "/dev/random"
//
// This implementation is based on the specification described at http://man7.org/linux/man-pages/man2/getrandom.2.html
// with following limitations:
//
// - given /dev/random and /dev/urandom provide same behavior in OSv (backed by the same code per drivers/random.cc),
// getrandom does not read flags argument to differentiate quality of random data returned
// - does not check if random source data is available
// - does not differentiate between blocking vs non-blocking behavior
// - return ENOSYS when GRND_NONBLOCK passed in flags
ssize_t getrandom(void *buf, size_t count, unsigned int flags)
{
int fd;
ssize_t read;
if(flags & GRND_NONBLOCK) {
errno = ENOSYS;
return -1;
}
fd = open(RANDOM_PATH, O_RDONLY);
if (fd < 0) {
errno = EAGAIN;
return -1;
}
memset(buf, 0, count);
read = pread(fd, buf, count, 0);
if(read <= 0) {
errno = EAGAIN;
close(fd);
return -1;
}
close(fd);
return read;
}