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 <string.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) {
char path[128];
int fd;

View File

@ -1,10 +1,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/sysmacros.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <libgen.h>
@ -48,7 +45,7 @@ static void setup_klog() {
// Prevent file descriptor confusion
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");
xdup3(null, STDIN_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;
}
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 {
public:
RecoveryInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
@ -161,7 +126,7 @@ public:
LOGD("Ramdisk is recovery, abort\n");
rename("/.backup/init", "/init");
rm_rf("/.backup");
re_exec_init();
exec_init();
}
};

View File

@ -1,3 +1,5 @@
#include <sys/mount.h>
#include <unistd.h>
#include <stdlib.h>
struct cmdline {
@ -21,8 +23,16 @@ protected:
cmdline *cmd;
char **argv;
void re_exec_init();
virtual void cleanup();
void exec_init(const char *init = "/init") {
cleanup();
execv(init, argv);
exit(1);
}
virtual void cleanup() {
umount("/sys");
umount("/proc");
umount("/dev");
}
public:
BaseInit(char *argv[], cmdline *cmd) : cmd(cmd), argv(argv) {}
virtual ~BaseInit() = default;
@ -52,7 +62,11 @@ protected:
virtual void setup_rootfs();
public:
RootFSInit(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) {};
void start() override;
void start() override {
early_mount();
setup_rootfs();
exec_init();
}
};
class SARCommon : public MagiskInit {
@ -63,7 +77,11 @@ protected:
void patch_rootdir();
public:
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();
public:
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 {
@ -120,13 +141,6 @@ public:
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);
int dump_magisk(const char *path, mode_t mode);
int magisk_proxy_main(int argc, char *argv[]);

View File

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