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