Init code rearrangement

This commit is contained in:
topjohnwu 2019-06-30 11:39:13 -07:00
parent e8b73ba6d1
commit db8dd9f186
4 changed files with 46 additions and 63 deletions

View File

@ -1,4 +1,3 @@
#include <sys/mount.h>
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -77,6 +76,13 @@ static dev_t setup_block(const char *partname, char *block_dev = nullptr) {
} }
} }
static bool is_lnk(const char *name) {
struct stat st;
if (lstat(name, &st))
return false;
return S_ISLNK(st.st_mode);
}
bool MagiskInit::read_dt_fstab(const char *name, char *partname, char *fstype) { bool MagiskInit::read_dt_fstab(const char *name, char *partname, char *fstype) {
char path[128]; char path[128];
int fd; int fd;

View File

@ -1,10 +1,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/mount.h>
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
@ -48,7 +45,7 @@ static void setup_klog() {
// Prevent file descriptor confusion // Prevent file descriptor confusion
mknod("/null", S_IFCHR | 0666, makedev(1, 3)); mknod("/null", S_IFCHR | 0666, makedev(1, 3));
int null = open("/null", O_RDWR | O_CLOEXEC); int null = xopen("/null", O_RDWR | O_CLOEXEC);
unlink("/null"); unlink("/null");
xdup3(null, STDIN_FILENO, O_CLOEXEC); xdup3(null, STDIN_FILENO, O_CLOEXEC);
xdup3(null, STDOUT_FILENO, O_CLOEXEC); xdup3(null, STDOUT_FILENO, O_CLOEXEC);
@ -122,38 +119,6 @@ static int dump_manager(const char *path, mode_t mode) {
return 0; return 0;
} }
void BaseInit::cleanup() {
umount("/sys");
umount("/proc");
umount("/dev");
}
void BaseInit::re_exec_init() {
LOGD("Re-exec /init\n");
cleanup();
execv("/init", argv);
exit(1);
}
void RootFSInit::start() {
early_mount();
setup_rootfs();
re_exec_init();
}
void SARCommon::start() {
early_mount();
patch_rootdir();
re_exec_init();
}
void FirstStageInit::start() {
patch_fstab();
cleanup();
execv("/system/bin/init", argv);
exit(1);
}
class RecoveryInit : public BaseInit { class RecoveryInit : public BaseInit {
public: public:
RecoveryInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {}; RecoveryInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
@ -161,7 +126,7 @@ public:
LOGD("Ramdisk is recovery, abort\n"); LOGD("Ramdisk is recovery, abort\n");
rename("/.backup/init", "/init"); rename("/.backup/init", "/init");
rm_rf("/.backup"); rm_rf("/.backup");
re_exec_init(); exec_init();
} }
}; };

View File

@ -1,3 +1,5 @@
#include <sys/mount.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
struct cmdline { struct cmdline {
@ -21,8 +23,16 @@ protected:
cmdline *cmd; cmdline *cmd;
char **argv; char **argv;
void re_exec_init(); void exec_init(const char *init = "/init") {
virtual void cleanup(); cleanup();
execv(init, argv);
exit(1);
}
virtual void cleanup() {
umount("/sys");
umount("/proc");
umount("/dev");
}
public: public:
BaseInit(char *argv[], cmdline *cmd) : cmd(cmd), argv(argv) {} BaseInit(char *argv[], cmdline *cmd) : cmd(cmd), argv(argv) {}
virtual ~BaseInit() = default; virtual ~BaseInit() = default;
@ -52,7 +62,11 @@ protected:
virtual void setup_rootfs(); 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 {
early_mount();
setup_rootfs();
exec_init();
}
}; };
class SARCommon : public MagiskInit { class SARCommon : public MagiskInit {
@ -63,7 +77,11 @@ protected:
void patch_rootdir(); void patch_rootdir();
public: public:
SARCommon(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) {}; SARCommon(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) {};
void start() override; void start() override {
early_mount();
patch_rootdir();
exec_init();
}
}; };
/* ******************* /* *******************
@ -75,7 +93,10 @@ protected:
void patch_fstab(); void patch_fstab();
public: public:
FirstStageInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {}; FirstStageInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
void start() override; void start() override {
patch_fstab();
exec_init("/system/bin/init");
}
}; };
class SecondStageInit : public SARCommon { class SecondStageInit : public SARCommon {
@ -120,13 +141,6 @@ public:
SARCompatInit(char *argv[], cmdline *cmd) : RootFSInit(argv, cmd) {}; SARCompatInit(char *argv[], cmdline *cmd) : RootFSInit(argv, cmd) {};
}; };
static inline bool is_lnk(const char *name) {
struct stat st;
if (lstat(name, &st))
return false;
return S_ISLNK(st.st_mode);
}
void load_kernel_info(cmdline *cmd); void load_kernel_info(cmdline *cmd);
int dump_magisk(const char *path, mode_t mode); int dump_magisk(const char *path, mode_t mode);
int magisk_proxy_main(int argc, char *argv[]); int magisk_proxy_main(int argc, char *argv[]);

View File

@ -1,6 +1,5 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/mount.h>
#include <fcntl.h> #include <fcntl.h>
#include <magisk.h> #include <magisk.h>
@ -396,19 +395,18 @@ 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
{ char path[256];
char path[256]; int sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC);
int sbin = xopen("/sbin", O_RDONLY | O_CLOEXEC); DIR *dir = xopendir("/root");
unique_ptr<DIR, decltype(&closedir)> dir(xopendir("/root"), &closedir); struct dirent *entry;
struct dirent *entry; while((entry = xreaddir(dir))) {
while((entry = xreaddir(dir.get()))) { if (entry->d_name == "."sv || entry->d_name == ".."sv)
if (entry->d_name == "."sv || entry->d_name == ".."sv) continue;
continue; sprintf(path, "/root/%s", entry->d_name);
sprintf(path, "/root/%s", entry->d_name); xsymlinkat(path, sbin, entry->d_name);
xsymlinkat(path, sbin, entry->d_name);
}
close(sbin);
} }
close(sbin);
closedir(dir);
setenv("REMOUNT_ROOT", "1", 1); setenv("REMOUNT_ROOT", "1", 1);
execv("/sbin/magisk", argv); execv("/sbin/magisk", argv);