mirror of
https://git.musl-libc.org/git/musl
synced 2025-01-23 06:32:05 +03:00
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.
This commit is contained in:
parent
e206582091
commit
82f176803a
@ -176,6 +176,7 @@ int acct(const char *);
|
|||||||
long syscall(long, ...);
|
long syscall(long, ...);
|
||||||
int execvpe(const char *, char *const [], char *const []);
|
int execvpe(const char *, char *const [], char *const []);
|
||||||
int issetugid(void);
|
int issetugid(void);
|
||||||
|
int getentropy(void *, size_t);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _GNU_SOURCE
|
#ifdef _GNU_SOURCE
|
||||||
|
31
src/misc/getentropy.c
Normal file
31
src/misc/getentropy.c
Normal file
@ -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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user