/* Random bytes straight from the kernel, so the app needs no /dev/urandom inside its sandbox. */ #include #include #include #include #include #include #include CAMLprim value todo_getrandom(value len) { CAMLparam1(len); CAMLlocal1(buf); size_t n = Long_val(len), got = 0; buf = caml_alloc_string(n); while (got < n) { ssize_t r = getrandom((char *)Bytes_val(buf) + got, n - got, 0); if (r < 0) { if (errno == EINTR) continue; caml_failwith(strerror(errno)); } got += r; } CAMLreturn(buf); }