On platforms which do have a <sys/random.h> that does not declare
getrandom() - such as Mac OS X 10.5 or HP-UX 11, gnulib should not override
the system's <sys/random.h>, but augment it. Otherwise we hide system
functions that the user may want to use.

Also, the getrandom() declaration lacks an 'extern "C"' marker in C++ mode.

Also, in getrandom.m4, we must make sure that the initialization of the
variable HAVE_GETRANDOM precedes the HAVE_GETRANDOM=0 statement.

So, let's use the standard idioms.


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

        sys_random: Add C++ tests.
        * tests/test-sys_random-c++.cc: New file.
        * modules/sys_random-c++-tests: New file.
        * modules/sys_random-tests (Depends-on): Depend on it.

        sys_random: Add tests.
        * tests/test-sys_random.c: New file.
        * modules/sys_random-tests: New file.

        sys_random: New module.
        * lib/sys_random.in.h: Use the common idioms for overridable header
        files.
        * m4/sys_random_h.m4: New file.
        * m4/getrandom.m4 (gl_FUNC_GETRANDOM): Require gl_SYS_RANDOM_H_DEFAULTS.
        * modules/sys_random: New file.
        * modules/getrandom (Files): Remove lib/sys_random.in.h.
        (Depends-on): Add sys_random.
        (configure.ac): Use gl_SYS_RANDOM_MODULE_INDICATOR, not
        gl_UNISTD_MODULE_INDICATOR.
        (Makefile.am): Don't generate sys/random.h here.
        * doc/glibc-headers/sys_random.texi: New file.
        * doc/gnulib.texi: Include it.

>From d1b9cea779c7e11af92c88cd546412ecc2a7fcaa Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 30 May 2020 16:18:36 +0200
Subject: [PATCH 1/3] sys_random: New module.

* lib/sys_random.in.h: Use the common idioms for overridable header
files.
* m4/sys_random_h.m4: New file.
* m4/getrandom.m4 (gl_FUNC_GETRANDOM): Require gl_SYS_RANDOM_H_DEFAULTS.
* modules/sys_random: New file.
* modules/getrandom (Files): Remove lib/sys_random.in.h.
(Depends-on): Add sys_random.
(configure.ac): Use gl_SYS_RANDOM_MODULE_INDICATOR, not
gl_UNISTD_MODULE_INDICATOR.
(Makefile.am): Don't generate sys/random.h here.
* doc/glibc-headers/sys_random.texi: New file.
* doc/gnulib.texi: Include it.
---
 ChangeLog                         | 16 +++++++++
 doc/glibc-headers/sys_random.texi | 33 ++++++++++++++++++
 doc/gnulib.texi                   |  2 ++
 lib/sys_random.in.h               | 73 ++++++++++++++++++++++++++++++++++++---
 m4/getrandom.m4                   |  3 +-
 m4/sys_random_h.m4                | 43 +++++++++++++++++++++++
 modules/getrandom                 | 17 ++-------
 modules/sys_random                | 51 +++++++++++++++++++++++++++
 8 files changed, 217 insertions(+), 21 deletions(-)
 create mode 100644 doc/glibc-headers/sys_random.texi
 create mode 100644 m4/sys_random_h.m4
 create mode 100644 modules/sys_random

diff --git a/ChangeLog b/ChangeLog
index 2cb7edd..3c4dd93 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
 2020-05-30  Bruno Haible  <br...@clisp.org>
 
+	sys_random: New module.
+	* lib/sys_random.in.h: Use the common idioms for overridable header
+	files.
+	* m4/sys_random_h.m4: New file.
+	* m4/getrandom.m4 (gl_FUNC_GETRANDOM): Require gl_SYS_RANDOM_H_DEFAULTS.
+	* modules/sys_random: New file.
+	* modules/getrandom (Files): Remove lib/sys_random.in.h.
+	(Depends-on): Add sys_random.
+	(configure.ac): Use gl_SYS_RANDOM_MODULE_INDICATOR, not
+	gl_UNISTD_MODULE_INDICATOR.
+	(Makefile.am): Don't generate sys/random.h here.
+	* doc/glibc-headers/sys_random.texi: New file.
+	* doc/gnulib.texi: Include it.
+
+2020-05-30  Bruno Haible  <br...@clisp.org>
+
 	unistd: Remove conflicting declaration of getrandom().
 	* lib/unistd.in.h (getrandom): Remove declaration.
 	* m4/unistd_h.m4 (gl_UNISTD_H): Don't test whether getrandom is
