summaryrefslogtreecommitdiff
path: root/src/librandom/random_devurandom.c
blob: 5f6b79150b42062c29f59d8e1ab4da5857d1a8bf (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/strerr2.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 ;
}