diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index d9789d3f44560ea241c19d5c0cdfc7b15c1c6aae..c358dea66a8a6c0edcb2ae9ee6a71e60c6ec7bfa 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -47,6 +47,7 @@ #include "b_json/b_json_cached_entity.h" #include "b_jsonutil/b_jsonutil.h" #include "b_ohos/startup/backup_para.h" +#include "b_radar/b_radar.h" #include "b_tarball/b_tarball_factory.h" #include "filemgmt_libhilog.h" #include "hitrace_meter.h" @@ -126,10 +127,20 @@ void BackupExtExtension::VerifyCaller() uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID(); int tokenType = Security::AccessToken::AccessTokenKit::GetTokenType(tokenCaller); if (tokenType != Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) { + // 权限校验失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("BackupExtExtension::VerifyCaller", + AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + "{\"reason\":\"Calling tokenType error\"}"); throw BError(BError::Codes::EXT_BROKEN_IPC, string("Calling tokenType is error, token type is ").append(to_string(tokenType))); } if (IPCSkeleton::GetCallingUid() != BConstants::BACKUP_UID) { + // 权限校验失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("BackupExtExtension::VerifyCaller", + AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + "{\"reason\":\"Calling uid invalid\"}"); throw BError(BError::Codes::EXT_BROKEN_IPC, string("Calling uid is invalid, calling uid is ").append(to_string(IPCSkeleton::GetCallingUid()))); } @@ -826,7 +837,14 @@ void BackupExtExtension::AsyncTaskBackup(const string config) HILOGI("Do backup, start fwk timer end."); BJsonCachedEntity cachedEntity(config); auto cache = cachedEntity.Structuralize(); + auto start = std::chrono::system_clock::now(); auto ret = ptr->DoBackup(cache); + auto end = std::chrono::system_clock::now(); + auto exportDuration = std::chrono::duration_cast(end - start); + // 备份数据导出耗时 dadian + Backup::AppRadar::GetInstance().RecordDoBackUpRes("BackupExtExtension::AsyncTaskBackup", + AppRadar::GetInstance().GetUserId(), getpid(), static_cast(ret), + static_cast(exportDuration.count())); // REM: 处理返回结果 ret ptr->AppDone(ret); HILOGI("backup app done %{public}d", ret); @@ -1494,6 +1512,7 @@ void BackupExtExtension::AsyncTaskOnBackup() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::FULL_BACKUP); auto callBackup = ptr->OnBackupCallback(obj); auto callBackupEx = ptr->OnBackupExCallback(obj); + onBackUp_start = std::chrono::system_clock::now(); ErrCode err = ptr->extension_->OnBackup(callBackup, callBackupEx); if (err != ERR_OK) { HILOGE("OnBackup done, err = %{pubilc}d", err); @@ -1879,6 +1898,7 @@ void BackupExtExtension::AsyncTaskOnIncrementalBackup() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::INCREMENTAL_BACKUP); auto callBackup = ptr->IncOnBackupCallback(obj); auto callBackupEx = ptr->IncOnBackupExCallback(obj); + onBackUp_start = std::chrono::system_clock::now(); ErrCode err = ptr->extension_->OnBackup(callBackup, callBackupEx); if (err != ERR_OK) { HILOGE("OnBackup done, err = %{pubilc}d", err); diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index ae77b3e75a4f85071c412f921d32d4eadc5f4c1e..583f7945a108f2aecc3c0b1adece4508add43360 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -47,6 +47,7 @@ #include "b_json/b_json_cached_entity.h" #include "b_jsonutil/b_jsonutil.h" #include "b_ohos/startup/backup_para.h" +#include "b_radar/b_radar.h" #include "b_tarball/b_tarball_factory.h" #include "filemgmt_libhilog.h" #include "hitrace_meter.h" @@ -378,6 +379,14 @@ std::function BackupExtExtension::OnBackupCall HILOGE("Extension handle have been released"); return; } + // onBackUp耗时统计 dadian + if (errCode == ERR_OK) { + auto onBackUp_end = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(onBackUp_end - onBackUp_start).count(); + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::OnBackupCallback", + "bundleName", AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_ON_BACKUP, 0, + string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); + } extensionPtr->FinishOnProcessTask(); extensionPtr->AsyncTaskBackup(extensionPtr->extension_->GetUsrConfig()); }; @@ -397,6 +406,14 @@ std::function BackupExtExtension::OnBackupExCa HILOGE("Extension handle have been released"); return; } + // onBackUp耗时统计 dadian + if (errCode == ERR_OK && !backupExRetInfo.empty()) { + auto onBackUp_end = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(onBackUp_end - onBackUp_start).count(); + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::OnBackupExCallback", + "bundleName", AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_ON_BACKUP, 0, + string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); + } extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo); if (backupExRetInfo.size()) { HILOGI("Will notify backup result report"); @@ -422,6 +439,14 @@ std::function BackupExtExtension::IncOnBackupC return; } HILOGI("Start GetAppLocalListAndDoIncrementalBackup"); + // onBackUp耗时统计 dadian + if (errCode == ERR_OK) { + auto onBackUp_end = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(onBackUp_end - onBackUp_start).count(); + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::IncOnBackupCallback", + "bundleName", AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_ON_BACKUP, 0, + string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); + } extPtr->FinishOnProcessTask(); proxy->GetAppLocalListAndDoIncrementalBackup(); }; @@ -444,6 +469,14 @@ std::function BackupExtExtension::IncOnBackupE HILOGE("Extension handle have been released"); return; } + // onBackUp耗时统计 dadian + if (errCode == ERR_OK && !backupExRetInfo.empty()) { + auto onBackUp_end = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(onBackUp_end - onBackUp_start).count(); + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::IncOnBackupExCallback", + "bundleName", AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_ON_BACKUP, 0, + string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); + } extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo); if (backupExRetInfo.size()) { HILOGI("Start GetAppLocalListAndDoIncrementalBackup"); diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp index f747d28c7662c0b22edf870320e7253fee023ee2..e84ca093e475b5a57b3fec9f30f567755678b223 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp @@ -16,6 +16,7 @@ #include "b_incremental_backup_session.h" #include "b_error/b_error.h" +#include "b_radar/b_radar.h" #include "filemgmt_libhilog.h" #include "service_proxy.h" #include "service_reverse.h" @@ -55,6 +56,9 @@ unique_ptr BIncrementalBackupSession::Init(Callbacks int32_t res = proxy->InitIncrementalBackupSession(sptr(new ServiceReverse(callbacks))); if (res != 0) { HILOGE("Failed to Backup because of %{public}d", res); + // createSession失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("BIncrementalBackupSession::Init", + AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_CREATE_SESSION_BACKUP, res); return nullptr; } @@ -93,7 +97,17 @@ ErrCode BIncrementalBackupSession::AppendBundles(vector bundle return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup); + int32_t res = proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup); + if (res != 0) { + // appendBundles失败 dadian + std::string ss; + for (const auto &bundle:bundlesToBackup) { + ss += bundle.bundleName + ", "; + } + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BIncrementalBackupSession::AppendBundles", + ss.c_str(), AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_APPEND_BUNDLES, res); + } + return res; } ErrCode BIncrementalBackupSession::AppendBundles(vector bundlesToBackup, @@ -104,7 +118,17 @@ ErrCode BIncrementalBackupSession::AppendBundles(vector bundle return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup, infos); + int32_t res = proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup, infos); + if (res != 0) { + // appendBundles失败 dadian + std::string ss; + for (const auto &bundle:bundlesToBackup) { + ss += bundle.bundleName + ", "; + } + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BIncrementalBackupSession::AppendBundles", + ss.c_str(), AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_APPEND_BUNDLES, res); + } + return res; } ErrCode BIncrementalBackupSession::Release() diff --git a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp index 04176686bd8da61518fe2d153cc41f938d62503d..8933af7dd319df901d34b7cf38147987d213541f 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp @@ -16,6 +16,7 @@ #include "b_session_backup.h" #include "b_error/b_error.h" +#include "b_radar/b_radar.h" #include "filemgmt_libhilog.h" #include "service_proxy.h" #include "service_reverse.h" @@ -55,6 +56,9 @@ unique_ptr BSessionBackup::Init(Callbacks callbacks) int32_t res = proxy->InitBackupSession(sptr(new ServiceReverse(callbacks))); if (res != 0) { HILOGE("Failed to Backup because of %{public}d", res); + // createSession失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("BSessionBackup::Init", + AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_CREATE_SESSION_BACKUP, res); return nullptr; } @@ -103,7 +107,17 @@ ErrCode BSessionBackup::AppendBundles(vector bundlesToBackup) return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesBackupSession(bundlesToBackup); + int32_t res = proxy->AppendBundlesBackupSession(bundlesToBackup); + if (res != 0) { + // appendBundles失败 dadian + std::string ss; + for (const auto &bundleName:bundlesToBackup) { + ss += bundleName + ", "; + } + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BSessionBackup::AppendBundles", + ss.c_str(), AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_APPEND_BUNDLES, res); + } + return res; } ErrCode BSessionBackup::AppendBundles(vector bundlesToBackup, vector detailInfos) @@ -113,7 +127,17 @@ ErrCode BSessionBackup::AppendBundles(vector bundlesToBackup, vector return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesDetailsBackupSession(bundlesToBackup, detailInfos); + int32_t res = proxy->AppendBundlesDetailsBackupSession(bundlesToBackup, detailInfos); + if (res != 0) { + // appendBundles失败 dadian + std::string ss; + for (const auto &bundleName:bundlesToBackup) { + ss += bundleName + ", "; + } + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BSessionBackup::AppendBundles", + ss.c_str(), AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_APPEND_BUNDLES, res); + } + return res; } ErrCode BSessionBackup::Finish() diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 1870448e36d8b64b0980ce2579ddc4eb933a1af9..f25211ea35fc58c69d251df91f96db1a5bb37ba6 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -20,6 +20,7 @@ #include "b_error/b_error.h" #include "b_error/b_excep_utils.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "filemgmt_libhilog.h" #include "svc_death_recipient.h" @@ -446,6 +447,10 @@ sptr ServiceProxy::GetInstance() [loadCallback]() { return loadCallback->isLoadSuccess_.load(); }); if (!waitStatus) { HILOGE("Load backup sa timeout"); + // 拉起backup_sa失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("ServiceProxy::GetInstance", + AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_BACKUP_SA, + static_cast(BError::Codes::SA_INVAL_ARG), "{\"reason\":\"Load backup sa timeout\"}"); return nullptr; } return serviceProxy_; @@ -481,6 +486,11 @@ void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(int32_t HILOGE("Failed to get remote object"); serviceProxy_ = nullptr; isLoadSuccess_.store(false); + // 拉起backup_sa失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle( + "ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess", + AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_BACKUP_SA, + static_cast(BError::Codes::SA_INVAL_ARG), "{\"reason\":\"Load backup sa fail\"}"); proxyConVar_.notify_one(); return; } diff --git a/interfaces/inner_api/native/backup_kit_inner/BUILD.gn b/interfaces/inner_api/native/backup_kit_inner/BUILD.gn index c84d227f23979db6436a0793c0b705f4ce0d7a81..2ee1a876dd7b91829c20172f45856f18b82206e9 100644 --- a/interfaces/inner_api/native/backup_kit_inner/BUILD.gn +++ b/interfaces/inner_api/native/backup_kit_inner/BUILD.gn @@ -27,6 +27,7 @@ config("private_config") { "${path_backup}/frameworks/native/backup_kit_inner/include", "${path_backup}/interfaces/inner_api/native/backup_kit_inner", "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/utils/include", ] } diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index fa1da2e9384f79580e255cd006fe9ea4ecc77483..78ed2c935ca8f771cb4e770bc2f7ab452da6ed74 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -91,6 +91,9 @@ public: void OnStop() override; void StopAll(const wptr &obj, bool force = false); int Dump(int fd, const std::vector &args) override; + void PermissionCheckFailRadar(const std::string &info); + void GetLocalCapabilitiesRadar(const std::string &func, int32_t resultCode, + const std::string &info); /** * @brief 执行启动 backup extension diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp index f49ed5bd306f00f7e545f4441814dc09b4cc5a30..ea850705b9787150dd1056515751709b2f18b05a 100644 --- a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp +++ b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp @@ -17,6 +17,7 @@ #include +#include "b_radar/b_radar.h" #include "message_parcel.h" #include "want.h" @@ -64,13 +65,27 @@ sptr AppGalleryDisposeProxy::GetInstance() DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName) { HILOGI("StartBackup, app %{public}s", bundleName.c_str()); - return DoDispose(bundleName, DisposeOperation::START_BACKUP); + // 处置应用失败 dadian + auto res = DoDispose(bundleName, DisposeOperation::START_BACKUP); + if (res != DisposeErr::OK) { + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("AppGalleryDisposeProxy::StartBackup", + bundleName, AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_APPLICATION, + static_cast(res)); + } + return res; } DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName) { HILOGI("EndBackup, app %{public}s", bundleName.c_str()); - return DoDispose(bundleName, DisposeOperation::END_BACKUP); + // 清理处置失败 dadian + auto res = DoDispose(bundleName, DisposeOperation::END_BACKUP); + if (res != DisposeErr::OK) { + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("AppGalleryDisposeProxy::EndBackup", + bundleName, AppRadar::GetInstance().GetUserId(), getpid(), BizStage::BIZ_STAGE_APPLICATION, + static_cast(res)); + } + return res; } DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName) diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 6e834db31c8d0902b8dde96de5581cd09fae1d42..4f4b9fc57ad435353a40302ce7badf7f64f7b0e8 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -47,6 +47,7 @@ #include "b_jsonutil/b_jsonutil.h" #include "b_ohos/startup/backup_para.h" #include "b_process/b_multiuser.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "b_sa/b_sa_utils.h" #include "bundle_mgr_client.h" @@ -138,6 +139,14 @@ void Service::OnStop() HILOGI("SA OnStop End."); } +void Service::GetLocalCapabilitiesRadar(const std::string &func, int32_t resultCode, + const std::string &info) +{ + std::string funcPos = "Service::"; + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle(funcPos.append(func), + GetUserIdDefault(), getpid(), BizStage::BIZ_STAGE_GET_LOCAL_CAPABILITIES, resultCode, info); +} + UniqueFd Service::GetLocalCapabilities() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -149,6 +158,7 @@ UniqueFd Service::GetLocalCapabilities() HILOGI("Begin"); if (session_ == nullptr) { HILOGE("GetLocalCapabilities error, session is empty."); + GetLocalCapabilitiesRadar("GetLocalCapabilities", EPERM, "{\"reason\":\"session empty\"}"); return UniqueFd(-EPERM); } session_->IncreaseSessionCnt(); @@ -160,6 +170,8 @@ UniqueFd Service::GetLocalCapabilities() if (fd < 0) { HILOGE("Failed to open config file = %{private}s, err = %{public}d", path.c_str(), errno); session_->DecreaseSessionCnt(); + GetLocalCapabilitiesRadar("GetLocalCapabilities", EPERM, + "{\"reason\":\"failed to open config file\"}"); return UniqueFd(-EPERM); } BJsonCachedEntity cachedEntity(std::move(fd)); @@ -177,14 +189,17 @@ UniqueFd Service::GetLocalCapabilities() } catch (const BError &e) { session_->DecreaseSessionCnt(); HILOGE("GetLocalCapabilities failed, errCode = %{public}d", e.GetCode()); + GetLocalCapabilitiesRadar("GetLocalCapabilities", e.GetCode(), "{\"reason\":\"failed\"}"); return UniqueFd(-e.GetCode()); } catch (const exception &e) { session_->DecreaseSessionCnt(); HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); + GetLocalCapabilitiesRadar("GetLocalCapabilities", EPERM, "{\"reason\":\"exception\"}"); return UniqueFd(-EPERM); } catch (...) { session_->DecreaseSessionCnt(); HILOGI("Unexpected exception"); + GetLocalCapabilitiesRadar("GetLocalCapabilities", EPERM, "{\"reason\":\"exception\"}"); return UniqueFd(-EPERM); } } @@ -203,6 +218,11 @@ string Service::VerifyCallerAndGetCallerName() if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) { Security::AccessToken::HapTokenInfo hapTokenInfo; if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenCaller, hapTokenInfo) != 0) { + // 权限校验失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("Service::VerifyCallerAndGetCallerName", + GetUserIdDefault(), getpid(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + "{\"reason\":\"Get hap token info failed\"}"); throw BError(BError::Codes::SA_INVAL_ARG, "Get hap token info failed"); } std::string bundleNameIndexInfo = BJsonUtil::BuildBundleNameIndexInfo(hapTokenInfo.bundleName, @@ -212,10 +232,22 @@ string Service::VerifyCallerAndGetCallerName() } else { string str = to_string(tokenCaller); HILOGE("tokenID = %{private}s", GetAnonyString(str).c_str()); + // 权限校验失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("Service::VerifyCallerAndGetCallerName", + GetUserIdDefault(), getpid(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + string("{\"reason\":\"Invalid token type ").append(to_string(tokenType)).append(string("\"}"))); throw BError(BError::Codes::SA_INVAL_ARG, string("Invalid token type ").append(to_string(tokenType))); } } +void Service::PermissionCheckFailRadar(const std::string &info) +{ + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("Service::VerifyCaller", + GetUserIdDefault(), getpid(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), info); +} + void Service::VerifyCaller() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -225,6 +257,8 @@ void Service::VerifyCaller() case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE: { /* Update Service */ if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, BACKUP_PERMISSION) != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + std::string info = "{\"reason\":\"Permission denied, token type is " + to_string(tokenType) + "\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, string("Permission denied, token type is ").append(to_string(tokenType))); } @@ -233,11 +267,15 @@ void Service::VerifyCaller() case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: { if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, BACKUP_PERMISSION) != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + std::string info = "{\"reason\":\"Permission denied, token type is " + to_string(tokenType) + "\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, string("Permission denied, token type is ").append(to_string(tokenType))); } uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) { + std::string info = "{\"reason\":\"Permission denied, token type is " + to_string(tokenType) + "\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, string("Permission denied, token type is ").append(to_string(tokenType))); } @@ -245,10 +283,14 @@ void Service::VerifyCaller() } case Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL: if (IPCSkeleton::GetCallingUid() != BConstants::SYSTEM_UID) { + std::string info = "{\"reason\":\"invalid calling uid\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, "Calling uid is invalid"); } break; default: + std::string info = "{\"reason\":\"Permission denied, token type is " + to_string(tokenType) + "\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, string("Invalid token type ").append(to_string(tokenType))); break; } @@ -1003,6 +1045,10 @@ ErrCode Service::GetFileHandle(const string &bundleName, const string &fileName) void Service::OnBackupExtensionDied(const string &&bundleName) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + // backupextension运行中异常退出 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("Service::OnBackupExtensionDied", + bundleName, GetUserIdDefault(), getpid(), BizStage::BIZ_STAGE_EXTENSION_STATUS, + static_cast(BError::Codes::EXT_ABILITY_DIED)); try { string callName = move(bundleName); HILOGE("Backup <%{public}s> Extension Process Died", callName.c_str()); @@ -1520,6 +1566,9 @@ ErrCode Service::GetBackupInfoCmdHandle(BundleName &bundleName, std::string &res backupConnection->DisconnectBackupExtAbility(); if (ret != ERR_OK) { HILOGE("Call Ext GetBackupInfo faild."); + // getbackupinfo失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("Service::GetBackupInfoCmdHandle", + bundleName, GetUserIdDefault(), getpid(), BizStage::BIZ_STAGE_GET_BACKUP_INFO, ret); return BError(BError::Codes::SA_INVAL_ARG); } diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index deb63ff3cfd57d693b892709645f62cc732a952f..0f5f29345f093759e5c9fe0a37f08bc20adef6b5 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -38,6 +38,7 @@ #include "b_json/b_json_entity_caps.h" #include "b_ohos/startup/backup_para.h" #include "b_process/b_multiuser.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "b_sa/b_sa_utils.h" #include "filemgmt_libhilog.h" @@ -95,6 +96,7 @@ UniqueFd Service::GetLocalCapabilitiesIncremental(const std::vectorIncreaseSessionCnt(); @@ -106,6 +108,7 @@ UniqueFd Service::GetLocalCapabilitiesIncremental(const std::vectorDecreaseSessionCnt(); + GetLocalCapabilitiesRadar("GetLocalCapabilitiesIncremental", ENOENT, "{\"reason\":\"open file failed\"}"); return UniqueFd(-ENOENT); } BJsonCachedEntity cachedEntity(move(fd)); @@ -123,14 +126,17 @@ UniqueFd Service::GetLocalCapabilitiesIncremental(const std::vectorDecreaseSessionCnt(); HILOGE("GetLocalCapabilitiesIncremental failed, errCode = %{public}d", e.GetCode()); + GetLocalCapabilitiesRadar("GetLocalCapabilitiesIncremental", e.GetCode(), "{\"reason\":\"fail\"}"); return UniqueFd(-e.GetCode()); } catch (const exception &e) { session_->DecreaseSessionCnt(); HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); + GetLocalCapabilitiesRadar("GetLocalCapabilitiesIncremental", EPERM, "{\"reason\":\"exception\"}"); return UniqueFd(-EPERM); } catch (...) { session_->DecreaseSessionCnt(); HILOGI("Unexpected exception"); + GetLocalCapabilitiesRadar("GetLocalCapabilitiesIncremental", EPERM, "{\"reason\":\"exception\"}"); return UniqueFd(-EPERM); } } diff --git a/services/backup_sa/src/module_ipc/svc_session_manager.cpp b/services/backup_sa/src/module_ipc/svc_session_manager.cpp index 7a353705aa42ca515b35519eee8a0d9b68ab07eb..4b70969881368dd4d935e4161f5adadcbc284ef4 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -26,6 +26,7 @@ #include "b_file_info.h" #include "b_json/b_json_entity_caps.h" #include "b_json/b_json_entity_ext_manage.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "b_sa/b_sa_utils.h" #include "b_utils/b_time.h" @@ -42,9 +43,21 @@ void SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceRe shared_lock lock(lock_); if (impl_.scenario != scenario) { HILOGE("Inconsistent scenario, impl scenario:%{public}d", impl_.scenario); + // 权限校验失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle( + "SvcSessionManager::VerifyCallerAndScenario", + impl_.userId, getpid(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SDK_MIXED_SCENARIO), + "{\"reason\":\"Inconsistent scenario\"}"); throw BError(BError::Codes::SDK_MIXED_SCENARIO); } if (impl_.clientToken != clientToken) { + // 权限校验失败 dadian + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle( + "SvcSessionManager::VerifyCallerAndScenario", + impl_.userId, getpid(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + "{\"reason\":\"Caller mismatched\"}"); throw BError(BError::Codes::SA_REFUSED_ACT, "Caller mismatched"); } HILOGD("Succeed to verify the caller"); @@ -357,6 +370,11 @@ void SvcSessionManager::InitClient(Impl &newImpl) HILOGW("It's curious that the backup sa dies before the backup client"); return; } + // 克隆APP/云空间异常退出 dadian + AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("SvcSessionManager::InitClient", + AppRadar::GetInstance().GetUserId(), + getpid(), BizStage::BIZ_STAGE_CLIENT_STATUS, + static_cast(BError::Codes::SA_BROKEN_IPC), "cloud_clone"); (void)revPtrStrong->SessionDeactive(); }; deathRecipient_ = sptr(new SvcDeathRecipient(callback)); diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 1ff282364003bb5634aeb68643db99d54c8dcc05..90126bd99e2d6c4508069b34d92621f0e3d0cb81 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -74,6 +74,7 @@ ohos_shared_library("backup_utils") { "src/b_process/b_guard_cwd.cpp", "src/b_process/b_guard_signal.cpp", "src/b_process/b_process.cpp", + "src/b_radar/b_radar.cpp", "src/b_sa/b_sa_utils.cpp", "src/b_tarball/b_tarball_cmdline.cpp", "src/b_tarball/b_tarball_factory.cpp", @@ -90,6 +91,7 @@ ohos_shared_library("backup_utils") { "c_utils:utils", "faultloggerd:libdfx_dumpcatcher", "hilog:libhilog", + "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -98,6 +100,7 @@ ohos_shared_library("backup_utils") { include_dirs = [ "${path_init}/interfaces/innerkits/include/syspara", "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/utils/include", ] deps = [ diff --git a/utils/include/b_radar/b_radar.h b/utils/include/b_radar/b_radar.h new file mode 100644 index 0000000000000000000000000000000000000000..8ac9769d1712f20569e16b8cd0a75c81d3119e58 --- /dev/null +++ b/utils/include/b_radar/b_radar.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_B_RADAR_H +#define OHOS_FILEMGMT_BACKUP_B_RADAR_H + +#include +#include + +namespace { +std::chrono::time_point onBackUp_start; +} +namespace OHOS::FileManagement::Backup { +enum class BizScene : int32_t { + BACKUP = 1, + RESTORE = 2 +}; + +enum class BizStage : int32_t { + BIZ_STAGE_GET_LOCAL_CAPABILITIES = 1, + BIZ_STAGE_GET_BACKUP_INFO, + BIZ_STAGE_CREATE_SESSION_BACKUP, + BIZ_STAGE_BACKUP_SA, + BIZ_STAGE_APPEND_BUNDLES, + BIZ_STAGE_BACKUP_EXTENSION, + BIZ_STAGE_APPLICATION, + BIZ_STAGE_EXTENSION_STATUS, + BIZ_STAGE_ON_BACKUP, + BIZ_STAGE_DO_BACKUP, + BIZ_STAGE_DATA_CONSISTENCY, + BIZ_STAGE_STATUS_CLEAR, + BIZ_STAGE_CLIENT_STATUS, + BIZ_STAGE_PERMISSION_CHECK +}; + +class AppRadar { +public: + static AppRadar &GetInstance() + { + static AppRadar instance; + return instance; + } + +public: + int32_t GetUserId(); + void RecordBackUpFuncResWithBundle(const std::string &func, const std::string &bundleName, + int32_t userId, int32_t pid, enum BizStage bizStage, int32_t resultCode, + const std::string &resultInfo = "{\"reason\":\"failed\"}"); + void RecordBackUpFuncResWithoutBundle(const std::string &func, int32_t userId, int32_t pid, + enum BizStage bizStage, int32_t resultCode, + const std::string &resultInfo = "{\"reason\":\"failed\"}"); + void RecordDoBackUpRes(const std::string &func, int32_t userId, int32_t pid, + int32_t resultCode, int32_t exportDuration); +private: + AppRadar() = default; + ~AppRadar() = default; + AppRadar(const AppRadar &) = delete; + AppRadar &operator=(const AppRadar &) = delete; + AppRadar(AppRadar &&) = delete; + AppRadar &operator=(AppRadar &&) = delete; +}; +} // namespace OHOS::FileManagement::AppRadar +#endif // OHOS_FILEMGMT_BACKUP_B_RADAR_H \ No newline at end of file diff --git a/utils/src/b_radar/b_radar.cpp b/utils/src/b_radar/b_radar.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4453c65d0a19d1870e7d8c40a5db24eab2001872 --- /dev/null +++ b/utils/src/b_radar/b_radar.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "b_radar/b_radar.h" + +#include +#include +#include +#include + +#include "b_process/b_multiuser.h" +#include "b_resources/b_constants.h" +#include "hisysevent.h" + +namespace { +constexpr uint8_t INDEX = 3; +constexpr int32_t MS_1000 = 1000; +const std::string FILE_BACKUP_EVENTS = "FILE_BACKUP_EVENTS"; +} + +namespace OHOS::FileManagement::Backup { +static std::string GetCurrentTime() +{ + auto now = std::chrono::system_clock::now(); + auto time = std::chrono::system_clock::to_time_t(now); + auto ms = std::chrono::duration_cast(now.time_since_epoch()); + std::stringstream strTime; + strTime << (std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S:")) << (std::setfill('0')) + << (std::setw(INDEX)) << (ms.count() % MS_1000); + return strTime.str(); +} + +int32_t AppRadar::GetUserId() +{ + auto multiuser = BMultiuser::ParseUid(getuid()); + if ((multiuser.userId == BConstants::SYSTEM_UID) || (multiuser.userId == BConstants::XTS_UID)) { + return BConstants::DEFAULT_USER_ID; + } + return multiuser.userId; +} + +void AppRadar::RecordBackUpFuncResWithBundle(const std::string &func, const std::string &bundleName, + int32_t userId, int32_t pid, enum BizStage bizStage, int32_t resultCode, + const std::string &resultInfo) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "BUNDLE_NAME", bundleName, + "USER_ID", userId, + "PID", pid, + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::BACKUP), + "BIZ_STAGE", static_cast(bizStage), + "RESULT_CODE", resultCode, + "RESULT_INFO", resultInfo); +} + +void AppRadar::RecordBackUpFuncResWithoutBundle(const std::string &func, int32_t userId, int32_t pid, + enum BizStage bizStage, int32_t resultCode, + const std::string &resultInfo) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "USER_ID", userId, + "PID", pid, + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::BACKUP), + "BIZ_STAGE", static_cast(bizStage), + "RESULT_CODE", resultCode, + "RESULT_INFO", resultInfo); +} + +void AppRadar::RecordDoBackUpRes(const std::string &func, int32_t userId, int32_t pid, + int32_t resultCode, int32_t exportDuration) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "USER_ID", userId, + "PID", pid, + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::BACKUP), + "BIZ_STAGE", static_cast(BizStage::BIZ_STAGE_DO_BACKUP), + "EXEC_STATUS", "{\"spend_time\":" + std::to_string(exportDuration) + "ms\"}", + "RESULT_CODE", resultCode); +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file