cuserid: support invocation with a null pointer argument
[musl.git] / src / misc / getentropy.c
blob651ea95f14310cda867b88378a0aedf2225d4462
1 #define _BSD_SOURCE
2 #include <unistd.h>
3 #include <sys/random.h>
4 #include <pthread.h>
5 #include <errno.h>
7 int getentropy(void *buffer, size_t len)
9 int cs, ret = 0;
10 char *pos = buffer;
12 if (len > 256) {
13 errno = EIO;
14 return -1;
17 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
19 while (len) {
20 ret = getrandom(pos, len, 0);
21 if (ret < 0) {
22 if (errno == EINTR) continue;
23 else break;
25 pos += ret;
26 len -= ret;
27 ret = 0;
30 pthread_setcancelstate(cs, 0);
32 return ret;