mirror of
https://github.com/zeromq/libzmq.git
synced 2025-09-17 19:44:01 +02:00
Problem: reading from /dev/urandom is clunky
Solution: if available use the getrandom function as it doesn't require any synchronization, state or cleanup
This commit is contained in:
parent
2626fdfa23
commit
fbb6bbdcb8
@ -331,6 +331,7 @@ if (NOT CMAKE_CROSSCOMPILING)
|
||||
zmq_check_tcp_keepalive ()
|
||||
zmq_check_tcp_tipc ()
|
||||
zmq_check_pthread_setname ()
|
||||
zmq_check_getrandom ()
|
||||
endif ()
|
||||
|
||||
if ( CMAKE_SYSTEM_NAME MATCHES "Linux"
|
||||
|
23
acinclude.m4
23
acinclude.m4
@ -829,6 +829,29 @@ int main (int argc, char *argv [])
|
||||
AS_IF([test "x$libzmq_cv_tcp_keepalive" = "xyes"], [$1], [$2])
|
||||
}])
|
||||
|
||||
dnl ################################################################################
|
||||
dnl # LIBZMQ_CHECK_GETRANDOM([action-if-found], [action-if-not-found]) #
|
||||
dnl # Checks if getrandom is supported #
|
||||
dnl ################################################################################
|
||||
AC_DEFUN([LIBZMQ_CHECK_GETRANDOM], [{
|
||||
AC_CACHE_CHECK([whether getrandom is supported], [libzmq_cv_getrandom],
|
||||
[AC_TRY_RUN([/* thread-local storage test */
|
||||
#include <sys/random.h>
|
||||
|
||||
int main (int argc, char *argv [])
|
||||
{
|
||||
char buf[4];
|
||||
getrandom(buf, 4, 0);
|
||||
}
|
||||
],
|
||||
[libzmq_cv_getrandom="yes"],
|
||||
[libzmq_cv_getrandom="no"],
|
||||
[libzmq_cv_getrandom="not during cross-compile"]
|
||||
)]
|
||||
)
|
||||
AS_IF([test "x$libzmq_cv_getrandom" = "xyes"], [$1], [$2])
|
||||
}])
|
||||
|
||||
dnl ################################################################################
|
||||
dnl # LIBZMQ_CHECK_POLLER_KQUEUE([action-if-found], [action-if-not-found]) #
|
||||
dnl # Checks kqueue polling system #
|
||||
|
@ -245,3 +245,19 @@ int main(int argc, char *argv [])
|
||||
ZMQ_HAVE_PTHREAD_SET_NAME)
|
||||
set(CMAKE_REQUIRED_FLAGS ${SAVE_CMAKE_REQUIRED_FLAGS})
|
||||
endmacro()
|
||||
|
||||
|
||||
macro(zmq_check_getrandom)
|
||||
message(STATUS "Checking whether getrandom is supported")
|
||||
check_c_source_runs(
|
||||
"
|
||||
#include <sys/random.h>
|
||||
|
||||
int main (int argc, char *argv [])
|
||||
{
|
||||
char buf[4];
|
||||
getrandom(buf, 4, 0);
|
||||
}
|
||||
"
|
||||
ZMQ_HAVE_GETRANDOM)
|
||||
endmacro()
|
||||
|
@ -699,6 +699,12 @@ LIBZMQ_CHECK_TCP_KEEPALIVE([
|
||||
[Whether TCP_KEEPALIVE is supported.])
|
||||
])
|
||||
|
||||
LIBZMQ_CHECK_GETRANDOM([
|
||||
AC_DEFINE([ZMQ_HAVE_GETRANDOM],
|
||||
[1],
|
||||
[Whether getrandom is supported.])
|
||||
])
|
||||
|
||||
AM_CONDITIONAL(HAVE_FORK, test "x$ac_cv_func_fork" = "xyes")
|
||||
|
||||
if test "x$cross_compiling" = "xyes"; then
|
||||
|
@ -89,7 +89,7 @@ static zmq::mutex_t random_sync;
|
||||
void zmq::random_open (void)
|
||||
{
|
||||
#if defined (ZMQ_USE_LIBSODIUM) || \
|
||||
(defined (ZMQ_USE_TWEETNACL) && !defined (ZMQ_HAVE_WINDOWS))
|
||||
(defined (ZMQ_USE_TWEETNACL) && !defined (ZMQ_HAVE_WINDOWS) && !defined (ZMQ_HAVE_GETRANDOM))
|
||||
scoped_lock_t locker (random_sync);
|
||||
|
||||
if (random_refcount == 0) {
|
||||
@ -106,7 +106,7 @@ void zmq::random_open (void)
|
||||
void zmq::random_close (void)
|
||||
{
|
||||
#if defined (ZMQ_USE_LIBSODIUM) || \
|
||||
(defined (ZMQ_USE_TWEETNACL) && !defined (ZMQ_HAVE_WINDOWS))
|
||||
(defined (ZMQ_USE_TWEETNACL) && !defined (ZMQ_HAVE_WINDOWS) && !defined (ZMQ_HAVE_GETRANDOM))
|
||||
scoped_lock_t locker (random_sync);
|
||||
--random_refcount;
|
||||
|
||||
|
@ -905,27 +905,38 @@ int sodium_init (void)
|
||||
|
||||
#else
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef ZMQ_HAVE_GETRANDOM
|
||||
#include <sys/random.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
static int fd = -1;
|
||||
#endif
|
||||
|
||||
void randombytes (unsigned char *x,unsigned long long xlen)
|
||||
{
|
||||
int i;
|
||||
#ifndef ZMQ_HAVE_GETRANDOM
|
||||
// Require that random_open has already been called, to avoid
|
||||
// race conditions.
|
||||
assert (fd != -1);
|
||||
#endif
|
||||
while (xlen > 0) {
|
||||
if (xlen < 1048576)
|
||||
i = xlen;
|
||||
else
|
||||
i = 1048576;
|
||||
|
||||
#ifdef ZMQ_HAVE_GETRANDOM
|
||||
i = getrandom (x, i);
|
||||
#else
|
||||
i = read(fd,x,i);
|
||||
#endif
|
||||
if (i < 1) {
|
||||
sleep (1);
|
||||
continue;
|
||||
@ -939,16 +950,19 @@ void randombytes (unsigned char *x,unsigned long long xlen)
|
||||
int randombytes_close (void)
|
||||
{
|
||||
int rc = -1;
|
||||
#ifndef ZMQ_HAVE_GETRANDOM
|
||||
if (fd != -1 && close(fd) == 0) {
|
||||
fd = -1;
|
||||
rc = 0;
|
||||
}
|
||||
#endif // ZMQ_HAVE_GETRANDOM
|
||||
return rc;
|
||||
}
|
||||
|
||||
// Do not call manually! Use random_open from random.hpp
|
||||
int sodium_init (void)
|
||||
{
|
||||
#ifndef ZMQ_HAVE_GETRANDOM
|
||||
if (fd == -1) {
|
||||
for (;;) {
|
||||
int flags = O_RDONLY;
|
||||
@ -965,6 +979,7 @@ int sodium_init (void)
|
||||
assert (rc != -1);
|
||||
#endif
|
||||
}
|
||||
#endif // ZMQ_HAVE_GETRANDOM
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user