diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/portability.c | 12 | ||||
-rw-r--r-- | lib/portability.h | 2 |
2 files changed, 8 insertions, 6 deletions
diff --git a/lib/portability.c b/lib/portability.c index a80ca56c..5c754d7d 100644 --- a/lib/portability.c +++ b/lib/portability.c @@ -30,18 +30,20 @@ pid_t xfork(void) } #endif -void xgetrandom(void *buf, unsigned buflen, unsigned flags) +int xgetrandom(void *buf, unsigned buflen, unsigned flags) { int fd; #if CFG_TOYBOX_GETRANDOM - if (buflen == getrandom(buf, buflen, flags)) return; - if (!CFG_TOYBOX_ON_ANDROID || errno!=ENOSYS) perror_exit("getrandom"); + if (buflen == getrandom(buf, buflen, flags&~WARN_ONLY)) return 1; + if (errno!=ENOSYS && !(flags&WARN_ONLY)) perror_exit("getrandom"); #endif - - fd = xopen(flags ? "/dev/random" : "/dev/urandom", O_RDONLY); + fd = xopen(flags ? "/dev/random" : "/dev/urandom",O_RDONLY|(flags&WARN_ONLY)); + if (fd == -1) return 0; xreadall(fd, buf, buflen); close(fd); + + return 1; } #if defined(__APPLE__) diff --git a/lib/portability.h b/lib/portability.h index 21d0b8a1..60d40498 100644 --- a/lib/portability.h +++ b/lib/portability.h @@ -266,7 +266,7 @@ extern CODE prioritynames[], facilitynames[]; #if CFG_TOYBOX_GETRANDOM #include <sys/random.h> #endif -void xgetrandom(void *buf, unsigned len, unsigned flags); +int xgetrandom(void *buf, unsigned len, unsigned flags); // Android's bionic libc doesn't have confstr. #ifdef __BIONIC__ |