mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-25 19:17:37 +00:00
Small magiskinit cleanup
This commit is contained in:
parent
43029f37b1
commit
765d5d9729
@ -209,30 +209,31 @@ int main(int argc, char *argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
setup_klog();
|
setup_klog();
|
||||||
|
|
||||||
unique_ptr<BaseInit> init;
|
BaseInit *init;
|
||||||
cmdline cmd{};
|
cmdline cmd{};
|
||||||
|
|
||||||
if (argc > 1 && argv[1] == "selinux_setup"sv) {
|
if (argc > 1 && argv[1] == "selinux_setup"sv) {
|
||||||
init = make_unique<SecondStageInit>(argv);
|
init = new SecondStageInit(argv);
|
||||||
} else {
|
} else {
|
||||||
// This will also mount /sys and /proc
|
// This will also mount /sys and /proc
|
||||||
load_kernel_info(&cmd);
|
load_kernel_info(&cmd);
|
||||||
|
|
||||||
if (access("/apex", F_OK) == 0) {
|
bool two_stage = access("/apex", F_OK) == 0;
|
||||||
if (cmd.force_normal_boot)
|
if (cmd.skip_initramfs) {
|
||||||
init = make_unique<ForcedFirstStageInit>(argv, &cmd);
|
if (two_stage)
|
||||||
else if (cmd.skip_initramfs)
|
init = new SARFirstStageInit(argv, &cmd);
|
||||||
init = make_unique<SARFirstStageInit>(argv, &cmd);
|
|
||||||
else
|
else
|
||||||
init = make_unique<FirstStageInit>(argv, &cmd);
|
init = new SARInit(argv, &cmd);
|
||||||
} else if (cmd.skip_initramfs) {
|
|
||||||
init = make_unique<SARInit>(argv, &cmd);
|
|
||||||
} else {
|
} else {
|
||||||
decompress_ramdisk();
|
decompress_ramdisk();
|
||||||
if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0)
|
if (cmd.force_normal_boot)
|
||||||
init = make_unique<RecoveryInit>(argv, &cmd);
|
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
|
else
|
||||||
init = make_unique<RootFSInit>(argv, &cmd);
|
init = new RootFSInit(argv, &cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,9 +31,9 @@ struct raw_data {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/* *************
|
/***************
|
||||||
* Base classes
|
* Base classes
|
||||||
* *************/
|
***************/
|
||||||
|
|
||||||
class BaseInit {
|
class BaseInit {
|
||||||
protected:
|
protected:
|
||||||
@ -41,9 +41,9 @@ protected:
|
|||||||
char **argv;
|
char **argv;
|
||||||
std::vector<std::string> mount_list;
|
std::vector<std::string> mount_list;
|
||||||
|
|
||||||
void exec_init(const char *init = "/init") {
|
void exec_init() {
|
||||||
cleanup();
|
cleanup();
|
||||||
execv(init, argv);
|
execv("/init", argv);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
virtual void cleanup();
|
virtual void cleanup();
|
||||||
@ -60,7 +60,7 @@ protected:
|
|||||||
std::string persist_dir;
|
std::string persist_dir;
|
||||||
|
|
||||||
virtual void early_mount() = 0;
|
virtual void early_mount() = 0;
|
||||||
bool patch_sepolicy(const char *file = "/sepolicy");
|
bool patch_sepolicy(const char *file);
|
||||||
public:
|
public:
|
||||||
MagiskInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {}
|
MagiskInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {}
|
||||||
};
|
};
|
||||||
@ -82,22 +82,9 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/* *************
|
/***************
|
||||||
* 2 Stage Init
|
* 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 {
|
class FirstStageInit : public BaseInit {
|
||||||
private:
|
private:
|
||||||
@ -137,9 +124,9 @@ public:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/* ***********
|
/*************
|
||||||
* Legacy SAR
|
* Legacy SAR
|
||||||
* ***********/
|
*************/
|
||||||
|
|
||||||
class SARInit : public SARBase {
|
class SARInit : public SARBase {
|
||||||
protected:
|
protected:
|
||||||
@ -150,9 +137,9 @@ public:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/* **********
|
/************
|
||||||
* Initramfs
|
* Initramfs
|
||||||
* **********/
|
************/
|
||||||
|
|
||||||
class RootFSInit : public MagiskInit {
|
class RootFSInit : public MagiskInit {
|
||||||
private:
|
private:
|
||||||
|
@ -81,7 +81,7 @@ static void load_overlay_rc(const char *overlay) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RootFSInit::setup_rootfs() {
|
void RootFSInit::setup_rootfs() {
|
||||||
if (patch_sepolicy()) {
|
if (patch_sepolicy("/sepolicy")) {
|
||||||
char *addr;
|
char *addr;
|
||||||
size_t size;
|
size_t size;
|
||||||
mmap_rw("/init", addr, size);
|
mmap_rw("/init", addr, size);
|
||||||
|
@ -10,10 +10,10 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
static void patch_fstab(const string &fstab) {
|
static void patch_fstab(const char *fstab) {
|
||||||
string patched = fstab + ".p";
|
string patched = fstab + ".p"s;
|
||||||
FILE *fp = xfopen(patched.data(), "we");
|
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)
|
if (l[0] == '#' || l.length() == 1)
|
||||||
return true;
|
return true;
|
||||||
char *line = (char *) l.data();
|
char *line = (char *) l.data();
|
||||||
@ -42,55 +42,38 @@ static void patch_fstab(const string &fstab) {
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
// Replace old fstab
|
// Replace old fstab
|
||||||
clone_attr(fstab.data(), patched.data());
|
clone_attr(fstab, patched.data());
|
||||||
rename(patched.data(), fstab.data());
|
rename(patched.data(), fstab);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define FSR "/first_stage_ramdisk"
|
#define FSR "/first_stage_ramdisk"
|
||||||
|
|
||||||
void ForcedFirstStageInit::prepare() {
|
void FirstStageInit::prepare() {
|
||||||
// It is actually possible to NOT have FSR, create it just in case
|
if (cmd->force_normal_boot) {
|
||||||
xmkdir(FSR, 0755);
|
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) {
|
chdir(FSR);
|
||||||
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);
|
|
||||||
} else {
|
} else {
|
||||||
return;
|
xmkdir("/system", 0755);
|
||||||
|
xmkdir("/system/bin", 0755);
|
||||||
|
rename("/init" /* magiskinit */ , "/system/bin/init");
|
||||||
|
rename("/.backup/init", "/init");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move stuffs for next stage
|
// Patch fstab
|
||||||
xmkdir(FSR "/system", 0755);
|
auto dir = xopen_dir(".");
|
||||||
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("/");
|
|
||||||
for (dirent *de; (de = xreaddir(dir.get()));) {
|
for (dirent *de; (de = xreaddir(dir.get()));) {
|
||||||
if (strstr(de->d_name, "fstab")) {
|
if (strstr(de->d_name, "fstab")) {
|
||||||
patch_fstab(de->d_name);
|
patch_fstab(de->d_name);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
chdir("/");
|
||||||
// Move stuffs for next stage
|
|
||||||
xmkdir("/system", 0755);
|
|
||||||
xmkdir("/system/bin", 0755);
|
|
||||||
rename("/init" /* magiskinit */ , "/system/bin/init");
|
|
||||||
rename("/.backup/init", "/init");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline long xptrace(int request, pid_t pid, void *addr, void *data) {
|
static inline long xptrace(int request, pid_t pid, void *addr, void *data) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user