+ if (n == 0)
+ return;
+#ifdef __NR_getrandom
+ static bool getrandom_unavailable;
+
+ if (getrandom_unavailable)
+ goto try_dev_urandom;
+ do {
+ int res = syscall(__NR_getrandom, p, n, 0);
+ if (unlikely(res < 0)) {
+ if (errno == ENOSYS) {
+ getrandom_unavailable = true;
+ goto try_dev_urandom;
+ }
+ if (errno == EINTR)
+ continue;
+ ERROR_WITH_ERRNO("getrandom() failed");
+ wimlib_assert(0);
+ res = 0;
+ }
+ p += res;
+ n -= res;
+ } while (n != 0);
+ return;
+
+try_dev_urandom:
+ ;
+#endif /* __NR_getrandom */
+ int fd = open("/dev/urandom", O_RDONLY);
+ if (fd < 0) {
+ ERROR_WITH_ERRNO("Unable to open /dev/urandom");
+ wimlib_assert(0);
+ }
+ do {
+ int res = read(fd, p, min(n, INT_MAX));
+ if (unlikely(res < 0)) {
+ if (errno == EINTR)
+ continue;
+ ERROR_WITH_ERRNO("Error reading from /dev/urandom");
+ wimlib_assert(0);
+ res = 0;
+ }
+ p += res;
+ n -= res;
+ } while (n != 0);
+ close(fd);