Small magiskinit cleanup

This commit is contained in:
topjohnwu 2020-04-19 04:56:56 -07:00
parent 43029f37b1
commit 765d5d9729
4 changed files with 47 additions and 76 deletions

View File

@ -209,30 +209,31 @@ int main(int argc, char *argv[]) {
return 1;
setup_klog();
unique_ptr<BaseInit> init;
BaseInit *init;
cmdline cmd{};
if (argc > 1 && argv[1] == "selinux_setup"sv) {
init = make_unique<SecondStageInit>(argv);
init = new SecondStageInit(argv);
} else {
// This will also mount /sys and /proc
load_kernel_info(&cmd);
if (access("/apex", F_OK) == 0) {
if (cmd.force_normal_boot)
init = make_unique<ForcedFirstStageInit>(argv, &cmd);
else if (cmd.skip_initramfs)
init = make_unique<SARFirstStageInit>(argv, &cmd);
bool two_stage = access("/apex", F_OK) == 0;
if (cmd.skip_initramfs) {
if (two_stage)
init = new SARFirstStageInit(argv, &cmd);
else
init = make_unique<FirstStageInit>(argv, &cmd);
} else if (cmd.skip_initramfs) {
init = make_unique<SARInit>(argv, &cmd);
init = new SARInit(argv, &cmd);
} else {
decompress_ramdisk();
if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0)
init = make_unique<RecoveryInit>(argv, &cmd);
if (cmd.force_normal_boot)
init = new FirstStageInit(argv, &cmd);
else if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0)
init = new RecoveryInit(argv, &cmd);
else if (two_stage)
init = new FirstStageInit(argv, &cmd);
else
init = make_unique<RootFSInit>(argv, &cmd);
init = new RootFSInit(argv, &cmd);
}
}

View File

@ -31,9 +31,9 @@ struct raw_data {
}
};
/* *************
/***************
* Base classes
* *************/
***************/
class BaseInit {
protected:
@ -41,9 +41,9 @@ protected:
char **argv;
std::vector<std::string> mount_list;
void exec_init(const char *init = "/init") {
void exec_init() {
cleanup();
execv(init, argv);
execv("/init", argv);
exit(1);
}
virtual void cleanup();
@ -60,7 +60,7 @@ protected:
std::string persist_dir;
virtual void early_mount() = 0;
bool patch_sepolicy(const char *file = "/sepolicy");
bool patch_sepolicy(const char *file);
public:
MagiskInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {}
};
@ -82,22 +82,9 @@ public:
}
};
/* *************
/***************
* 2 Stage Init
* *************/
class ForcedFirstStageInit : public BaseInit {
private:
void prepare();
public:
ForcedFirstStageInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {
LOGD("%s\n", __FUNCTION__);
};
void start() override {
prepare();
exec_init("/system/bin/init");
}
};
***************/
class FirstStageInit : public BaseInit {
private:
@ -137,9 +124,9 @@ public:
};
};
/* ***********
/*************
* Legacy SAR
* ***********/
*************/
class SARInit : public SARBase {
protected:
@ -150,9 +137,9 @@ public:
};
};
/* **********
/************
* Initramfs
* **********/
************/
class RootFSInit : public MagiskInit {
private:

View File

@ -81,7 +81,7 @@ static void load_overlay_rc(const char *overlay) {
}
void RootFSInit::setup_rootfs() {
if (patch_sepolicy()) {
if (patch_sepolicy("/sepolicy")) {
char *addr;
size_t size;
mmap_rw("/init", addr, size);

View File

@ -10,10 +10,10 @@
using namespace std;
static void patch_fstab(const string &fstab) {
string patched = fstab + ".p";
static void patch_fstab(const char *fstab) {
string patched = fstab + ".p"s;
FILE *fp = xfopen(patched.data(), "we");
file_readline(fstab.data(), [=](string_view l) -> bool {
file_readline(fstab, [=](string_view l) -> bool {
if (l[0] == '#' || l.length() == 1)
return true;
char *line = (char *) l.data();
@ -42,55 +42,38 @@ static void patch_fstab(const string &fstab) {
fclose(fp);
// Replace old fstab
clone_attr(fstab.data(), patched.data());
rename(patched.data(), fstab.data());
clone_attr(fstab, patched.data());
rename(patched.data(), fstab);
}
#define FSR "/first_stage_ramdisk"
void ForcedFirstStageInit::prepare() {
// It is actually possible to NOT have FSR, create it just in case
xmkdir(FSR, 0755);
void FirstStageInit::prepare() {
if (cmd->force_normal_boot) {
xmkdirs(FSR "/system/bin", 0755);
rename("/init" /* magiskinit */, FSR "/system/bin/init");
symlink("/system/bin/init", FSR "/init");
rename("/.backup", FSR "/.backup");
rename("/overlay.d", FSR "/overlay.d");
xsymlink("/system/bin/init", "/init");
if (auto dir = xopen_dir(FSR); dir) {
string fstab(FSR "/");
for (dirent *de; (de = xreaddir(dir.get()));) {
if (strstr(de->d_name, "fstab")) {
fstab += de->d_name;
break;
}
}
if (fstab.length() == sizeof(FSR))
return;
patch_fstab(fstab);
chdir(FSR);
} else {
return;
xmkdir("/system", 0755);
xmkdir("/system/bin", 0755);
rename("/init" /* magiskinit */ , "/system/bin/init");
rename("/.backup/init", "/init");
}
// Move stuffs for next stage
xmkdir(FSR "/system", 0755);
xmkdir(FSR "/system/bin", 0755);
rename("/init" /* magiskinit */, FSR "/system/bin/init");
symlink("/system/bin/init", FSR "/init");
rename("/.backup", FSR "/.backup");
rename("/overlay.d", FSR "/overlay.d");
}
void FirstStageInit::prepare() {
auto dir = xopen_dir("/");
// Patch fstab
auto dir = xopen_dir(".");
for (dirent *de; (de = xreaddir(dir.get()));) {
if (strstr(de->d_name, "fstab")) {
patch_fstab(de->d_name);
break;
}
}
// Move stuffs for next stage
xmkdir("/system", 0755);
xmkdir("/system/bin", 0755);
rename("/init" /* magiskinit */ , "/system/bin/init");
rename("/.backup/init", "/init");
chdir("/");
}
static inline long xptrace(int request, pid_t pid, void *addr, void *data) {