From 94d6e5436dd62a02d21bdbb1f7c729d4c0c79077 Mon Sep 17 00:00:00 2001 From: youshugen Date: Sun, 27 Apr 2025 15:11:10 +0800 Subject: [PATCH] fix: alarming improve Signed-off-by: youshugen --- frameworks/napi/power/BUILD.gn | 16 ++++++++- frameworks/napi/power/power_napi.cpp | 11 ++++-- powermgr.gni | 9 +++++ services/native/src/power_mgr_service.cpp | 13 +++++++ utils/appmgr/include/app_manager_utils.h | 1 + utils/appmgr/src/app_manager_utils.cpp | 43 +++++++++++++++++++++++ 6 files changed, 89 insertions(+), 4 deletions(-) diff --git a/frameworks/napi/power/BUILD.gn b/frameworks/napi/power/BUILD.gn index e3f24ce9..cfd6453e 100644 --- a/frameworks/napi/power/BUILD.gn +++ b/frameworks/napi/power/BUILD.gn @@ -14,6 +14,13 @@ import("../../../powermgr.gni") ohos_shared_library("power") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + branch_protector_ret = "pac_ret" + include_dirs = [ "../utils" ] sources = [ "../utils/async_callback_info.cpp", @@ -27,7 +34,10 @@ ohos_shared_library("power") { "${powermgr_utils_path}:utils_config", "${powermgr_utils_path}:coverage_flags", ] - deps = [ "${powermgr_inner_api}:powermgr_client" ] + deps = [ + "${powermgr_inner_api}:powermgr_client", + "${powermgr_utils_path}/appmgr:power_appmgr", + ] deps_ex = [ "c_utils:utils", @@ -42,6 +52,10 @@ ohos_shared_library("power") { external_deps += [ "napi:ace_napi" ] } + if (has_ability_runtime_part) { + external_deps += [ "ability_runtime:app_manager" ] + } + relative_install_dir = "module" subsystem_name = "powermgr" diff --git a/frameworks/napi/power/power_napi.cpp b/frameworks/napi/power/power_napi.cpp index 738886c6..e6f5a45e 100644 --- a/frameworks/napi/power/power_napi.cpp +++ b/frameworks/napi/power/power_napi.cpp @@ -23,6 +23,7 @@ #include #include #include +#include "app_manager_utils.h" #define SET_REBOOT _IOW(BOOT_DETECTOR_IOCTL_BASE, 109, int) @@ -72,7 +73,9 @@ napi_value PowerNapi::Wakeup(napi_env env, napi_callback_info info) std::string detail = NapiUtils::GetStringFromNapi(env, argv[INDEX_0]); POWER_HILOGD(FEATURE_WAKEUP, "Wakeup type: APPLICATION, reason: %{public}s", detail.c_str()); - PowerErrors code = g_powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail); + int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); + PowerErrors code = g_powerMgrClient.WakeupDevice( + WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail, std::to_string(apiVersion)); if (code != PowerErrors::ERR_OK) { error.ThrowError(env, code); } @@ -99,10 +102,12 @@ napi_value PowerNapi::Suspend(napi_env env, napi_callback_info info) napi_get_value_bool(env, argv[0], &isForce); PowerErrors code; + int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); if (isForce) { - code = g_powerMgrClient.ForceSuspendDevice(); + code = g_powerMgrClient.ForceSuspendDevice(std::to_string(apiVersion)); } else { - code = g_powerMgrClient.SuspendDevice(); + code = g_powerMgrClient.SuspendDevice( + SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false, std::to_string(apiVersion)); } if (code != PowerErrors::ERR_OK) { POWER_HILOGE(FEATURE_WAKEUP, "Suspend Device fail, isForce:%{public}d", isForce); diff --git a/powermgr.gni b/powermgr.gni index 5b271b71..cb31cd2c 100644 --- a/powermgr.gni +++ b/powermgr.gni @@ -103,6 +103,15 @@ if (!defined(global_parts_info) || if (power_manager_feature_enable_s4) { defines += [ "POWER_MANAGER_POWER_ENABLE_S4" ] } + +if (!defined(global_parts_info) || + defined(global_parts_info.ability_ability_runtime)) { + has_ability_runtime_part = true + defines += [ "HAS_ABILITY_RUNTIME_PART" ] +} else { + has_ability_runtime_part = false +} + ability_runtime_path = "//foundation/ability/ability_runtime" ability_runtime_inner_api_path = "${ability_runtime_path}/interfaces/inner_api" diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index ffa644ff..5f90ffb2 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -80,6 +80,7 @@ SysParam::BootCompletedCallback g_bootCompletedCallback; #ifdef POWER_PICKUP_ENABLE bool g_isPickUpOpen = false; #endif +constexpr int32_t API18 = 18; } // namespace std::atomic_bool PowerMgrService::isBootCompleted_ = false; @@ -1009,8 +1010,14 @@ PowerErrors PowerMgrService::SuspendDevice( pid_t pid = IPCSkeleton::GetCallingPid(); auto uid = IPCSkeleton::GetCallingUid(); if (!Permission::IsSystem()) { + POWER_HILOGI(FEATURE_SUSPEND, "SuspendDevice failed, System permission intercept"); return PowerErrors::ERR_SYSTEM_API_DENIED; } + int32_t version = static_cast(strtol(apiVersion.c_str(), nullptr, 10)); + if (version >= API18 && !Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) { + POWER_HILOGI(FEATURE_SUSPEND, "SuspendDevice failed, The application does not have the permission"); + return PowerErrors::ERR_PERMISSION_DENIED; + } #ifdef POWER_MANAGER_ENABLE_WATCH_BOOT_COMPLETED if (isBootCompleted_ == false) { @@ -1034,8 +1041,14 @@ PowerErrors PowerMgrService::WakeupDevice( { std::lock_guard lock(wakeupMutex_); if (!Permission::IsSystem()) { + POWER_HILOGI(FEATURE_SUSPEND, "WakeupDevice failed, System permission intercept"); return PowerErrors::ERR_SYSTEM_API_DENIED; } + int32_t version = static_cast(strtol(apiVersion.c_str(), nullptr, 10)); + if (version >= API18 && !Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) { + POWER_HILOGI(FEATURE_SUSPEND, "WakeupDevice failed, The application does not have the permission"); + return PowerErrors::ERR_PERMISSION_DENIED; + } pid_t pid = IPCSkeleton::GetCallingPid(); auto uid = IPCSkeleton::GetCallingUid(); diff --git a/utils/appmgr/include/app_manager_utils.h b/utils/appmgr/include/app_manager_utils.h index fa74e4c2..46605094 100644 --- a/utils/appmgr/include/app_manager_utils.h +++ b/utils/appmgr/include/app_manager_utils.h @@ -28,6 +28,7 @@ public: static sptr GetAppManagerInstance(); static void GetForegroundApplications(std::vector& appsData); static bool IsForegroundApplication(const std::string& appName); + static int32_t GetApiTargetVersion(); private: static sptr appManagerInstance_; diff --git a/utils/appmgr/src/app_manager_utils.cpp b/utils/appmgr/src/app_manager_utils.cpp index c763cdb3..ef8a2ebc 100644 --- a/utils/appmgr/src/app_manager_utils.cpp +++ b/utils/appmgr/src/app_manager_utils.cpp @@ -14,6 +14,10 @@ */ #include "app_manager_utils.h" +#ifdef HAS_ABILITY_RUNTIME_PART +#include "bundle_mgr_interface.h" +#include "system_ability_definition.h" +#endif #include "power_log.h" #include @@ -25,6 +29,9 @@ namespace OHOS { namespace PowerMgr { static constexpr uint32_t APP_MGR_SERVICE_ID = 501; sptr AppManagerUtils::appManagerInstance_ = nullptr; +namespace { +const int32_t API_VERSION_MOD = 1000; +} sptr AppManagerUtils::GetAppManagerInstance() { @@ -83,5 +90,41 @@ bool AppManagerUtils::IsForegroundApplication(const std::string& appName) return IsForeground; } +int32_t AppManagerUtils::GetApiTargetVersion() +{ +#ifdef HAS_ABILITY_RUNTIME_PART + static int32_t apiTargetVersion = -1; + if (apiTargetVersion != -1) { + return apiTargetVersion; + } + sptr saManager = + OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saManager == nullptr) { + POWER_HILOGE(FEATURE_UTIL, "Failed to get ISystemAbilityManager"); + return 0; + } + sptr remoteObject = saManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + sptr bundleMgrProxy = iface_cast(remoteObject); + if (bundleMgrProxy == nullptr) { + POWER_HILOGE(FEATURE_UTIL, "GetApiTargetVersion: bundleMgrProxy is nullptr"); + return 0; + } + OHOS::AppExecFwk::BundleInfo bundleInfo; + auto ret = + bundleMgrProxy->GetBundleInfoForSelf(OHOS::AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo); + if (ret != 0) { + POWER_HILOGI(FEATURE_UTIL, "GetApiTargetVersion: GetBundleInfoForSelf failed"); + return 0; + } + int32_t hapApiVersion = bundleInfo.applicationInfo.apiTargetVersion % API_VERSION_MOD; + apiTargetVersion = hapApiVersion; + POWER_HILOGI(FEATURE_UTIL, "GetApiTargetVersion: hapApiVersion is %{public}d", hapApiVersion); + return hapApiVersion; +#else + POWER_HILOGI(FEATURE_UTIL, "GetApiTargetVersion not support"); + return -1; +#endif +} + } // namespace PowerMgr } // namespace OHOS \ No newline at end of file -- Gitee