Now that we're done with the 'getrandom' module, a fix and a tweak for
the 'getentropy' module.


2020-05-31  Bruno Haible  <br...@clisp.org>

        getentropy: Enhance tests.
        * tests/test-getentropy.c (main): Add one more test.
        * tests/test-unistd-c++.cc: Check the signature of getentropy.

2020-05-31  Bruno Haible  <br...@clisp.org>

        getentropy: Work around a macOS and Solaris problem.
        * lib/unistd.in.h: Include <sys/random.h>, when needed for the
        'getentropy' module.
        * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize
        UNISTD_H_HAVE_SYS_RANDOM_H.
        * m4/sys_random_h.m4 (gl_HEADER_SYS_RANDOM): Set
        UNISTD_H_HAVE_SYS_RANDOM_H.
        * modules/unistd (Makefile.am): Substitute UNISTD_H_HAVE_SYS_RANDOM_H.
        * doc/glibc-functions/getentropy.texi: Document the macOS and Solaris
        problem. List more platforms.

>From 7ed87a1bcaabe12c823b46f67b4e36402581f41e Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sun, 31 May 2020 20:12:04 +0200
Subject: [PATCH 1/2] getentropy: Work around a macOS and Solaris problem.

* lib/unistd.in.h: Include <sys/random.h>, when needed for the
'getentropy' module.
* m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize
UNISTD_H_HAVE_SYS_RANDOM_H.
* m4/sys_random_h.m4 (gl_HEADER_SYS_RANDOM): Set
UNISTD_H_HAVE_SYS_RANDOM_H.
* modules/unistd (Makefile.am): Substitute UNISTD_H_HAVE_SYS_RANDOM_H.
* doc/glibc-functions/getentropy.texi: Document the macOS and Solaris
problem. List more platforms.
---
 ChangeLog                           | 13 +++++++++++++
 doc/glibc-functions/getentropy.texi | 10 +++++-----
 lib/unistd.in.h                     | 11 +++++++++++
 m4/sys_random_h.m4                  |  7 ++++++-
 m4/unistd_h.m4                      |  3 ++-
 modules/unistd                      |  1 +
 6 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ecb8d4b..8f03864 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2020-05-31  Bruno Haible  <br...@clisp.org>
 
+	getentropy: Work around a macOS and Solaris problem.
+	* lib/unistd.in.h: Include <sys/random.h>, when needed for the
+	'getentropy' module.
+	* m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize
+	UNISTD_H_HAVE_SYS_RANDOM_H.
+	* m4/sys_random_h.m4 (gl_HEADER_SYS_RANDOM): Set
+	UNISTD_H_HAVE_SYS_RANDOM_H.
+	* modules/unistd (Makefile.am): Substitute UNISTD_H_HAVE_SYS_RANDOM_H.
+	* doc/glibc-functions/getentropy.texi: Document the macOS and Solaris
+	problem. List more platforms.
+
+2020-05-31  Bruno Haible  <br...@clisp.org>
+
 	getrandom: Doc and test tweaks.
 	* lib/getrandom.c (getrandom): Mention that it never returns 0, and that
 	it sets errno when failing.
diff --git a/doc/glibc-functions/getentropy.texi b/doc/glibc-functions/getentropy.texi
index 5281dcd..b7717e5 100644
--- a/doc/glibc-functions/getentropy.texi
+++ b/doc/glibc-functions/getentropy.texi
@@ -21,13 +21,13 @@ Portability problems fixed by Gnulib:
 @itemize
 @item
 This function is missing on some platforms:
-glibc 2.24, Mac OS X 10.5, FreeBSD 11.0, NetBSD 5.0, OpenBSD 3.8,
-Solaris 11.0, Android 9.0.
+glibc 2.24, Mac OS X 10.11, FreeBSD 11.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.0, Cygwin, mingw, MSVC 14, Android 9.0.
+@item
+This function is declared in @code{<sys/random.h>}, not in @code{<unistd.h>},
+on some platforms:
+Mac OS X 10.13, Solaris 11.4, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on some platforms:
-Minix 3.1.8, IRIX 6.5, mingw, MSVC 14.
 @end itemize
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index 906f806..71904fe 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -118,6 +118,17 @@
 # include <netdb.h>
 #endif
 
+/* Mac OS X 10.13, Solaris 11.4, and Android 9.0 declare getentropy in
+   <sys/random.h>, not in <unistd.h>.  */
+/* But avoid namespace pollution on glibc systems.  */
+#if (@GNULIB_GETENTROPY@ || defined GNULIB_POSIXCHECK) \
+    && ((defined __APPLE__ && defined __MACH__) || defined __sun \
+        || defined __ANDROID__) \
+    && @UNISTD_H_HAVE_SYS_RANDOM_H@ \
+    && !defined __GLIBC__
+# include <sys/random.h>
+#endif
+
 /* Android 4.3 declares fchownat in <sys/stat.h>, not in <unistd.h>.  */
 /* But avoid namespace pollution on glibc systems.  */
 #if (@GNULIB_FCHOWNAT@ || defined GNULIB_POSIXCHECK) && defined __ANDROID__ \
