diff --git a/include/unistd.h b/include/unistd.h index 09190af4e..c17eace9f 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -176,6 +176,7 @@ int acct(const char *); long syscall(long, ...); int execvpe(const char *, char *const [], char *const []); int issetugid(void); +int getentropy(void *, size_t); #endif #ifdef _GNU_SOURCE diff --git a/src/misc/getentropy.c b/src/misc/getentropy.c new file mode 100644 index 000000000..4c61ae261 --- /dev/null +++ b/src/misc/getentropy.c @@ -0,0 +1,31 @@ +#include +#include +#include + +int getentropy(void *buffer, size_t len) +{ + int cs, ret; + 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; +}