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 | |
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')
-rw-r--r-- | contrib/libs/libc_compat/CMakeLists.linux.txt | 5 | ||||
-rw-r--r-- | contrib/libs/libc_compat/random/getentropy.c | 33 | ||||
-rw-r--r-- | contrib/libs/libc_compat/random/getrandom.c (renamed from contrib/libs/libc_compat/getrandom.c) | 0 | ||||
-rw-r--r-- | contrib/libs/libc_compat/random/sys/random.h (renamed from contrib/libs/libc_compat/include/random/sys/random.h) | 13 |
4 files changed, 42 insertions, 9 deletions
diff --git a/contrib/libs/libc_compat/CMakeLists.linux.txt b/contrib/libs/libc_compat/CMakeLists.linux.txt index a43ee74fe5..78e11d618b 100644 --- a/contrib/libs/libc_compat/CMakeLists.linux.txt +++ b/contrib/libs/libc_compat/CMakeLists.linux.txt @@ -13,16 +13,17 @@ target_compile_options(contrib-libs-libc_compat PRIVATE ) target_include_directories(contrib-libs-libc_compat PUBLIC ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/include/readpassphrase - ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/include/random ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/reallocarray + ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/random ) target_sources(contrib-libs-libc_compat PRIVATE ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/string.c ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/readpassphrase.c ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/explicit_bzero.c - ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/getrandom.c ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/memfd_create.c ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/strlcat.c ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/strlcpy.c ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/reallocarray/reallocarray.c + ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/random/getrandom.c + ${CMAKE_SOURCE_DIR}/contrib/libs/libc_compat/random/getentropy.c ) 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; +} diff --git a/contrib/libs/libc_compat/getrandom.c b/contrib/libs/libc_compat/random/getrandom.c index 2184fad710..2184fad710 100644 --- a/contrib/libs/libc_compat/getrandom.c +++ b/contrib/libs/libc_compat/random/getrandom.c diff --git a/contrib/libs/libc_compat/include/random/sys/random.h b/contrib/libs/libc_compat/random/sys/random.h index 8c13837f72..b5b1662c27 100644 --- a/contrib/libs/libc_compat/include/random/sys/random.h +++ b/contrib/libs/libc_compat/random/sys/random.h @@ -1,20 +1,19 @@ -#ifndef _SYS_RANDOM_H -#define _SYS_RANDOM_H +#pragma once + #ifdef __cplusplus extern "C" { #endif #include <sys/types.h> -#define SYS_getrandom 318 +#define SYS_getrandom 318 #define GRND_NONBLOCK 0x0001 -#define GRND_RANDOM 0x0002 +#define GRND_RANDOM 0x0002 #define GRND_INSECURE 0x0004 -ssize_t getrandom(void *, size_t, unsigned); +ssize_t getrandom(void* buf, size_t buflen, unsigned int flags); #ifdef __cplusplus -} -#endif +} // extern "C" #endif |