Skip to content

Commit d549f50

Browse files
authored
Merge pull request #90 from jvoisin/old_glibc
Use getrandom on linux when not using old glibc
2 parents cdd5e39 + 31b96f1 commit d549f50

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/iso_alloc_random.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
/* Contributed by Oscar Reparaz (@oreparaz)
55
* https://github.com/struct/isoalloc/pull/5 */
66

7-
#if __linux__
8-
#define GRND_NONBLOCK 0x0001
7+
#include "iso_alloc_internal.h"
8+
9+
#define OLD_GLIBC (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 24)
10+
11+
#if OLD_GLIBC
12+
#include <linux/random.h>
913
#include <sys/syscall.h>
1014
#elif __APPLE__
1115
#include <Security/SecRandom.h>
12-
#elif __FreeBSD__
16+
#elif __FreeBSD__ || __linux__
1317
#include <sys/random.h>
1418
#else
1519
#error "unknown OS"
1620
#endif
17-
#include "iso_alloc_internal.h"
1821

1922
INTERNAL_HIDDEN uint64_t rand_uint64(void) {
2023
uint64_t val = 0;
@@ -25,11 +28,11 @@ INTERNAL_HIDDEN uint64_t rand_uint64(void) {
2528
* Use the raw system call as a lower common denominator.
2629
* We give up on checking the return value. The alternative would be
2730
* to crash. We prefer here to keep going with degraded randomness. */
28-
#if __linux__
31+
#if OLD_GLIBC
2932
ret = syscall(SYS_getrandom, &val, sizeof(val), GRND_NONBLOCK) != sizeof(val);
3033
#elif __APPLE__
3134
ret = SecRandomCopyBytes(kSecRandomDefault, sizeof(val), &val);
32-
#elif __FreeBSD__
35+
#elif __FreeBSD__ || __linux__
3336
ret = getrandom(&val, sizeof(val), GRND_NONBLOCK) != sizeof(val);
3437
#endif
3538

0 commit comments

Comments
 (0)