diff options
author | thegeorg <thegeorg@yandex-team.com> | 2022-07-27 10:14:03 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2022-07-27 10:14:03 +0300 |
commit | dda72e831a7b5f7cb932d99db79e2ee8d1a0d520 (patch) | |
tree | a4587f408ec33d67cca32cebc3a2170966550176 /contrib/libs/libc_compat/random/getentropy.c | |
parent | 9762d6d10b478ec2dadd9d91e716738ea937e3a3 (diff) | |
download | ydb-dda72e831a7b5f7cb932d99db79e2ee8d1a0d520.tar.gz |
Provide getrandom() and getentropy() by the means of contrib/libs/libc_compat
Diffstat (limited to 'contrib/libs/libc_compat/random/getentropy.c')
-rw-r--r-- | contrib/libs/libc_compat/random/getentropy.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/contrib/libs/libc_compat/random/getentropy.c b/contrib/libs/libc_compat/random/getentropy.c new file mode 100644 index 0000000000..651ea95f14 --- /dev/null +++ b/contrib/libs/libc_compat/random/getentropy.c @@ -0,0 +1,33 @@ +#define _BSD_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; +} |