Do not use std::random_device

Directly read from urandom instead of using std::random_device.
libc++ will use iostream under-the-hood, which brings significant
binary size increase that is not welcomed, especially in magiskinit.
This commit is contained in:
topjohnwu 2019-07-14 21:56:21 -07:00
parent 41045b62dc
commit 52fd508fea
2 changed files with 9 additions and 9 deletions

View File

@ -48,7 +48,7 @@ static void patch_init_rc(FILE *rc) {
fprintf(rc, "%s", line.data()); fprintf(rc, "%s", line.data());
return true; return true;
}); });
char pfd_svc[32], ls_svc[32], bc_svc[32]; char pfd_svc[16], ls_svc[16], bc_svc[16];
gen_rand_str(pfd_svc, sizeof(pfd_svc)); gen_rand_str(pfd_svc, sizeof(pfd_svc));
gen_rand_str(ls_svc, sizeof(ls_svc)); gen_rand_str(ls_svc, sizeof(ls_svc));
gen_rand_str(bc_svc, sizeof(bc_svc)); gen_rand_str(bc_svc, sizeof(bc_svc));

View File

@ -53,16 +53,16 @@ int fork_no_zombie() {
constexpr char ALPHANUM[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; constexpr char ALPHANUM[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static bool seeded = false; static bool seeded = false;
static std::mt19937 gen; static std::mt19937 gen;
static std::uniform_int_distribution<int> dist(0, sizeof(ALPHANUM) - 1); static std::uniform_int_distribution<int> dist(0, sizeof(ALPHANUM) - 2);
void gen_rand_str(char *buf, int len, bool varlen) { void gen_rand_str(char *buf, int len, bool varlen) {
if (!seeded) { if (!seeded) {
if (access("/dev/urandom", F_OK) == 0) { if (access("/dev/urandom", F_OK) != 0)
std::random_device rdev; mknod("/dev/urandom", 0600 | S_IFCHR, makedev(1, 9));
gen.seed(rdev()); int fd = xopen("/dev/urandom", O_RDONLY | O_CLOEXEC);
} else { unsigned seed;
// In magiskinit xxread(fd, &seed, sizeof(seed));
gen.seed(time(nullptr)); gen.seed(seed);
} close(fd);
seeded = true; seeded = true;
} }
if (varlen) { if (varlen) {