Skip to content

Commit

Permalink
Fix getrandom() syscall not existing while function exists in header
Browse files Browse the repository at this point in the history
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.
(cherry picked from commit 2962dd2)
  • Loading branch information
Cizzle authored and vanosg committed Dec 15, 2018
1 parent 502b56f commit e9d0a43
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit e9d0a43

Please sign in to comment.