From 72d320d8196533dcd6ee97a5a54d5902755bd1a8 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Thu, 3 Dec 2020 02:18:03 +0800 Subject: [PATCH] ConfigManager only initialize once --- .../src/main/cpp/main/src/config_manager.cpp | 6 +++--- .../src/main/cpp/main/src/config_manager.h | 12 ++++++----- .../src/main/cpp/main/src/edxp_context.cpp | 21 ++++++++++++------- .../src/main/cpp/main/src/edxp_context.h | 2 +- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/edxp-core/src/main/cpp/main/src/config_manager.cpp b/edxp-core/src/main/cpp/main/src/config_manager.cpp index 8b85bfaa..04d22687 100644 --- a/edxp-core/src/main/cpp/main/src/config_manager.cpp +++ b/edxp-core/src/main/cpp/main/src/config_manager.cpp @@ -146,12 +146,12 @@ namespace edxp { } } - ConfigManager::ConfigManager(uid_t user) : + ConfigManager::ConfigManager(uid_t user, bool initialized) : user_(user), data_path_prefix_(fs::path(use_prot_storage_ ? "/data/user_de" : "/data/user") / std::to_string(user_)), base_config_path_(RetrieveBaseConfigPath()), - initialized_(InitConfigPath()), + initialized_(initialized || InitConfigPath()), installer_pkg_name_(RetrieveInstallerPkgName()), white_list_enable_(path_exists(GetConfigPath("usewhitelist"))), deopt_boot_image_enabled_(path_exists(GetConfigPath("deoptbootimage"))), @@ -225,7 +225,7 @@ namespace edxp { scope.emplace(std::move(app_pkg_name)); } scope.insert(module_pkg_name); // Always add module itself - LOGD("scope of %s is:\n%s", module_pkg_name.c_str(), ([&scope = scope]() { + LOGI("scope of %s is:\n%s", module_pkg_name.c_str(), ([&scope = scope]() { std::ostringstream join; std::copy(scope.begin(), scope.end(), std::ostream_iterator(join, "\n ")); diff --git a/edxp-core/src/main/cpp/main/src/config_manager.h b/edxp-core/src/main/cpp/main/src/config_manager.h index 2ebc527f..b190c1c2 100644 --- a/edxp-core/src/main/cpp/main/src/config_manager.h +++ b/edxp-core/src/main/cpp/main/src/config_manager.h @@ -30,11 +30,14 @@ namespace edxp { return instances_[current_user].get(); } + inline auto IsInitialized() const { return initialized_; } + inline static void SetCurrentUser(uid_t user) { if (auto instance = instances_.find(user); - instance == instances_.end() || !instance->second || - instance->second->NeedUpdateConfig()) { + instance == instances_.end() || !instance->second) { instances_[user] = std::make_unique(user); + } else if(instance->second->NeedUpdateConfig()) { + instances_[user] = std::make_unique(user, instance->second->IsInitialized()); } } @@ -42,8 +45,6 @@ namespace edxp { return std::move(instances_); } - inline auto IsInitialized() const { return initialized_; } - // Always true now inline auto IsBlackWhiteListEnabled() const { return true; } @@ -116,7 +117,7 @@ namespace edxp { const std::filesystem::file_time_type last_write_time_; - ConfigManager(uid_t uid); + ConfigManager(uid_t uid, bool initialized=false); static std::unordered_set GetAppList(const std::filesystem::path &dir); @@ -131,6 +132,7 @@ namespace edxp { bool InitConfigPath() const; friend std::unique_ptr std::make_unique(uid_t &); + friend std::unique_ptr std::make_unique(uid_t &, bool&&); std::filesystem::path RetrieveBaseConfigPath() const; diff --git a/edxp-core/src/main/cpp/main/src/edxp_context.cpp b/edxp-core/src/main/cpp/main/src/edxp_context.cpp index 7c4675b8..c9833e60 100644 --- a/edxp-core/src/main/cpp/main/src/edxp_context.cpp +++ b/edxp-core/src/main/cpp/main/src/edxp_context.cpp @@ -236,7 +236,7 @@ namespace edxp { skip_ = true; LOGW("skip injecting into android because no module hooks it"); } - if(!skip_) { + if (!skip_) { PreLoadDex(ConfigManager::GetInjectDexPaths()); } } @@ -297,12 +297,14 @@ namespace edxp { } bool Context::ShouldSkipInject(const std::string &package_name, uid_t user, uid_t uid, - bool info_res, const std::vector &app_modules_list_, + bool info_res, + const std::function &empty_list, bool is_child_zygote) { const auto app_id = uid % PER_USER_RANGE; bool skip = false; if (!ConfigManager::GetInstance()->IsInitialized()) { - LOGE("skip injecting into %s because configurations are not loaded properly", package_name.c_str()); + LOGE("skip injecting into %s because configurations are not loaded properly", + package_name.c_str()); skip = true; } if (!skip && !info_res) { @@ -328,7 +330,7 @@ namespace edxp { package_name.c_str()); } - if (!skip && app_modules_list_.empty() && + if (!skip && empty_list() && package_name != ConfigManager::GetInstance()->GetInstallerPackageName()) { skip = true; LOGW("skip injecting xposed into %s because no module hooks it", @@ -354,9 +356,14 @@ namespace edxp { app_data_dir_ = app_data_dir; nice_name_ = nice_name; ConfigManager::SetCurrentUser(user); - skip_ = ShouldSkipInject(package_name, user, uid, res, app_modules_list_, is_child_zygote); - if(!skip_) { - app_modules_list_ = ConfigManager::GetInstance()->GetAppModuleList(package_name); + skip_ = ShouldSkipInject(package_name, user, uid, res, + // Only obtains when needed + [this, &package_name = package_name]() { + app_modules_list_ = ConfigManager::GetInstance()->GetAppModuleList( + package_name); + return app_modules_list_.empty(); + }, is_child_zygote); + if (!skip_) { ConfigManager::GetInstance()->EnsurePermission(package_name, uid % PER_USER_RANGE); PreLoadDex(ConfigManager::GetInjectDexPaths()); } diff --git a/edxp-core/src/main/cpp/main/src/edxp_context.h b/edxp-core/src/main/cpp/main/src/edxp_context.h index c14921f9..b8adafee 100644 --- a/edxp-core/src/main/cpp/main/src/edxp_context.h +++ b/edxp-core/src/main/cpp/main/src/edxp_context.h @@ -114,7 +114,7 @@ namespace edxp { static bool ShouldSkipInject(const std::string &package_name, uid_t user, uid_t uid, bool res, - const std::vector &app_module_list, + const std::function& empty_list, bool is_child_zygote); static std::tuple GetAppInfoFromDir(JNIEnv *env, jstring dir, jstring nice_name);