chiark / gitweb /
util: make use of the new getrandom() syscall if it is available when needing entropy
authorLennart Poettering <lennart@poettering.net>
Wed, 29 Oct 2014 16:06:32 +0000 (17:06 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 29 Oct 2014 16:06:32 +0000 (17:06 +0100)
Doesn't require an fd, and could be a bit faster, so let's make use of
it, if it is available.

configure.ac
src/shared/missing.h
src/shared/util.c

index f8a95cb9c812d4b0db7b55417b731ed31ee00cd0..82bc7a63ff7e0446dfd1b962d362981ce1819849 100644 (file)
@@ -308,7 +308,7 @@ LIBS="$save_LIBS"
 
 AC_CHECK_FUNCS([memfd_create])
 AC_CHECK_FUNCS([__secure_getenv secure_getenv])
-AC_CHECK_DECLS([gettid, pivot_root, name_to_handle_at, setns, LO_FLAGS_PARTSCAN],
+AC_CHECK_DECLS([gettid, pivot_root, name_to_handle_at, setns, getrandom, LO_FLAGS_PARTSCAN],
                [], [], [[
 #include <sys/types.h>
 #include <unistd.h>
@@ -316,6 +316,7 @@ AC_CHECK_DECLS([gettid, pivot_root, name_to_handle_at, setns, LO_FLAGS_PARTSCAN]
 #include <fcntl.h>
 #include <sched.h>
 #include <linux/loop.h>
+#include <linux/random.h>
 ]])
 
 AC_CHECK_DECLS([IFLA_MACVLAN_FLAGS,
index bb4f8f23a8c2d7138de00e9067fa833b0a47b8b9..5b87e23eec311d6d34b3693de361a25a09c3055d 100644 (file)
@@ -140,6 +140,21 @@ static inline int memfd_create(const char *name, unsigned int flags) {
 }
 #endif
 
+#ifndef __NR_getrandom
+#  if defined __x86_64__
+#    define __NR_getrandom 278
+#  else
+#    warning "__NR_getrandom unknown for your architecture"
+#    define __NR_getrandom 0xffffffff
+#  endif
+#endif
+
+#if !HAVE_DECL_GETRANDOM
+static inline int getrandom(void *buffer, size_t count, unsigned flags) {
+        return syscall(__NR_getrandom, buffer, count, flags);
+}
+#endif
+
 #ifndef BTRFS_IOCTL_MAGIC
 #define BTRFS_IOCTL_MAGIC 0x94
 #endif
index 4143f6d643ebca2cb02c105ba0504d1a55d9f72d..ceafba86af4201dcda117aee038ffc37a44f705f 100644 (file)
@@ -2466,14 +2466,37 @@ char* dirname_malloc(const char *path) {
 }
 
 int dev_urandom(void *p, size_t n) {
-        _cleanup_close_ int fd;
+        static int have_syscall = -1;
+        int r, fd;
         ssize_t k;
 
+        /* Use the syscall unless we know we don't have it, or when
+         * the requested size is too large for it. */
+        if (have_syscall != 0 || (size_t) (int) n != n) {
+                r = getrandom(p, n, 0);
+                if (r == (int) n) {
+                        have_syscall = true;
+                        return 0;
+                }
+
+                if (r < 0) {
+                        if (errno == ENOSYS)
+                                /* we lack the syscall, continue with reading from /dev/urandom */
+                                have_syscall = false;
+                        else
+                                return -errno;
+                } else
+                        /* too short read? */
+                        return -EIO;
+        }
+
         fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
         if (fd < 0)
                 return errno == ENOENT ? -ENOSYS : -errno;
 
         k = loop_read(fd, p, n, true);
+        safe_close(fd);
+
         if (k < 0)
                 return (int) k;
         if ((size_t) k != n)