Fully deprecate <mount_point>/.core folder

Symlinks are preserved for backwards compatibility
This commit is contained in:
topjohnwu 2018-11-15 22:55:28 -05:00
parent ee4548230b
commit 572e078d87
4 changed files with 49 additions and 42 deletions

View File

@ -25,6 +25,7 @@
static char buf[PATH_MAX], buf2[PATH_MAX]; static char buf[PATH_MAX], buf2[PATH_MAX];
static Vector<CharArray> module_list; static Vector<CharArray> module_list;
static bool seperate_vendor;
char *system_block, *vendor_block, *magiskloop; char *system_block, *vendor_block, *magiskloop;
@ -304,27 +305,27 @@ static void set_mirror_path() {
static void exec_common_script(const char* stage) { static void exec_common_script(const char* stage) {
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
snprintf(buf2, PATH_MAX, "%s/%s.d", COREDIR, stage); snprintf(buf2, PATH_MAX, "%s/%s.d", SECURE_DIR, stage);
if (!(dir = xopendir(buf2))) if (!(dir = xopendir(buf2)))
return; return;
chdir(buf2);
while ((entry = xreaddir(dir))) { while ((entry = xreaddir(dir))) {
if (entry->d_type == DT_REG) { if (entry->d_type == DT_REG) {
snprintf(buf2, PATH_MAX, "%s/%s.d/%s", COREDIR, stage, entry->d_name); if (access(entry->d_name, X_OK) == -1)
if (access(buf2, X_OK) == -1)
continue; continue;
LOGI("%s.d: exec [%s]\n", stage, entry->d_name); LOGI("%s.d: exec [%s]\n", stage, entry->d_name);
int pid = exec_command( int pid = exec_command(0, nullptr,
0, nullptr,
strcmp(stage, "post-fs-data") ? set_path : set_mirror_path, strcmp(stage, "post-fs-data") ? set_path : set_mirror_path,
"sh", buf2, nullptr); "sh", entry->d_name, nullptr);
if (pid != -1) if (pid != -1)
waitpid(pid, nullptr, 0); waitpid(pid, nullptr, 0);
} }
} }
closedir(dir); closedir(dir);
chdir("/");
} }
static void exec_module_script(const char* stage) { static void exec_module_script(const char* stage) {
@ -427,6 +428,10 @@ static bool magisk_env() {
xmkdir(MOUNTPOINT, 0755); xmkdir(MOUNTPOINT, 0755);
xmkdir(BLOCKDIR, 0755); xmkdir(BLOCKDIR, 0755);
// Boot script directories
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
xmkdir(SECURE_DIR "/service.d", 0755);
LOGI("* Mounting mirrors"); LOGI("* Mounting mirrors");
Vector<CharArray> mounts; Vector<CharArray> mounts;
file_to_vector("/proc/mounts", mounts); file_to_vector("/proc/mounts", mounts);
@ -447,7 +452,7 @@ static bool magisk_env() {
LOGI("mount: %s\n", MIRRDIR "/system"); LOGI("mount: %s\n", MIRRDIR "/system");
#endif #endif
} else if (line.contains(" /vendor ")) { } else if (line.contains(" /vendor ")) {
seperate_vendor = 1; seperate_vendor = true;
sscanf(line, "%s %*s %s", buf, buf2); sscanf(line, "%s %*s %s", buf, buf2);
vendor_block = strdup2(buf); vendor_block = strdup2(buf);
xmkdir(MIRRDIR "/vendor", 0755); xmkdir(MIRRDIR "/vendor", 0755);
@ -475,7 +480,10 @@ static bool magisk_env() {
LOGI("* Setting up internal busybox"); LOGI("* Setting up internal busybox");
exec_command_sync(MIRRDIR "/bin/busybox", "--install", "-s", BBPATH, nullptr); exec_command_sync(MIRRDIR "/bin/busybox", "--install", "-s", BBPATH, nullptr);
xsymlink(MIRRDIR "/bin/busybox", BBPATH "/busybox"); xsymlink(MIRRDIR "/bin/busybox", BBPATH "/busybox");
return true;
}
static bool prepare_img() {
const char *alt_img[] = const char *alt_img[] =
{ "/cache/magisk.img", "/data/magisk_merge.img", "/data/adb/magisk_merge.img" }; { "/cache/magisk.img", "/data/magisk_merge.img", "/data/adb/magisk_merge.img" };
@ -496,9 +504,21 @@ static bool magisk_env() {
if (magiskloop == nullptr) if (magiskloop == nullptr)
return false; return false;
xmkdir(COREDIR, 0755); // Migrate legacy boot scripts
xmkdir(COREDIR "/post-fs-data.d", 0755); struct stat st;
xmkdir(COREDIR "/service.d", 0755); if (stat(LEGACY_CORE "/post-fs-data.d", &st) == 0 && S_ISDIR(st.st_mode)) {
cp_afc(LEGACY_CORE "/post-fs-data.d", SECURE_DIR "/post-fs-data.d");
rm_rf(LEGACY_CORE "/post-fs-data.d");
}
if (stat(LEGACY_CORE "/service.d", &st) == 0 && S_ISDIR(st.st_mode)) {
cp_afc(LEGACY_CORE "/service.d", SECURE_DIR "/service.d");
rm_rf(LEGACY_CORE "/service.d");
}
// Links for legacy paths
xmkdir(LEGACY_CORE, 0755);
symlink(SECURE_DIR "/post-fs-data.d", LEGACY_CORE "/post-fs-data.d");
symlink(SECURE_DIR "/service.d", LEGACY_CORE "/service.d");
DIR *dir = xopendir(MOUNTPOINT); DIR *dir = xopendir(MOUNTPOINT);
struct dirent *entry; struct dirent *entry;
@ -581,18 +601,16 @@ static bool check_data() {
extern int launch_magiskhide(int client = -1); extern int launch_magiskhide(int client = -1);
static void *start_magisk_hide(void *) {
launch_magiskhide();
return nullptr;
}
static void auto_start_magiskhide() { static void auto_start_magiskhide() {
if (!start_log_daemon()) if (!start_log_daemon())
return; return;
CharArray hide_prop = getprop(MAGISKHIDE_PROP, true); CharArray hide_prop = getprop(MAGISKHIDE_PROP, true);
if (hide_prop != "0") { if (hide_prop != "0") {
pthread_t thread; pthread_t thread;
xpthread_create(&thread, nullptr, start_magisk_hide, nullptr); xpthread_create(&thread, nullptr, [](void*) -> void* {
launch_magiskhide();
return nullptr;
}, nullptr);
pthread_detach(thread); pthread_detach(thread);
} }
} }
@ -622,7 +640,7 @@ void unlock_blocks() {
* Entry points * * Entry points *
****************/ ****************/
static void unblock_boot_process() { [[noreturn]] static void unblock_boot_process() {
close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0)); close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0));
pthread_exit(nullptr); pthread_exit(nullptr);
} }
@ -751,7 +769,7 @@ void startup() {
execl("/sbin/magisk.bin", "magisk", "--post-fs-data", nullptr); execl("/sbin/magisk.bin", "magisk", "--post-fs-data", nullptr);
} }
static void core_only() { [[noreturn]] static void core_only() {
auto_start_magiskhide(); auto_start_magiskhide();
unblock_boot_process(); unblock_boot_process();
} }
@ -761,23 +779,24 @@ void post_fs_data(int client) {
write_int(client, 0); write_int(client, 0);
close(client); close(client);
// If post-fs-data mode is started, it means startup succeeded
setup_done = 1;
xmount(nullptr, "/", nullptr, MS_REMOUNT | MS_RDONLY, nullptr); xmount(nullptr, "/", nullptr, MS_REMOUNT | MS_RDONLY, nullptr);
LOGI("** post-fs-data mode running\n"); LOGI("** post-fs-data mode running\n");
// If magisk environment setup failed, only run core only operations
if (!magisk_env()) { if (!magisk_env()) {
free(magiskloop); LOGE("* Magisk environment setup incomplete, abort\n");
magiskloop = nullptr; unblock_boot_process();
core_only();
return;
} }
start_log_daemon(); start_log_daemon();
if (!prepare_img()) {
LOGE("* Magisk image mount failed, switch to core-only mode\n");
free(magiskloop);
magiskloop = nullptr;
creat(DISABLEFILE, 0644);
}
restorecon(); restorecon();
chmod(SECURE_DIR, 0700); chmod(SECURE_DIR, 0700);
@ -786,10 +805,8 @@ void post_fs_data(int client) {
exec_common_script("post-fs-data"); exec_common_script("post-fs-data");
// Core only mode // Core only mode
if (access(DISABLEFILE, F_OK) == 0) { if (access(DISABLEFILE, F_OK) == 0)
core_only(); core_only();
return;
}
// Execute module scripts // Execute module scripts
LOGI("* Running module post-fs-data scripts\n"); LOGI("* Running module post-fs-data scripts\n");
@ -858,12 +875,8 @@ void late_start(int client) {
if (access(SECURE_DIR, F_OK) != 0) { if (access(SECURE_DIR, F_OK) != 0) {
// It's safe to create the folder at this point if the system didn't create it // It's safe to create the folder at this point if the system didn't create it
xmkdir(SECURE_DIR, 0700); xmkdir(SECURE_DIR, 0700);
} // And reboot to make proper setup possible
if (!setup_done) {
// The setup failed for some reason, reboot and try again
exec_command_sync("/system/bin/reboot", nullptr); exec_command_sync("/system/bin/reboot", nullptr);
return;
} }
auto_start_magiskhide(); auto_start_magiskhide();

View File

@ -20,9 +20,6 @@
#include "selinux.h" #include "selinux.h"
#include "flags.h" #include "flags.h"
int setup_done = 0;
int seperate_vendor = 0;
static void get_client_cred(int fd, struct ucred *cred) { static void get_client_cred(int fd, struct ucred *cred) {
socklen_t ucred_length = sizeof(*cred); socklen_t ucred_length = sizeof(*cred);
if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &ucred_length)) if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &ucred_length))

View File

@ -9,9 +9,6 @@
#include <sys/un.h> #include <sys/un.h>
#include <sys/socket.h> #include <sys/socket.h>
extern int setup_done;
extern int seperate_vendor;
// Commands require connecting to daemon // Commands require connecting to daemon
enum { enum {
DO_NOTHING = 0, DO_NOTHING = 0,

View File

@ -17,7 +17,7 @@
#define MIRRDIR MAGISKTMP "/mirror" #define MIRRDIR MAGISKTMP "/mirror"
#define BBPATH MAGISKTMP "/busybox" #define BBPATH MAGISKTMP "/busybox"
#define MOUNTPOINT MAGISKTMP "/img" #define MOUNTPOINT MAGISKTMP "/img"
#define COREDIR MOUNTPOINT "/.core" #define LEGACY_CORE MOUNTPOINT "/.core"
#define SECURE_DIR "/data/adb" #define SECURE_DIR "/data/adb"
#define MAINIMG SECURE_DIR "/magisk.img" #define MAINIMG SECURE_DIR "/magisk.img"
#define DATABIN SECURE_DIR "/magisk" #define DATABIN SECURE_DIR "/magisk"