Permalink
Browse files

Fix getrandom() syscall not existing while function exists in header

Found by: jack3
Patch by: Cizzle

On systems running Linux older than 3.17 and using glibc 2.25 or newer, the getrandom() function exists in the system header but the system call does not exist. This checks and skips the call if not found.
  • Loading branch information...
Cizzle authored and vanosg committed Dec 13, 2018
1 parent 35808b2 commit 2962dd20cc174a723a6fa621b8d6e4795b8e345b
Showing with 14 additions and 6 deletions.
  1. +14 −6 src/main.c
@@ -1021,12 +1021,20 @@ int mainloop(int toplevel)
static void init_random(void) {
unsigned int seed;
#ifdef HAVE_GETRANDOM
if (getrandom(&seed, sizeof(seed), 0) != sizeof(seed))
fatal("ERROR: getrandom()\n", 0);
#else
struct timeval tp;
gettimeofday(&tp, NULL);
seed = (tp.tv_sec * tp.tv_usec) ^ getpid();
if (getrandom(&seed, sizeof(seed), 0) != sizeof(seed)) {
if (errno != ENOSYS) {
fatal("ERROR: getrandom()\n", 0);
} else {
/* getrandom() is available in header but syscall is not!
* This can happen with glibc>=2.25 and linux<3.17
*/
#endif
struct timeval tp;
gettimeofday(&tp, NULL);
seed = (tp.tv_sec * tp.tv_usec) ^ getpid();
#ifdef HAVE_GETRANDOM
}
}
#endif
srandom(seed);
}

0 comments on commit 2962dd2

Please sign in to comment.