urandom fallback

This commit is contained in:
Kent Overstreet 2017-08-15 17:19:06 -06:00
parent 4b9e40b23a
commit 8aaf7d913a
2 changed files with 23 additions and 0 deletions

View File

@ -10,10 +10,19 @@
#include <sys/syscall.h>
#include <linux/bug.h>
#ifdef __NR_getrandom
static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
{
return syscall(SYS_getrandom, buf, buflen, flags);
}
#else
extern int urandom_fd;
static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
{
return read(urandom_fd, buf, buflen);
}
#endif
static inline void get_random_bytes(void *buf, int nbytes)
{

View File

@ -179,3 +179,17 @@ static void sched_init(void)
rcu_init();
rcu_register_thread();
}
#ifndef __NR_getrandom
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
int urandom_fd;
__attribute__((constructor(101)))
static void rand_init(void)
{
urandom_fd = open("/dev/urandom", O_RDONLY);
BUG_ON(urandom_fd < 0);
}
#endif