diff --git a/cmd/ztest.c b/cmd/ztest.c index d94451906..dc8ac85b6 100644 --- a/cmd/ztest.c +++ b/cmd/ztest.c @@ -8952,6 +8952,12 @@ main(int argc, char **argv) libspl_init(); + /* + * Force random_get_bytes() to use /dev/urandom in order to prevent + * ztest from needlessly depleting the system entropy pool. + */ + random_force_pseudo(B_TRUE); + if (!fd_data_str) { process_options(argc, argv); diff --git a/lib/libspl/include/sys/random.h b/lib/libspl/include/sys/random.h index 27f2d4e3a..d11580829 100644 --- a/lib/libspl/include/sys/random.h +++ b/lib/libspl/include/sys/random.h @@ -32,6 +32,8 @@ extern int random_get_bytes(uint8_t *ptr, size_t len); extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len); +extern void random_force_pseudo(boolean_t onoff); + static __inline__ uint32_t random_in_range(uint32_t range) { diff --git a/lib/libspl/random.c b/lib/libspl/random.c index 30281504f..c6f0ee7ae 100644 --- a/lib/libspl/random.c +++ b/lib/libspl/random.c @@ -37,6 +37,8 @@ static int random_fd = -1, urandom_fd = -1; +static boolean_t force_pseudo = B_FALSE; + void random_init(void) { @@ -60,6 +62,12 @@ random_fini(void) urandom_fd = -1; } +void +random_force_pseudo(boolean_t onoff) +{ + force_pseudo = onoff; +} + static int random_get_bytes_common(uint8_t *ptr, size_t len, int fd) { @@ -81,6 +89,8 @@ random_get_bytes_common(uint8_t *ptr, size_t len, int fd) int random_get_bytes(uint8_t *ptr, size_t len) { + if (force_pseudo) + return (random_get_pseudo_bytes(ptr, len)); return (random_get_bytes_common(ptr, len, random_fd)); }