From e40596f9f83629c9b03146a2644111d952eebea6 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Mon, 7 Mar 2022 22:39:21 +0800 Subject: [PATCH] Wait zygote socket before launching daemon (#1748) --- core/magisk_module/daemon | 8 ++++++++ core/src/main/cpp/main/api/zygisk_main.cpp | 1 + core/src/main/cpp/main/src/context.cpp | 11 +++++------ core/src/main/cpp/main/src/native_hook.cpp | 2 +- core/src/main/cpp/main/src/symbol_cache.cpp | 8 +++++--- core/src/main/cpp/main/src/symbol_cache.h | 2 +- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/core/magisk_module/daemon b/core/magisk_module/daemon index ecb58757..34cec9d2 100644 --- a/core/magisk_module/daemon +++ b/core/magisk_module/daemon @@ -26,5 +26,13 @@ fi mount tmpfs -t tmpfs /data/resource-cache +if [ ! -S "/dev/socket/zygote" ]; then + timeout 1 inotifyd - /dev/socket:near | while read -r; do + if [ -S "/dev/socket/zygote" ]; then + touch /dev/socket& + exit + fi + done +fi # shellcheck disable=SC2086 exec /system/bin/app_process $java_options /system/bin --nice-name=lspd org.lsposed.lspd.Main "$@" >/dev/null 2>&1 diff --git a/core/src/main/cpp/main/api/zygisk_main.cpp b/core/src/main/cpp/main/api/zygisk_main.cpp index 35bb3645..7e3755dc 100644 --- a/core/src/main/cpp/main/api/zygisk_main.cpp +++ b/core/src/main/cpp/main/api/zygisk_main.cpp @@ -331,6 +331,7 @@ namespace lspd { JNI_CallStaticVoidMethod(env_, process, set_argv0, JNI_NewStringUTF(env_, "system_server")); } Context::GetInstance()->OnNativeForkSystemServerPost(env_); + if (*allowUnload) api_->setOption(zygisk::DLCLOSE_MODULE_LIBRARY); } }; diff --git a/core/src/main/cpp/main/src/context.cpp b/core/src/main/cpp/main/src/context.cpp index 687e3452..ea716b80 100644 --- a/core/src/main/cpp/main/src/context.cpp +++ b/core/src/main/cpp/main/src/context.cpp @@ -207,12 +207,11 @@ namespace lspd { InstallInlineHooks(); Init(env); FindAndCall(env, "forkSystemServerPost", "(Landroid/os/IBinder;)V", application_binder); - } else skip_ = true; + } else { + LOGI("skipped system server"); + GetArt(true); + } } - if (skip_) [[unlikely]] { - LOGW("skipped system server"); - } - setAllowUnload(skip_); } void Context::OnNativeForkAndSpecializePre(JNIEnv *env, @@ -277,7 +276,7 @@ namespace lspd { } else { auto context = Context::ReleaseInstance(); auto service = Service::ReleaseInstance(); - GetArt().reset(); + GetArt(true); LOGD("skipped %s", process_name.get()); setAllowUnload(true); } diff --git a/core/src/main/cpp/main/src/native_hook.cpp b/core/src/main/cpp/main/src/native_hook.cpp index ac821132..712daa88 100644 --- a/core/src/main/cpp/main/src/native_hook.cpp +++ b/core/src/main/cpp/main/src/native_hook.cpp @@ -62,7 +62,7 @@ namespace lspd { art::thread_list::ScopedSuspendAll::Setup(handle_libart); art::gc::ScopedGCCriticalSection::Setup(handle_libart); art::jit::jit_code_cache::Setup(handle_libart); - GetArt().reset(); + GetArt(true); LOGD("Inline hooks installed"); } } // namespace lspd diff --git a/core/src/main/cpp/main/src/symbol_cache.cpp b/core/src/main/cpp/main/src/symbol_cache.cpp index 423d386d..05671239 100644 --- a/core/src/main/cpp/main/src/symbol_cache.cpp +++ b/core/src/main/cpp/main/src/symbol_cache.cpp @@ -33,9 +33,11 @@ namespace lspd { std::unique_ptr symbol_cache = std::make_unique(); - std::unique_ptr &GetArt() { + std::unique_ptr &GetArt(bool release) { static std::unique_ptr kArtImg = nullptr; - if (!kArtImg) { + if (release) { + kArtImg.reset(); + } else if (!kArtImg) { kArtImg = std::make_unique(kLibArtName); } return kArtImg; @@ -77,7 +79,7 @@ namespace lspd { symbol_cache->do_dlopen = SandHook::ElfImg("/linker").getSymbAddress( "__dl__Z9do_dlopenPKciPK17android_dlextinfoPKv"); if (!ok) [[unlikely]] { - GetArt().reset(); + GetArt(true); LOGE("Init symbol cache failed"); } else { symbol_cache->initialized.test_and_set(std::memory_order_relaxed); diff --git a/core/src/main/cpp/main/src/symbol_cache.h b/core/src/main/cpp/main/src/symbol_cache.h index 66493556..24f13e2b 100644 --- a/core/src/main/cpp/main/src/symbol_cache.h +++ b/core/src/main/cpp/main/src/symbol_cache.h @@ -63,7 +63,7 @@ namespace lspd { void InitSymbolCache(SymbolCache *other); - std::unique_ptr &GetArt(); + std::unique_ptr &GetArt(bool release=false); } #endif //LSPOSED_SYMBOL_CACHE_H