5 #ifndef HAVE_GETENTROPY
6 static int random_fd = -1;
9 void random_init(void) {
10 #ifndef HAVE_GETENTROPY
11 random_fd = open("/dev/urandom", O_RDONLY);
14 random_fd = open("/dev/random", O_RDONLY);
18 fprintf(stderr, "Could not open source of random numbers: %s\n", strerror(errno));
25 void random_exit(void) {
26 #ifndef HAVE_GETENTROPY
31 void randomize(void *vout, size_t outlen) {
35 #ifdef HAVE_GETENTROPY
36 int reqlen = (int) MIN(256, outlen);
37 int len = !getentropy(out, reqlen) ? reqlen : -1;
39 ssize_t len = read(random_fd, out, outlen);
43 if(len == -1 && (errno == EAGAIN || errno == EINTR)) {
47 fprintf(stderr, "Could not read random numbers: %s\n", strerror(errno));