From 45729b29480f8d0a43559805a4b683f4a32dc71d Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 24 Dec 2024 09:38:13 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=89=B4=E6=9D=83?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- services/common/src/bundle_active_service.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index d187bea..db7102b 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -546,11 +546,6 @@ ErrCode BundleActiveService::CheckBundleIsSystemAppAndHasPermission(const int32_ ErrCode BundleActiveService::CheckNativePermission(OHOS::Security::AccessToken::AccessTokenID tokenId) { - int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, "ohos.permission.DUMP"); - if (ret == Security::AccessToken::PermissionState::PERMISSION_GRANTED) { - BUNDLE_ACTIVE_LOGD("check native permission success, request from dump"); - return ERR_OK; - } int32_t bundleHasPermission = AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, NEEDED_PERMISSION); if (bundleHasPermission != 0) { BUNDLE_ACTIVE_LOGE("check native permission not have permission"); @@ -751,7 +746,7 @@ int32_t BundleActiveService::ShellDump(const std::vector &dumpOptio int64_t beginTime = std::stoll(dumpOption[2]); int64_t endTime = std::stoll(dumpOption[3]); int32_t userId = std::stoi(dumpOption[4]); - this->QueryBundleEvents(eventResult, beginTime, endTime, userId); + bundleActiveCore_->QueryBundleEvents(eventResult, userId, beginTime, endTime, ""); for (auto& oneEvent : eventResult) { dumpInfo.emplace_back(oneEvent.ToString()); } @@ -764,7 +759,9 @@ int32_t BundleActiveService::ShellDump(const std::vector &dumpOptio int64_t beginTime = std::stoll(dumpOption[3]); int64_t endTime = std::stoll(dumpOption[4]); int32_t userId = std::stoi(dumpOption[5]); - this->QueryBundleStatsInfoByInterval(packageUsageResult, intervalType, beginTime, endTime, userId); + int32_t convertedIntervalType = ConvertIntervalType(intervalType); + bundleActiveCore_->QueryBundleStatsInfos( + packageUsageResult, userId, convertedIntervalType, beginTime, endTime, ""); for (auto& onePackageRecord : packageUsageResult) { dumpInfo.emplace_back(onePackageRecord.ToString()); } @@ -776,7 +773,10 @@ int32_t BundleActiveService::ShellDump(const std::vector &dumpOptio int32_t maxNum = std::stoi(dumpOption[2]); int32_t userId = std::stoi(dumpOption[3]); BUNDLE_ACTIVE_LOGI("M is %{public}d, u is %{public}d", maxNum, userId); - ret = this->QueryModuleUsageRecords(maxNum, moduleResult, userId); + ret = bundleActiveCore_->QueryModuleUsageRecords(maxNum, moduleResult, userId); + for (auto& oneResult : moduleResult) { + QueryModuleRecordInfos(oneResult); + } for (auto& oneModuleRecord : moduleResult) { dumpInfo.emplace_back(oneModuleRecord.ToString()); for (uint32_t i = 0; i < oneModuleRecord.formRecords_.size(); i++) { -- Gitee From 7fd5f700b8f329bbfa778efaf82c39f23d6e9d41 Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 24 Dec 2024 10:03:48 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=89=B4=E6=9D=83?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- services/common/src/bundle_active_service.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index db7102b..fba6e41 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -547,7 +547,7 @@ ErrCode BundleActiveService::CheckBundleIsSystemAppAndHasPermission(const int32_ ErrCode BundleActiveService::CheckNativePermission(OHOS::Security::AccessToken::AccessTokenID tokenId) { int32_t bundleHasPermission = AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, NEEDED_PERMISSION); - if (bundleHasPermission != 0) { + if (bundleHasPermission != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { BUNDLE_ACTIVE_LOGE("check native permission not have permission"); return ERR_PERMISSION_DENIED; } @@ -738,6 +738,9 @@ int32_t BundleActiveService::Dump(int32_t fd, const std::vector int32_t BundleActiveService::ShellDump(const std::vector &dumpOption, std::vector &dumpInfo) { int32_t ret = -1; + if (!bundleActiveCore_) { + return ret; + } if (dumpOption[1] == "Events") { std::vector eventResult; if (static_cast(dumpOption.size()) != EVENTS_PARAM) { -- Gitee From 21b209d5c6762966a2eb413c28d73c5e14ef5c4f Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 24 Dec 2024 14:25:44 +0800 Subject: [PATCH 3/7] push Signed-off-by: fengyang --- services/common/src/bundle_active_service.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index fba6e41..c90b6b4 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -758,13 +758,12 @@ int32_t BundleActiveService::ShellDump(const std::vector &dumpOptio if (static_cast(dumpOption.size()) != PACKAGE_USAGE_PARAM) { return ret; } - int32_t intervalType = std::stoi(dumpOption[2]); + int32_t intervalType = ConvertIntervalType(std::stoi(dumpOption[2])); int64_t beginTime = std::stoll(dumpOption[3]); int64_t endTime = std::stoll(dumpOption[4]); int32_t userId = std::stoi(dumpOption[5]); - int32_t convertedIntervalType = ConvertIntervalType(intervalType); bundleActiveCore_->QueryBundleStatsInfos( - packageUsageResult, userId, convertedIntervalType, beginTime, endTime, ""); + packageUsageResult, userId, intervalType, beginTime, endTime, ""); for (auto& onePackageRecord : packageUsageResult) { dumpInfo.emplace_back(onePackageRecord.ToString()); } -- Gitee From 118d2b40c7960cd4780109b1c46a68fc7a44f8eb Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 24 Dec 2024 14:49:11 +0800 Subject: [PATCH 4/7] push Signed-off-by: fengyang --- .../common/include/bundle_active_service.h | 3 + services/common/src/bundle_active_service.cpp | 103 +++++++++++------- 2 files changed, 65 insertions(+), 41 deletions(-) diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index d91c143..cf33fee 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -251,6 +251,9 @@ private: void DumpUsage(std::string &result); bool AllowDump(); int32_t ShellDump(const std::vector &dumpOption, std::vector &dumpInfo); + int32_t DumpEvents(const std::vector &dumpOption, std::vector &dumpInfo); + int32_t DumpPackageUsage(const std::vector &dumpOption, std::vector &dumpInfo); + int32_t DumpModuleUsage(const std::vector &dumpOption, std::vector &dumpInfo); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index c90b6b4..776eafc 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -742,49 +742,70 @@ int32_t BundleActiveService::ShellDump(const std::vector &dumpOptio return ret; } if (dumpOption[1] == "Events") { - std::vector eventResult; - if (static_cast(dumpOption.size()) != EVENTS_PARAM) { - return ret; - } - int64_t beginTime = std::stoll(dumpOption[2]); - int64_t endTime = std::stoll(dumpOption[3]); - int32_t userId = std::stoi(dumpOption[4]); - bundleActiveCore_->QueryBundleEvents(eventResult, userId, beginTime, endTime, ""); - for (auto& oneEvent : eventResult) { - dumpInfo.emplace_back(oneEvent.ToString()); - } + ret = DumpEvents(dumpOption, dumpInfo); } else if (dumpOption[1] == "PackageUsage") { - std::vector packageUsageResult; - if (static_cast(dumpOption.size()) != PACKAGE_USAGE_PARAM) { - return ret; - } - int32_t intervalType = ConvertIntervalType(std::stoi(dumpOption[2])); - int64_t beginTime = std::stoll(dumpOption[3]); - int64_t endTime = std::stoll(dumpOption[4]); - int32_t userId = std::stoi(dumpOption[5]); - bundleActiveCore_->QueryBundleStatsInfos( - packageUsageResult, userId, intervalType, beginTime, endTime, ""); - for (auto& onePackageRecord : packageUsageResult) { - dumpInfo.emplace_back(onePackageRecord.ToString()); - } + ret = DumpPackageUsage(dumpOption, dumpInfo); } else if (dumpOption[1] == "ModuleUsage") { - std::vector moduleResult; - if (static_cast(dumpOption.size()) != MODULE_USAGE_PARAM) { - return ret; - } - int32_t maxNum = std::stoi(dumpOption[2]); - int32_t userId = std::stoi(dumpOption[3]); - BUNDLE_ACTIVE_LOGI("M is %{public}d, u is %{public}d", maxNum, userId); - ret = bundleActiveCore_->QueryModuleUsageRecords(maxNum, moduleResult, userId); - for (auto& oneResult : moduleResult) { - QueryModuleRecordInfos(oneResult); - } - for (auto& oneModuleRecord : moduleResult) { - dumpInfo.emplace_back(oneModuleRecord.ToString()); - for (uint32_t i = 0; i < oneModuleRecord.formRecords_.size(); i++) { - std::string oneFormInfo = "form " + std::to_string(static_cast(i) + 1) + ", "; - dumpInfo.emplace_back(oneFormInfo + oneModuleRecord.formRecords_[i].ToString()); - } + ret = DumpModuleUsage(dumpOption, dumpInfo); + } + return ret; +} + +int32_t BundleActiveService::DumpEvents(const std::vector &dumpOption, std::vector &dumpInfo) +{ + int32_t ret = -1; + std::vector eventResult; + if (static_cast(dumpOption.size()) != EVENTS_PARAM) { + return ret; + } + int64_t beginTime = std::stoll(dumpOption[2]); + int64_t endTime = std::stoll(dumpOption[3]); + int32_t userId = std::stoi(dumpOption[4]); + bundleActiveCore_->QueryBundleEvents(eventResult, userId, beginTime, endTime, ""); + for (auto& oneEvent : eventResult) { + dumpInfo.emplace_back(oneEvent.ToString()); + } + return ret; +} + +int32_t BundleActiveService::DumpPackageUsage(const std::vector &dumpOption, std::vector &dumpInfo) +{ + int32_t ret = -1; + std::vector packageUsageResult; + if (static_cast(dumpOption.size()) != PACKAGE_USAGE_PARAM) { + return ret; + } + int32_t intervalType = ConvertIntervalType(std::stoi(dumpOption[2])); + int64_t beginTime = std::stoll(dumpOption[3]); + int64_t endTime = std::stoll(dumpOption[4]); + int32_t userId = std::stoi(dumpOption[5]); + bundleActiveCore_->QueryBundleStatsInfos( + packageUsageResult, userId, intervalType, beginTime, endTime, ""); + for (auto& onePackageRecord : packageUsageResult) { + dumpInfo.emplace_back(onePackageRecord.ToString()); + } + return ret; +} + +int32_t BundleActiveService::DumpModuleUsage(const std::vector &dumpOption, std::vector &dumpInfo) +{ + int32_t ret = -1; + std::vector moduleResult; + if (static_cast(dumpOption.size()) != MODULE_USAGE_PARAM) { + return ret; + } + int32_t maxNum = std::stoi(dumpOption[2]); + int32_t userId = std::stoi(dumpOption[3]); + BUNDLE_ACTIVE_LOGI("M is %{public}d, u is %{public}d", maxNum, userId); + ret = bundleActiveCore_->QueryModuleUsageRecords(maxNum, moduleResult, userId); + for (auto& oneResult : moduleResult) { + QueryModuleRecordInfos(oneResult); + } + for (auto& oneModuleRecord : moduleResult) { + dumpInfo.emplace_back(oneModuleRecord.ToString()); + for (uint32_t i = 0; i < oneModuleRecord.formRecords_.size(); i++) { + std::string oneFormInfo = "form " + std::to_string(static_cast(i) + 1) + ", "; + dumpInfo.emplace_back(oneFormInfo + oneModuleRecord.formRecords_[i].ToString()); } } return ret; -- Gitee From 9dc6b5f502a289449086ce33c1d21788d921c31b Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 24 Dec 2024 15:03:21 +0800 Subject: [PATCH 5/7] push Signed-off-by: fengyang --- services/common/src/bundle_active_service.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 776eafc..77c2052 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -768,7 +768,8 @@ int32_t BundleActiveService::DumpEvents(const std::vector &dumpOpti return ret; } -int32_t BundleActiveService::DumpPackageUsage(const std::vector &dumpOption, std::vector &dumpInfo) +int32_t BundleActiveService::DumpPackageUsage(const std::vector &dumpOption, + std::vector &dumpInfo) { int32_t ret = -1; std::vector packageUsageResult; @@ -787,7 +788,8 @@ int32_t BundleActiveService::DumpPackageUsage(const std::vector &du return ret; } -int32_t BundleActiveService::DumpModuleUsage(const std::vector &dumpOption, std::vector &dumpInfo) +int32_t BundleActiveService::DumpModuleUsage(const std::vector &dumpOption, + std::vector &dumpInfo) { int32_t ret = -1; std::vector moduleResult; -- Gitee From 4663067106256daf24136b51d80cb1fed8382d56 Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 24 Dec 2024 15:47:48 +0800 Subject: [PATCH 6/7] push Signed-off-by: fengyang --- services/common/src/bundle_active_service.cpp | 19 +++++++------- utils/include/bundle_active_util.h | 2 ++ utils/src/bundle_active_util.cpp | 25 +++++++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 77c2052..6453fa2 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -32,6 +32,7 @@ #include "bundle_active_shutdown_callback_service.h" #include "tokenid_kit.h" #include "xcollie/watchdog.h" +#include "bundle_active_utils.h" #include "bundle_active_service.h" @@ -758,9 +759,9 @@ int32_t BundleActiveService::DumpEvents(const std::vector &dumpOpti if (static_cast(dumpOption.size()) != EVENTS_PARAM) { return ret; } - int64_t beginTime = std::stoll(dumpOption[2]); - int64_t endTime = std::stoll(dumpOption[3]); - int32_t userId = std::stoi(dumpOption[4]); + int64_t beginTime = BundleActiveUtil::StringToInt64(dumpOption[2]); + int64_t endTime = BundleActiveUtil::StringToInt64(dumpOption[3]); + int32_t userId = BundleActiveUtil::StringToInt32(dumpOption[4]); bundleActiveCore_->QueryBundleEvents(eventResult, userId, beginTime, endTime, ""); for (auto& oneEvent : eventResult) { dumpInfo.emplace_back(oneEvent.ToString()); @@ -776,10 +777,10 @@ int32_t BundleActiveService::DumpPackageUsage(const std::vector &du if (static_cast(dumpOption.size()) != PACKAGE_USAGE_PARAM) { return ret; } - int32_t intervalType = ConvertIntervalType(std::stoi(dumpOption[2])); - int64_t beginTime = std::stoll(dumpOption[3]); - int64_t endTime = std::stoll(dumpOption[4]); - int32_t userId = std::stoi(dumpOption[5]); + int32_t intervalType = ConvertIntervalType(BundleActiveUtil::StringToInt32(dumpOption[2])); + int64_t beginTime = BundleActiveUtil::StringToInt64(dumpOption[3]); + int64_t endTime = BundleActiveUtil::StringToInt64(dumpOption[4]); + int32_t userId = BundleActiveUtil::StringToInt32(dumpOption[5]); bundleActiveCore_->QueryBundleStatsInfos( packageUsageResult, userId, intervalType, beginTime, endTime, ""); for (auto& onePackageRecord : packageUsageResult) { @@ -796,8 +797,8 @@ int32_t BundleActiveService::DumpModuleUsage(const std::vector &dum if (static_cast(dumpOption.size()) != MODULE_USAGE_PARAM) { return ret; } - int32_t maxNum = std::stoi(dumpOption[2]); - int32_t userId = std::stoi(dumpOption[3]); + int32_t maxNum = BundleActiveUtil::StringToInt32(dumpOption[2]); + int32_t userId = BundleActiveUtil::StringToInt32(dumpOption[3]); BUNDLE_ACTIVE_LOGI("M is %{public}d, u is %{public}d", maxNum, userId); ret = bundleActiveCore_->QueryModuleUsageRecords(maxNum, moduleResult, userId); for (auto& oneResult : moduleResult) { diff --git a/utils/include/bundle_active_util.h b/utils/include/bundle_active_util.h index 12a2c04..67f3805 100644 --- a/utils/include/bundle_active_util.h +++ b/utils/include/bundle_active_util.h @@ -30,6 +30,8 @@ public: static std::string GetBundleUsageKey(const std::string& bundleName, const int32_t uid); static int64_t GetFFRTDelayTime(const int64_t& delayTime); static int64_t GetIntervalTypeStartTime(const int64_t& timeStamp, const int32_t& intervalType); + static int32_t StringToInt32(const std::string& str); + static int64_t StringToInt64(const std::string& str); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/utils/src/bundle_active_util.cpp b/utils/src/bundle_active_util.cpp index a92f91f..8bc7444 100644 --- a/utils/src/bundle_active_util.cpp +++ b/utils/src/bundle_active_util.cpp @@ -14,6 +14,7 @@ */ #include +#include #include "bundle_active_util.h" namespace OHOS { @@ -74,6 +75,30 @@ int64_t BundleActiveUtil::GetIntervalTypeStartTime(const int64_t& timeStamp, con } return mktime(tm_time) * SECOND_TO_MILLISECOND; } + +int32_t BundleActiveUtil::StringToInt32(const std::string& str) +{ + char* pEnd = nullptr; + errno = 0; + int64_t res = std::strtol(str.c_str(), &pEnd, 10); + if (errno == ERANGE || pEnd == str.c_str() || *pEnd != '\0' || + (res < std::numeric_limits::min()) || + res > std::numeric_limits::max()) { + return 0; + } + return static_cast(res); +} + +int64_t BundleActiveUtil::StringToInt64(const std::string& str) +{ + char* pEnd = nullptr; + errno = 0; + int64_t res = std::strtol(str.c_str(), &pEnd, 10); + if (errno == ERANGE || pEnd == str.c_str() || *pEnd != '\0') { + return 0; + } + return res; +} } // namespace DeviceUsageStats } // namespace OHOS -- Gitee From bdf564b57468c1b29aba6214a6076a37735cdd70 Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 24 Dec 2024 15:54:42 +0800 Subject: [PATCH 7/7] push Signed-off-by: fengyang --- services/common/src/bundle_active_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 6453fa2..d9203ca 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -32,7 +32,7 @@ #include "bundle_active_shutdown_callback_service.h" #include "tokenid_kit.h" #include "xcollie/watchdog.h" -#include "bundle_active_utils.h" +#include "bundle_active_util.h" #include "bundle_active_service.h" -- Gitee