OSDN Git Service

libc: add getrandom(2)
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Sun, 22 Mar 2015 13:47:29 +0000 (14:47 +0100)
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Sun, 22 Mar 2015 13:49:27 +0000 (14:49 +0100)
Introduce a <sys/random.h> for it.

/* FIXME: aren't there a couple of __restrict and const missing ?
 */
extern int getrandom(void *__buf, size_t count, unsigned int flags)
    __nonnull ((1)) __wur;

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Makefile.in
extra/Configs/Config.in
libc/sysdeps/linux/common/Makefile.in
libc/sysdeps/linux/common/bits/kernel-features.h
libc/sysdeps/linux/common/getrandom.c [new file with mode: 0644]
libc/sysdeps/linux/common/stubs.c
libc/sysdeps/linux/common/sys/random.h [new file with mode: 0644]

index 5d60dda..a64fec9 100644 (file)
@@ -310,6 +310,7 @@ HEADERS_RM-$(UCLIBC_LINUX_SPECIFIC)          += \
        sys/perm.h \
        sys/personality.h \
        sys/prctl.h \
+       sys/random.h \
        sys/reboot.h \
        sys/sendfile.h \
        bits/signalfd.h \
index f5210cd..35c1cee 100644 (file)
@@ -1025,7 +1025,7 @@ config UCLIBC_LINUX_SPECIFIC
        help
          accept4(), bdflush(),
          capget(), capset(), eventfd(), fallocate(),
-         fstatfs(), inotify_*(), ioperm(), iopl(),
+         fstatfs(), getrandom(), inotify_*(), ioperm(), iopl(),
          madvise(), modify_ldt(), pipe2(), personality(),
          prctl()/arch_prctl(), pivot_root(), modify_ldt(),
          ppoll(), readahead(), reboot(), remap_file_pages(),
index 8ee956b..8252598 100644 (file)
@@ -27,6 +27,7 @@ CSRC-$(UCLIBC_LINUX_SPECIFIC) += \
        eventfd.c \
        eventfd_read.c \
        eventfd_write.c \
+       getrandom.c \
        inotify.c \
        ioperm.c \
        iopl.c \
index 6184c2b..708bb49 100644 (file)
 #if defined __x86_64__ && __LINUX_KERNEL_VERSION >= 0x030100
 # define __ASSUME_GETCPU_SYSCALL        1
 #endif
+
+/* getrandom syscall (widely) appeared around 4.0.0 */
+#if __LINUX_KERNEL_VERSION >= 0x040000
+# define __ASSUME_GETRANDOM_SYSCALL 1
+#endif
diff --git a/libc/sysdeps/linux/common/getrandom.c b/libc/sysdeps/linux/common/getrandom.c
new file mode 100644 (file)
index 0000000..d33d522
--- /dev/null
@@ -0,0 +1,14 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * getrandom() for uClibc
+ *
+ * Copyright (C) 2015 Bernhard Reutner-Fischer
+ *
+ * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
+ */
+
+#include <sys/syscall.h>
+#include <sys/random.h>
+#ifdef __NR_getrandom
+_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
+#endif
index 2c50307..19a3371 100644 (file)
@@ -157,6 +157,10 @@ make_stub(getpeername)
 make_stub(getpgrp)
 #endif
 
+#if !defined __NR_getrandom && defined __UCLIBC_LINUX_SPECIFIC__
+make_stub(getrandom)
+#endif
+
 #if !defined __NR_getsockname && !defined __NR_socketcall && defined __UCLIBC_HAS_SOCKET__
 make_stub(getsockname)
 #endif
diff --git a/libc/sysdeps/linux/common/sys/random.h b/libc/sysdeps/linux/common/sys/random.h
new file mode 100644 (file)
index 0000000..42f8025
--- /dev/null
@@ -0,0 +1,33 @@
+/* Copyright (C) 2015 Bernhard Reutner-Fischer
+ * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
+*/
+
+#ifndef        _SYS_RANDOM_H
+#define        _SYS_RANDOM_H   1
+#include <features.h>
+
+__BEGIN_DECLS
+
+#if defined __UCLIBC_LINUX_SPECIFIC__ && defined __USE_GNU
+# if 0 /*def __ASSUME_GETRANDOM_SYSCALL */
+#  include <linux/random.h>
+# else
+#  undef GRND_NONBLOCK
+#  undef GRND_RANDOM
+/*
+ * Flags for getrandom(2)
+ *
+ * GRND_NONBLOCK       Don't block and return EAGAIN instead
+ * GRND_RANDOM         Use the /dev/random pool instead of /dev/urandom
+ */
+#  define GRND_NONBLOCK        0x0001
+#  define GRND_RANDOM  0x0002
+# endif
+/* FIXME: aren't there a couple of __restrict and const missing ? */
+extern int getrandom(void *__buf, size_t count, unsigned int flags)
+          __nonnull ((1)) __wur;
+#endif
+
+__END_DECLS
+
+#endif /* sys/random.h */