From d49b02b274618593be9fac1b3bbed70bd2fdb0bb Mon Sep 17 00:00:00 2001 From: LoveSy Date: Wed, 6 Dec 2023 22:17:29 +0800 Subject: [PATCH] Fix zygiskd not restart when zygote restarts --- native/src/core/include/core.hpp | 1 + native/src/core/module.cpp | 22 ---------------------- native/src/core/zygisk/entry.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/native/src/core/include/core.hpp b/native/src/core/include/core.hpp index 1acfa1992..429c70640 100644 --- a/native/src/core/include/core.hpp +++ b/native/src/core/include/core.hpp @@ -38,6 +38,7 @@ struct module_info { extern bool zygisk_enabled; extern std::vector *module_list; +extern std::string native_bridge; void reset_zygisk(bool restore); int connect_daemon(int req, bool create = false); diff --git a/native/src/core/module.cpp b/native/src/core/module.cpp index 280516f51..467f146d6 100644 --- a/native/src/core/module.cpp +++ b/native/src/core/module.cpp @@ -15,8 +15,6 @@ using namespace std; #define VLOGD(tag, from, to) LOGD("%-8s: %s <- %s\n", tag, to, from) -static string native_bridge = "0"; - static int bind_mount(const char *reason, const char *from, const char *to) { int ret = xmount(from, to, nullptr, MS_BIND | MS_REC, nullptr); if (ret == 0) @@ -517,23 +515,3 @@ void exec_module_scripts(const char *stage) { [](const module_info &info) -> string_view { return info.name; }); exec_module_scripts(stage, module_names); } - -void reset_zygisk(bool restore) { - if (!zygisk_enabled) return; - static atomic_uint zygote_start_count{1}; - if (restore) { - zygote_start_count = 1; - } else if (zygote_start_count.fetch_add(1) > 3) { - LOGW("zygote crashes too many times, rolling-back\n"); - restore = true; - } - if (restore) { - string native_bridge_orig = "0"; - if (native_bridge.length() > strlen(ZYGISKLDR)) { - native_bridge_orig = native_bridge.substr(strlen(ZYGISKLDR)); - } - set_prop(NBPROP, native_bridge_orig.data(), true); - } else { - set_prop(NBPROP, native_bridge.data(), true); - } -} diff --git a/native/src/core/zygisk/entry.cpp b/native/src/core/zygisk/entry.cpp index 7344aacca..86fdab8b6 100644 --- a/native/src/core/zygisk/entry.cpp +++ b/native/src/core/zygisk/entry.cpp @@ -14,6 +14,7 @@ using namespace std; void *self_handle = nullptr; +string native_bridge = "0"; extern "C" [[maybe_unused]] void zygisk_inject_entry(void *handle) { self_handle = handle; @@ -224,3 +225,26 @@ void zygisk_handler(int client, const sock_cred *cred) { } close(client); } + +void reset_zygisk(bool restore) { + if (!zygisk_enabled) return; + static atomic_uint zygote_start_count{1}; + close(zygiskd_sockets[0]); + close(zygiskd_sockets[1]); + zygiskd_sockets[0] = zygiskd_sockets[1] = -1; + if (restore) { + zygote_start_count = 1; + } else if (zygote_start_count.fetch_add(1) > 3) { + LOGW("zygote crashes too many times, rolling-back\n"); + restore = true; + } + if (restore) { + string native_bridge_orig = "0"; + if (native_bridge.length() > strlen(ZYGISKLDR)) { + native_bridge_orig = native_bridge.substr(strlen(ZYGISKLDR)); + } + set_prop(NBPROP, native_bridge_orig.data(), true); + } else { + set_prop(NBPROP, native_bridge.data(), true); + } +}