From 640b8e42ae9fe75978b812f50f962b7ceeef9ec9 Mon Sep 17 00:00:00 2001 From: j21p3 Date: Thu, 29 Aug 2024 10:09:00 +0800 Subject: [PATCH] add retry mechanism MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I22bf80af806da84e3feae04bbf3a34f893e9d585 Signed-off-by: j21p3 (cherry picked commit from --- interfaces/innerkits/sandbox_manager/BUILD.gn | 2 - .../include/sandbox_manager_client.h | 24 +- .../include/sandbox_manager_death_recipient.h | 34 -- .../include/sandbox_manager_load_callback.h | 35 -- .../src/sandbox_manager_client.cpp | 349 ++++++------------ .../src/sandbox_manager_death_recipient.cpp | 28 -- .../src/sandbox_manager_load_callback.cpp | 62 ---- .../unittest/src/sandbox_manager_kit_test.cpp | 1 - 8 files changed, 125 insertions(+), 410 deletions(-) delete mode 100644 interfaces/innerkits/sandbox_manager/include/sandbox_manager_death_recipient.h delete mode 100644 interfaces/innerkits/sandbox_manager/include/sandbox_manager_load_callback.h delete mode 100644 interfaces/innerkits/sandbox_manager/src/sandbox_manager_death_recipient.cpp delete mode 100644 interfaces/innerkits/sandbox_manager/src/sandbox_manager_load_callback.cpp diff --git a/interfaces/innerkits/sandbox_manager/BUILD.gn b/interfaces/innerkits/sandbox_manager/BUILD.gn index 48603bb..1d814fb 100644 --- a/interfaces/innerkits/sandbox_manager/BUILD.gn +++ b/interfaces/innerkits/sandbox_manager/BUILD.gn @@ -51,9 +51,7 @@ if (is_standard_system) { sources = [ "src/sandbox_manager_client.cpp", - "src/sandbox_manager_death_recipient.cpp", "src/sandbox_manager_kit.cpp", - "src/sandbox_manager_load_callback.cpp", "src/sandbox_manager_proxy.cpp", ] diff --git a/interfaces/innerkits/sandbox_manager/include/sandbox_manager_client.h b/interfaces/innerkits/sandbox_manager/include/sandbox_manager_client.h index 9c9f0c5..f7e8ea0 100644 --- a/interfaces/innerkits/sandbox_manager/include/sandbox_manager_client.h +++ b/interfaces/innerkits/sandbox_manager/include/sandbox_manager_client.h @@ -30,7 +30,6 @@ #include "nocopyable.h" #include "policy_info.h" #include "refbase.h" -#include "sandbox_manager_death_recipient.h" namespace OHOS { namespace AccessControl { @@ -59,29 +58,18 @@ public: int32_t StartAccessingByTokenId(uint32_t tokenId); int32_t UnSetAllPolicyByToken(uint32_t tokenId); - void FinishStartSASuccess(const sptr &remoteObject); - void FinishStartSAFail(); - void OnRemoteDiedHandle(); - private: SandboxManagerClient(); DISALLOW_COPY_AND_MOVE(SandboxManagerClient); - sptr proxy_ = nullptr; - sptr GetProxy(bool doLoadSa); - void GetProxyFromRemoteObject(const sptr &remoteObject); - - bool StartLoadSandboxManagerSa(); - void WaitForSandboxManagerSa(); - void LoadSandboxManagerSa(); - void GetSandboxManagerSa(); - std::mutex cvLock_; - bool readyFlag_ = false; - std::condition_variable sandboxManagerCon_; std::mutex proxyMutex_; - sptr serviceDeathObserver_ = nullptr; + sptr GetProxy(); + + int32_t CallProxyWithRetry(const std::function &)> &func, + const char *funcName); + bool IsRequestNeedRetry(int32_t ret); }; } // SandboxManager } // AccessControl } // OHOS -#endif //SANDBOXMANAGER_CLIENT_H \ No newline at end of file +#endif // SANDBOXMANAGER_CLIENT_H \ No newline at end of file diff --git a/interfaces/innerkits/sandbox_manager/include/sandbox_manager_death_recipient.h b/interfaces/innerkits/sandbox_manager/include/sandbox_manager_death_recipient.h deleted file mode 100644 index fbe1652..0000000 --- a/interfaces/innerkits/sandbox_manager/include/sandbox_manager_death_recipient.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2023 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 SANDBOX_MANAGER_DEATH_RECIPIENT_H -#define SANDBOX_MANAGER_DEATH_RECIPIENT_H - -#include "iremote_object.h" - -namespace OHOS { -namespace AccessControl { -namespace SandboxManager { -class SandboxManagerDeathRecipient : public IRemoteObject::DeathRecipient { -public: - SandboxManagerDeathRecipient() - {} - virtual ~SandboxManagerDeathRecipient() = default; - void OnRemoteDied(const wptr &object) override; -}; -} // SandboxManager -} // AccessControl -} // OHOS -#endif // SANDBOX_MANAGER_DEATH_RECIPIENT_H \ No newline at end of file diff --git a/interfaces/innerkits/sandbox_manager/include/sandbox_manager_load_callback.h b/interfaces/innerkits/sandbox_manager/include/sandbox_manager_load_callback.h deleted file mode 100644 index 663a69c..0000000 --- a/interfaces/innerkits/sandbox_manager/include/sandbox_manager_load_callback.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2023 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 SANDBOX_MANAGER_LOAD_CALLBACK_H -#define SANDBOX_MANAGER_LOAD_CALLBACK_H -#include -#include "i_sandbox_manager.h" -#include "system_ability_load_callback_stub.h" - -namespace OHOS { -namespace AccessControl { -namespace SandboxManager { -class SandboxManagerLoadCallback : public SystemAbilityLoadCallbackStub { -public: - explicit SandboxManagerLoadCallback(); - - void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr &remoteObject); - void OnLoadSystemAbilityFail(int32_t systemAbilityId); -}; -} // SandboxManager -} // AccessControl -} // OHOS -#endif // SANDBOX_MANAGER_LOAD_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/sandbox_manager/src/sandbox_manager_client.cpp b/interfaces/innerkits/sandbox_manager/src/sandbox_manager_client.cpp index dd870e4..bce3393 100644 --- a/interfaces/innerkits/sandbox_manager/src/sandbox_manager_client.cpp +++ b/interfaces/innerkits/sandbox_manager/src/sandbox_manager_client.cpp @@ -16,21 +16,29 @@ #include "sandbox_manager_client.h" #include -#include "iservice_registry.h" +#include #include "i_sandbox_manager.h" +#include "iservice_registry.h" #include "refbase.h" -#include "sandbox_manager_death_recipient.h" #include "sandbox_manager_err_code.h" -#include "sandbox_manager_load_callback.h" #include "sandbox_manager_log.h" +#include "sys_binder.h" namespace OHOS { namespace AccessControl { namespace SandboxManager { +namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, ACCESSCONTROL_DOMAIN_SANDBOXMANAGER, "SandboxManagerClient" }; -static const int32_t SANDBOX_MANAGER_LOAD_SA_TIMEOUT_MS = 4000; +static const int32_t SANDBOX_MANAGER_LOAD_SA_TIMEOUT_SEC = 4; +static const int32_t SANDBOX_MANAGER_LOAD_SA_TRY_TIMES = 2; +static const int32_t SA_REQUEST_RETRY_TIMES = 1; + +static const int32_t SENDREQ_FAIL_ERR = 32; +static const std::vector RETRY_CODE_LIST = { + BR_DEAD_REPLY, BR_FAILED_REPLY, SENDREQ_FAIL_ERR }; +} SandboxManagerClient& SandboxManagerClient::GetInstance() { @@ -46,308 +54,189 @@ SandboxManagerClient::~SandboxManagerClient() int32_t SandboxManagerClient::CleanPersistPolicyByPath(const std::vector& filePathList) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->CleanPersistPolicyByPath(filePathList); + std::function &)> func = + [&](sptr &proxy) { return proxy->CleanPersistPolicyByPath(filePathList); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::PersistPolicy(const std::vector &policy, std::vector &result) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->PersistPolicy(policy, result); + std::function &)> func = + [&](sptr &proxy) { return proxy->PersistPolicy(policy, result); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::UnPersistPolicy(const std::vector &policy, std::vector &result) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->UnPersistPolicy(policy, result); + std::function &)> func = + [&](sptr &proxy) { return proxy->UnPersistPolicy(policy, result); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::PersistPolicyByTokenId( uint32_t tokenId, const std::vector &policy, std::vector &result) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->PersistPolicyByTokenId(tokenId, policy, result); + std::function &)> func = + [&](sptr &proxy) { return proxy->PersistPolicyByTokenId(tokenId, policy, result); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::UnPersistPolicyByTokenId( uint32_t tokenId, const std::vector &policy, std::vector &result) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->UnPersistPolicyByTokenId(tokenId, policy, result); + std::function &)> func = + [&](sptr &proxy) { return proxy->UnPersistPolicyByTokenId(tokenId, policy, result); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::SetPolicy(uint32_t tokenId, const std::vector &policy, uint64_t policyFlag, std::vector &result) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Proxy is null."); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->SetPolicy(tokenId, policy, policyFlag, result); + std::function &)> func = + [&](sptr &proxy) { return proxy->SetPolicy(tokenId, policy, policyFlag, result); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::UnSetPolicy(uint32_t tokenId, const PolicyInfo &policy) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Proxy is null."); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->UnSetPolicy(tokenId, policy); + std::function &)> func = + [&](sptr &proxy) { return proxy->UnSetPolicy(tokenId, policy); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::SetPolicyAsync(uint32_t tokenId, const std::vector &policy, uint64_t policyFlag) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Proxy is null."); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->SetPolicyAsync(tokenId, policy, policyFlag); + std::function &)> func = + [&](sptr &proxy) { return proxy->SetPolicyAsync(tokenId, policy, policyFlag); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::UnSetPolicyAsync(uint32_t tokenId, const PolicyInfo &policy) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Proxy is null."); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->UnSetPolicyAsync(tokenId, policy); + std::function &)> func = + [&](sptr &proxy) { return proxy->UnSetPolicyAsync(tokenId, policy); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::CheckPolicy(uint32_t tokenId, const std::vector &policy, std::vector &result) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Proxy is null."); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->CheckPolicy(tokenId, policy, result); + std::function &)> func = + [&](sptr &proxy) { return proxy->CheckPolicy(tokenId, policy, result); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::StartAccessingPolicy( const std::vector &policy, std::vector &result) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->StartAccessingPolicy(policy, result); + std::function &)> func = + [&](sptr &proxy) { return proxy->StartAccessingPolicy(policy, result); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::StopAccessingPolicy(const std::vector &policy, std::vector &result) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->StopAccessingPolicy(policy, result); + std::function &)> func = + [&](sptr &proxy) { return proxy->StopAccessingPolicy(policy, result); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::CheckPersistPolicy( uint32_t tokenId, const std::vector &policy, std::vector &result) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->CheckPersistPolicy(tokenId, policy, result); + std::function &)> func = + [&](sptr &proxy) { return proxy->CheckPersistPolicy(tokenId, policy, result); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::StartAccessingByTokenId(uint32_t tokenId) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->StartAccessingByTokenId(tokenId); + std::function &)> func = + [&](sptr &proxy) { return proxy->StartAccessingByTokenId(tokenId); }; + return CallProxyWithRetry(func, __FUNCTION__); } int32_t SandboxManagerClient::UnSetAllPolicyByToken(uint32_t tokenId) { - auto proxy = GetProxy(true); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Proxy is null"); - return SANDBOX_MANAGER_SERVICE_NOT_EXIST; - } - return proxy->UnSetAllPolicyByToken(tokenId); -} - -bool SandboxManagerClient::StartLoadSandboxManagerSa() -{ - { - std::unique_lock lock(cvLock_); - readyFlag_ = false; - } - auto sam = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (sam == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "GetSystemAbilityManager is null"); - return false; - } - sptr ptrSandboxManagerLoadCallback = new (std::nothrow) SandboxManagerLoadCallback(); - if (ptrSandboxManagerLoadCallback == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "New ptrSandboxManagerLoadCallback fail."); - return false; - } - - int32_t result = sam->LoadSystemAbility(ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE, - ptrSandboxManagerLoadCallback); - if (result != SANDBOX_MANAGER_OK) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "LoadSystemAbility %{public}d failed", - ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE); - return false; - } - SANDBOXMANAGER_LOG_INFO(LABEL, "Notify samgr load sa %{public}d success", - ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE); - return true; + std::function &)> func = + [&](sptr &proxy) { return proxy->UnSetAllPolicyByToken(tokenId); }; + return CallProxyWithRetry(func, __FUNCTION__); } -void SandboxManagerClient::WaitForSandboxManagerSa() +sptr SandboxManagerClient::GetProxy() { - // wait_for release lock and block until time out(1s) or match the condition with notice - std::unique_lock lock(cvLock_); - auto waitStatus = sandboxManagerCon_.wait_for( - lock, std::chrono::milliseconds(SANDBOX_MANAGER_LOAD_SA_TIMEOUT_MS), [this]() { return readyFlag_; }); - if (!waitStatus) { - // time out or loadcallback fail - SANDBOXMANAGER_LOG_ERROR(LABEL, "Sandbox manager load sa timeout"); - return; - } -} - -void SandboxManagerClient::GetSandboxManagerSa() -{ - auto sam = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (sam == nullptr) { + std::unique_lock lock(proxyMutex_); + auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { SANDBOXMANAGER_LOG_ERROR(LABEL, "GetSystemAbilityManager return null"); - return; - } - - auto sa = sam->GetSystemAbility(ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE); - if (sa == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null", - ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE); - return; - } - - GetProxyFromRemoteObject(sa); -} - -void SandboxManagerClient::LoadSandboxManagerSa() -{ - if (!StartLoadSandboxManagerSa()) { - return; + return nullptr; + } + // If sa is loaded, CheckSystemAbility return not null. + auto remoteObject = samgr->CheckSystemAbility(ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE); + if (remoteObject != nullptr) { + auto proxy = iface_cast(remoteObject); + if (proxy != nullptr) { + return proxy; + } } - WaitForSandboxManagerSa(); -} - -void SandboxManagerClient::OnRemoteDiedHandle() -{ - SANDBOXMANAGER_LOG_ERROR(LABEL, "Remote service died"); - std::unique_lock lock(proxyMutex_); - proxy_ = nullptr; - serviceDeathObserver_ = nullptr; - { - std::unique_lock lock1(cvLock_); - readyFlag_ = false; + // Try to load sa for SANDBOX_MANAGER_LOAD_SA_TRY_TIMES times. + for (int32_t i = 0; i < SANDBOX_MANAGER_LOAD_SA_TRY_TIMES; i++) { + remoteObject = samgr->LoadSystemAbility(ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE, + SANDBOX_MANAGER_LOAD_SA_TIMEOUT_SEC); + if (remoteObject != nullptr) { + auto proxy = iface_cast(remoteObject); + if (proxy != nullptr) { + return proxy; + } + SANDBOXMANAGER_LOG_WARN(LABEL, "Get iface_cast return NULL."); + } + SANDBOXMANAGER_LOG_WARN(LABEL, "Try to load SandboxManager Sa failed, times %{public}d / %{public}d", + i, SANDBOX_MANAGER_LOAD_SA_TRY_TIMES); } + SANDBOXMANAGER_LOG_ERROR(LABEL, "Get proxy retry failed %{public}d times.", + SANDBOX_MANAGER_LOAD_SA_TRY_TIMES); + return nullptr; } -void SandboxManagerClient::FinishStartSASuccess(const sptr &remoteObject) +int32_t SandboxManagerClient::CallProxyWithRetry( + const std::function &)> &func, const char *funcName) { - SANDBOXMANAGER_LOG_INFO(LABEL, "Get sandbox_manager sa success."); - - GetProxyFromRemoteObject(remoteObject); - - // get lock which wait_for release and send a notice so that wait_for can out of block - std::unique_lock lock(cvLock_); - readyFlag_ = true; - sandboxManagerCon_.notify_one(); -} - -void SandboxManagerClient::FinishStartSAFail() -{ - SANDBOXMANAGER_LOG_ERROR(LABEL, "get sandbox_manager sa failed."); - - // get lock which wait_for release and send a notice - std::unique_lock lock(cvLock_); - readyFlag_ = true; - sandboxManagerCon_.notify_one(); -} - -void SandboxManagerClient::GetProxyFromRemoteObject(const sptr &remoteObject) -{ - if (remoteObject == nullptr) { - return; - } - - sptr serviceDeathObserver = new (std::nothrow) SandboxManagerDeathRecipient(); - if (serviceDeathObserver == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Alloc service death observer fail"); - return; - } - - if (!remoteObject->AddDeathRecipient(serviceDeathObserver)) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Add service death observer fail"); - return; - } - - auto proxy = iface_cast(remoteObject); - if (proxy == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "iface_cast get null"); - return; + auto proxy = GetProxy(); + if (proxy != nullptr) { + int32_t ret = func(proxy); + if (!IsRequestNeedRetry(ret)) { + return ret; + } else { + SANDBOXMANAGER_LOG_WARN(LABEL, "First try call %{public}s failed, " \ + "err = %{public}d. Begin retry.", funcName, ret); + } + } else { + SANDBOXMANAGER_LOG_WARN(LABEL, "First try call %{public}s failed, proxy is NULL. Begin retry.", funcName); + } + // begin retry + for (int32_t i = 0; i < SA_REQUEST_RETRY_TIMES; i++) { + proxy = GetProxy(); + if (proxy == nullptr) { + SANDBOXMANAGER_LOG_WARN(LABEL, "Get proxy %{public}s failed, retry time = %{public}d.", funcName, i); + continue; + } + int32_t ret = func(proxy); + if (!IsRequestNeedRetry(ret)) { + return ret; + } + SANDBOXMANAGER_LOG_WARN(LABEL, "Call %{public}s failed, retry time = %{public}d, " \ + "result = %{public}d.", funcName, i, ret); } - std::unique_lock lock(proxyMutex_); - proxy_ = proxy; - serviceDeathObserver_ = serviceDeathObserver; - SANDBOXMANAGER_LOG_INFO(LABEL, "GetSystemAbility %{public}d success", - ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE); - return; + SANDBOXMANAGER_LOG_ERROR(LABEL, "Retry call service %{public}s error, tried %{public}d times.", + funcName, SA_REQUEST_RETRY_TIMES); + return SANDBOX_MANAGER_SERVICE_REMOTE_ERR; } -sptr SandboxManagerClient::GetProxy(bool doLoadSa) +bool SandboxManagerClient::IsRequestNeedRetry(int32_t ret) { - { - std::unique_lock lock(proxyMutex_); - if (proxy_ != nullptr) { - return proxy_; - } - } - if (doLoadSa) { - LoadSandboxManagerSa(); - } else { - GetSandboxManagerSa(); - } - std::unique_lock lock(proxyMutex_); - return proxy_; + auto it = std::find(RETRY_CODE_LIST.begin(), RETRY_CODE_LIST.end(), ret); + return it != RETRY_CODE_LIST.end(); } } // SandboxManager } // AccessControl diff --git a/interfaces/innerkits/sandbox_manager/src/sandbox_manager_death_recipient.cpp b/interfaces/innerkits/sandbox_manager/src/sandbox_manager_death_recipient.cpp deleted file mode 100644 index f59849a..0000000 --- a/interfaces/innerkits/sandbox_manager/src/sandbox_manager_death_recipient.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2023 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 "sandbox_manager_death_recipient.h" -#include "sandbox_manager_client.h" - -namespace OHOS { -namespace AccessControl { -namespace SandboxManager { -void SandboxManagerDeathRecipient::OnRemoteDied(const wptr &object) -{ - SandboxManagerClient::GetInstance().OnRemoteDiedHandle(); -} -} // SandboxManager -} // AccessControl -} // OHOS \ No newline at end of file diff --git a/interfaces/innerkits/sandbox_manager/src/sandbox_manager_load_callback.cpp b/interfaces/innerkits/sandbox_manager/src/sandbox_manager_load_callback.cpp deleted file mode 100644 index 2ec15ce..0000000 --- a/interfaces/innerkits/sandbox_manager/src/sandbox_manager_load_callback.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2023 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 "sandbox_manager_load_callback.h" - -#include "sandbox_manager_client.h" -#include "sandbox_manager_log.h" - -namespace OHOS { -namespace AccessControl { -namespace SandboxManager { -static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, ACCESSCONTROL_DOMAIN_SANDBOXMANAGER, "SandboxManagerClient" -}; - -SandboxManagerLoadCallback::SandboxManagerLoadCallback() {} - -void SandboxManagerLoadCallback::OnLoadSystemAbilitySuccess( - int32_t systemAbilityId, const sptr &remoteObject) -{ - if (systemAbilityId != ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "start systemabilityId is not sandbox_manager!"); - return; - } - - if (remoteObject == nullptr) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "remoteObject is null."); - SandboxManagerClient::GetInstance().FinishStartSAFail(); - return; - } - - SANDBOXMANAGER_LOG_INFO(LABEL, "Start systemAbilityId: %{public}d success!", systemAbilityId); - - SandboxManagerClient::GetInstance().FinishStartSASuccess(remoteObject); -} - -void SandboxManagerLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) -{ - if (systemAbilityId != ISandboxManager::SA_ID_SANDBOX_MANAGER_SERVICE) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "start systemabilityId is not sandbox_manager!"); - return; - } - - SANDBOXMANAGER_LOG_ERROR(LABEL, "Start systemAbilityId: %{public}d failed.", systemAbilityId); - - SandboxManagerClient::GetInstance().FinishStartSAFail(); -} -} // SandboxManager -} // AccessControl -} // OHOS \ No newline at end of file diff --git a/interfaces/innerkits/sandbox_manager/test/unittest/src/sandbox_manager_kit_test.cpp b/interfaces/innerkits/sandbox_manager/test/unittest/src/sandbox_manager_kit_test.cpp index 96275ac..d5a22f0 100644 --- a/interfaces/innerkits/sandbox_manager/test/unittest/src/sandbox_manager_kit_test.cpp +++ b/interfaces/innerkits/sandbox_manager/test/unittest/src/sandbox_manager_kit_test.cpp @@ -30,7 +30,6 @@ #include "sandbox_manager_client.h" #undef private #include "sandbox_manager_err_code.h" -#include "sandbox_manager_load_callback.h" #include "sandbox_manager_log.h" #include "sandbox_manager_kit.h" #include "token_setproc.h" -- Gitee