Small init refactoring
This commit is contained in:
parent
3e275b7dba
commit
4bb8ad19cf
@ -106,7 +106,7 @@ LOCAL_C_INCLUDES := \
|
|||||||
|
|
||||||
LOCAL_SRC_FILES := \
|
LOCAL_SRC_FILES := \
|
||||||
init/init.cpp \
|
init/init.cpp \
|
||||||
init/early_mount.cpp \
|
init/mount.cpp \
|
||||||
init/rootdir.cpp \
|
init/rootdir.cpp \
|
||||||
init/getinfo.cpp \
|
init/getinfo.cpp \
|
||||||
magiskpolicy/api.cpp \
|
magiskpolicy/api.cpp \
|
||||||
|
@ -112,7 +112,7 @@ void load_kernel_info(cmdline *cmd) {
|
|||||||
cmd->slot[0] = '_';
|
cmd->slot[0] = '_';
|
||||||
strcpy(cmd->slot + 1, value);
|
strcpy(cmd->slot + 1, value);
|
||||||
} else if (key == "skip_initramfs") {
|
} else if (key == "skip_initramfs") {
|
||||||
cmd->system_as_root = true;
|
cmd->skip_initramfs = true;
|
||||||
} else if (key == "androidboot.force_normal_boot") {
|
} else if (key == "androidboot.force_normal_boot") {
|
||||||
cmd->force_normal_boot = value[0] == '1';
|
cmd->force_normal_boot = value[0] == '1';
|
||||||
} else if (key == "androidboot.android_dt_dir") {
|
} else if (key == "androidboot.android_dt_dir") {
|
||||||
@ -143,13 +143,13 @@ void load_kernel_info(cmdline *cmd) {
|
|||||||
|
|
||||||
if (recovery_mode) {
|
if (recovery_mode) {
|
||||||
LOGD("Running in recovery mode, waiting for key...\n");
|
LOGD("Running in recovery mode, waiting for key...\n");
|
||||||
cmd->system_as_root = !check_key_combo();
|
cmd->skip_initramfs = !check_key_combo();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmd->dt_dir[0] == '\0')
|
if (cmd->dt_dir[0] == '\0')
|
||||||
strcpy(cmd->dt_dir, DEFAULT_DT_DIR);
|
strcpy(cmd->dt_dir, DEFAULT_DT_DIR);
|
||||||
|
|
||||||
LOGD("system_as_root=[%d]\n", cmd->system_as_root);
|
LOGD("skip_initramfs=[%d]\n", cmd->skip_initramfs);
|
||||||
LOGD("force_normal_boot=[%d]\n", cmd->force_normal_boot);
|
LOGD("force_normal_boot=[%d]\n", cmd->force_normal_boot);
|
||||||
LOGD("slot=[%s]\n", cmd->slot);
|
LOGD("slot=[%s]\n", cmd->slot);
|
||||||
LOGD("dt_dir=[%s]\n", cmd->dt_dir);
|
LOGD("dt_dir=[%s]\n", cmd->dt_dir);
|
||||||
|
@ -75,12 +75,12 @@ static bool unxz(int fd, const uint8_t *buf, size_t size) {
|
|||||||
xz_crc32_init();
|
xz_crc32_init();
|
||||||
struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26);
|
struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26);
|
||||||
struct xz_buf b = {
|
struct xz_buf b = {
|
||||||
.in = buf,
|
.in = buf,
|
||||||
.in_pos = 0,
|
.in_pos = 0,
|
||||||
.in_size = size,
|
.in_size = size,
|
||||||
.out = out,
|
.out = out,
|
||||||
.out_pos = 0,
|
.out_pos = 0,
|
||||||
.out_size = sizeof(out)
|
.out_size = sizeof(out)
|
||||||
};
|
};
|
||||||
enum xz_ret ret;
|
enum xz_ret ret;
|
||||||
do {
|
do {
|
||||||
@ -147,11 +147,11 @@ class TestInit : public BaseInit {
|
|||||||
public:
|
public:
|
||||||
TestInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
|
TestInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
|
||||||
void start() override {
|
void start() override {
|
||||||
// Write init tests here
|
// Place init tests here
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static void setup_test(const char *dir) {
|
[[maybe_unused]] static int test_main(int argc, char *argv[]) {
|
||||||
// Log to console
|
// Log to console
|
||||||
cmdline_logging();
|
cmdline_logging();
|
||||||
log_cb.ex = nop_ex;
|
log_cb.ex = nop_ex;
|
||||||
@ -167,13 +167,21 @@ static void setup_test(const char *dir) {
|
|||||||
mounts.emplace_back(me->mnt_dir);
|
mounts.emplace_back(me->mnt_dir);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
for (auto m = mounts.rbegin(); m != mounts.rend(); ++m)
|
for (auto &m : reversed(mounts))
|
||||||
xumount(m->data());
|
xumount(m.data());
|
||||||
|
|
||||||
// chroot jail
|
// chroot jail
|
||||||
chdir(dir);
|
chdir(dirname(argv[0]));
|
||||||
chroot(".");
|
chroot(".");
|
||||||
chdir("/");
|
chdir("/");
|
||||||
|
|
||||||
|
cmdline cmd{};
|
||||||
|
load_kernel_info(&cmd);
|
||||||
|
|
||||||
|
auto init = make_unique<TestInit>(argv, &cmd);
|
||||||
|
init->start();
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
@ -184,6 +192,11 @@ int main(int argc, char *argv[]) {
|
|||||||
return (*init_applet_main[i])(argc, argv);
|
return (*init_applet_main[i])(argc, argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef MAGISK_DEBUG
|
||||||
|
if (getenv("INIT_TEST") != nullptr)
|
||||||
|
return test_main(argc, argv);
|
||||||
|
#endif
|
||||||
|
|
||||||
if (argc > 1 && strcmp(argv[1], "-x") == 0) {
|
if (argc > 1 && strcmp(argv[1], "-x") == 0) {
|
||||||
if (strcmp(argv[2], "magisk") == 0)
|
if (strcmp(argv[2], "magisk") == 0)
|
||||||
return dump_magisk(argv[3], 0755);
|
return dump_magisk(argv[3], 0755);
|
||||||
@ -191,34 +204,22 @@ int main(int argc, char *argv[]) {
|
|||||||
return dump_manager(argv[3], 0644);
|
return dump_manager(argv[3], 0644);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (getpid() != 1)
|
||||||
|
return 1;
|
||||||
|
setup_klog();
|
||||||
|
|
||||||
if (argc > 1 && argv[1] == "selinux_setup"sv) {
|
if (argc > 1 && argv[1] == "selinux_setup"sv) {
|
||||||
auto init = make_unique<SecondStageInit>(argv);
|
auto init = make_unique<SecondStageInit>(argv);
|
||||||
init->start();
|
init->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MAGISK_DEBUG
|
|
||||||
bool run_test = getenv("INIT_TEST") != nullptr;
|
|
||||||
#else
|
|
||||||
constexpr bool run_test = false;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (run_test) {
|
|
||||||
setup_test(dirname(argv[0]));
|
|
||||||
} else {
|
|
||||||
if (getpid() != 1)
|
|
||||||
return 1;
|
|
||||||
setup_klog();
|
|
||||||
}
|
|
||||||
|
|
||||||
cmdline cmd{};
|
cmdline cmd{};
|
||||||
load_kernel_info(&cmd);
|
load_kernel_info(&cmd);
|
||||||
|
|
||||||
unique_ptr<BaseInit> init;
|
unique_ptr<BaseInit> init;
|
||||||
if (run_test) {
|
if (cmd.force_normal_boot) {
|
||||||
init = make_unique<TestInit>(argv, &cmd);
|
|
||||||
} else if (cmd.force_normal_boot) {
|
|
||||||
init = make_unique<ABFirstStageInit>(argv, &cmd);
|
init = make_unique<ABFirstStageInit>(argv, &cmd);
|
||||||
} else if (cmd.system_as_root) {
|
} else if (cmd.skip_initramfs) {
|
||||||
if (access("/overlay", F_OK) == 0) /* Compatible mode */
|
if (access("/overlay", F_OK) == 0) /* Compatible mode */
|
||||||
init = make_unique<SARCompatInit>(argv, &cmd);
|
init = make_unique<SARCompatInit>(argv, &cmd);
|
||||||
else
|
else
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
struct cmdline {
|
struct cmdline {
|
||||||
bool system_as_root;
|
bool skip_initramfs;
|
||||||
bool force_normal_boot;
|
bool force_normal_boot;
|
||||||
char slot[3];
|
char slot[3];
|
||||||
char dt_dir[128];
|
char dt_dir[128];
|
||||||
|
Loading…
Reference in New Issue
Block a user