diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2017-08-15 17:19:06 -0600 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@gmail.com> | 2017-08-23 16:29:28 -0600 |
commit | 8aaf7d913a382291f2770e6ce72a8919d5441bef (patch) | |
tree | 17eab2a1ccfbb90a78b377482adc48bade2b9734 | |
parent | 4b9e40b23ad171cb1120a2befc3c2eb4a144ed63 (diff) |
urandom fallback
-rw-r--r-- | include/linux/random.h | 9 | ||||
-rw-r--r-- | linux/sched.c | 14 |
2 files changed, 23 insertions, 0 deletions
diff --git a/include/linux/random.h b/include/linux/random.h index bd3dc61b..95a15d05 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -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) { diff --git a/linux/sched.c b/linux/sched.c index 898ccb19..cc614b12 100644 --- a/linux/sched.c +++ b/linux/sched.c @@ -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 |