From 252ea41be8bab992dadabec0e4f1d77650949f4c Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 24 Dec 2019 14:01:42 -0800 Subject: fixes #1038 Linux should use getentropy or getrandom --- CMakeLists.txt | 3 +++ src/platform/posix/posix_config.h | 16 +++++++++++++--- tests/testutil.c | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fa3bac8b..abe6586b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -327,6 +327,9 @@ else () nng_check_func(lockf NNG_HAVE_LOCKF) nng_check_func(flock NNG_HAVE_FLOCK) + nng_check_func(getentropy NNG_HAVE_GETENTROPY) + nng_check_func(getrandom NNG_HAVE_GETRANDOM) + nng_check_func(arc4random_buf NNG_HAVE_ARC4RANDOM) nng_check_lib(rt clock_gettime NNG_HAVE_CLOCK_GETTIME) nng_check_lib(pthread sem_wait NNG_HAVE_SEMAPHORE_PTHREAD) diff --git a/src/platform/posix/posix_config.h b/src/platform/posix/posix_config.h index 9afa70c8..2abe51b4 100644 --- a/src/platform/posix/posix_config.h +++ b/src/platform/posix/posix_config.h @@ -1,5 +1,5 @@ // -// Copyright 2016 Garrett D'Amore +// Copyright 2019 Staysail Systems, Inc. // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -36,20 +36,30 @@ // #define NNG_USE_GETENTROPY // #define NNG_USE_ARC4RANDOM // #define NNG_USE_DEVURANDOM -// Thesse are options for obtaining entropy to seed the pRNG. +// These are options for obtaining entropy to seed the pRNG. // All known modern UNIX variants can support NNG_USE_DEVURANDOM, // but the other options are better still, but not portable. #include #ifdef NNG_PLATFORM_POSIX - +#if !defined(NNG_USE_DEVURANDOM) && !defined(NNG_USE_GETENTROPY) && \ + !defined(NNG_USE_GETRANDOM) && !defined(NNG_USE_ARC4RANDOM) +#if defined(NNG_HAVE_GETENTROPY) +#define NNG_USE_GETENTROPY 1 +#elif defined(NNG_HAVE_GETRANDOM) +#define NNG_USE_GETRANDOM 1 +#elif defined(NNG_HAVE_ARC4RANDOM) +#define NNG_USE_ARC4RANDOM +#else // It should never hurt to use DEVURANDOM, since if the device does not // exist then we won't open it. (Provided: it would be bad if the device // exists but has somehow very very different semantics. We don't know // of any such concerns.) This won't be used if any of the other options // are defined and work. #define NNG_USE_DEVURANDOM 1 +#endif +#endif #define NNG_USE_CLOCKID CLOCK_REALTIME #ifndef CLOCK_REALTIME diff --git a/tests/testutil.c b/tests/testutil.c index 77b985a3..ef748b99 100644 --- a/tests/testutil.c +++ b/tests/testutil.c @@ -203,7 +203,7 @@ testutil_next_port(void) (void) UnlockFileEx(h, 0, MAXDWORD, MAXDWORD, &olp); #else fseek(f, 0, SEEK_SET); - ftruncate(fileno(f), 0); + (void) ftruncate(fileno(f), 0); (void) fprintf(f, "%u", port); (void) lockf(fileno(f), 0, F_ULOCK); -- cgit v1.2.3-70-g09d2