Adjust class structures

This commit is contained in:
topjohnwu 2019-06-24 01:50:47 -07:00
parent 6cde695194
commit bb67a837d3
4 changed files with 35 additions and 42 deletions

View File

@ -115,6 +115,12 @@ void LegacyInit::early_mount() {
char fstype[32]; char fstype[32];
char block_dev[64]; char block_dev[64];
full_read("/init", &self.buf, &self.sz);
LOGD("Reverting /init\n");
root = xopen("/", O_RDONLY | O_CLOEXEC);
rename("/.backup/init", "/init");
mount_root(system); mount_root(system);
mount_root(vendor); mount_root(vendor);
mount_root(product); mount_root(product);
@ -126,6 +132,12 @@ void SARCompatInit::early_mount() {
char fstype[32]; char fstype[32];
char block_dev[64]; char block_dev[64];
full_read("/init", &self.buf, &self.sz);
LOGD("Cleaning rootfs\n");
root = xopen("/", O_RDONLY | O_CLOEXEC);
frm_rf(root, { ".backup", "overlay", "proc", "sys" });
LOGD("Early mount system_root\n"); LOGD("Early mount system_root\n");
sprintf(partname, "system%s", cmd->slot); sprintf(partname, "system%s", cmd->slot);
setup_block(partname, block_dev); setup_block(partname, block_dev);
@ -164,6 +176,14 @@ static void switch_root(const string &path) {
void SARInit::early_mount() { void SARInit::early_mount() {
char partname[32]; char partname[32];
full_read("/init", &self.buf, &self.sz);
full_read("/.backup/.magisk", &config.buf, &config.sz);
LOGD("Cleaning rootfs\n");
int root = xopen("/", O_RDONLY | O_CLOEXEC);
frm_rf(root, { "proc", "sys" });
close(root);
LOGD("Early mount system_root\n"); LOGD("Early mount system_root\n");
sprintf(partname, "system%s", cmd->slot); sprintf(partname, "system%s", cmd->slot);
system_dev = setup_block(partname); system_dev = setup_block(partname);

View File

@ -124,40 +124,13 @@ void BaseInit::re_exec_init() {
exit(1); exit(1);
} }
void LegacyInit::preset() {
full_read("/init", &self.buf, &self.sz);
LOGD("Reverting /init\n");
root = open("/", O_RDONLY | O_CLOEXEC);
rename("/.backup/init", "/init");
}
void SARInit::preset() {
full_read("/init", &self.buf, &self.sz);
full_read("/.backup/.magisk", &config.buf, &config.sz);
LOGD("Cleaning rootfs\n");
root = open("/", O_RDONLY | O_CLOEXEC);
frm_rf(root, { "proc", "sys" });
}
void SARCompatInit::preset() {
full_read("/init", &self.buf, &self.sz);
LOGD("Cleaning rootfs\n");
root = open("/", O_RDONLY | O_CLOEXEC);
frm_rf(root, { ".backup", "overlay", "proc", "sys" });
}
void RootFSInit::start() { void RootFSInit::start() {
preset();
early_mount(); early_mount();
setup_rootfs(); setup_rootfs();
re_exec_init(); re_exec_init();
} }
void SARInit::start() { void SARInit::start() {
preset();
early_mount(); early_mount();
patch_rootdir(); patch_rootdir();
re_exec_init(); re_exec_init();
@ -178,8 +151,8 @@ class TestInit : public SARInit {
public: public:
TestInit(char *argv[], cmdline *cmd) : SARInit(argv, cmd) {}; TestInit(char *argv[], cmdline *cmd) : SARInit(argv, cmd) {};
void start() override { void start() override {
preset();
early_mount(); early_mount();
patch_rootdir();
cleanup(); cleanup();
} }
}; };

View File

@ -26,13 +26,11 @@ public:
class MagiskInit : public BaseInit { class MagiskInit : public BaseInit {
protected: protected:
raw_data self{}; raw_data self{};
int root = -1;
bool mnt_system = false; bool mnt_system = false;
bool mnt_vendor = false; bool mnt_vendor = false;
bool mnt_product = false; bool mnt_product = false;
bool mnt_odm = false; bool mnt_odm = false;
virtual void preset() = 0;
virtual void early_mount() = 0; virtual void early_mount() = 0;
bool read_dt_fstab(const char *name, char *partname, char *fstype); bool read_dt_fstab(const char *name, char *partname, char *fstype);
bool patch_sepolicy(); bool patch_sepolicy();
@ -46,7 +44,6 @@ protected:
raw_data config{}; raw_data config{};
dev_t system_dev; dev_t system_dev;
void preset() override;
void early_mount() override; void early_mount() override;
void patch_rootdir(); void patch_rootdir();
public: public:
@ -56,7 +53,9 @@ public:
class RootFSInit : public MagiskInit { class RootFSInit : public MagiskInit {
protected: protected:
void setup_rootfs(); int root = -1;
virtual void setup_rootfs();
public: public:
RootFSInit(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) {}; RootFSInit(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) {};
void start() override; void start() override;
@ -64,7 +63,6 @@ public:
class LegacyInit : public RootFSInit { class LegacyInit : public RootFSInit {
protected: protected:
void preset() override;
void early_mount() override; void early_mount() override;
public: public:
LegacyInit(char *argv[], cmdline *cmd) : RootFSInit(argv, cmd) {}; LegacyInit(char *argv[], cmdline *cmd) : RootFSInit(argv, cmd) {};
@ -72,8 +70,8 @@ public:
class SARCompatInit : public RootFSInit { class SARCompatInit : public RootFSInit {
protected: protected:
void preset() override;
void early_mount() override; void early_mount() override;
void setup_rootfs() override;
public: public:
SARCompatInit(char *argv[], cmdline *cmd) : RootFSInit(argv, cmd) {}; SARCompatInit(char *argv[], cmdline *cmd) : RootFSInit(argv, cmd) {};
}; };

View File

@ -40,14 +40,6 @@ constexpr const char wrapper[] =
; ;
void RootFSInit::setup_rootfs() { void RootFSInit::setup_rootfs() {
if (cmd->system_as_root) {
// Clone rootfs
LOGD("Clone root dir from system to rootfs\n");
int system_root = xopen("/system_root", O_RDONLY | O_CLOEXEC);
clone_dir(system_root, root, false);
close(system_root);
}
if (patch_sepolicy()) { if (patch_sepolicy()) {
constexpr char SYSTEM_INIT[] = "/system/bin/init"; constexpr char SYSTEM_INIT[] = "/system/bin/init";
// If init is symlink, copy it to rootfs so we can patch // If init is symlink, copy it to rootfs so we can patch
@ -131,6 +123,16 @@ void RootFSInit::setup_rootfs() {
close(fd); close(fd);
} }
void SARCompatInit::setup_rootfs() {
// Clone rootfs
LOGD("Clone root dir from system to rootfs\n");
int system_root = xopen("/system_root", O_RDONLY | O_CLOEXEC);
clone_dir(system_root, root, false);
close(system_root);
RootFSInit::setup_rootfs();
}
bool MagiskInit::patch_sepolicy() { bool MagiskInit::patch_sepolicy() {
bool patch_init = false; bool patch_init = false;