diff --git a/doc/glibc-headers/sys_random.texi b/doc/glibc-headers/sys_random.texi
new file mode 100644
index 0000000..1c28595
--- /dev/null
+++ b/doc/glibc-headers/sys_random.texi
@@ -0,0 +1,33 @@
+@node sys/random.h
+@section @file{sys/random.h}
+
+Declares the function @code{getrandom} and the flags for it.
+
+Documentation:
+@itemize
+@item
+@ifinfo
+@ref{Unpredictable Bytes,,Generating Unpredictable Bytes,libc},
+@end ifinfo
+@ifnotinfo
+@url{https://www.gnu.org/software/libc/manual/html_node/Unpredictable-Bytes.html},
+@end ifnotinfo
+@item
+@uref{https://www.kernel.org/doc/man-pages/online/pages/man2/getrandom.2.html,,man getrandom}.
+@end itemize
+
+Gnulib module: sys_random
+
+Portability problems fixed by Gnulib:
+@itemize
+@item
+This header file is missing on some platforms:
+glibc 2.24, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.11, IRIX 6.5, Cygwin, mingw, MSVC 14.
+@item
+This header file does not declare the @code{getrandom} function on some platforms:
+Mac OS X 10.5, FreeBSD 11.0, HP-UX 11.31, Solaris 11.0.
+@end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
+@end itemize
diff --git a/doc/gnulib.texi b/doc/gnulib.texi
index 3719978..fd9d9b0 100644
--- a/doc/gnulib.texi
+++ b/doc/gnulib.texi
@@ -3684,6 +3684,7 @@ not worked around by Gnulib.
 * resolv.h::
 * shadow.h::
 * sys/ioctl.h::
+* sys/random.h::
 * sysexits.h::
 * ttyent.h::
 @end menu
@@ -3718,6 +3719,7 @@ not worked around by Gnulib.
 @include glibc-headers/resolv.texi
 @include glibc-headers/shadow.texi
 @include glibc-headers/sys_ioctl.texi
+@include glibc-headers/sys_random.texi
 @include glibc-headers/sysexits.texi
 @include glibc-headers/ttyent.texi
 
diff --git a/lib/sys_random.in.h b/lib/sys_random.in.h
index e3f308c..3997cf4 100644
--- a/lib/sys_random.in.h
+++ b/lib/sys_random.in.h
@@ -1,9 +1,72 @@
-#ifndef _SYS_RANDOM_H
-#define _SYS_RANDOM_H 1
+/* Substitute for <sys/random.h>.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
+
+# if __GNUC__ >= 3
+@PRAGMA_SYSTEM_HEADER@
+# endif
+@PRAGMA_COLUMNS@
+
+#ifndef _@GUARD_PREFIX@_SYS_RANDOM_H
+
+#if @HAVE_SYS_RANDOM_H@
+
+/* The include_next requires a split double-inclusion guard.  */
+# @INCLUDE_NEXT@ @NEXT_SYS_RANDOM_H@
+
+#endif
+
+#ifndef _@GUARD_PREFIX@_SYS_RANDOM_H
+#define _@GUARD_PREFIX@_SYS_RANDOM_H
 
 #include <sys/types.h>
-#define GRND_NONBLOCK 1
-#define GRND_RANDOM 2
-ssize_t getrandom (void *, size_t, unsigned int);
 
+/* Define the GRND_* constants.  */
+#ifndef GRND_NONBLOCK
+# define GRND_NONBLOCK 1
+# define GRND_RANDOM 2
 #endif
+
+/* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
+
+/* The definition of _GL_ARG_NONNULL is copied here.  */
+
+/* The definition of _GL_WARN_ON_USE is copied here.  */
+
+
+/* Declare overridden functions.  */
+
+
+#if @GNULIB_GETRANDOM@
+/* Fill a buffer with random bytes.  */
+# if !@HAVE_GETRANDOM@
+_GL_FUNCDECL_SYS (getrandom, ssize_t,
+                  (void *buffer, size_t length, unsigned int flags)
+                  _GL_ARG_NONNULL ((1)));
+# endif
+_GL_CXXALIAS_SYS (getrandom, ssize_t,
+                  (void *buffer, size_t length, unsigned int flags));
+_GL_CXXALIASWARN (getrandom);
+#elif defined GNULIB_POSIXCHECK
+# undef getrandom
+# if HAVE_RAW_DECL_GETRANDOM
+_GL_WARN_ON_USE (getrandom, "getrandom is unportable - "
+                 "use gnulib module getrandom for portability");
+# endif
+#endif
+
+
+#endif /* _@GUARD_PREFIX@_SYS_RANDOM_H */
+#endif /* _@GUARD_PREFIX@_SYS_RANDOM_H */
diff --git a/m4/getrandom.m4 b/m4/getrandom.m4
index ecb44ae..79e5520 100644
--- a/m4/getrandom.m4
+++ b/m4/getrandom.m4
@@ -1,4 +1,4 @@
-# getrandom.m4
+# getrandom.m4 serial 2
 dnl Copyright 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,
@@ -8,6 +8,7 @@ dnl Written by Paul Eggert.
 
 AC_DEFUN([gl_FUNC_GETRANDOM],
 [
+  AC_REQUIRE([gl_SYS_RANDOM_H_DEFAULTS])
   AC_CHECK_FUNCS_ONCE([getrandom])
   if test "$ac_cv_func_getrandom" != yes; then
     HAVE_GETRANDOM=0
diff --git a/m4/sys_random_h.m4 b/m4/sys_random_h.m4
new file mode 100644
index 0000000..d78307f
--- /dev/null
+++ b/m4/sys_random_h.m4
@@ -0,0 +1,43 @@
+# sys_random_h.m4 serial 1
+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,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_HEADER_SYS_RANDOM],
+[
+  AC_REQUIRE([gl_SYS_RANDOM_H_DEFAULTS])
+  dnl <sys/random.h> is always overridden, because of GNULIB_POSIXCHECK.
+  gl_CHECK_NEXT_HEADERS([sys/random.h])
+  if test $ac_cv_header_sys_random_h = yes; then
+    HAVE_SYS_RANDOM_H=1
+  else
+    HAVE_SYS_RANDOM_H=0
+  fi
+  AC_SUBST([HAVE_SYS_RANDOM_H])
+
+  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([[
+#if HAVE_SYS_RANDOM_H
+# include <sys/random.h>
+#endif
+    ]],
+    [getrandom])
+])
+
+AC_DEFUN([gl_SYS_RANDOM_MODULE_INDICATOR],
+[
+  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
+  AC_REQUIRE([gl_SYS_RANDOM_H_DEFAULTS])
+  gl_MODULE_INDICATOR_SET_VARIABLE([$1])
+  dnl Define it also as a C macro, for the benefit of the unit tests.
+  gl_MODULE_INDICATOR_FOR_TESTS([$1])
+])
+
+AC_DEFUN([gl_SYS_RANDOM_H_DEFAULTS],
+[
+  GNULIB_GETRANDOM=0;     AC_SUBST([GNULIB_GETRANDOM])
+  dnl Assume proper GNU behavior unless another module says otherwise.
+  HAVE_GETRANDOM=1;       AC_SUBST([HAVE_GETRANDOM])
+])
diff --git a/modules/getrandom b/modules/getrandom
index 2ae374f..f7b3ff9 100644
--- a/modules/getrandom
+++ b/modules/getrandom
@@ -3,10 +3,10 @@ Fill buffer with random bytes.
 
 Files:
 lib/getrandom.c
-lib/sys_random.in.h
 m4/getrandom.m4
 
 Depends-on:
+sys_random
 crypto/gc-random    [test $HAVE_GETRANDOM = 0]
 fcntl-h             [test $HAVE_GETRANDOM = 0]
 minmax              [test $HAVE_GETRANDOM = 0]
@@ -17,22 +17,9 @@ gl_FUNC_GETRANDOM
 if test $HAVE_GETRANDOM = 0; then
   AC_LIBOBJ([getrandom])
 fi
-gl_UNISTD_MODULE_INDICATOR([getrandom])
+gl_SYS_RANDOM_MODULE_INDICATOR([getrandom])
 
 Makefile.am:
-BUILT_SOURCES += sys/random.h
-
-# We need the following in order to create <sys/random.h> when the system
-# doesn't have one that works with the given compiler.
-sys/random.h: sys_random.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) $(ARG_NONNULL_H)
-	$(AM_V_at)$(MKDIR_P) sys
-	$(AM_V_GEN)rm -f $@-t $@ && \
-	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
-	  cat $(srcdir)/sys_random.in.h; \
-	} > $@-t && \
-	mv -f $@-t $@
-MOSTLYCLEANFILES += sys/random.h sys/random.h-t
-MOSTLYCLEANDIRS += sys
 
 Include:
 <sys/random.h>
