]> git.proxmox.com Git - wasi-libc.git/blame - libc-top-half/musl/src/misc/getentropy.c
Update to musl 1.2.1. (#222)
[wasi-libc.git] / libc-top-half / musl / src / misc / getentropy.c
CommitLineData
320054e8
DG
1#define _BSD_SOURCE
2#include <unistd.h>
3#include <sys/random.h>
4#include <pthread.h>
5#include <errno.h>
6
7int getentropy(void *buffer, size_t len)
8{
58795582 9 int cs, ret = 0;
320054e8
DG
10 char *pos = buffer;
11
12 if (len > 256) {
13 errno = EIO;
14 return -1;
15 }
16
17 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
18
19 while (len) {
20 ret = getrandom(pos, len, 0);
21 if (ret < 0) {
22 if (errno == EINTR) continue;
23 else break;
24 }
25 pos += ret;
26 len -= ret;
27 ret = 0;
28 }
29
30 pthread_setcancelstate(cs, 0);
31
32 return ret;
33}