summaryrefslogtreecommitdiff
path: root/src/librandom/random_devurandom.c
blob: 106c24fd0802a3359d31458123e64c2d493b8519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* ISC license. */

#include <stddef.h>
#include <errno.h>

#include <skalibs/allreadwrite.h>
#include <skalibs/strerr.h>
#include <skalibs/djbunix.h>
#include <skalibs/random.h>

void random_devurandom (char *s, size_t n)
{
  static int random_fd = -1 ;
  size_t r ;
  int e = errno ;
  if (random_fd < 0)
  {
    random_fd = openbc_read("/dev/urandom") ;
    if (random_fd < 0)
      strerr_diefu2sys(111, "open ", "/dev/urandom") ;
  }
  errno = EPIPE ;
  r = allread(random_fd, s, n) ;
  if (r < n) strerr_diefu2sys(111, "read from ", "/dev/urandom") ;
  errno = e ;
}