diff --git a/modules/sys_random b/modules/sys_random
new file mode 100644
index 0000000..1d0eb7c
--- /dev/null
+++ b/modules/sys_random
@@ -0,0 +1,51 @@
+Description:
+A GNU-like <sys/random.h>.
+
+Files:
+lib/sys_random.in.h
+m4/sys_random_h.m4
+
+Depends-on:
+include_next
+snippet/arg-nonnull
+snippet/c++defs
+snippet/warn-on-use
+
+configure.ac:
+gl_HEADER_SYS_RANDOM
+AC_PROG_MKDIR_P
+
+Makefile.am:
+BUILT_SOURCES += sys/random.h
+
+# We need the following in order to create <sys/random.h> when the system
+# doesn't have one.
+sys/random.h: sys_random.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
+	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_GEN)rm -f $@-t $@ && \
+	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
+	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
+	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
+	      -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
+	      -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
+	      -e 's|@''NEXT_SYS_RANDOM_H''@|$(NEXT_SYS_RANDOM_H)|g' \
+	      -e 's|@''HAVE_SYS_RANDOM_H''@|$(HAVE_SYS_RANDOM_H)|g' \
+	      -e 's/@''GNULIB_GETRANDOM''@/$(GNULIB_GETRANDOM)/g' \
+	      -e 's/@''HAVE_GETRANDOM''@/$(HAVE_GETRANDOM)/g' \
+	      -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
+	      -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
+	      -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
+	      < $(srcdir)/sys_random.in.h; \
+	} > $@-t && \
+	mv -f $@-t $@
+MOSTLYCLEANFILES += sys/random.h sys/random.h-t
+MOSTLYCLEANDIRS += sys
+
+Include:
+<sys/random.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all
-- 
2.7.4

