Commit fbb6bbdc authored by Luca Boccassi's avatar Luca Boccassi

Problem: reading from /dev/urandom is clunky

Solution: if available use the getrandom function as it doesn't
require any synchronization, state or cleanup
parent 2626fdfa
...@@ -331,6 +331,7 @@ if (NOT CMAKE_CROSSCOMPILING) ...@@ -331,6 +331,7 @@ if (NOT CMAKE_CROSSCOMPILING)
zmq_check_tcp_keepalive () zmq_check_tcp_keepalive ()
zmq_check_tcp_tipc () zmq_check_tcp_tipc ()
zmq_check_pthread_setname () zmq_check_pthread_setname ()
zmq_check_getrandom ()
endif () endif ()
if ( CMAKE_SYSTEM_NAME MATCHES "Linux" if ( CMAKE_SYSTEM_NAME MATCHES "Linux"
......
...@@ -829,6 +829,29 @@ int main (int argc, char *argv []) ...@@ -829,6 +829,29 @@ int main (int argc, char *argv [])
AS_IF([test "x$libzmq_cv_tcp_keepalive" = "xyes"], [$1], [$2]) 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 ################################################################################
dnl # LIBZMQ_CHECK_POLLER_KQUEUE([action-if-found], [action-if-not-found]) # dnl # LIBZMQ_CHECK_POLLER_KQUEUE([action-if-found], [action-if-not-found]) #
dnl # Checks kqueue polling system # dnl # Checks kqueue polling system #
......
...@@ -245,3 +245,19 @@ int main(int argc, char *argv []) ...@@ -245,3 +245,19 @@ int main(int argc, char *argv [])
ZMQ_HAVE_PTHREAD_SET_NAME) ZMQ_HAVE_PTHREAD_SET_NAME)
set(CMAKE_REQUIRED_FLAGS ${SAVE_CMAKE_REQUIRED_FLAGS}) set(CMAKE_REQUIRED_FLAGS ${SAVE_CMAKE_REQUIRED_FLAGS})
endmacro() 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([ ...@@ -699,6 +699,12 @@ LIBZMQ_CHECK_TCP_KEEPALIVE([
[Whether TCP_KEEPALIVE is supported.]) [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") AM_CONDITIONAL(HAVE_FORK, test "x$ac_cv_func_fork" = "xyes")
if test "x$cross_compiling" = "xyes"; then if test "x$cross_compiling" = "xyes"; then
......
...@@ -89,7 +89,7 @@ static zmq::mutex_t random_sync; ...@@ -89,7 +89,7 @@ static zmq::mutex_t random_sync;
void zmq::random_open (void) void zmq::random_open (void)
{ {
#if defined (ZMQ_USE_LIBSODIUM) || \ #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); scoped_lock_t locker (random_sync);
if (random_refcount == 0) { if (random_refcount == 0) {
...@@ -106,7 +106,7 @@ void zmq::random_open (void) ...@@ -106,7 +106,7 @@ void zmq::random_open (void)
void zmq::random_close (void) void zmq::random_close (void)
{ {
#if defined (ZMQ_USE_LIBSODIUM) || \ #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); scoped_lock_t locker (random_sync);
--random_refcount; --random_refcount;
......
...@@ -905,27 +905,38 @@ int sodium_init (void) ...@@ -905,27 +905,38 @@ int sodium_init (void)
#else #else
#include <unistd.h>
#include <assert.h>
#ifdef ZMQ_HAVE_GETRANDOM
#include <sys/random.h>
#else
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h>
#include <assert.h>
static int fd = -1; static int fd = -1;
#endif
void randombytes (unsigned char *x,unsigned long long xlen) void randombytes (unsigned char *x,unsigned long long xlen)
{ {
int i; int i;
#ifndef ZMQ_HAVE_GETRANDOM
// Require that random_open has already been called, to avoid // Require that random_open has already been called, to avoid
// race conditions. // race conditions.
assert (fd != -1); assert (fd != -1);
#endif
while (xlen > 0) { while (xlen > 0) {
if (xlen < 1048576) if (xlen < 1048576)
i = xlen; i = xlen;
else else
i = 1048576; i = 1048576;
#ifdef ZMQ_HAVE_GETRANDOM
i = getrandom (x, i);
#else
i = read(fd,x,i); i = read(fd,x,i);
#endif
if (i < 1) { if (i < 1) {
sleep (1); sleep (1);
continue; continue;
...@@ -939,16 +950,19 @@ void randombytes (unsigned char *x,unsigned long long xlen) ...@@ -939,16 +950,19 @@ void randombytes (unsigned char *x,unsigned long long xlen)
int randombytes_close (void) int randombytes_close (void)
{ {
int rc = -1; int rc = -1;
#ifndef ZMQ_HAVE_GETRANDOM
if (fd != -1 && close(fd) == 0) { if (fd != -1 && close(fd) == 0) {
fd = -1; fd = -1;
rc = 0; rc = 0;
} }
#endif // ZMQ_HAVE_GETRANDOM
return rc; return rc;
} }
// Do not call manually! Use random_open from random.hpp // Do not call manually! Use random_open from random.hpp
int sodium_init (void) int sodium_init (void)
{ {
#ifndef ZMQ_HAVE_GETRANDOM
if (fd == -1) { if (fd == -1) {
for (;;) { for (;;) {
int flags = O_RDONLY; int flags = O_RDONLY;
...@@ -965,6 +979,7 @@ int sodium_init (void) ...@@ -965,6 +979,7 @@ int sodium_init (void)
assert (rc != -1); assert (rc != -1);
#endif #endif
} }
#endif // ZMQ_HAVE_GETRANDOM
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment