Download this file
| 1 | /* Random bytes straight from the kernel, so the app needs no /dev/urandom |
| 2 | inside its sandbox. */ |
| 3 | #include <errno.h> |
| 4 | #include <string.h> |
| 5 | #include <sys/random.h> |
| 6 | #include <caml/alloc.h> |
| 7 | #include <caml/fail.h> |
| 8 | #include <caml/memory.h> |
| 9 | #include <caml/mlvalues.h> |
| 10 | |
| 11 | CAMLprim value todo_getrandom(value len) |
| 12 | { |
| 13 | CAMLparam1(len); |
| 14 | CAMLlocal1(buf); |
| 15 | size_t n = Long_val(len), got = 0; |
| 16 | buf = caml_alloc_string(n); |
| 17 | while (got < n) { |
| 18 | ssize_t r = getrandom((char *)Bytes_val(buf) + got, n - got, 0); |
| 19 | if (r < 0) { |
| 20 | if (errno == EINTR) continue; |
| 21 | caml_failwith(strerror(errno)); |
| 22 | } |
| 23 | got += r; |
| 24 | } |
| 25 | CAMLreturn(buf); |
| 26 | } |
| 27 | |