#include #include #define UV__ERR(errno) errno #define UV_EIO EIO #define ARRAY_SIZE(A) (sizeof(A)/sizeof((A)[0])) int uv__random_sysctl(void* buf, size_t len) { const int mib[] = {CTL_KERN, KERN_ARND}; unsigned char *p = buf; while (len) { size_t nreq = MIN(len, 32), n = nreq; if (sysctl(mib, ARRAY_SIZE(mib), p, &n, NULL, 0) == -1) return UV__ERR(errno); if (n != nreq) return UV_EIO; /* can't happen */ p += n; len -= n; } return 0; } #include #include #include #include int main(void) { unsigned char buf0[130], *buf = buf0 + 1; /* misalign */ size_t i, j; int error; for (i = 0; i < 128; i++) { memset(buf0, 0, sizeof buf0); error = uv__random_sysctl(buf, i); if (error) { errno = error; err(1, "rando"); } assert(buf0[0] == 0); assert(buf0[129] == 0); for (j = 0; j < i; j++) printf("%02hhx", buf[j]); printf("\n"); } printf("ok\n"); fflush(stdout); return ferror(stdout); }