>From 7168ecc9cc88793fedca103ae8fc7be56349ca41 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 30 May 2020 16:20:32 +0200
Subject: [PATCH 2/3] sys_random: Add tests.

* tests/test-sys_random.c: New file.
* modules/sys_random-tests: New file.
---
 ChangeLog                |  4 ++++
 modules/sys_random-tests | 10 ++++++++++
 tests/test-sys_random.c  | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 modules/sys_random-tests
 create mode 100644 tests/test-sys_random.c

diff --git a/ChangeLog b/ChangeLog
index 3c4dd93..f458e9d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2020-05-30  Bruno Haible  <br...@clisp.org>
 
+	sys_random: Add tests.
+	* tests/test-sys_random.c: New file.
+	* modules/sys_random-tests: New file.
+
 	sys_random: New module.
 	* lib/sys_random.in.h: Use the common idioms for overridable header
 	files.
diff --git a/modules/sys_random-tests b/modules/sys_random-tests
new file mode 100644
index 0000000..24fea63
--- /dev/null
+++ b/modules/sys_random-tests
@@ -0,0 +1,10 @@
+Files:
+tests/test-sys_random.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-sys_random
+check_PROGRAMS += test-sys_random
diff --git a/tests/test-sys_random.c b/tests/test-sys_random.c
new file mode 100644
index 0000000..56da27b
--- /dev/null
+++ b/tests/test-sys_random.c
@@ -0,0 +1,32 @@
+/* Test of <sys/random.h> substitute.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <sys/random.h>
+
+/* Check that the necessary constants are defined.  */
+int flags[] =
+  {
+    GRND_RANDOM,
+    GRND_NONBLOCK
+  };
+
+int
+main (void)
+{
+  return 0;
+}
-- 
2.7.4

