1
mirror of https://github.com/topjohnwu/Magisk synced 2024-11-14 22:28:37 +01:00

Adjust several operation orders

This commit is contained in:
topjohnwu 2017-08-13 01:15:18 +08:00
parent 6eb7c0b5d6
commit a7dfc20967

View File

@ -409,6 +409,64 @@ static void link_busybox() {
symlink(MIRRDIR "/bin/busybox", BBPATH "/busybox"); symlink(MIRRDIR "/bin/busybox", BBPATH "/busybox");
} }
static int prepare_img() {
int new_img = 0;
if (access(MAINIMG, F_OK) == -1) {
if (create_img(MAINIMG, 64))
return 1;
new_img = 1;
}
LOGI("* Mounting " MAINIMG "\n");
// Mounting magisk image
char *magiskloop = mount_image(MAINIMG, MOUNTPOINT);
if (magiskloop == NULL)
return 1;
vec_init(&module_list);
if (new_img) {
xmkdir(COREDIR, 0755);
xmkdir(COREDIR "/post-fs-data.d", 0755);
xmkdir(COREDIR "/service.d", 0755);
xmkdir(COREDIR "/props", 0755);
} else {
DIR *dir = xopendir(MOUNTPOINT);
struct dirent *entry;
while ((entry = xreaddir(dir))) {
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0 ||
strcmp(entry->d_name, ".core") == 0 ||
strcmp(entry->d_name, "lost+found") == 0)
continue;
snprintf(buf, PATH_MAX, "%s/%s/remove", MOUNTPOINT, entry->d_name);
if (access(buf, F_OK) == 0) {
rm_rf(buf);
continue;
}
snprintf(buf, PATH_MAX, "%s/%s/disable", MOUNTPOINT, entry->d_name);
if (access(buf, F_OK) == 0)
continue;
vec_push_back(&module_list, strdup(entry->d_name));
}
}
closedir(dir);
// Trim image
umount_image(MOUNTPOINT, magiskloop);
free(magiskloop);
trim_img(MAINIMG);
// Remount them back :)
magiskloop = mount_image(MAINIMG, MOUNTPOINT);
free(magiskloop);
}
return 0;
}
/**************** /****************
* Entry points * * Entry points *
****************/ ****************/
@ -511,38 +569,26 @@ void post_fs_data(int client) {
return; return;
} }
int new_img = 0; // Trim, mount magisk.img, which will also travel through the modules
// After this, it will create the module list
if (access(MAINIMG, F_OK) == -1) { if (prepare_img())
if (create_img(MAINIMG, 64))
goto unblock;
new_img = 1;
}
LOGI("* Mounting " MAINIMG "\n");
// Mounting magisk image
char *magiskloop = mount_image(MAINIMG, MOUNTPOINT);
if (magiskloop == NULL)
goto unblock; goto unblock;
if (new_img) { // Run common scripts
xmkdir(COREDIR, 0755); LOGI("* Running post-fs-data.d scripts\n");
xmkdir(COREDIR "/post-fs-data.d", 0755); exec_common_script("post-fs-data");
xmkdir(COREDIR "/service.d", 0755);
xmkdir(COREDIR "/props", 0755);
}
// Core only mode // Core only mode
if (access(DISABLEFILE, F_OK) == 0) if (access(DISABLEFILE, F_OK) == 0)
goto core_only; goto core_only;
DIR *dir; // Execute module scripts
struct dirent *entry; LOGI("* Running module post-fs-data scripts\n");
exec_module_script("post-fs-data");
char *module; char *module;
struct node_entry *sys_root, *ven_root = NULL, *child; struct node_entry *sys_root, *ven_root = NULL, *child;
dir = xopendir(MOUNTPOINT);
// Create the system root entry // Create the system root entry
sys_root = xcalloc(sizeof(*sys_root), 1); sys_root = xcalloc(sizeof(*sys_root), 1);
sys_root->name = strdup("system"); sys_root->name = strdup("system");
@ -550,70 +596,36 @@ void post_fs_data(int client) {
int has_modules = 0; int has_modules = 0;
// Travel through each modules
vec_init(&module_list);
LOGI("* Loading modules\n"); LOGI("* Loading modules\n");
while ((entry = xreaddir(dir))) { vec_for_each(&module_list, module) {
if (entry->d_type == DT_DIR) { // Read props
if (strcmp(entry->d_name, ".") == 0 || snprintf(buf, PATH_MAX, "%s/%s/system.prop", MOUNTPOINT, module);
strcmp(entry->d_name, "..") == 0 || if (access(buf, F_OK) == 0) {
strcmp(entry->d_name, ".core") == 0 || LOGI("%s: loading [system.prop]\n", module);
strcmp(entry->d_name, "lost+found") == 0) read_prop_file(buf, 0);
continue;
snprintf(buf, PATH_MAX, "%s/%s", MOUNTPOINT, entry->d_name);
// Check whether remove
snprintf(buf2, PATH_MAX, "%s/remove", buf);
if (access(buf2, F_OK) == 0) {
rm_rf(buf);
continue;
}
// Check whether disable
snprintf(buf2, PATH_MAX, "%s/disable", buf);
if (access(buf2, F_OK) == 0)
continue;
// Add the module to list
module = strdup(entry->d_name);
vec_push_back(&module_list, module);
// Read props
snprintf(buf2, PATH_MAX, "%s/system.prop", buf);
if (access(buf2, F_OK) == 0) {
LOGI("%s: loading [system.prop]\n", module);
read_prop_file(buf2, 0);
}
// Check whether enable auto_mount
snprintf(buf2, PATH_MAX, "%s/auto_mount", buf);
if (access(buf2, F_OK) == -1)
continue;
// Double check whether the system folder exists
snprintf(buf2, PATH_MAX, "%s/system", buf);
if (access(buf2, F_OK) == -1)
continue;
// Construct structure
has_modules = 1;
LOGI("%s: constructing magic mount structure\n", module);
// If /system/vendor exists in module, create a link outside
snprintf(buf2, PATH_MAX, "%s/system/vendor", buf);
if (access(buf2, F_OK) == 0) {
snprintf(buf, PATH_MAX, "%s/%s/vendor", MOUNTPOINT, module);
unlink(buf);
symlink(buf2, buf);
}
construct_tree(module, sys_root);
} }
// Check whether enable auto_mount
snprintf(buf, PATH_MAX, "%s/%s/auto_mount", MOUNTPOINT, module);
if (access(buf, F_OK) == -1)
continue;
// Double check whether the system folder exists
snprintf(buf, PATH_MAX, "%s/%s/system", MOUNTPOINT, module);
if (access(buf, F_OK) == -1)
continue;
// Construct structure
has_modules = 1;
LOGI("%s: constructing magic mount structure\n", module);
// If /system/vendor exists in module, create a link outside
snprintf(buf, PATH_MAX, "%s/%s/system/vendor", MOUNTPOINT, module);
if (access(buf, F_OK) == 0) {
snprintf(buf2, PATH_MAX, "%s/%s/vendor", MOUNTPOINT, module);
unlink(buf2);
symlink(buf, buf2);
}
construct_tree(module, sys_root);
} }
closedir(dir);
// Trim image
umount_image(MOUNTPOINT, magiskloop);
free(magiskloop);
trim_img(MAINIMG);
// Remount them back :)
magiskloop = mount_image(MAINIMG, MOUNTPOINT);
free(magiskloop);
if (has_modules) { if (has_modules) {
// Extract the vendor node out of system tree and swap with placeholder // Extract the vendor node out of system tree and swap with placeholder
vec_for_each(sys_root->children, child) { vec_for_each(sys_root->children, child) {
@ -640,15 +652,7 @@ void post_fs_data(int client) {
destroy_subtree(sys_root); destroy_subtree(sys_root);
if (ven_root) destroy_subtree(ven_root); if (ven_root) destroy_subtree(ven_root);
// Execute module scripts
LOGI("* Running module post-fs-data scripts\n");
exec_module_script("post-fs-data");
core_only: core_only:
// Run common scripts
LOGI("* Running post-fs-data.d scripts\n");
exec_common_script("post-fs-data");
// Systemless hosts // Systemless hosts
if (access(HOSTSFILE, F_OK) == 0) { if (access(HOSTSFILE, F_OK) == 0) {
LOGI("* Enabling systemless hosts file support"); LOGI("* Enabling systemless hosts file support");
@ -685,6 +689,10 @@ void late_start(int client) {
// Wait till the full patch is done // Wait till the full patch is done
pthread_join(sepol_patch, NULL); pthread_join(sepol_patch, NULL);
// Run scripts after full patch, most reliable way to run scripts
LOGI("* Running service.d scripts\n");
exec_common_script("service");
// Core only mode // Core only mode
if (access(DISABLEFILE, F_OK) == 0) if (access(DISABLEFILE, F_OK) == 0)
goto core_only; goto core_only;
@ -693,10 +701,6 @@ void late_start(int client) {
exec_module_script("service"); exec_module_script("service");
core_only: core_only:
// Run scripts after full patch, most reliable way to run scripts
LOGI("* Running service.d scripts\n");
exec_common_script("service");
// Install Magisk Manager if exists // Install Magisk Manager if exists
if (access(MANAGERAPK, F_OK) == 0) { if (access(MANAGERAPK, F_OK) == 0) {
while (1) { while (1) {