From e58c3d687811269541badc163374ae2daff7c7a5 Mon Sep 17 00:00:00 2001 From: baoyang Date: Fri, 3 Jan 2025 11:13:43 +0800 Subject: [PATCH] add retry Signed-off-by: baoyang Change-Id: I2e2ab715c82f5d0bb777e8ac71d222737530111d --- .../include/sec_comp_client.h | 3 ++ .../src/sec_comp_client.cpp | 28 ++++++++++++++++++- .../security_component/src/sec_comp_proxy.cpp | 9 +++--- .../sa/sa_main/sec_comp_stub.cpp | 2 ++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/frameworks/inner_api/security_component/include/sec_comp_client.h b/frameworks/inner_api/security_component/include/sec_comp_client.h index 563376e..ebb69a8 100644 --- a/frameworks/inner_api/security_component/include/sec_comp_client.h +++ b/frameworks/inner_api/security_component/include/sec_comp_client.h @@ -64,6 +64,9 @@ private: bool readyFlag_ = false; std::condition_variable secComCon_; std::mutex proxyMutex_; + bool secCompSAFlag_ = false; + std::condition_variable secCompSACon_; + std::mutex secCompSAMutex_; sptr proxy_ = nullptr; sptr serviceDeathObserver_ = nullptr; }; diff --git a/frameworks/inner_api/security_component/src/sec_comp_client.cpp b/frameworks/inner_api/security_component/src/sec_comp_client.cpp index 26836a7..65c52f1 100644 --- a/frameworks/inner_api/security_component/src/sec_comp_client.cpp +++ b/frameworks/inner_api/security_component/src/sec_comp_client.cpp @@ -19,7 +19,10 @@ #include "sec_comp_load_callback.h" #include "sec_comp_log.h" #include "sec_comp_proxy.h" +#include "sys_binder.h" #include "tokenid_kit.h" +#include +#include namespace OHOS { namespace Security { @@ -27,6 +30,10 @@ namespace SecurityComponent { namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompClient"}; static std::mutex g_instanceMutex; +static constexpr int32_t SENDREQ_FAIL_ERR = 32; +static const std::vector RETRY_CODE_LIST = { + SC_SERVICE_ERROR_SERVICE_NOT_EXIST, BR_DEAD_REPLY, BR_FAILED_REPLY, SENDREQ_FAIL_ERR }; +static const int32_t SA_DIED_TIME_OUT = 500; } // namespace SecCompClient& SecCompClient::GetInstance() @@ -64,7 +71,17 @@ int32_t SecCompClient::RegisterSecurityComponent(SecCompType type, return SC_SERVICE_ERROR_VALUE_INVALID; } - return proxy->RegisterSecurityComponent(type, componentInfo, scId); + auto res = proxy->RegisterSecurityComponent(type, componentInfo, scId); + if (std::find(RETRY_CODE_LIST.begin(), RETRY_CODE_LIST.end(), res) != RETRY_CODE_LIST.end()) { + std::unique_lock lock(secCompSAMutex_); + auto waitStatus = secCompSACon_.wait_for(lock, std::chrono::milliseconds(SA_DIED_TIME_OUT), + [this]() { return secCompSAFlag_; }); + if (waitStatus) { + proxy = GetProxy(true); + return proxy->RegisterSecurityComponent(type, componentInfo, scId); + } + } + return res; } int32_t SecCompClient::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) @@ -238,12 +255,21 @@ void SecCompClient::OnRemoteDiedHandle() { SC_LOG_ERROR(LABEL, "Remote service died"); std::unique_lock lock(proxyMutex_); + auto remoteObj = proxy_->AsObject(); + if ((remoteObj != nullptr) && (serviceDeathObserver_ != nullptr)) { + remoteObj->RemoveDeathRecipient(serviceDeathObserver_); + } proxy_ = nullptr; serviceDeathObserver_ = nullptr; { std::unique_lock lock1(cvLock_); readyFlag_ = false; } + { + std::unique_lock lock1(secCompSAMutex_); + secCompSAFlag_ = true; + secCompSACon_.notify_one(); + } } void SecCompClient::GetProxyFromRemoteObject(const sptr& remoteObject) diff --git a/frameworks/inner_api/security_component/src/sec_comp_proxy.cpp b/frameworks/inner_api/security_component/src/sec_comp_proxy.cpp index f8eb802..4eca722 100644 --- a/frameworks/inner_api/security_component/src/sec_comp_proxy.cpp +++ b/frameworks/inner_api/security_component/src/sec_comp_proxy.cpp @@ -70,17 +70,16 @@ int32_t SecCompProxy::RegisterSecurityComponent(SecCompType type, int32_t requestResult = remote->SendRequest( static_cast(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT), data, reply, option); + if (requestResult != SC_OK) { + SC_LOG_ERROR(LABEL, "Register request failed, result: %{public}d.", requestResult); + return requestResult; + } if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(reply, deserializedReply)) { SC_LOG_ERROR(LABEL, "Register deserialize session info failed."); return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; } - if (requestResult != SC_OK) { - SC_LOG_ERROR(LABEL, "Register request failed, result: %{public}d.", requestResult); - return requestResult; - } - int32_t res; if (!deserializedReply.ReadInt32(res)) { SC_LOG_ERROR(LABEL, "Register read res failed."); diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index b50d932..662e978 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -15,6 +15,7 @@ #include "sec_comp_stub.h" #include "accesstoken_kit.h" +#include "delay_exit_task.h" #include "ipc_skeleton.h" #include "sec_comp_click_event_parcel.h" #include "sec_comp_enhance_adapter.h" @@ -52,6 +53,7 @@ int32_t SecCompStub::OnRemoteRequest( int32_t SecCompStub::RegisterSecurityComponentInner(MessageParcel& data, MessageParcel& reply) { + SecurityComponent::DelayExitTask::GetInstance().Stop(); MessageParcel deserializedData; if (!SecCompEnhanceAdapter::EnhanceSrvDeserialize(data, deserializedData, reply)) { SC_LOG_ERROR(LABEL, "Register deserialize session info failed"); -- Gitee