From 6de837ba4e208260ac6043d521b0a1d79ffd58a7 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 10:08:07 +0200 Subject: Use arc4random()/getrandom() to get random bytes instead of sysctl() interface --- src/openssl.c | 63 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index d8eebb5..6addcaa 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -48,6 +48,19 @@ #if __APPLE__ #include /* mach_absolute_time() */ +#define HAVE_ARC4RANDOM +#endif + +#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) +#define HAVE_ARC4RANDOM +#endif + +#if defined(__linux__) +#include +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) +#define HAVE_GETRANDOM +#include +#endif #endif #include @@ -7811,44 +7824,16 @@ static struct randL_state *randL_getstate(lua_State *L) { return lua_touserdata(L, lua_upvalueindex(1)); } /* randL_getstate() */ -#ifndef HAVE_SYS_SYSCTL_H -#define HAVE_SYS_SYSCTL_H (BSD || __GLIBC__) -#endif - -#if HAVE_SYS_SYSCTL_H -#include /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */ -#endif - -#ifndef HAVE_RANDOM_UUID -#define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux) /* RANDOM_UUID is an enum, not macro */ -#endif - -#ifndef HAVE_KERN_URND -#define HAVE_KERN_URND (defined KERN_URND) -#endif - -#ifndef HAVE_KERN_ARND -#define HAVE_KERN_ARND (defined KERN_ARND) -#endif static int randL_stir(struct randL_state *st, unsigned rqstd) { unsigned count = 0; int error; unsigned char data[256]; -#if HAVE_RANDOM_UUID || HAVE_KERN_URND || HAVE_KERN_ARND -#if HAVE_RANDOM_UUID - int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; -#elif HAVE_KERN_URND - int mib[] = { CTL_KERN, KERN_URND }; -#else - int mib[] = { CTL_KERN, KERN_ARND }; -#endif - - while (count < rqstd) { +#if HAVE_ARC4RANDOM + while (count < rqst) { size_t n = MIN(rqstd - count, sizeof data); - if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) - break; + arc4random(data, n); RAND_add(data, n, n); @@ -7856,6 +7841,22 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { } #endif +#if HAVE_GETRANDOM + while (count < rqst) { + size_t n = MIN(rqstd - count, sizeof data); + + n = getrandom(data, n, 0); + + if (n == -1) { + break; + } + + RAND_add(data, n, n); + + count += n; + } +#endif + if (count < rqstd) { #if defined O_CLOEXEC && (!defined _AIX /* O_CLOEXEC overflows int */) int fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); -- cgit v1.2.3-59-g8ed1b From 7d6202bfa85e8f0bf90590a9381757f15e296a33 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 10:25:36 +0200 Subject: Reinstate sysctl call for older Linux kernels --- src/openssl.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index 6addcaa..7c28c84 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -60,6 +60,8 @@ #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) #define HAVE_GETRANDOM #include +#else +#define HAVE_SYS_SYSCTL_H #endif #endif @@ -7824,13 +7826,20 @@ static struct randL_state *randL_getstate(lua_State *L) { return lua_touserdata(L, lua_upvalueindex(1)); } /* randL_getstate() */ +#if HAVE_SYS_SYSCTL_H +#include /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */ +#endif + +#ifndef HAVE_RANDOM_UUID +#define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux__) /* RANDOM_UUID is an enum, not macro */ +#endif static int randL_stir(struct randL_state *st, unsigned rqstd) { unsigned count = 0; int error; unsigned char data[256]; -#if HAVE_ARC4RANDOM - while (count < rqst) { +#if defined(HAVE_ARC4RANDOM) + while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); arc4random(data, n); @@ -7839,22 +7848,34 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { count += n; } -#endif - -#if HAVE_GETRANDOM - while (count < rqst) { +#elif defined(HAVE_GETRANDOM) + while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); n = getrandom(data, n, 0); if (n == -1) { - break; + break; } - RAND_add(data, n, n); + RAND_add(data, n, n); + + count += n; + } +#elif HAVE_RANDOM_UUID + int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; + + while (count < rqstd) { + size_t n = MIN(rqstd - count, sizeof data); + + if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) + break; + + RAND_add(data, n, n); count += n; } + #endif if (count < rqstd) { -- cgit v1.2.3-59-g8ed1b From 45dfa8916f911e55b60cb03c1cd0f6cea1141b63 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 10:29:57 +0200 Subject: Use RAND_seed() instead of RAND_add() where appropriate --- src/openssl.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index 7c28c84..9fc4595 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -7844,7 +7844,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { arc4random(data, n); - RAND_add(data, n, n); + RAND_seed(data, n); count += n; } @@ -7858,7 +7858,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { break; } - RAND_add(data, n, n); + RAND_seed(data, n); count += n; } @@ -7871,7 +7871,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) break; - RAND_add(data, n, n); + RAND_seed(data, n); count += n; } @@ -7906,7 +7906,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { goto error; default: - RAND_add(data, n, n); + RAND_seed(data, n); count += n; } -- cgit v1.2.3-59-g8ed1b From 6a6a85b0ecfbe1a68e0b2d86ba1f75a17f8c1598 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 10:41:12 +0200 Subject: Add __DragonFly__ and generic BSD detection for ARC4RANDOM --- src/openssl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index 9fc4595..136110f 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -51,7 +51,7 @@ #define HAVE_ARC4RANDOM #endif -#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) +#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(BSD) #define HAVE_ARC4RANDOM #endif -- cgit v1.2.3-59-g8ed1b From 705053409dade9bc9f691a25f9754a129fa37406 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Tue, 30 Aug 2016 11:18:13 +0200 Subject: Use syscall() to call getrandom() --- src/openssl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index 136110f..c25651b 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -59,6 +59,7 @@ #include #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) #define HAVE_GETRANDOM +#include #include #else #define HAVE_SYS_SYSCTL_H @@ -7852,7 +7853,7 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); - n = getrandom(data, n, 0); + n = syscall(SYS_getrandom, data, n, 0); if (n == -1) { break; -- cgit v1.2.3-59-g8ed1b From 0e2d4c797e6eb22d87621928af252b6264d28ae0 Mon Sep 17 00:00:00 2001 From: William Ahern Date: Wed, 19 Oct 2016 13:10:50 -0700 Subject: use config.h.guess from autoguess project for system feature tests --- GNUmakefile | 3 + config.h.guess | 874 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/GNUmakefile | 6 +- 3 files changed, 881 insertions(+), 2 deletions(-) create mode 100644 config.h.guess (limited to 'src') diff --git a/GNUmakefile b/GNUmakefile index 2955d86..c58686c 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -133,6 +133,9 @@ endif # include $(d)/src/GNUmakefile +$(d)/config.h: $(d)/config.h.guess + $(CP) $< $@ + # # C L E A N R U L E S diff --git a/config.h.guess b/config.h.guess new file mode 100644 index 0000000..c40f6bf --- /dev/null +++ b/config.h.guess @@ -0,0 +1,874 @@ +/* ========================================================================== + * config.h.guess - Preprocessor-based feature detection + * -------------------------------------------------------------------------- + * Copyright (c) 2015-2016 William Ahern + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * ========================================================================== + */ +#ifndef CONFIG_H_GUESS +#define CONFIG_H_GUESS + +/* + * A U T O G U E S S V E R S I O N + * + * Change AG_VENDOR if maintaining a fork. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#define AG_VENDOR "william+autoguess@25thandClement.com" +#define AG_VERSION 20161019L + + +/* + * C O M P I L E R V E N D O R / V E R S I O N D E T E C T I O N + * + * See http://sourceforge.net/p/predef/wiki/Compilers/ + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#define AG_GNUC_2VER(M, m, p) (((M) * 10000) + ((m) * 100) + (p)) +#define AG_GNUC_PREREQ(M, m, p) (__GNUC__ > 0 && AG_GNUC_2VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) >= AG_GNUC_2VER((M), (m), (p))) + +#define AG_MSC_2VER(M, m, p) ((((M) + 6) * 10000000) + ((m) * 1000000) + (p)) +#define AG_MSC_PREREQ(M, m, p) (_MSC_VER_FULL > 0 && _MSC_VER_FULL >= AG_MSC_2VER((M), (m), (p))) + +#define AG_SUNPRO_PREREQ(M, m, p) (__SUNPRO_C > 0 && __SUNPRO_C >= 0x ## M ## m ## p) + + +/* + * C O M P I L E R / L A N G U A G E F E A T U R E D E T E C T I O N + * + * NOTE: The has_ and test_ macros are separate because if the test + * expression uses the preprocessor "defined" operator the operand + * identifier may be replaced before the expression is evaluated. Most tests + * will only use arithmetic operations, but if this is not possible then the + * test must be written inline, for example + * + * #if has_attribute(x) || (!HAVE_C___HAS_ATTRIBUTE && defined FOO) + * #define HAVE___ATTRIBUTE___X + * #endif + * + * NOTE: Solaris Studio 12.4 supports __has_attribute, but we must enclose + * it in parentheses because the expansion results in a token sequence that + * chokes the compiler: __has_attribute(nonnull) becomes + * __has_attribute__ (nonnull), with a literal space between the preprocessor + * identifier and the open parenthesis. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#if defined __has_attribute +#define ag_has_attribute(a) __has_attribute(a) +#define ag_test_attribute(a, E) (ag_has_attribute(a)) +#else +#define ag_has_attribute(a) 0 +#define ag_test_attribute(a, E) (E) +#endif + +#if defined __has_extension +#define ag_has_extension(x) __has_extension(x) +#define ag_test_extension(x, E) (ag_has_extension(x)) +#else +#define ag_has_extension(x) 0 +#define ag_test_extension(x, E) (E) +#endif + +#if defined __has_include +#define ag_has_include(p) __has_include(p) +#define ag_test_include(p, E) (ag_has_include(p)) +#else +#define ag_has_include(p) 0 +#define ag_test_include(p, E) (E) +#endif + +#if defined __has_builtin +#define ag_has_builtin(f) __has_builtin(f) +#define ag_test_builtin(f, E) (ag_has_builtin(f)) +#else +#define ag_has_builtin(f) 0 +#define ag_test_builtin(f, E) (E) +#endif + +#ifndef HAVE_C___ATTRIBUTE__ +#define HAVE_C___ATTRIBUTE__ (__GNUC__ || AG_SUNPRO_PREREQ(5,9,0)) +#endif + +#ifndef HAVE_C___ATTRIBUTE___CONSTRUCTOR +#define HAVE_C___ATTRIBUTE___CONSTRUCTOR ag_test_attribute(constructor, __GNUC__) +#endif + +#ifndef HAVE_C___ATTRIBUTE___NONNULL +#define HAVE_C___ATTRIBUTE___NONNULL ag_test_attribute(nonnull, AG_GNUC_PREREQ(3,3,1)) +#endif + +#ifndef HAVE_C___ATTRIBUTE___UNUSED +#define HAVE_C___ATTRIBUTE___UNUSED ag_test_attribute(unused, __GNUC__) +#endif + +#ifndef HAVE_C___ATTRIBUTE___USED +#define HAVE_C___ATTRIBUTE___USED ag_test_attribute(used, __GNUC__) +#endif + +#ifndef HAVE_C___ATTRIBUTE___VISIBILITY +#define HAVE_C___ATTRIBUTE___VISIBILITY ag_test_attribute(visibility, __GNUC__) +#endif + +#ifndef HAVE_C___HAS_EXTENSION +#define HAVE_C___HAS_EXTENSION (defined __has_extension) +#endif + +#ifndef HAVE_C___HAS_INCLUDE +#define HAVE_C___HAS_INCLUDE (defined __has_include) +#endif + +#ifndef HAVE_C___EXTENSION__ +#define HAVE_C___EXTENSION__ (__GNUC__) +#endif + +#ifndef HAVE_C___TYPEOF +#define HAVE_C___TYPEOF (_MSC_VER || __GNUC__ || AG_SUNPRO_PREREQ(5,9,0)) +#endif + +#ifndef HAVE_C___TYPEOF__ +#define HAVE_C___TYPEOF__ (__GNUC__ || __xlc__ || AG_SUNPRO_PREREQ(5,9,0)) +#endif + +#ifndef HAVE_C__GENERIC +#define HAVE_C__GENERIC ag_test_extension(c_generic_selections, (AG_GNUC_PREREQ(4,9,0) || __STDC_VERSION__ >= 201112L)) +#endif + +#ifndef HAVE_C_STATEMENT_EXPRESSION +#define HAVE_C_STATEMENT_EXPRESSION (__GNUC__ || AG_SUNPRO_PREREQ(5,9,0)) +#endif + +#ifndef HAVE_C_TYPEOF +#define HAVE_C_TYPEOF (__GNUC__ || __xlc__ || AG_SUNPRO_PREREQ(5,9,0)) +#endif + +#ifndef HAVE___ATOMIC_FETCH_ADD +#define HAVE___ATOMIC_FETCH_ADD (defined __ATOMIC_RELAXED) +#endif + +#ifndef HAVE___ATOMIC_FETCH_SUB +#define HAVE___ATOMIC_FETCH_SUB HAVE___ATOMIC_FETCH_ADD +#endif + +#ifndef HAVE___BUILTIN_CHOOSE_EXPR +#define HAVE___BUILTIN_CHOOSE_EXPR (AG_GNUC_PREREQ(3,1,1) || __clang__) +#endif + +#ifndef HAVE___BUILTIN_EXPECT +#define HAVE___BUILTIN_EXPECT ag_test_builtin(__builtin_expect, __GNUC__) +#endif + +#ifndef HAVE___BUILTIN_NAN +#define HAVE___BUILTIN_NAN ag_test_builtin(__builtin_nan, AG_GNUC_PREREQ(3,3,1)) +#endif + +#ifndef HAVE___BUILTIN_TRAP +#define HAVE___BUILTIN_TRAP ag_test_builtin(__builtin_trap, AG_GNUC_PREREQ(3,3,1)) +#endif + +#ifndef HAVE___BUILTIN_TYPES_COMPATIBLE_P +#define HAVE___BUILTIN_TYPES_COMPATIBLE_P (AG_GNUC_PREREQ(3,1,1) || __clang__) +#endif + +#ifndef HAVE___BUILTIN_UNREACHABLE +#define HAVE___BUILTIN_UNREACHABLE ag_test_builtin(__builtin_unreachable, AG_GNUC_PREREQ(4,5,0)) +#endif + +#ifndef HAVE__STATIC_ASSERT +#define HAVE__STATIC_ASSERT ag_test_extension(c_static_assert, (AG_GNUC_PREREQ(4,6,0) || __C11FEATURES__ || __STDC_VERSION__ >= 201112L)) +#endif + + +/* + * S Y S T E M E X T E N S I O N S + * + * We must set these before including any headers for feature detection. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#if AG_USE_SYSTEM_EXTENSIONS + +/* Solaris */ +#ifndef __EXTENSIONS__ +#define __EXTENSIONS__ 1 +#endif + +/* AIX */ +#ifndef _ALL_SOURCE +#define _ALL_SOURCE 1 +#endif + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE 1 +#endif + +#ifndef _MINIX +#define _MINIX 1 +#endif + +/* Solaris */ +#ifndef _POSIX_PTHREAD_SEMANTICS +#define _POSIX_PTHREAD_SEMANTICS 1 +#endif + +#endif /* AG_USE_SYSTEM_EXTENSIONS */ + +#if AG_SYS_LARGEFILE + +/* NOTE: BSDs and musl-libc always provide a 64-bit file API */ + +/* Apple */ +#ifndef _DARWIN_USE_64_BIT_INODE +#define _DARWIN_USE_64_BIT_INODE 1 +#endif + +/* Solaris and glibc (per Large File Summit recommendation) */ +#ifndef _FILE_OFFSET_BITS +#define _FILE_OFFSET_BITS 64 +#endif + +/* AIX */ +#ifndef _LARGE_FILES +#define _LARGE_FILES 1 +#endif + +#endif /* AG_SYS_LARGEFILE */ + + +/* + * S Y S T E M D E T E C T I O N (S T A G E 0) + * + * Define HAVE_FOO macros as arithmetic truth values for any predefined + * system macros which have truth values solely based on whether they're + * defined. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/* NOTE: None so far. See stage 3 below. */ + + +/* + * S Y S T E M D E T E C T I O N (S T A G E 1) + * + * Include any headers necessary for minimal libc feature checking, defining + * any prerequisite feature macros. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/* + * NOTE: will indirectly include , , + * , , , and similar + * system headers which define most of what we care about. Among the typical + * feature macros, we also get _DTRACE_VERSION. + */ +#include + +#ifndef AG_MUSL_MAYBE +#define AG_MUSL_MAYBE (__linux__ && !__GLIBC__ && !__BIONIC__) +#endif + +#ifndef HAVE_SYS_PARAM_H +#define HAVE_SYS_PARAM_H ag_test_include(, !AG_MUSL_MAYBE) +#endif + +/* + * NOTE: Conditionally load so we don't unnecessarily pollute + * the namespace. + */ +#if HAVE_SYS_PARAM_H && !__linux__ && !__sun && !_AIX +#include /* __FreeBSD_version __NetBSD_Prereq__ BSD OpenBSD */ +#endif + +#include /* F_DUPFD_CLOEXEC */ + + +/* + * S Y S T E M D E T E C T I O N (S T A G E 2) + * + * Macros which determine libc vendor and version. + * + * See http://sourceforge.net/p/predef/wiki/Libraries/ + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#define AG_AIX_PREREQ(M, m) (_AIX # M # m) + +#if defined __GLIBC_PREREQ && !defined __UCLIBC__ +#define AG_GLIBC_PREREQ(M, m) (__GLIBC_PREREQ(M, m)) +#else +#define AG_GLIBC_PREREQ(M, m) 0 +#endif + +#define AG_FREEBSD_2VER(M, m, p) (((M) * 100000) + ((m) * 1000) + (p)) +#define AG_FREEBSD_PREREQ(M, m, p) (__FreeBSD__ > 0 && __FreeBSD_version >= AG_FREEBSD_2VER((M), (m), (p))) + +#define AG_IPHONE_2VER(M, m) (((M) * 10000) + ((m) * 100)) +#if defined __IPHONE_OS_VERSION_MIN_REQUIRED +#define AG_IPHONE_PREREQ(M, m) (AG_IPHONE_2VER((M), (m)) <= __IPHONE_OS_VERSION_MIN_REQUIRED) +#else +#define AG_IPHONE_PREREQ(M, m) 0 +#endif + +#if defined __NetBSD_Prereq__ +#define AG_NETBSD_PREREQ(M, m, p) (!__minix && __NetBSD_Prereq__(M, m, p)) +#else +#define AG_NETBSD_PREREQ(M, m, p) 0 +#endif + +#define AG_MACOS_2VER_10_9(M, m, p) (((M) * 100) + ((m) * 10)) +#define AG_MACOS_2VER_10_10(M, m, p) (((M) * 10000) + ((m) * 100) + (p)) +#define AG_MACOS_PREREQ_10_10(M, m, p) (((M) > 10 || ((M) == 10 && (m) >= 10)) && AG_MACOS_2VER_10_10((M), (m), (p)) <= __MAC_OS_X_VERSION_MIN_REQUIRED) +#define AG_MACOS_PREREQ_10_9(M, m, p) (((M) == 10 && (m) < 10) && AG_MACOS_2VER_10_9((M), (m), (p)) <= __MAC_OS_X_VERSION_MIN_REQUIRED) +#if defined __MAC_OS_X_VERSION_MIN_REQUIRED +#define AG_MACOS_PREREQ(M, m, p) (AG_MACOS_PREREQ_10_10((M), (m), (p)) || AG_MACOS_PREREQ_10_9((M), (m), (p))) +#else +#define AG_MACOS_PREREQ(M, m, p) 0 +#endif + +#define AG_OPENBSD_PREREQ_0_0 (__OpenBSD__) +#define AG_OPENBSD_PREREQ_5_5 (OpenBSD >= 201405) +#define AG_OPENBSD_PREREQ_5_7 (OpenBSD >= 201505) +#define AG_OPENBSD_PREREQ(M, m) (AG_OPENBSD_PREREQ_ ## M ## _ ## m) + +#define AG_SUNOS_PREREQ_5_10 (__sun && _DTRACE_VERSION) +#define AG_SUNOS_PREREQ_5_11 (__sun && F_DUPFD_CLOEXEC) +#define AG_SUNOS_PREREQ(M, m) (AG_SUNOS_PREREQ_ ## M ## _ ## m) + +#define AG_UCLIBC_2VER(M, m, p) (((M) * 10000) + ((m) * 100) + (p)) +#if defined __UCLIBC__ +#define AG_UCLIBC_PREREQ(M, m, p) (AG_UCLIBC_2VER(__UCLIBC_MAJOR__, __UCLIBC_MINOR__, __UCLIBC_SUBLEVEL__) >= AG_UCLIBC_2VER((M), (m), (p))) +#else +#define AG_UCLIBC_PREREQ(M, m, p) 0 +#endif + + +/* + * S Y S T E M D E T E C T I O N (S T A G E 3) + * + * Define HAVE_FOO macros as arithmetic truth values for any system macros + * which have a truth value solely based on whether they're defined. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef HAVE___EXTENSIONS__ +#ifdef __EXTENSIONS__ +#define HAVE___EXTENSIONS__ 1 +#endif +#endif + +#ifndef HAVE__ALL_SOURCE +#ifdef _ALL_SOURCE +#define HAVE__ALL_SOURCE 1 +#endif +#endif + +#ifndef HAVE__GNU_SOURCE +#ifdef _GNU_SOURCE +#define HAVE__GNU_SOURCE 1 +#endif +#endif + +#ifndef HAVE__MINIX +#if defined _MINIX || (defined __minix && defined _NETBSD_SOURCE) +#define HAVE__MINIX 1 +#endif +#endif + +#ifndef HAVE__POSIX_PTHREAD_SEMANTICS +#ifdef _POSIX_PTHREAD_SEMANTICS +#define HAVE__POSIX_PTHREAD_SEMANTICS 1 +#endif +#endif + +#ifndef HAVE__REENTRANT +#ifdef _REENTRANT +#define HAVE__REENTRANT 1 +#endif +#endif + + +/* + * H E A D E R D E T E C T I O N + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef HAVE_DLFCN_H +#define HAVE_DLFCN_H ag_test_include(, 1) +#endif + +#ifndef HAVE_IFADDRS_H +#define HAVE_IFADDRS_H_ (!_AIX && (!__sun || AG_SUNOS_PREREQ(5,11))) +#define HAVE_IFADDRS_H ag_test_include(, HAVE_IFADDRS_H_) +#endif + +#ifndef HAVE_INTTYPES_H +#define HAVE_INTTYPES_H 1 +#endif + +#ifndef HAVE_MACH_CLOCK_H +#define HAVE_MACH_CLOCK_H ag_test_include(, __APPLE__) +#endif + +#ifndef HAVE_MACH_MACH_H +#define HAVE_MACH_MACH_H ag_test_include(, __APPLE__) +#endif + +#ifndef HAVE_MACH_MACH_TIME_H +#define HAVE_MACH_MACH_TIME_H ag_test_include(, __APPLE__) +#endif + +#ifndef HAVE_MEMORY_H +#define HAVE_MEMORY_H 1 +#endif + +#ifndef HAVE_PORT_H +#define HAVE_PORT_H ag_test_include(, AG_SUNOS_PREREQ(5,10)) +#endif + +/* TODO: Maybe test _POSIX_THREADS from . */ +#ifndef HAVE_PTHREAD_H +#define HAVE_PTHREAD_H ag_test_include(, !__minix) +#endif + +#ifndef HAVE_STDINT_H +#define HAVE_STDINT_H 1 +#endif + +#ifndef HAVE_STDLIB_H +#define HAVE_STDLIB_H 1 +#endif + +#ifndef HAVE_STRING_H +#define HAVE_STRING_H 1 +#endif + +#ifndef HAVE_STRINGS_H +#define HAVE_STRINGS_H 1 +#endif + +#ifndef HAVE_SYS_AUXV_H +#define HAVE_SYS_AUXV_H_ (AG_GLIBC_PREREQ(2,16) || (!AG_GLIBC_PREREQ(0,0) && __linux__) || __sun) +#define HAVE_SYS_AUXV_H ag_test_include(, HAVE_SYS_AUXV_H_) +#endif + +#ifndef HAVE_SYS_EPOLL_H +#define HAVE_SYS_EPOLL_H ag_test_include(, __linux__) +#endif + +#ifndef HAVE_SYS_EVENT_H +#define HAVE_SYS_EVENT_H ag_test_include(, BSD) +#endif + +#ifndef HAVE_SYS_EVENTFD_H +#define HAVE_SYS_EVENTFD_H_ (AG_GLIBC_PREREQ(2,8) || (!AG_GLIBC_PREREQ(0,0) && __linux__) || defined EFD_CLOEXEC) +#define HAVE_SYS_EVENTFD_H ag_test_include(, HAVE_SYS_EVENTFD_H_) +#endif + +#ifndef HAVE_SYS_INOTIFY_H +#define HAVE_SYS_INOTIFY_H ag_test_include(, __linux__) +#endif + +#ifndef HAVE_SYS_SIGNALFD_H +#define HAVE_SYS_SIGNALFD_H_ (AG_GLIBC_PREREQ(2,8) || (!AG_GLIBC_PREREQ(0,0) && __linux__) || defined SFD_CLOEXEC) +#define HAVE_SYS_SIGNALFD_H ag_test_include(, HAVE_SYS_SIGNALFD_H_) +#endif + +#ifndef HAVE_SYS_SOCKIO_H +#define HAVE_SYS_SOCKIO_H ag_test_include(, (__sun || BSD)) +#endif + +#ifndef HAVE_SYS_STAT_H +#define HAVE_SYS_STAT_H 1 +#endif + +#ifndef HAVE_SYS_SYSCALL_H +#define HAVE_SYS_SYSCALL_H_ (BSD || __linux__ || __sun) +#define HAVE_SYS_SYSCALL_H ag_test_include(, HAVE_SYS_SYSCALL_H_) +#endif + +#ifndef HAVE_SYS_SYSCTL_H +#define HAVE_SYS_SYSCTL_H ag_test_include(, (BSD || __GLIBC__)) +#endif + +#ifndef HAVE_SYS_TIMERFD_H +#define HAVE_SYS_TIMERFD_H_ (AG_GLIBC_PREREQ(2,8) || (!AG_GLIBC_PREREQ(0,0) && __linux__) || defined TFD_CLOEXEC) +#define HAVE_SYS_TIMERFD_H ag_test_include(, HAVE_SYS_TIMERFD_H_) +#endif + +#ifndef HAVE_SYS_TYPES_H +#define HAVE_SYS_TYPES_H 1 +#endif + +#ifndef HAVE_UNISTD_H +#define HAVE_UNISTD_H 1 +#endif + + +/* + * T Y P E D E T E C T I O N + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef HAVE_CLOCKID_T +#define HAVE_CLOCKID_T (defined CLOCK_MONOTONIC) +#endif + +#ifndef HAVE_STRUCT_SOCKADDR_SA_LEN +#define HAVE_STRUCT_SOCKADDR_SA_LEN (!__linux__ && !__sun) +#endif + +#ifndef HAVE_STRUCT_STAT_ST_ATIM +#define HAVE_STRUCT_STAT_ST_ATIM (defined st_atime && ((!__APPLE__ && (!__NetBSD__ || AG_NETBSD_PREREQ(7,0,0))) || !HAVE_STRUCT_STAT_ST_ATIMESPEC)) +#endif + +#ifndef HAVE_STRUCT_STAT_ST_CTIM +#define HAVE_STRUCT_STAT_ST_CTIM HAVE_STRUCT_STAT_ST_ATIM +#endif + +#ifndef HAVE_STRUCT_STAT_ST_MTIM +#define HAVE_STRUCT_STAT_ST_MTIM HAVE_STRUCT_STAT_ST_ATIM +#endif + +#ifndef HAVE_STRUCT_STAT_ST_ATIMESPEC +#define HAVE_STRUCT_STAT_ST_ATIMESPEC (__APPLE__ || defined st_atimespec || defined st_atimensec) +#endif + +#ifndef HAVE_STRUCT_STAT_ST_CTIMESPEC +#define HAVE_STRUCT_STAT_ST_CTIMESPEC HAVE_STRUCT_STAT_ST_ATIMESPEC +#endif + +#ifndef HAVE_STRUCT_STAT_ST_MTIMESPEC +#define HAVE_STRUCT_STAT_ST_MTIMESPEC HAVE_STRUCT_STAT_ST_ATIMESPEC +#endif + +#ifndef HAVE_STRUCT_STAT_ST_BLOCKS +#define HAVE_STRUCT_STAT_ST_BLOCKS 1 +#endif + +#ifndef HAVE_STRUCT_STAT_ST_BLKSIZE +#define HAVE_STRUCT_STAT_ST_BLKSIZE 1 +#endif + +#ifndef HAVE_STRUCT_STAT_ST_RDEV +#define HAVE_STRUCT_STAT_ST_RDEV 1 +#endif + + +/* + * D E C L A R A T I O N D E T E C T I O N + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef HAVE___DECL_LIBC_ENABLE_SECURE +#define HAVE___DECL_LIBC_ENABLE_SECURE 0 +#endif + +#ifndef HAVE_DECL_CLOCK_GETTIME +#define HAVE_DECL_CLOCK_GETTIME HAVE_DECL_CLOCK_MONOTONIC +#endif + +#ifndef HAVE_DECL_CLOCK_MONOTONIC +#define HAVE_DECL_CLOCK_MONOTONIC (defined CLOCK_MONOTONIC) +#endif + +#ifndef HAVE_DECL_CLOCK_REALTIME +#define HAVE_DECL_CLOCK_REALTIME (defined CLOCK_REALTIME) +#endif + +#ifndef HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME +#define HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME (__linux__ && HAVE__GNU_SOURCE) +#endif + +#ifndef HAVE_DECL_PTHREAD_MUTEX_ROBUST +#define HAVE_DECL_PTHREAD_MUTEX_ROBUST (defined PTHREAD_MUTEX_ROBUST || AG_GLIBC_PREREQ(2,12)) +#endif + +#ifndef HAVE_DECL_RANDOM_UUID +#define HAVE_DECL_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux__) /* RANDOM_UUID is an enum, not macro */ +#endif + +#ifndef HAVE_DECL_STRERROR_R +#define HAVE_DECL_STRERROR_R 1 +#endif + +#ifndef HAVE_DECL_SYS_SIGLIST +#define HAVE_DECL_SYS_SIGLIST (!AG_MUSL_MAYBE && !__sun && !_AIX) +#endif + +#ifndef HAVE_DECL_SYS_GETRANDOM +#define HAVE_DECL_SYS_GETRANDOM (defined SYS_getrandom) +#endif + + +/* + * V A R I A B L E D E T E C T I O N + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef HAVE___LIBC_ENABLE_SECURE +#define HAVE___LIBC_ENABLE_SECURE AG_GLIBC_PREREQ(2,1) /* added to glibc between 2.0.98 and 2.0.99 */ +#endif + +#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME +#define HAVE_PROGRAM_INVOCATION_SHORT_NAME (__linux__) +#endif + +#ifndef HAVE_SYS_SIGLIST +#define HAVE_SYS_SIGLIST HAVE_DECL_SYS_SIGLIST +#endif + + +/* + * F U N C T I O N D E T E C T I O N + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef HAVE_ACCEPT4 +#define HAVE_ACCEPT4 (defined SOCK_CLOEXEC && !__NetBSD__) +#endif + +#ifndef HAVE_ARC4RANDOM +#define HAVE_ARC4RANDOM \ + (__APPLE__ || __DragonFly__ || __FreeBSD__ || __NetBSD__ || \ + __OpenBSD__ || __minix) +#endif + +#ifndef HAVE_ARC4RANDOM_ADDRANDOM +#define HAVE_ARC4RANDOM_ADDRANDOM (HAVE_ARC4RANDOM && !AG_OPENBSD_PREREQ(5,5)) +#endif + +#ifndef HAVE_ARC4RANDOM_STIR +#define HAVE_ARC4RANDOM_STIR HAVE_ARC4RANDOM_ADDRANDOM +#endif + +#ifndef HAVE_CLOCK_GETTIME +#define HAVE_CLOCK_GETTIME (!__APPLE__ || AG_MACOS_PREREQ(10,12,0)) +#endif + +#ifndef HAVE_DLADDR +#define HAVE_DLADDR (HAVE_DLOPEN && !_AIX && ((!__GLIBC__ && !AG_MUSL_MAYBE) || HAVE__GNU_SOURCE)) +#endif + +#ifndef HAVE_DLOPEN +#define HAVE_DLOPEN HAVE_DLFCN_H +#endif + +#ifndef HAVE_DLSYM +#define HAVE_DLSYM HAVE_DLOPEN +#endif + +#ifndef HAVE_DUP2 +#define HAVE_DUP2 1 +#endif + +#ifndef HAVE_DUP3 +#define HAVE_DUP3 (AG_GLIBC_PREREQ(2,9) || AG_FREEBSD_PREREQ(10,0,0) || AG_NETBSD_PREREQ(6,0,0) || AG_UCLIBC_PREREQ(0,9,34) || AG_MUSL_MAYBE || __BIONIC__ || AG_OPENBSD_PREREQ(5,7)) +#endif + +#ifndef HAVE_FDOPENDIR +#define HAVE_FDOPENDIR ( \ + (!__APPLE__ || AG_MACOS_PREREQ(10,10,0) || AG_IPHONE_PREREQ(8,0)) \ + && (!__NetBSD__ || AG_NETBSD_PREREQ(6,0,0)) \ +) +#endif + +#ifndef HAVE_EPOLL_CREATE +#define HAVE_EPOLL_CREATE HAVE_SYS_EPOLL_H +#endif + +#if HAVE_SYS_EPOLL_H +#include +#endif + +#ifndef HAVE_EPOLL_CREATE1 +#define HAVE_EPOLL_CREATE1 (HAVE_EPOLL_CREATE && (defined EPOLL_CLOEXEC || AG_GLIBC_PREREQ(2,9))) +#endif + +#ifndef HAVE_EPOLL_CTL +#define HAVE_EPOLL_CTL HAVE_EPOLL_CREATE +#endif + +#ifndef HAVE_EPOLL_PWAIT +#define HAVE_EPOLL_PWAIT (HAVE_EPOLL_WAIT && (AG_GLIBC_PREREQ(2,6) || (!AG_GLIBC_PREREQ(0,0) && defined EPOLL_CLOEXEC))) +#endif + +#ifndef HAVE_EPOLL_WAIT +#define HAVE_EPOLL_WAIT HAVE_EPOLL_CREATE +#endif + +#ifndef HAVE_EVENTFD +#define HAVE_EVENTFD HAVE_SYS_EVENTFD_H +#endif + +#ifndef HAVE_GETAUXVAL +#define HAVE_GETAUXVAL (HAVE_SYS_AUXV_H && !__sun) +#endif + +#ifndef HAVE_GETENV_R +#define HAVE_GETENV_R (AG_NETBSD_PREREQ(4,0,0) || __minix) +#endif + +#ifndef HAVE_GETEXECNAME +#define HAVE_GETEXECNAME (__sun) +#endif + +#ifndef HAVE_GETIFADDRS +#define HAVE_GETIFADDRS (HAVE_IFADDRS_H && !__sun) +#endif + +#ifndef HAVE_GETPROGNAME +#define HAVE_GETPROGNAME (HAVE_ARC4RANDOM || AG_SUNOS_PREREQ(5,11)) +#endif + +#ifndef HAVE_INOTIFY_INIT +#define HAVE_INOTIFY_INIT HAVE_SYS_INOTIFY_H +#endif + +#ifndef HAVE_INOTIFY_INIT1 +#define HAVE_INOTIFY_INIT1 (HAVE_INOTIFY_INIT && defined IN_CLOEXEC) +#endif + +#ifndef HAVE_ISSETUGID +#define HAVE_ISSETUGID ((!__linux__ || (AG_MUSL_MAYBE && HAVE__GNU_SOURCE)) && !_AIX) +#endif + +#if HAVE_SYS_EVENT_H +#include +#endif + +#ifndef HAVE_KEVENT +#define HAVE_KEVENT (defined EV_SET) +#endif + +#ifndef HAVE_KQUEUE +#define HAVE_KQUEUE HAVE_KEVENT +#endif + +#ifndef HAVE_KQUEUE1 +#define HAVE_KQUEUE1 (HAVE_KQUEUE && AG_NETBSD_PREREQ(6,0,0)) +#endif + +#ifndef HAVE_OPENAT +#define HAVE_OPENAT \ + ((!__APPLE__ || AG_MACOS_PREREQ(10,10,0) || AG_IPHONE_PREREQ(8,0)) \ + && (!__NetBSD__ || AG_NETBSD_PREREQ(7,0,0))) +#endif + +#ifndef HAVE_PACCEPT +#define HAVE_PACCEPT AG_NETBSD_PREREQ(6,0,0) +#endif + +#ifndef HAVE_PIPE2 +#define HAVE_PIPE2 (AG_GLIBC_PREREQ(2,9) || AG_FREEBSD_PREREQ(10,0,0) || AG_NETBSD_PREREQ(6,0,0) || AG_UCLIBC_PREREQ(0,9,32) || AG_MUSL_MAYBE || __BIONIC__ || AG_OPENBSD_PREREQ(5,7)) +#endif + +#ifndef HAVE_PORT_ALERT +#define HAVE_PORT_ALERT HAVE_PORT_CREATE +#endif + +#ifndef HAVE_PORT_ASSOCIATE +#define HAVE_PORT_ASSOCIATE HAVE_PORT_CREATE +#endif + +#ifndef HAVE_PORT_CREATE +#define HAVE_PORT_CREATE HAVE_PORT_H +#endif + +#ifndef HAVE_PORT_DISSOCIATE +#define HAVE_PORT_DISSOCIATE HAVE_PORT_CREATE +#endif + +#ifndef HAVE_PORT_GET +#define HAVE_PORT_GET HAVE_PORT_CREATE +#endif + +#ifndef HAVE_PORT_GETN +#define HAVE_PORT_GETN HAVE_PORT_CREATE +#endif + +#ifndef HAVE_PORT_SEND +#define HAVE_PORT_SEND HAVE_PORT_CREATE +#endif + +#ifndef HAVE_PORT_SENDN +#define HAVE_PORT_SENDN HAVE_PORT_CREATE +#endif + +#ifndef HAVE_POSIX_FADVISE +#define HAVE_POSIX_FADVISE (defined POSIX_FADV_NORMAL || AG_GLIBC_PREREQ(2,2) || __sun || AG_MUSL_MAYBE || AG_FREEBSD_PREREQ(9,0,0)) +#endif + +#ifndef HAVE_POSIX_FALLOCATE +#define HAVE_POSIX_FALLOCATE (_AIX || AG_FREEBSD_PREREQ(9,0,0) || AG_GLIBC_PREREQ(2,2) || AG_MUSL_MAYBE || AG_NETBSD_PREREQ(7,0,0) || __sun) +#endif + +#ifndef HAVE_SIGNALFD +#define HAVE_SIGNALFD HAVE_SYS_SIGNALFD_H +#endif + +#ifndef HAVE_SIGTIMEDWAIT +#define HAVE_SIGTIMEDWAIT (!__APPLE__ && !__OpenBSD__) +#endif + +#ifndef HAVE_SIGWAIT +#define HAVE_SIGWAIT (!__minix) +#endif + +#ifndef HAVE_STATIC_ASSERT +#if AG_GLIBC_PREREQ(0,0) && !HAVE__STATIC_ASSERT +#define HAVE_STATIC_ASSERT 0 /* glibc doesn't check GCC version */ +#else +#define HAVE_STATIC_ASSERT (defined static_assert) +#endif +#endif + +#ifndef HAVE_STRERROR_R +#define HAVE_STRERROR_R 1 +#endif + +#ifndef HAVE_SYSCALL +#define HAVE_SYSCALL HAVE_SYS_SYSCALL_H +#endif + +#ifndef HAVE_SYSCTL +#define HAVE_SYSCTL HAVE_SYS_SYSCTL_H +#endif + +#ifndef HAVE_TIMERFD_CREATE +#define HAVE_TIMERFD_CREATE HAVE_SYS_TIMERFD_H +#endif + +#ifndef HAVE_TIMERFD_GETTIME +#define HAVE_TIMERFD_GETTIME HAVE_TIMERFD_CREATE +#endif + +#ifndef HAVE_TIMERFD_SETTIME +#define HAVE_TIMERFD_SETTIME HAVE_TIMERFD_CREATE +#endif + +#ifndef STRERROR_R_CHAR_P +#define STRERROR_R_CHAR_P ((AG_GLIBC_PREREQ(0,0) || AG_UCLIBC_PREREQ(0,0,0)) && (HAVE__GNU_SOURCE || !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600))) +#endif + + +#endif /* CONFIG_H_GUESS */ diff --git a/src/GNUmakefile b/src/GNUmakefile index 34d9cd2..e7cb54d 100644 --- a/src/GNUmakefile +++ b/src/GNUmakefile @@ -20,7 +20,7 @@ OS_$(d) = $(shell $(d)/../mk/vendor.os) CC_$(d) = $(shell env CC="$(CC) "$(d)/../mk/vendor.cc) LUAPATH_$(d) = $(shell env CC="$(CC)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" $( Date: Wed, 19 Oct 2016 13:15:48 -0700 Subject: there are no kernel headers installed by default in Alpine Linux (Linux/musl) so use alternate feature test; support running binaries built with getrandom on older kernels with only sysctl(RANDOM_UUID); change preprocessor test for feature macros so they can be easily overridden with build flags; and remove feature macro definitions in favor of those in config.h (config.h.guess) --- src/openssl.c | 56 +++++++++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index c25651b..78dbd65 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -23,6 +23,10 @@ * USE OR OTHER DEALINGS IN THE SOFTWARE. * ========================================================================== */ +#if HAVE_CONFIG_H +#include "config.h" +#endif + #include /* INT_MAX INT_MIN LLONG_MAX LLONG_MIN UCHAR_MAX ULLONG_MAX */ #include /* uintptr_t */ #include /* memset(3) strerror_r(3) */ @@ -48,22 +52,6 @@ #if __APPLE__ #include /* mach_absolute_time() */ -#define HAVE_ARC4RANDOM -#endif - -#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(BSD) -#define HAVE_ARC4RANDOM -#endif - -#if defined(__linux__) -#include -#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) -#define HAVE_GETRANDOM -#include -#include -#else -#define HAVE_SYS_SYSCTL_H -#endif #endif #include @@ -95,10 +83,6 @@ #define LIBRESSL_PREREQ(M, m, p) \ (LIBRESSL_VERSION_NUMBER >= (((M) << 28) | ((m) << 20) | ((p) << 12))) -#ifndef HAVE_DLADDR -#define HAVE_DLADDR (!defined _AIX) /* TODO: https://root.cern.ch/drupal/content/aix-and-dladdr */ -#endif - #ifndef HAVE_SSL_CTX_SET_ALPN_PROTOS #define HAVE_SSL_CTX_SET_ALPN_PROTOS OPENSSL_PREREQ(1, 0, 2) #endif @@ -7827,19 +7811,20 @@ static struct randL_state *randL_getstate(lua_State *L) { return lua_touserdata(L, lua_upvalueindex(1)); } /* randL_getstate() */ -#if HAVE_SYS_SYSCTL_H -#include /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */ +#if HAVE_SYS_SYSCALL_H +#include /* SYS_getrandom syscall(2) */ #endif -#ifndef HAVE_RANDOM_UUID -#define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux__) /* RANDOM_UUID is an enum, not macro */ +#if HAVE_SYS_SYSCTL_H +#include /* CTL_KERN KERN_RANDOM RANDOM_UUID sysctl(2) */ #endif static int randL_stir(struct randL_state *st, unsigned rqstd) { unsigned count = 0; int error; unsigned char data[256]; -#if defined(HAVE_ARC4RANDOM) + +#if HAVE_ARC4RANDOM while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); @@ -7849,11 +7834,14 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { count += n; } -#elif defined(HAVE_GETRANDOM) +#endif + +#if HAVE_SYSCALL && HAVE_DECL_SYS_GETRANDOM while (count < rqstd) { - size_t n = MIN(rqstd - count, sizeof data); + size_t lim = MIN(rqstd - count, sizeof data); + int n; - n = syscall(SYS_getrandom, data, n, 0); + n = syscall(SYS_getrandom, data, lim, 0); if (n == -1) { break; @@ -7863,10 +7851,11 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { count += n; } -#elif HAVE_RANDOM_UUID - int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; +#endif +#if HAVE_SYS_SYSCTL_H && HAVE_DECL_RANDOM_UUID while (count < rqstd) { + int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; size_t n = MIN(rqstd - count, sizeof data); if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) @@ -7945,7 +7934,12 @@ error:; #elif defined __sun /* * NOTE: Linux requires -lrt for clock_gettime, and in any event - * already has RANDOM_UUID. The BSDs have KERN_URND and KERN_ARND. + * should have RANDOM_UUID or getrandom. (Though, some middle-aged + * kernels might have neither). The BSDs have arc4random which + * should be using KERN_URND, KERN_ARND, and more recently + * getentropy. (Though, again, some older BSD kernels used an + * arc4random implementation that opened /dev/urandom.) + * * Just do this for Solaris to keep things simple. We've already * crossed the line of what can be reasonably accomplished on * unreasonable platforms. -- cgit v1.2.3-59-g8ed1b