From 24f5bc98d8340823f77d3699db9418fc41cdd3df Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 5 Apr 2019 07:00:30 -0400 Subject: [PATCH] Add boot_complete trigger back Samsung does not like running cmd before system services are started. Instead of failing, it will enter an infinite wait on binder. Move APK installation to boot complete to make sure pm can be run without blocking process. --- native/jni/core/bootstages.cpp | 31 ++++++++++++++----------------- native/jni/core/init.cpp | 15 +++++++++------ native/jni/core/magiskrc.h | 10 ++++------ native/jni/core/scripting.cpp | 17 ++++++----------- native/jni/utils/misc.cpp | 18 +++++++----------- 5 files changed, 40 insertions(+), 51 deletions(-) diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index 44969b187..9c040e143 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -700,13 +700,22 @@ void late_start(int client) { exec_common_script("service"); // Core only mode - if (access(DISABLEFILE, F_OK) == 0) - goto core_only; + if (access(DISABLEFILE, F_OK) != 0) { + LOGI("* Running module service scripts\n"); + exec_module_script("service", module_list); + } - LOGI("* Running module service scripts\n"); - exec_module_script("service", module_list); + // All boot stage done, cleanup + module_list.clear(); + module_list.shrink_to_fit(); +} + +void boot_complete(int client) { + LOGI("** boot_complete triggered\n"); + // ack + write_int(client, 0); + close(client); -core_only: if (access(MANAGERAPK, F_OK) == 0) { // Install Magisk Manager if exists rename(MANAGERAPK, "/data/magisk.apk"); @@ -721,16 +730,4 @@ core_only: install_apk("/data/magisk.apk"); } } - - // All boot stage done, cleanup - module_list.clear(); -} - -void boot_complete(int client) { - LOGI("** boot_complete triggered\n"); - // ack - write_int(client, 0); - close(client); - - unlink(BOOTCOUNT); } diff --git a/native/jni/core/init.cpp b/native/jni/core/init.cpp index 2d37268cf..9666bca03 100644 --- a/native/jni/core/init.cpp +++ b/native/jni/core/init.cpp @@ -516,12 +516,15 @@ void MagiskInit::setup_rootfs() { fprintf(rc, "%s", line.data()); return true; }); - char pfd_svc[8], ls_svc[8]; - gen_rand_str(pfd_svc, sizeof(pfd_svc)); - do { - gen_rand_str(ls_svc, sizeof(ls_svc)); - } while (strcmp(pfd_svc, ls_svc) == 0); - fprintf(rc, magiskrc, pfd_svc, pfd_svc, ls_svc); + char pfd_svc[8], ls_svc[8], bc_svc[8]; + // Make sure to be unique + pfd_svc[0] = 'a'; + ls_svc[0] = '0'; + bc_svc[0] = 'A'; + gen_rand_str(pfd_svc + 1, sizeof(pfd_svc) - 1); + gen_rand_str(ls_svc + 1, sizeof(ls_svc) - 1); + gen_rand_str(bc_svc + 1, sizeof(bc_svc) - 1); + fprintf(rc, magiskrc, pfd_svc, pfd_svc, ls_svc, bc_svc, bc_svc); fclose(rc); clone_attr("/init.rc", "/init.p.rc"); rename("/init.p.rc", "/init.rc"); diff --git a/native/jni/core/magiskrc.h b/native/jni/core/magiskrc.h index 2ea6e0ff6..40c0028c2 100644 --- a/native/jni/core/magiskrc.h +++ b/native/jni/core/magiskrc.h @@ -32,15 +32,13 @@ static const char magiskrc[] = " oneshot\n" "\n" -#if 0 "on property:sys.boot_completed=1\n" -" start magisk_bc\n" +" start %s\n" "\n" -"service magisk_bc /sbin/magisk --boot-complete\n" +"service %s /sbin/magisk --boot-complete\n" " user root\n" -" seclabel u:r:"SEPOL_PROC_DOMAIN":s0\n" +" seclabel u:r:" SEPOL_PROC_DOMAIN ":s0\n" " oneshot\n" -; -#endif +"\n" ; diff --git a/native/jni/core/scripting.cpp b/native/jni/core/scripting.cpp index 2ae0f7547..c0e30087d 100644 --- a/native/jni/core/scripting.cpp +++ b/native/jni/core/scripting.cpp @@ -101,21 +101,16 @@ void migrate_img(const char *img) { static const char install_script[] = "APK=%s;" -"while true; do" -" OUT=`pm install -r $APK 2>&1`;" -" log -t Magisk \"apk_install: $OUT\";" -" if echo \"$OUT\" | grep -qE \"Can't|Error:\"; then" -" sleep 5;" -" continue;" -" fi;" -" break;" -"done;" +"log -t Magisk \"apk_install: $APK\";" +"log -t Magisk \"apk_install: `pm install -r $APK 2>&1`\";" "rm -f $APK;"; void install_apk(const char *apk) { setfilecon(apk, "u:object_r:" SEPOL_FILE_DOMAIN ":s0"); - LOGI("apk_install: %s\n", apk); - exec_t exec { .pre_exec = set_path }; + exec_t exec { + .pre_exec = set_path, + .fork = fork_no_zombie + }; char cmds[sizeof(install_script) + 4096]; sprintf(cmds, install_script, apk); exec_command_sync(exec, "/system/bin/sh", "-c", cmds); diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index 781759a08..b82a06006 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -49,20 +49,16 @@ int fork_no_zombie() { return 0; } +static bool rand_init = false; + void gen_rand_str(char *buf, int len) { - const char base[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - int urandom; - if (access("/dev/urandom", R_OK) == 0) { - urandom = xopen("/dev/urandom", O_RDONLY | O_CLOEXEC); - } else { - mknod("/urandom", S_IFCHR | 0666, makedev(1, 9)); - urandom = xopen("/urandom", O_RDONLY | O_CLOEXEC); - unlink("/urandom"); + constexpr const char base[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + if (!rand_init) { + srand(time(nullptr)); + rand_init = true; } - xxread(urandom, buf, len - 1); - close(urandom); for (int i = 0; i < len - 1; ++i) { - buf[i] = base[buf[i] % (sizeof(base) - 1)]; + buf[i] = base[rand() % (sizeof(base) - 1)]; } buf[len - 1] = '\0'; }