Description:add app mgr death recipient

Match-id-2ac698c5c2f74ba7f9d153827cfee93314bb338a
This commit is contained in:
libing23
2023-08-08 21:59:49 +08:00
parent bfdf515f19
commit 7a97461e03
8 changed files with 117 additions and 2 deletions
@@ -46,6 +46,7 @@ ohos_shared_library("security_component_service") {
]
sources = [
"sa_main/app_mgr_death_recipient.cpp",
"sa_main/app_state_observer.cpp",
"sa_main/delay_exit_task.cpp",
"sa_main/first_use_dialog.cpp",
@@ -0,0 +1,28 @@
/*
* 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 "app_mgr_death_recipient.h"
#include "sec_comp_manager.h"
namespace OHOS {
namespace Security {
namespace SecurityComponent {
void AppMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
{
SecCompManager::GetInstance().ExitWhenAppMgrDied();
}
} // namespace SecurityComponent
} // namespace Security
} // namespace OHOS
@@ -0,0 +1,33 @@
/*
* 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 APP_MGR_DEATH_RECIPIENT_H
#define APP_MGR_DEATH_RECIPIENT_H
#include "iremote_object.h"
namespace OHOS {
namespace Security {
namespace SecurityComponent {
class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient {
public:
AppMgrDeathRecipient() {}
virtual ~AppMgrDeathRecipient() = default;
void OnRemoteDied(const wptr<IRemoteObject>& object) override;
};
} // namespace SecurityComponent
} // namespace Security
} // namespace OHOS
#endif // APP_MGR_DEATH_RECIPIENT_H
@@ -218,6 +218,7 @@ void SecCompManager::NotifyProcessDied(int32_t pid)
// notify enhance process died.
SecCompEnhanceAdapter::NotifyProcessDied(pid);
RemoveAppFromMaliciousAppList(pid);
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
auto iter = componentMap_.find(pid);
if (iter == componentMap_.end()) {
@@ -235,7 +236,6 @@ void SecCompManager::NotifyProcessDied(int32_t pid)
SecCompEnhanceAdapter::DisableInputEnhance();
}
RemoveAppFromMaliciousAppList(pid);
DelayExitTask::GetInstance().Start();
}
@@ -264,6 +264,36 @@ void SecCompManager::ExitSaProcess()
SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!");
}
void SecCompManager::ExitWhenAppMgrDied()
{
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) {
std::vector<SecCompEntity>& list = iter->second.compList;
for (auto it = list.begin(); it != list.end(); ++it) {
it->RevokeTempPermission();
}
list.clear();
}
componentMap_.clear();
// no need exit enhance service, only disable input enhance.
SecCompEnhanceAdapter::DisableInputEnhance();
isSaExit_ = true;
SC_LOG_INFO(LABEL, "app mgr died, start sa exit");
auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (systemAbilityMgr == nullptr) {
SC_LOG_ERROR(LABEL, "failed to get SystemAbilityManager.");
return;
}
int32_t ret = systemAbilityMgr->UnloadSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE);
if (ret != SC_OK) {
SC_LOG_ERROR(LABEL, "failed to UnloadSystemAbility service! errcode=%{public}d", ret);
return;
}
SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!");
}
void SecCompManager::SendCheckInfoEnhanceSysEvent(int32_t scId,
SecCompType type, const std::string& scene, int32_t res)
{
@@ -65,6 +65,7 @@ public:
void DumpSecComp(std::string& dumpStr);
bool Initialize();
void ExitSaProcess();
void ExitWhenAppMgrDied();
private:
SecCompManager();
@@ -17,6 +17,7 @@
#include <unistd.h>
#include "app_mgr_death_recipient.h"
#include "hisysevent.h"
#include "hitrace_meter.h"
#include "ipc_skeleton.h"
@@ -107,7 +108,8 @@ bool SecCompService::RegisterAppStateObserver()
appStateObserver_ = nullptr;
return false;
}
iAppMgr_ = iface_cast<AppExecFwk::IAppMgr>(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID));
auto remoteObject = samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID);
iAppMgr_ = iface_cast<AppExecFwk::IAppMgr>(remoteObject);
if (iAppMgr_ == nullptr) {
SC_LOG_ERROR(LABEL, "Failed to get ability manager service");
appStateObserver_ = nullptr;
@@ -121,6 +123,17 @@ bool SecCompService::RegisterAppStateObserver()
return false;
}
sptr<AppMgrDeathRecipient> appMgrDeathRecipient = new (std::nothrow) AppMgrDeathRecipient();
if (appMgrDeathRecipient == nullptr) {
SC_LOG_ERROR(LABEL, "Alloc appMgr death observer fail");
return false;
}
if (!remoteObject->AddDeathRecipient(appMgrDeathRecipient)) {
SC_LOG_ERROR(LABEL, "Add service death observer fail");
return false;
}
std::vector<AppExecFwk::AppStateData> list;
if (iAppMgr_->GetForegroundApplications(list) == ERR_OK) {
for (auto it = list.begin(); it != list.end(); ++it) {
@@ -32,6 +32,7 @@ ohos_unittest("sec_comp_service_test") {
]
sources = [
"${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp",
"${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp",
"${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp",
"${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp",
@@ -94,6 +95,7 @@ ohos_unittest("sec_comp_service_mock_test") {
]
sources = [
"${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp",
"${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp",
"${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp",
"${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp",
@@ -21,6 +21,13 @@
namespace OHOS {
class MockIRemoteObject : public virtual RefBase {
public:
bool AddDeathRecipient(const sptr<IPCObjectProxy::DeathRecipient>& recipient)
{
(void)recipient;
return addResult;
}
bool addResult = true;
};
template <typename INTERFACE> inline sptr<INTERFACE> iface_cast(const sptr<MockIRemoteObject> &object)