Cleanup inheritance

This commit is contained in:
topjohnwu 2022-03-14 04:22:09 -07:00
parent 810d27a618
commit b10379e700
5 changed files with 44 additions and 53 deletions

View File

@ -56,7 +56,7 @@ static int dump_manager(const char *path, mode_t mode) {
class RecoveryInit : public BaseInit { class RecoveryInit : public BaseInit {
public: public:
RecoveryInit(char *argv[], BootConfig *cmd) : BaseInit(argv, cmd) {} using BaseInit::BaseInit;
void start() override { void start() override {
LOGD("Ramdisk is recovery, abort\n"); LOGD("Ramdisk is recovery, abort\n");
rename(backup_init(), "/init"); rename(backup_init(), "/init");
@ -144,14 +144,13 @@ int main(int argc, char *argv[]) {
BootConfig config{}; BootConfig config{};
if (argc > 1 && argv[1] == "selinux_setup"sv) { if (argc > 1 && argv[1] == "selinux_setup"sv) {
setup_klog();
init = new 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(&config); load_kernel_info(&config);
if (config.skip_initramfs) if (config.skip_initramfs)
init = new SARInit(argv, &config); init = new LegacySARInit(argv, &config);
else if (config.force_normal_boot) else if (config.force_normal_boot)
init = new FirstStageInit(argv, &config); init = new FirstStageInit(argv, &config);
else if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0) else if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0)

View File

@ -59,7 +59,7 @@ protected:
[[noreturn]] void exec_init(); [[noreturn]] void exec_init();
void read_dt_fstab(std::vector<fstab_entry> &fstab); void read_dt_fstab(std::vector<fstab_entry> &fstab);
public: public:
BaseInit(char *argv[], BootConfig *config) : config(config), argv(argv) {} BaseInit(char *argv[], BootConfig *config = nullptr) : config(config), argv(argv) {}
virtual ~BaseInit() = default; virtual ~BaseInit() = default;
virtual void start() = 0; virtual void start() = 0;
}; };
@ -67,7 +67,7 @@ public:
class MagiskInit : public BaseInit { class MagiskInit : public BaseInit {
protected: protected:
mmap_data self; mmap_data self;
mmap_data magisk_config; mmap_data magisk_cfg;
std::string custom_rules_dir; std::string custom_rules_dir;
#if ENABLE_AVD_HACK #if ENABLE_AVD_HACK
@ -83,19 +83,20 @@ protected:
bool patch_sepolicy(const char *file); bool patch_sepolicy(const char *file);
void setup_tmp(const char *path); void setup_tmp(const char *path);
void mount_rules_dir(const char *dev_base, const char *mnt_base); void mount_rules_dir(const char *dev_base, const char *mnt_base);
void patch_rw_root();
public: public:
MagiskInit(char *argv[], BootConfig *cmd) : BaseInit(argv, cmd) {} using BaseInit::BaseInit;
}; };
class SARBase : virtual public MagiskInit { class SARBase : public MagiskInit {
protected: protected:
std::vector<raw_file> overlays; std::vector<raw_file> overlays;
void backup_files(); void backup_files();
void patch_rootdir(); void patch_ro_root();
void mount_system_root(); void mount_system_root();
public: public:
SARBase() = default; using MagiskInit::MagiskInit;
}; };
/*************** /***************
@ -106,7 +107,7 @@ class FirstStageInit : public BaseInit {
private: private:
void prepare(); void prepare();
public: public:
FirstStageInit(char *argv[], BootConfig *cmd) : BaseInit(argv, cmd) { FirstStageInit(char *argv[], BootConfig *config) : BaseInit(argv, config) {
LOGD("%s\n", __FUNCTION__); LOGD("%s\n", __FUNCTION__);
}; };
void start() override { void start() override {
@ -115,23 +116,41 @@ public:
} }
}; };
class SecondStageInit : public SARBase {
private:
bool prepare();
public:
SecondStageInit(char *argv[]) : SARBase(argv) {
setup_klog();
LOGD("%s\n", __FUNCTION__);
};
void start() override {
if (prepare())
patch_rw_root();
else
patch_ro_root();
exec_init();
}
};
/************* /*************
* Legacy SAR * Legacy SAR
*************/ *************/
class SARInit : public SARBase { class LegacySARInit : public SARBase {
private: private:
bool early_mount(); bool early_mount();
void first_stage_prep(); void first_stage_prep();
public: public:
SARInit(char *argv[], BootConfig *cmd) : MagiskInit(argv, cmd) { LegacySARInit(char *argv[], BootConfig *config) : SARBase(argv, config) {
LOGD("%s\n", __FUNCTION__); LOGD("%s\n", __FUNCTION__);
}; };
void start() override { void start() override {
if (early_mount()) if (early_mount())
first_stage_prep(); first_stage_prep();
else else
patch_rootdir(); patch_ro_root();
exec_init(); exec_init();
} }
}; };
@ -140,50 +159,23 @@ public:
* Initramfs * Initramfs
************/ ************/
class RootFSBase : virtual public MagiskInit { class RootFSInit : public MagiskInit {
protected:
void patch_rootfs();
public:
RootFSBase() = default;
void start() = 0;
};
class RootFSInit : public RootFSBase {
private: private:
void early_mount(); void early_mount();
public: public:
RootFSInit(char *argv[], BootConfig *cmd) : MagiskInit(argv, cmd) { RootFSInit(char *argv[], BootConfig *config) : MagiskInit(argv, config) {
LOGD("%s\n", __FUNCTION__); LOGD("%s\n", __FUNCTION__);
} }
void start() override { void start() override {
early_mount(); early_mount();
patch_rootfs(); patch_rw_root();
exec_init(); exec_init();
} }
}; };
class SecondStageInit : public RootFSBase, public SARBase {
private:
bool prepare();
public:
SecondStageInit(char *argv[]) : MagiskInit(argv, nullptr) {
LOGD("%s\n", __FUNCTION__);
};
void start() override {
if (prepare())
patch_rootfs();
else
patch_rootdir();
exec_init();
}
};
class MagiskProxy : public MagiskInit { class MagiskProxy : public MagiskInit {
public: public:
explicit MagiskProxy(char *argv[]) : MagiskInit(argv, nullptr) { explicit MagiskProxy(char *argv[]) : MagiskInit(argv) {
LOGD("%s\n", __FUNCTION__); LOGD("%s\n", __FUNCTION__);
} }
void start() override; void start() override;

View File

@ -311,9 +311,9 @@ void SARBase::backup_files() {
self = mmap_data("/proc/self/exe"); self = mmap_data("/proc/self/exe");
if (access("/.backup/.magisk", R_OK) == 0) if (access("/.backup/.magisk", R_OK) == 0)
magisk_config = mmap_data("/.backup/.magisk"); magisk_cfg = mmap_data("/.backup/.magisk");
else if (access("/data/.backup/.magisk", R_OK) == 0) else if (access("/data/.backup/.magisk", R_OK) == 0)
magisk_config = mmap_data("/data/.backup/.magisk"); magisk_cfg = mmap_data("/data/.backup/.magisk");
} }
void SARBase::mount_system_root() { void SARBase::mount_system_root() {
@ -350,7 +350,7 @@ mount_root:
xmount("/dev/root", "/system_root", "erofs", MS_RDONLY, nullptr); xmount("/dev/root", "/system_root", "erofs", MS_RDONLY, nullptr);
} }
bool SARInit::early_mount() { bool LegacySARInit::early_mount() {
backup_files(); backup_files();
mount_system_root(); mount_system_root();
switch_root("/system_root"); switch_root("/system_root");
@ -394,7 +394,7 @@ void MagiskInit::setup_tmp(const char *path) {
xmkdir(BLOCKDIR, 0); xmkdir(BLOCKDIR, 0);
int fd = xopen(INTLROOT "/config", O_WRONLY | O_CREAT, 0); int fd = xopen(INTLROOT "/config", O_WRONLY | O_CREAT, 0);
xwrite(fd, magisk_config.buf, magisk_config.sz); xwrite(fd, magisk_cfg.buf, magisk_cfg.sz);
close(fd); close(fd);
fd = xopen("magiskinit", O_WRONLY | O_CREAT, 0755); fd = xopen("magiskinit", O_WRONLY | O_CREAT, 0755);
xwrite(fd, self.buf, self.sz); xwrite(fd, self.buf, self.sz);

View File

@ -202,7 +202,7 @@ static void patch_socket_name(const char *path) {
#define MONOPOLICY "/sepolicy" #define MONOPOLICY "/sepolicy"
#define NEW_INITRC "/system/etc/init/hw/init.rc" #define NEW_INITRC "/system/etc/init/hw/init.rc"
void SARBase::patch_rootdir() { void SARBase::patch_ro_root() {
string tmp_dir; string tmp_dir;
const char *sepol; const char *sepol;
@ -327,7 +327,7 @@ void SARBase::patch_rootdir() {
#define TMP_MNTDIR "/dev/mnt" #define TMP_MNTDIR "/dev/mnt"
#define TMP_RULESDIR "/.backup/.sepolicy.rules" #define TMP_RULESDIR "/.backup/.sepolicy.rules"
void RootFSBase::patch_rootfs() { void MagiskInit::patch_rw_root() {
// Create hardlink mirror of /sbin to /root // Create hardlink mirror of /sbin to /root
mkdir("/root", 0777); mkdir("/root", 0777);
clone_attr("/sbin", "/root"); clone_attr("/sbin", "/root");
@ -378,7 +378,7 @@ void MagiskProxy::start() {
// Backup stuffs before removing them // Backup stuffs before removing them
self = mmap_data("/sbin/magisk"); self = mmap_data("/sbin/magisk");
magisk_config = mmap_data("/.backup/.magisk"); magisk_cfg = mmap_data("/.backup/.magisk");
auto magisk = mmap_data("/sbin/magisk32.xz"); auto magisk = mmap_data("/sbin/magisk32.xz");
auto magisk64 = mmap_data("/sbin/magisk64.xz"); auto magisk64 = mmap_data("/sbin/magisk64.xz");
char custom_rules_dir[64]; char custom_rules_dir[64];

View File

@ -30,7 +30,7 @@ void FirstStageInit::prepare() {
cp_afc("overlay.d", "/data/overlay.d"); cp_afc("overlay.d", "/data/overlay.d");
} }
void SARInit::first_stage_prep() { void LegacySARInit::first_stage_prep() {
xmount("tmpfs", "/data", "tmpfs", 0, "mode=755"); xmount("tmpfs", "/data", "tmpfs", 0, "mode=755");
// Patch init binary // Patch init binary
@ -57,7 +57,7 @@ void SARInit::first_stage_prep() {
xmkdir("/data/overlay.d", 0); xmkdir("/data/overlay.d", 0);
restore_folder("/data/overlay.d", overlays); restore_folder("/data/overlay.d", overlays);
int cfg = xopen("/data/.backup/config", O_WRONLY | O_CREAT, 0); int cfg = xopen("/data/.backup/config", O_WRONLY | O_CREAT, 0);
xwrite(cfg, magisk_config.buf, magisk_config.sz); xwrite(cfg, magisk_cfg.buf, magisk_cfg.sz);
close(cfg); close(cfg);
} }