Add libglibc-compatibility/musl/getentropy.c

This commit is contained in:
proller 2019-03-27 19:12:47 +03:00
parent 6e3f7f62eb
commit 0fff8a785b
2 changed files with 34 additions and 0 deletions

View File

@ -19,6 +19,7 @@ musl/sched_cpucount.c
musl/glob.c
musl/exp2f.c
musl/pwritev.c
musl/getentropy.c
musl/getrandom.c
musl/fcntl.c
musl/timespec_get.c

View File

@ -0,0 +1,33 @@
#define _DEFAULT_SOURCE
#include <unistd.h>
#include <sys/random.h>
#include <pthread.h>
#include <errno.h>
int getentropy(void *buffer, size_t len)
{
int cs, ret = 0;
char *pos = buffer;
if (len > 256) {
errno = EIO;
return -1;
}
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
while (len) {
ret = getrandom(pos, len, 0);
if (ret < 0) {
if (errno == EINTR) continue;
else break;
}
pos += ret;
len -= ret;
ret = 0;
}
pthread_setcancelstate(cs, 0);
return ret;
}