Revert "Directly use getrandom system call if possible"

This reverts commit e7c82f20e3d7d7b47221b454508d07aefc14e7c0.
Fix #5516
This commit is contained in:
topjohnwu 2022-03-02 19:50:47 -08:00
parent e51aacb0b7
commit e0b5645064

View File

@ -35,21 +35,17 @@ int fork_no_orphan() {
int gen_rand_str(char *buf, int len, bool varlen) { int gen_rand_str(char *buf, int len, bool varlen) {
constexpr char ALPHANUM[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; constexpr char ALPHANUM[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static mt19937 gen([]{ static std::mt19937 gen([]{
mt19937::result_type seed; if (access("/dev/urandom", F_OK) != 0)
if (syscall(__NR_getrandom, &seed, sizeof(seed), 0) < 0) { mknod("/dev/urandom", 0600 | S_IFCHR, makedev(1, 9));
// This can happen if device is running a too low Linux version int fd = xopen("/dev/urandom", O_RDONLY | O_CLOEXEC);
if (access("/dev/urandom", F_OK) != 0) unsigned seed;
mknod("/dev/urandom", 0600 | S_IFCHR, makedev(1, 9)); xxread(fd, &seed, sizeof(seed));
int fd = xopen("/dev/urandom", O_RDONLY | O_CLOEXEC);
xxread(fd, &seed, sizeof(seed));
close(fd);
}
return seed; return seed;
}()); }());
uniform_int_distribution<int> dist(0, sizeof(ALPHANUM) - 2); std::uniform_int_distribution<int> dist(0, sizeof(ALPHANUM) - 2);
if (varlen) { if (varlen) {
uniform_int_distribution<int> len_dist(len / 2, len); std::uniform_int_distribution<int> len_dist(len / 2, len);
len = len_dist(gen); len = len_dist(gen);
} }
for (int i = 0; i < len - 1; ++i) for (int i = 0; i < len - 1; ++i)