Ensure that /dev/urandom is a char device.

This commit is contained in:
Frank Denis
2014-05-05 14:39:38 -07:00
parent baa4deb1f4
commit b4e98c0253
2 changed files with 22 additions and 8 deletions
@@ -1,6 +1,7 @@
#include <sys/types.h>
#ifndef _WIN32
# include <sys/stat.h>
# include <sys/time.h>
#endif
@@ -107,17 +108,23 @@ safe_read(const int fd, void * const buf_, size_t count)
static int
randombytes_salsa20_random_random_dev_open(void)
{
static const char * const devices[] = {
struct stat st;
static const char *devices[] = {
# ifndef USE_BLOCKING_RANDOM
"/dev/urandom",
# endif
"/dev/random", NULL
};
const char * const *device = devices;
const char ** device = devices;
int fd;
do {
if (access(*device, F_OK | R_OK) == 0) {
return open(*device, O_RDONLY);
if (access(*device, F_OK | R_OK) == 0 &&
(fd = open(*device, O_RDONLY)) != -1) {
if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) {
return fd;
}
(void) close(fd);
}
device++;
} while (*device != NULL);
@@ -1,6 +1,7 @@
#include <sys/types.h>
#ifndef _WIN32
# include <sys/stat.h>
# include <sys/time.h>
#endif
@@ -99,17 +100,23 @@ safe_read(const int fd, void * const buf_, size_t count)
static int
randombytes_sysrandom_random_dev_open(void)
{
static const char * const devices[] = {
struct stat st;
static const char *devices[] = {
# ifndef USE_BLOCKING_RANDOM
"/dev/urandom",
# endif
"/dev/random", NULL
};
const char * const *device = devices;
const char ** device = devices;
int fd;
do {
if (access(*device, F_OK | R_OK) == 0) {
return open(*device, O_RDONLY);
if (access(*device, F_OK | R_OK) == 0 &&
(fd = open(*device, O_RDONLY)) != -1) {
if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) {
return fd;
}
(void) close(fd);
}
device++;
} while (*device != NULL);