+#ifdef __linux__
+ fd_set infd;
+ struct timeval tv = { 0, 0 };
+#endif
+#ifdef HAVE_GETENTROPY
+ size_t nn;
+#endif
+
+#if defined(HAVE_LINUX_RANDOM_H) && \
+ defined(GRND_NONBLOCK) && \
+ defined(SYS_getrandom)
+ /* --- Use the new shinies if available --- */
+
+ while (n < sizeof(buf)) {
+ if ((len = syscall(SYS_getrandom, buf + n, sizeof(buf) - n,
+ GRND_NONBLOCK)) <= 0) {
+ if (errno == ENOSYS) break;
+ else goto done;
+ }
+ n += len;
+ }
+ if (n == sizeof(buf)) goto win;
+#endif
+
+#ifdef HAVE_GETENTROPY
+ /* --- OpenBSD-flavoured shinies --- */
+
+ while (n < sizeof(buf)) {
+ nn = sizeof(buf) - n;
+ if (nn > 256) nn = 256;
+ if (getentropy(buf + n, nn)) break;
+ n += nn;
+ }
+ if (n == sizeof(buf)) goto win;
+#endif
+
+#ifdef __linux__
+ /* --- Don't take from `/dev/urandom' if `/dev/random' would block --- */
+
+ if ((fd = open("/dev/random", O_RDONLY | O_NONBLOCK)) < 0) goto done;
+ FD_ZERO(&infd);
+ FD_SET(fd, &infd);
+ if (select(fd + 1, &infd, 0, 0, &tv) < 0 || !FD_ISSET(fd, &infd))
+ goto done;
+ close(fd); fd = -1;
+#endif