1 /* 2 * getentropy() by wrapping getrandom(), for µClibc-ng 3 * 4 * © 2025 mirabilos Ⓕ CC0 or MirBSD or GNU LGPLv2 5 * 6 * Note: may be a thread cancellation point, unlike the 7 * implementations in glibc and musl libc. Should this 8 * ever become a concern, it will need patching. 9 */ 10 11 #define _DEFAULT_SOURCE 12 #include <errno.h> 13 #include <unistd.h> 14 #include <sys/random.h> 15 #include <sys/syscall.h> 16 17 #ifdef __NR_getrandom 18 int getentropy(void * __buf,size_t __len)19getentropy(void *__buf, size_t __len) 20 { 21 ssize_t n; 22 23 if (__len > 256U) { 24 errno = EIO; 25 return (-1); 26 } 27 28 again: 29 if ((n = getrandom(__buf, __len, 0)) == -1) 30 switch (errno) { 31 case EAGAIN: /* should not happen but better safe than sorry */ 32 case EINTR: 33 goto again; 34 default: 35 errno = EIO; 36 /* FALLTHROUGH */ 37 case EFAULT: 38 case ENOSYS: 39 return (-1); 40 } 41 if ((size_t)n != __len) 42 /* also shouldn’t happen (safety net) */ 43 goto again; 44 return (0); 45 } 46 #endif 47