>From 9e7d098310117f0b17e7868124652a392b0a9854 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 30 May 2020 16:22:25 +0200
Subject: [PATCH 3/3] sys_random: Add C++ tests.

* tests/test-sys_random-c++.cc: New file.
* modules/sys_random-c++-tests: New file.
* modules/sys_random-tests (Depends-on): Depend on it.
---
 ChangeLog                    |  5 +++++
 modules/sys_random-c++-tests | 18 ++++++++++++++++++
 modules/sys_random-tests     |  1 +
 tests/test-sys_random-c++.cc | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 59 insertions(+)
 create mode 100644 modules/sys_random-c++-tests
 create mode 100644 tests/test-sys_random-c++.cc

diff --git a/ChangeLog b/ChangeLog
index f458e9d..c5a3bd5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2020-05-30  Bruno Haible  <br...@clisp.org>
 
+	sys_random: Add C++ tests.
+	* tests/test-sys_random-c++.cc: New file.
+	* modules/sys_random-c++-tests: New file.
+	* modules/sys_random-tests (Depends-on): Depend on it.
+
 	sys_random: Add tests.
 	* tests/test-sys_random.c: New file.
 	* modules/sys_random-tests: New file.
diff --git a/modules/sys_random-c++-tests b/modules/sys_random-c++-tests
new file mode 100644
index 0000000..e07d81f
--- /dev/null
+++ b/modules/sys_random-c++-tests
@@ -0,0 +1,18 @@
+Files:
+tests/test-sys_random-c++.cc
+tests/signature.h
+
+Status:
+c++-test
+
+Depends-on:
+ansi-c++-opt
+
+configure.ac:
+
+Makefile.am:
+if ANSICXX
+TESTS += test-sys_random-c++
+check_PROGRAMS += test-sys_random-c++
+test_sys_random_c___SOURCES = test-sys_random-c++.cc
+endif
diff --git a/modules/sys_random-tests b/modules/sys_random-tests
index 24fea63..2208c5a 100644
--- a/modules/sys_random-tests
+++ b/modules/sys_random-tests
@@ -2,6 +2,7 @@ Files:
 tests/test-sys_random.c
 
 Depends-on:
+sys_random-c++-tests
 
 configure.ac:
 
diff --git a/tests/test-sys_random-c++.cc b/tests/test-sys_random-c++.cc
new file mode 100644
index 0000000..7477b3a
--- /dev/null
+++ b/tests/test-sys_random-c++.cc
@@ -0,0 +1,35 @@
+/* Test of <sys/random.h> substitute in C++ mode.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <br...@clisp.org>, 2020.  */
+
+#define GNULIB_NAMESPACE gnulib
+#include <config.h>
+
+#include <sys/random.h>
+
+#include "signature.h"
+
+
+#if GNULIB_TEST_GETRANDOM
+SIGNATURE_CHECK (GNULIB_NAMESPACE::getrandom, ssize_t, (void *, size_t, unsigned int));
+#endif
+
+
+int
+main ()
+{
+}
-- 
2.7.4

Reply via email to