]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
urandom fallback
authorKent Overstreet <kent.overstreet@gmail.com>
Tue, 15 Aug 2017 23:19:06 +0000 (17:19 -0600)
committerKent Overstreet <kent.overstreet@gmail.com>
Wed, 23 Aug 2017 22:29:28 +0000 (16:29 -0600)
include/linux/random.h
linux/sched.c

index bd3dc61b598a6dc5dde792a1c4488ea71d3fbf11..95a15d059039378b3adb8e908726b3ff9b24a524 100644 (file)
 #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)
 {
index 898ccb19ec2ce470e64d4524d7456050c78ab20c..cc614b123ba36408176b70293204ec5e7d0725b6 100644 (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