summaryrefslogtreecommitdiffstats
path: root/src/utils/lib/randutils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/lib/randutils.c')
-rw-r--r--src/utils/lib/randutils.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/utils/lib/randutils.c b/src/utils/lib/randutils.c
index bd2a8f6..39edf4e 100644
--- a/src/utils/lib/randutils.c
+++ b/src/utils/lib/randutils.c
@@ -13,9 +13,9 @@
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
-
+#ifdef __linux__
#include <sys/syscall.h>
-
+#endif
#include "c.h"
#include "randutils.h"
#include "nls.h"
@@ -36,7 +36,7 @@
#endif
#if !defined(HAVE_GETRANDOM) && defined(SYS_getrandom)
-/* libc without function, but we have syscal */
+/* libc without function, but we have syscall */
#define GRND_NONBLOCK 0x01
#define GRND_RANDOM 0x02
static int getrandom(void *buf, size_t buflen, unsigned int flags)
@@ -102,7 +102,12 @@ int random_get_fd(void)
#define UL_RAND_READ_ATTEMPTS 8
#define UL_RAND_READ_DELAY 125000 /* microseconds */
-void random_get_bytes(void *buf, size_t nbytes)
+/*
+ * Write @nbytes random bytes into @buf.
+ *
+ * Returns 0 for good quality of random bytes or 1 for weak quality.
+ */
+int ul_random_get_bytes(void *buf, size_t nbytes)
{
unsigned char *cp = (unsigned char *)buf;
size_t i, n = nbytes;
@@ -118,7 +123,7 @@ void random_get_bytes(void *buf, size_t nbytes)
n -= x;
cp += x;
lose_counter = 0;
-
+ errno = 0;
} else if (errno == ENOSYS) { /* kernel without getrandom() */
break;
@@ -177,6 +182,8 @@ void random_get_bytes(void *buf, size_t nbytes)
sizeof(ul_jrand_seed)-sizeof(unsigned short));
}
#endif
+
+ return n != 0;
}
@@ -216,7 +223,7 @@ int main(int argc, char *argv[])
printf("Multiple random calls:\n");
for (i = 0; i < n; i++) {
- random_get_bytes(&v, sizeof(v));
+ ul_random_get_bytes(&v, sizeof(v));
printf("#%02zu: %25"PRIu64"\n", i, v);
}
@@ -227,7 +234,7 @@ int main(int argc, char *argv[])
if (!buf)
err(EXIT_FAILURE, "failed to allocate buffer");
- random_get_bytes(buf, bufsz);
+ ul_random_get_bytes(buf, bufsz);
for (i = 0; i < n; i++) {
vp = (int64_t *) (buf + (i * sizeof(*vp)));
printf("#%02zu: %25"PRIu64"\n", i, *vp);