>From d1b9cea779c7e11af92c88cd546412ecc2a7fcaa Mon Sep 17 00:00:00 2001 From: Bruno Haible 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 + 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 + 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 . + 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 . */ + +# 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 -#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 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 +#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 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: 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 . + +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 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: + + +License: +LGPLv2+ + +Maintainer: +all -- 2.7.4