diff options
author | Thorsten Glaser <tg@debian.org> | 2025-04-07 00:23:45 +0200 |
---|---|---|
committer | Waldemar Brodkorb <wbx@openadk.org> | 2025-04-07 00:24:40 +0200 |
commit | 8a6bb3fb10bef1a31088e845f6b4b2ae44959ef3 (patch) | |
tree | 20976e9dde9b449d8d0acca305f00b43659c3936 /libc/sysdeps/linux/common/getentropy.c | |
parent | f82757461a7fdec3233766494d7003bc89f71241 (diff) |
Signed-off-by: Thorsten Glaser <tg@debian.org>
Diffstat (limited to 'libc/sysdeps/linux/common/getentropy.c')
-rw-r--r-- | libc/sysdeps/linux/common/getentropy.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/libc/sysdeps/linux/common/getentropy.c b/libc/sysdeps/linux/common/getentropy.c new file mode 100644 index 000000000..971674c4f --- /dev/null +++ b/libc/sysdeps/linux/common/getentropy.c @@ -0,0 +1,43 @@ +/* + * getentropy() by wrapping getrandom(), for µClibc-ng + * + * © 2025 mirabilos Ⓕ CC0 or MirBSD or GNU LGPLv2 + * + * Note: may be a thread cancellation point, unlike the + * implementations in glibc and musl libc. Should this + * ever become a concern, it will need patching. + */ + +#define _DEFAULT_SOURCE +#include <errno.h> +#include <unistd.h> +#include <sys/random.h> + +int +getentropy(void *__buf, size_t __len) +{ + ssize_t n; + + if (__len > 256U) { + errno = EIO; + return (-1); + } + + again: + if ((n = getrandom(__buf, __len, 0)) == -1) + switch (errno) { + case EAGAIN: /* should not happen but better safe than sorry */ + case EINTR: + goto again; + default: + errno = EIO; + /* FALLTHROUGH */ + case EFAULT: + case ENOSYS: + return (-1); + } + if ((size_t)n != __len) + /* also shouldn’t happen (safety net) */ + goto again; + return (0); +} |