Skip to content

Commit

Permalink
Merge pull request #482 from zenfey/syscall_of_getrandom_for_glibc_ve…
Browse files Browse the repository at this point in the history
…rsion_before_2_25

use raw system call of getrandom for glibc version before 2.25
  • Loading branch information
talex5 authored Apr 9, 2023
2 parents 39179a2 + c1f47f8 commit 233cf01
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib_eio_linux/eio_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/eventfd.h>
#if __GLIBC__ > 2 || __GLIBC_MINOR__ > 24
#include <sys/random.h>
#endif
#include <sys/syscall.h>
#include <limits.h>
#include <errno.h>
Expand Down Expand Up @@ -97,7 +99,11 @@ CAMLprim value caml_eio_getrandom(value v_ba, value v_off, value v_len) {
do {
void *buf = Caml_ba_data_val(v_ba) + off;
caml_enter_blocking_section();
#if __GLIBC__ > 2 || __GLIBC_MINOR__ > 24
ret = getrandom(buf, len, 0);
#else
ret = syscall(SYS_getrandom, buf, len, 0);
#endif
caml_leave_blocking_section();
} while (ret == -1 && errno == EINTR);
if (ret == -1) uerror("getrandom", Nothing);
Expand Down
8 changes: 8 additions & 0 deletions lib_eio_posix/eio_posix_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

#include <sys/types.h>
#ifdef __linux__
#if __GLIBC__ > 2 || __GLIBC_MINOR__ > 24
#include <sys/random.h>
#else
#include <sys/syscall.h>
#endif
#endif
#include <sys/uio.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -40,7 +44,11 @@ CAMLprim value caml_eio_posix_getrandom(value v_ba, value v_off, value v_len) {
void *buf = (uint8_t *)Caml_ba_data_val(v_ba) + off;
caml_enter_blocking_section();
#ifdef __linux__
#if __GLIBC__ > 2 || __GLIBC_MINOR__ > 24
ret = getrandom(buf, len, 0);
#else
ret = syscall(SYS_getrandom, buf, len, 0);
#endif
#else
arc4random_buf(buf, len);
ret = len;
Expand Down

0 comments on commit 233cf01

Please sign in to comment.