YarrPP  0.1
C++ Library for Pirates
random.cpp
1 #include "random.hpp"
2 
3 #include <cerrno>
4 #include <system_error>
5 
6 #include <linux/random.h>
7 #include <sys/syscall.h>
8 #include <unistd.h>
9 
10 namespace yarr
11 {
12 
13 static long getrandom(void* buf, std::size_t buf_len, unsigned int flags)
14 {
15  return syscall(SYS_getrandom, buf, buf_len, 0);
16 }
17 
18 void random_fill(char* begin, char* end)
19 {
20  while (begin < end)
21  {
22  long bytes_read = getrandom(begin, end - begin, 0);
23  if (bytes_read > 0)
24  {
25  begin += bytes_read;
26  continue;
27  }
28  else if (errno == EINTR)
29  {
30  continue;
31  }
32  else
33  {
34  throw std::system_error(errno, std::system_category());
35  }
36  }
37 }
38 
39 }