29#if defined(HAVE_SYS_RANDOM_H) && defined(HAVE_GETENTROPY)
30#include <sys/random.h>
37#ifdef FREECIV_MSWINDOWS
60 if (getentropy(&seed,
sizeof(seed)) == 0) {
65 log_error(
_(
"getentropy() failed: %s"), strerror(errno));
79#ifdef HAVE_BCRYPTGENRANDOM
82 Status = BCryptGenRandom(NULL, (PUCHAR)ret,
sizeof(
randseed),
83 BCRYPT_USE_SYSTEM_PREFERRED_RNG);
85 if (NT_SUCCESS(Status)) {
100#if HAVE_USABLE_URANDOM
110 static const char *random_device =
"/dev/urandom";
117 fd = open(random_device, O_RDONLY);
120 log_warn(
_(
"Opening %s failed: %s"), random_device, strerror(errno));
122 int n = read(fd, &seed,
sizeof(seed));
125 log_warn(
_(
"Reading %s failed: %s"), random_device, strerror(errno));
126 }
else if (
n !=
sizeof(seed)) {
127 log_warn(
_(
"Reading %s: short read without error"), random_device);
150#if HAVE_CLOCK_GETTIME
161 if (clock_gettime(CLOCK_REALTIME, &tp) == 0) {
169 log_error(
_(
"clock_gettime(CLOCK_REALTIME) failed: %s"), strerror(errno));
198 log_debug(
"Got random seed from getentropy()");
202 log_debug(
"Got random seed from BCryptGenRandom()");
206 log_debug(
"Got random seed from urandom()");
211 log_normal(
_(
"No good random source usable. Falling back to time-based random seeding."));
214 log_debug(
"Got random seed with clock_gettime()");
220 log_warn(
_(
"Falling back to predictable random seed from current coarse-granularity time."));
222 log_debug(
"Got random seed from time()");
#define log_warn(message,...)
#define log_debug(message,...)
#define log_normal(message,...)
#define log_error(message,...)
static bool generate_seed_getentropy(randseed *ret)
static bool generate_seed_clock_gettime(randseed *ret)
static bool generate_seed_bcryptgenrandom(randseed *ret)
randseed generate_game_seed(void)
static bool generate_seed_urandom(randseed *ret)
static bool generate_seed_time(randseed *ret)