diff --git a/m4/sys_random_h.m4 b/m4/sys_random_h.m4
index c4505b6..a964b15 100644
--- a/m4/sys_random_h.m4
+++ b/m4/sys_random_h.m4
@@ -1,4 +1,4 @@
-# sys_random_h.m4 serial 3
+# sys_random_h.m4 serial 4
 dnl Copyright (C) 2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -16,6 +16,11 @@ AC_DEFUN([gl_HEADER_SYS_RANDOM],
   fi
   AC_SUBST([HAVE_SYS_RANDOM_H])
 
+  m4_ifdef([gl_UNISTD_H_DEFAULTS], [AC_REQUIRE([gl_UNISTD_H_DEFAULTS])])
+  if test $ac_cv_header_sys_random_h = yes; then
+    UNISTD_H_HAVE_SYS_RANDOM_H=1
+  fi
+
   dnl Check for declarations of anything we want to poison if the
   dnl corresponding gnulib module is not in use.
   gl_WARN_ON_USE_PREPARE([[
diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4
index a3b4633..dfa38f8 100644
--- a/m4/unistd_h.m4
+++ b/m4/unistd_h.m4
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 79
+# unistd_h.m4 serial 80
 dnl Copyright (C) 2006-2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -200,6 +200,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   REPLACE_UNLINKAT=0;     AC_SUBST([REPLACE_UNLINKAT])
   REPLACE_USLEEP=0;       AC_SUBST([REPLACE_USLEEP])
   REPLACE_WRITE=0;        AC_SUBST([REPLACE_WRITE])
+  UNISTD_H_HAVE_SYS_RANDOM_H=0; AC_SUBST([UNISTD_H_HAVE_SYS_RANDOM_H])
   UNISTD_H_HAVE_WINSOCK2_H=0; AC_SUBST([UNISTD_H_HAVE_WINSOCK2_H])
   UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS=0;
                            AC_SUBST([UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS])
diff --git a/modules/unistd b/modules/unistd
index f075b78..5550646 100644
--- a/modules/unistd
+++ b/modules/unistd
@@ -174,6 +174,7 @@ unistd.h: unistd.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H
 	      -e 's|@''REPLACE_UNLINKAT''@|$(REPLACE_UNLINKAT)|g' \
 	      -e 's|@''REPLACE_USLEEP''@|$(REPLACE_USLEEP)|g' \
 	      -e 's|@''REPLACE_WRITE''@|$(REPLACE_WRITE)|g' \
+	      -e 's|@''UNISTD_H_HAVE_SYS_RANDOM_H''@|$(UNISTD_H_HAVE_SYS_RANDOM_H)|g' \
 	      -e 's|@''UNISTD_H_HAVE_WINSOCK2_H''@|$(UNISTD_H_HAVE_WINSOCK2_H)|g' \
 	      -e 's|@''UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS''@|$(UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS)|g' \
 	      -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
-- 
2.7.4

>From c73b331059b79e9d53c9c7ad3a905580f292d972 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sun, 31 May 2020 20:13:27 +0200
Subject: [PATCH 2/2] getentropy: Enhance tests.

* tests/test-getentropy.c (main): Add one more test.
* tests/test-unistd-c++.cc: Check the signature of getentropy.
---
 ChangeLog                |  6 ++++++
 tests/test-getentropy.c  | 11 +++++++++++
 tests/test-unistd-c++.cc |  4 ++++
 3 files changed, 21 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 8f03864..fbda5ec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2020-05-31  Bruno Haible  <br...@clisp.org>
 
+	getentropy: Enhance tests.
+	* tests/test-getentropy.c (main): Add one more test.
+	* tests/test-unistd-c++.cc: Check the signature of getentropy.
+
+2020-05-31  Bruno Haible  <br...@clisp.org>
+
 	getentropy: Work around a macOS and Solaris problem.
 	* lib/unistd.in.h: Include <sys/random.h>, when needed for the
 	'getentropy' module.
diff --git a/tests/test-getentropy.c b/tests/test-getentropy.c
index 845c5dc..8659b6d 100644
--- a/tests/test-getentropy.c
+++ b/tests/test-getentropy.c
@@ -39,5 +39,16 @@ main (int argc, char *argv[])
   /* This test fails with probability 2**-2048.  (Run it again if so. :-) */
   ASSERT (memcmp (buf, empty_buf, sizeof buf) != 0);
 
+  /* It is very unlikely that two calls to getentropy produce the same
+     results.  */
+  {
+    char buf1[8];
+    char buf2[8];
+
+    ASSERT (getentropy (buf1, sizeof (buf1)) == 0);
+    ASSERT (getentropy (buf2, sizeof (buf2)) == 0);
+    ASSERT (memcmp (buf1, buf2, sizeof (buf1)) != 0);
+  }
+
   return 0;
 }
diff --git a/tests/test-unistd-c++.cc b/tests/test-unistd-c++.cc
index cf8d3fc..6d4c8fb 100644
--- a/tests/test-unistd-c++.cc
+++ b/tests/test-unistd-c++.cc
@@ -94,6 +94,10 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::getdomainname, int, (char *, size_t));
 SIGNATURE_CHECK (GNULIB_NAMESPACE::getdtablesize, int, (void));
 #endif
 
+#if GNULIB_TEST_GETENTROPY
+SIGNATURE_CHECK (GNULIB_NAMESPACE::getentropy, int, (void *, size_t));
+#endif
+
 #if GNULIB_TEST_GETGROUPS
 SIGNATURE_CHECK (GNULIB_NAMESPACE::getgroups, int, (int, gid_t *));
 #endif
-- 
2.7.4

Reply via email to