diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2019-04-03 10:09:02 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2019-04-03 10:09:02 +0200 |
commit | b66f10141b1062865bc7fa89c5e4ff0664665269 (patch) | |
tree | 8aa864545e7903c103c7d0c6ff3f799da81b88d0 /src/utils.c | |
parent | f615ca91176a486b75bd3b060e4e264fb470c755 (diff) | |
parent | 1c04661dc9b11c6506e96a21e79b3587b4038a28 (diff) |
Merge branch 'master' into release
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/src/utils.c b/src/utils.c index 6188543..a65f947 100644 --- a/src/utils.c +++ b/src/utils.c @@ -53,12 +53,12 @@ #include <stdarg.h> #include <string.h> #include <time.h> -#include <assert.h> #ifdef HAVE_BSD_STDLIB_H #include <bsd/stdlib.h> #endif #ifndef WIN32 +#include <errno.h> #include <syslog.h> #include <sys/types.h> #include <sys/stat.h> @@ -97,7 +97,7 @@ void pt_log(int level, const char *fmt, ...) { } else #endif /* !WIN32 */ - fprintf(opts.log_file, "%s", header[level]), vfprintf(opts.log_file, fmt, args); + fprintf(opts.log_file, "%s", header[level]), vfprintf(opts.log_file, fmt, args); va_end(args); #ifndef WIN32 if (opts.log_file != stdout && !opts.use_syslog) @@ -160,13 +160,28 @@ int pt_random(void) { #ifdef HAVE_ARC4RANDOM return arc4random(); #else -#if defined(HAVE_RANDOM) && !defined(_WIN32) +#if defined(USE_CUSTOMRNG) && !defined(_WIN32) static int rng_fd = -1; + ssize_t bytes_read; int rnd_val; - if (rng_fd < 0) - rng_fd = open("/dev/random", O_RDONLY); - assert(rng_fd >= 0); - assert( read(rng_fd, &rnd_val, sizeof rnd_val) == sizeof rnd_val ); + if (rng_fd < 0) { + rng_fd = open(RNGDEV, O_RDONLY); + if (rng_fd < 0) { + pt_log(kLog_error, "FATAL: Could not open random device '%s': %s\n", + RNGDEV, strerror(errno)); + exit(EXIT_FAILURE); + } + } + bytes_read = read(rng_fd, &rnd_val, sizeof rnd_val); + if (bytes_read != sizeof rnd_val) { + if (bytes_read < 0) + pt_log(kLog_error, "FATAL: Read from random device failed: %s\n", + strerror(errno)); + else + pt_log(kLog_error, "FATAL: Read only %zd bytes (wanted %zd bytes)\n", + bytes_read, sizeof rnd_val); + exit(EXIT_FAILURE); + } return rnd_val; #else srand(time(0)); |