Minor adjustments to prevent crashes

This commit is contained in:
topjohnwu 2020-01-09 23:42:27 +08:00
parent fafa92d44b
commit 3a0e3c98f7

View File

@ -221,46 +221,35 @@ static void sbin_overlay(const raw_data &self, const raw_data &config) {
xsymlink("./magiskinit", "/sbin/supolicy"); xsymlink("./magiskinit", "/sbin/supolicy");
} }
static void recreate_sbin(const char *mirror) { static void recreate_sbin(const char *mirror, bool use_bind_mount) {
int src = xopen(mirror, O_RDONLY | O_CLOEXEC); auto dp = xopen_dir(mirror);
int dest = xopen("/sbin", O_RDONLY | O_CLOEXEC); int src = dirfd(dp.get());
DIR *fp = fdopendir(src); char buf[4096];
char buf[256]; for (dirent *entry; (entry = xreaddir(dp.get()));) {
bool use_bind_mount = true;
for (dirent *entry; (entry = xreaddir(fp));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv) if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue; continue;
string sbin_path = "/sbin/"s + entry->d_name;
struct stat st; struct stat st;
fstatat(src, entry->d_name, &st, AT_SYMLINK_NOFOLLOW); fstatat(src, entry->d_name, &st, AT_SYMLINK_NOFOLLOW);
if (S_ISLNK(st.st_mode)) { if (S_ISLNK(st.st_mode)) {
xreadlinkat(src, entry->d_name, buf, sizeof(buf)); xreadlinkat(src, entry->d_name, buf, sizeof(buf));
xsymlinkat(buf, dest, entry->d_name); xsymlink(buf, sbin_path.data());
} else { } else {
char sbin_path[256];
sprintf(buf, "%s/%s", mirror, entry->d_name); sprintf(buf, "%s/%s", mirror, entry->d_name);
sprintf(sbin_path, "/sbin/%s", entry->d_name);
if (use_bind_mount) { if (use_bind_mount) {
auto mode = st.st_mode & 0777;
// Create dummy // Create dummy
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
xmkdir(sbin_path, st.st_mode & 0777); xmkdir(sbin_path.data(), mode);
else else
close(xopen(sbin_path, O_CREAT | O_WRONLY | O_CLOEXEC, st.st_mode & 0777)); close(xopen(sbin_path.data(), O_CREAT | O_WRONLY | O_CLOEXEC, mode));
if (xmount(buf, sbin_path, nullptr, MS_BIND, nullptr)) { xmount(buf, sbin_path.data(), nullptr, MS_BIND, nullptr);
// Bind mount failed, fallback to symlink } else {
remove(sbin_path); xsymlink(buf, sbin_path.data());
use_bind_mount = false;
} else {
continue;
}
} }
xsymlink(buf, sbin_path);
} }
} }
close(src);
close(dest);
} }
#define ROOTMIR MIRRDIR "/system_root" #define ROOTMIR MIRRDIR "/system_root"
@ -304,7 +293,7 @@ void SARBase::patch_rootdir() {
xmount(ROOTBLK, ROOTMIR, "erofs", MS_RDONLY, nullptr); xmount(ROOTBLK, ROOTMIR, "erofs", MS_RDONLY, nullptr);
// Recreate original sbin structure // Recreate original sbin structure
recreate_sbin(ROOTMIR "/sbin"); recreate_sbin(ROOTMIR "/sbin", true);
// Patch init // Patch init
raw_data init; raw_data init;
@ -491,7 +480,7 @@ int magisk_proxy_main(int argc, char *argv[]) {
sbin_overlay(self, config); sbin_overlay(self, config);
// Create symlinks pointing back to /root // Create symlinks pointing back to /root
recreate_sbin("/root"); recreate_sbin("/root", false);
setenv("REMOUNT_ROOT", "1", 1); setenv("REMOUNT_ROOT", "1", 1);
execv("/sbin/magisk", argv); execv("/sbin/magisk", argv);