[git commit] libc: add getrandom(2)

Bernhard Reutner-Fischer rep.dot.nop at gmail.com
Sun Mar 22 13:49:27 UTC 2015


commit: http://git.uclibc.org/uClibc/commit/?id=09ff424905d5de0b2a21a3960d9756a90b07ba26
branch: http://git.uclibc.org/uClibc/commit/?id=refs/heads/master

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 at gmail.com>
---
 Makefile.in                                      |    1 +
 extra/Configs/Config.in                          |    2 +-
 libc/sysdeps/linux/common/Makefile.in            |    1 +
 libc/sysdeps/linux/common/bits/kernel-features.h |    5 +++
 libc/sysdeps/linux/common/getrandom.c            |   14 +++++++++
 libc/sysdeps/linux/common/stubs.c                |    4 ++
 libc/sysdeps/linux/common/sys/random.h           |   33 ++++++++++++++++++++++
 7 files changed, 59 insertions(+), 1 deletions(-)

diff --git a/Makefile.in b/Makefile.in
index 5d60dda..a64fec9 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -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 \
diff --git a/extra/Configs/Config.in b/extra/Configs/Config.in
index f5210cd..35c1cee 100644
--- a/extra/Configs/Config.in
+++ b/extra/Configs/Config.in
@@ -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(),
diff --git a/libc/sysdeps/linux/common/Makefile.in b/libc/sysdeps/linux/common/Makefile.in
index 8ee956b..8252598 100644
--- a/libc/sysdeps/linux/common/Makefile.in
+++ b/libc/sysdeps/linux/common/Makefile.in
@@ -27,6 +27,7 @@ CSRC-$(UCLIBC_LINUX_SPECIFIC) += \
 	eventfd.c \
 	eventfd_read.c \
 	eventfd_write.c \
+	getrandom.c \
 	inotify.c \
 	ioperm.c \
 	iopl.c \
diff --git a/libc/sysdeps/linux/common/bits/kernel-features.h b/libc/sysdeps/linux/common/bits/kernel-features.h
index 6184c2b..708bb49 100644
--- a/libc/sysdeps/linux/common/bits/kernel-features.h
+++ b/libc/sysdeps/linux/common/bits/kernel-features.h
@@ -507,3 +507,8 @@
 #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
index 0000000..d33d522
--- /dev/null
+++ b/libc/sysdeps/linux/common/getrandom.c
@@ -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
diff --git a/libc/sysdeps/linux/common/stubs.c b/libc/sysdeps/linux/common/stubs.c
index 2c50307..19a3371 100644
--- a/libc/sysdeps/linux/common/stubs.c
+++ b/libc/sysdeps/linux/common/stubs.c
@@ -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
index 0000000..42f8025
--- /dev/null
+++ b/libc/sysdeps/linux/common/sys/random.h
@@ -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 */


More information about the uClibc-cvs mailing list