From 82f176803ae07e34229906d5c7c62889e665dc97 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Fri, 23 Feb 2018 02:54:01 -0500 Subject: add getentropy function based loosely on patch by Hauke Mehrtens; converted to wrap the public API of the underlying getrandom function rather than direct syscalls, so that if/when a fallback implementation of getrandom is added it will automatically get picked up by getentropy too. --- src/misc/getentropy.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/misc/getentropy.c (limited to 'src/misc') diff --git a/src/misc/getentropy.c b/src/misc/getentropy.c new file mode 100644 index 00000000..4c61ae26 --- /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; +} -- cgit v1.2.3-70-g09d2