From 8929a18a1032b9b4c21875383a5d54bd2703939e Mon Sep 17 00:00:00 2001 From: Ziyang Zhou Date: Sun, 9 May 2021 23:09:00 +0800 Subject: [PATCH 1/6] fix booting --- fs_mgr/libfstab/boot_config.cpp | 6 ++++-- init/first_stage_init.cpp | 22 ++++++++++++++++++---- init/service.cpp | 3 ++- rootdir/init.rc | 1 - 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/fs_mgr/libfstab/boot_config.cpp b/fs_mgr/libfstab/boot_config.cpp index b21495e..15e597f 100644 --- a/fs_mgr/libfstab/boot_config.cpp +++ b/fs_mgr/libfstab/boot_config.cpp @@ -154,13 +154,15 @@ bool GetKernelCmdlineFromString(const std::string& cmdline, const std::string& k void ImportKernelCmdline(const std::function& fn) { std::string cmdline; - android::base::ReadFileToString("/proc/cmdline", &cmdline); + android::base::ReadFileToString("/proc/self/cmdline", &cmdline); + std::replace(cmdline.begin(), cmdline.end(), '\0', ' '); ImportKernelCmdlineFromString(android::base::Trim(cmdline), fn); } bool GetKernelCmdline(const std::string& key, std::string* out) { std::string cmdline; - android::base::ReadFileToString("/proc/cmdline", &cmdline); + android::base::ReadFileToString("/proc/self/cmdline", &cmdline); + std::replace(cmdline.begin(), cmdline.end(), '\0', ' '); return GetKernelCmdlineFromString(android::base::Trim(cmdline), key, out); } diff --git a/init/first_stage_init.cpp b/init/first_stage_init.cpp index c4d0f75..fdad43a 100644 --- a/init/first_stage_init.cpp +++ b/init/first_stage_init.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -334,6 +335,8 @@ int FirstStageMain(int argc, char** argv) { CHECKCALL(mkdir("/dev/pts", 0755)); CHECKCALL(mkdir("/dev/socket", 0755)); CHECKCALL(mkdir("/dev/dm-user", 0755)); + mount("/system/etc", "/etc", "none", MS_BIND, NULL); // cgroup fix + unshare(CLONE_NEWCGROUP); CHECKCALL(mount("devpts", "/dev/pts", "devpts", 0, NULL)); #define MAKE_STR(x) __STRING(x) CHECKCALL(mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC))); @@ -397,7 +400,6 @@ int FirstStageMain(int argc, char** argv) { for (const auto& [error_string, error_errno] : errors) { LOG(ERROR) << error_string << " " << strerror(error_errno); } - LOG(FATAL) << "Init encountered errors starting first stage, aborting"; } LOG(INFO) << "init first stage started!"; @@ -515,6 +517,7 @@ int FirstStageMain(int argc, char** argv) { if (!fsm) { fsm = CreateFirstStageMount(cmdline); } + #if 0 if (!fsm) { LOG(FATAL) << "FirstStageMount not available"; } @@ -524,8 +527,9 @@ int FirstStageMain(int argc, char** argv) { } if (!fsm->DoFirstStageMount()) { - LOG(FATAL) << "Failed to mount required partitions early ..."; + LOG(ERROR) << "Failed to mount required partitions early ..."; } + #endif } struct stat new_root_info {}; @@ -544,12 +548,22 @@ int FirstStageMain(int argc, char** argv) { 1); const char* path = "/system/bin/init"; - const char* args[] = {path, "selinux_setup", nullptr}; + std::vector args = {path, "second_stage"}; + std::string init_cmdline; + android::base::ReadFileToString("/proc/self/cmdline", &init_cmdline); + std::replace(init_cmdline.begin(), init_cmdline.end(), '\0', ' '); + auto cmd_vector = android::base::Split(android::base::Trim(init_cmdline), " "); + int i = 0; + for (const auto& entry : cmd_vector) { + if (i++ == 0) continue; // ignore first arg '/init' + args.push_back(entry.c_str()); + } + args.push_back(nullptr); auto fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); close(fd); - execv(path, const_cast(args)); + execv(path, const_cast(args.data())); // execv() only returns if an error happened, in which case we // panic and never fall through this conditional. diff --git a/init/service.cpp b/init/service.cpp index eb24dd5..9ab2f94 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -76,6 +76,7 @@ namespace android { namespace init { static Result ComputeContextFromExecutable(const std::string& service_path) { + se_hack1("HACKED"); std::string computed_context; char* raw_con = nullptr; @@ -367,7 +368,7 @@ void Service::Reap(const siginfo_t& siginfo) { if (!GetBoolProperty("init.svc_debug.no_fatal." + name_, false)) { // Aborts into `fatal_reboot_target_'. SetFatalRebootTarget(fatal_reboot_target_); - LOG(FATAL) << "critical process '" << name_ << "' exited 4 times " + LOG(ERROR) << "critical process '" << name_ << "' exited 4 times " << exit_reason; } } else { diff --git a/rootdir/init.rc b/rootdir/init.rc index 8a2ed9f..e14d7f1 100644 --- a/rootdir/init.rc +++ b/rootdir/init.rc @@ -572,7 +572,6 @@ on post-fs # Once everything is setup, no need to modify /. # The bind+remount combination allows this to work in containers. - mount rootfs rootfs / remount bind ro nodev # Mount default storage into root namespace mount none /mnt/user/0 /storage bind rec -- 2.49.0