diff options
author | Rich Felker <dalias@aerifal.cx> | 2018-02-23 02:54:01 -0500 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2018-02-23 02:57:52 -0500 |
commit | 82f176803ae07e34229906d5c7c62889e665dc97 (patch) | |
tree | 8ba87ec513170697964a04c7502a6abb6b8aacc4 /src | |
parent | e20658209177667e490c661dfd35b976749ef3f7 (diff) | |
download | musl-82f176803ae07e34229906d5c7c62889e665dc97.zip musl-82f176803ae07e34229906d5c7c62889e665dc97.tar.gz musl-82f176803ae07e34229906d5c7c62889e665dc97.tar.bz2 |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/misc/getentropy.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/misc/getentropy.c b/src/misc/getentropy.c new file mode 100644 index 0000000..4c61ae2 --- /dev/null +++ b/src/misc/getentropy.c @@ -0,0 +1,31 @@ +#include <sys/random.h> +#include <pthread.h> +#include <errno.h> + +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; +} |