为了防止霸屏,被KillProcessWithAccount杀的进程,一段时间内无法作为调用方发起LoadAbility

Signed-off-by: jsjzju <jinsenjun@huawei.com>
Change-Id: I62235670b9f0318cad847ff3422435edf47c5cf6
This commit is contained in:
jsjzju
2024-11-08 14:13:55 +08:00
parent b74b250af2
commit b2bd96b32e
36 changed files with 200 additions and 250 deletions
@@ -449,11 +449,6 @@ public:
return false;
}
virtual bool IsAppKilling(sptr<IRemoteObject> token)
{
return false;
}
virtual void SetAppExceptionCallback(sptr<IRemoteObject> callback) {}
enum class Message {
@@ -507,7 +502,6 @@ public:
CLEAN_UIABILITY_BY_USER_REQUEST,
FORCE_KILL_APPLICATION_BY_ACCESS_TOKEN_ID = 49,
IS_PROCESS_ATTACHED,
IS_APP_KILLING,
ENABLE_START_PROCESS_FLAG_BY_USER_ID,
SET_APP_EXCEPTION_CALLBACK,
SET_KEEP_ALIVE_DKV,
@@ -400,8 +400,6 @@ public:
*/
virtual bool IsProcessAttached(sptr<IRemoteObject> token) override;
virtual bool IsAppKilling(sptr<IRemoteObject> token) override;
virtual void SetAppExceptionCallback(sptr<IRemoteObject> callback) override;
private:
@@ -102,7 +102,6 @@ private:
int32_t HandleCleanAbilityByUserRequest(MessageParcel &data, MessageParcel &reply);
int32_t HandleIsProcessContainsOnlyUIAbility(MessageParcel &data, MessageParcel &reply);
int32_t HandleIsProcessAttached(MessageParcel &data, MessageParcel &reply);
int32_t HandleIsAppKilling(MessageParcel &data, MessageParcel &reply);
int32_t HandleSetAppExceptionCallback(MessageParcel &data, MessageParcel &reply);
DISALLOW_COPY_AND_MOVE(AmsMgrStub);
};
@@ -899,8 +899,6 @@ public:
*/
bool IsProcessAttached(sptr<IRemoteObject> token) const;
bool IsAppKilling(sptr<IRemoteObject> token) const;
/**
* Check whether the bundle is running.
*
@@ -1344,28 +1344,6 @@ bool AmsMgrProxy::IsProcessAttached(sptr<IRemoteObject> token)
return reply.ReadBool();
}
bool AmsMgrProxy::IsAppKilling(sptr<IRemoteObject> token)
{
MessageParcel data;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(data)) {
TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
return false;
}
if (!data.WriteRemoteObject(token.GetRefPtr())) {
TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
return false;
}
auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::IS_APP_KILLING), data, reply, option);
if (ret != NO_ERROR) {
TAG_LOGE(AAFwkTag::APPMGR, "Send request failed, error code is %{public}d.", ret);
return false;
}
return reply.ReadBool();
}
void AmsMgrProxy::SetAppExceptionCallback(sptr<IRemoteObject> callback)
{
MessageParcel data;
@@ -213,8 +213,6 @@ int32_t AmsMgrStub::OnRemoteRequestInnerFourth(uint32_t code, MessageParcel &dat
return HandleKillProcessesByAccessTokenId(data, reply);
case static_cast<uint32_t>(IAmsMgr::Message::IS_PROCESS_ATTACHED):
return HandleIsProcessAttached(data, reply);
case static_cast<uint32_t>(IAmsMgr::Message::IS_APP_KILLING):
return HandleIsAppKilling(data, reply);
case static_cast<uint32_t>(IAmsMgr::Message::SET_KEEP_ALIVE_DKV):
return HandleSetKeepAliveDkv(data, reply);
}
@@ -863,18 +861,6 @@ int32_t AmsMgrStub::HandleIsProcessAttached(MessageParcel &data, MessageParcel &
return NO_ERROR;
}
int32_t AmsMgrStub::HandleIsAppKilling(MessageParcel &data, MessageParcel &reply)
{
HITRACE_METER(HITRACE_TAG_APP);
sptr<IRemoteObject> token = data.ReadRemoteObject();
auto isAppKilling = IsAppKilling(token);
if (!reply.WriteBool(isAppKilling)) {
TAG_LOGE(AAFwkTag::APPMGR, "Fail to write result");
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t AmsMgrStub::HandleSetAppExceptionCallback(MessageParcel &data, MessageParcel &reply)
{
sptr<IRemoteObject> callback = data.ReadRemoteObject();
@@ -1399,19 +1399,6 @@ bool AppMgrClient::IsProcessAttached(sptr<IRemoteObject> token) const
return amsService->IsProcessAttached(token);
}
bool AppMgrClient::IsAppKilling(sptr<IRemoteObject> token) const
{
sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
if (service == nullptr) {
return false;
}
sptr<IAmsMgr> amsService = service->GetAmsMgr();
if (amsService == nullptr) {
return false;
}
return amsService->IsAppKilling(token);
}
AppMgrResultCode AppMgrClient::IsAppRunning(const std::string &bundleName, int32_t appCloneIndex,
bool &isRunning)
{
@@ -568,8 +568,6 @@ public:
bool IsProcessAttached(sptr<IRemoteObject> token) const;
bool IsAppKilling(sptr<IRemoteObject> token) const;
protected:
/**
* OnAbilityRequestDone, app manager service call this interface after ability request done.
@@ -85,8 +85,6 @@ public:
*/
int32_t CheckMultiInstanceKeyForExtension(const AbilityRequest &abilityRequest);
bool VerifyCallerToken(AbilityRequest &abilityRequest);
private:
/**
* AbilityPermissionUtil, the private constructor.
+1 -1
View File
@@ -355,7 +355,7 @@ int AbilityRecord::LoadAbility(bool isShellCall)
auto result = DelayedSingleton<AppScheduler>::GetInstance()->LoadAbility(
loadParam, abilityInfo_, abilityInfo_.applicationInfo, want_);
want_.RemoveParam(ABILITY_OWNER_USERID);
want_.RemoveParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST);
want_.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
SetLoadState(AbilityLoadState::LOADING);
if (DelayedSingleton<AppScheduler>::GetInstance()->IsAttachDebug(abilityInfo_.bundleName)) {
SetAttachDebug(true);
@@ -655,15 +655,6 @@ bool AppScheduler::IsProcessAttached(sptr<IRemoteObject> token) const
return appMgrClient_->IsProcessAttached(token);
}
bool AppScheduler::IsAppKilling(sptr<IRemoteObject> token) const
{
if (!appMgrClient_) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "appMgrClient is nullptr");
return false;
}
return appMgrClient_->IsAppKilling(token);
}
void AppScheduler::SetProcessCacheStatus(int32_t pid, bool isSupport)
{
if (!appMgrClient_) {
@@ -133,7 +133,7 @@ std::shared_ptr<AbilityRecord> UIAbilityLifecycleManager::GenerateAbilityRecord(
TAG_LOGE(AAFwkTag::ABILITYMGR, "sessionToken invalid");
return nullptr;
}
abilityRequest.want.RemoveParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST);
abilityRequest.want.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
uiAbilityRecord->SetIsNewWant(sessionInfo->isNewWant);
if (sessionInfo->isNewWant) {
uiAbilityRecord->SetWant(abilityRequest.want);
@@ -168,14 +168,6 @@ bool UIAbilityLifecycleManager::CheckSessionInfo(sptr<SessionInfo> sessionInfo)
TAG_LOGE(AAFwkTag::ABILITYMGR, "token's Descriptor: %{public}s", descriptor.c_str());
return false;
}
bool needCheckCallerIsExist = sessionInfo->want.GetBoolParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST, false);
if (needCheckCallerIsExist && sessionInfo->callerToken) {
auto callerAbility = Token::GetAbilityRecordByToken(sessionInfo->callerToken);
if (callerAbility == nullptr) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "callerAbility not exist");
return false;
}
}
return true;
}
@@ -351,10 +343,10 @@ int UIAbilityLifecycleManager::NotifySCBToStartUIAbility(AbilityRequest &ability
{
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
abilityRequest.want.SetParam(IS_SHELL_CALL, AAFwk::PermissionVerification::GetInstance()->IsShellCall());
std::string callerKey = std::to_string(IPCSkeleton::GetCallingPid()) + ":" +
std::to_string(IPCSkeleton::GetCallingUid());
abilityRequest.want.SetParam(Want::PARAMS_REAL_CALLER_KEY, callerKey);
std::lock_guard<ffrt::mutex> guard(sessionLock_);
if (!AbilityPermissionUtil::GetInstance().VerifyCallerToken(abilityRequest)) {
return ERR_INVALID_VALUE;
}
// start ability with persistentId by dms
int32_t persistentId = abilityRequest.want.GetIntParam(DMS_PERSISTENT_ID, 0);
TAG_LOGD(AAFwkTag::ABILITYMGR, "NotifySCBToStartUIAbility, want with persistentId: %{public}d.", persistentId);
@@ -231,23 +231,5 @@ int32_t AbilityPermissionUtil::CheckMultiInstanceKeyForExtension(const AbilityRe
}
return ERR_OK;
}
bool AbilityPermissionUtil::VerifyCallerToken(AbilityRequest &abilityRequest)
{
if (abilityRequest.callerToken == nullptr) {
return true;
}
if (PermissionVerification::GetInstance()->JudgeCallerIsAllowedToUseSystemAPI()) {
return true;
}
abilityRequest.want.SetParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST, true);
bool isAppKilling = IN_PROCESS_CALL(DelayedSingleton<AppScheduler>::GetInstance()->IsAppKilling(
abilityRequest.callerToken));
if (isAppKilling) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "caller killing");
return false;
}
return true;
}
} // AAFwk
} // OHOS
@@ -68,7 +68,7 @@ void UpdateCallerInfoUtil::UpdateCallerInfo(Want& want, const sptr<IRemoteObject
want.SetParam(Want::PARAM_RESV_CALLER_PID, callerPid);
want.RemoveParam(WANT_PARAMS_APP_RESTART_FLAG);
want.RemoveParam(IS_SHELL_CALL);
want.RemoveParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST);
want.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
auto abilityRecord = Token::GetAbilityRecordByToken(callerToken);
if (!abilityRecord) {
@@ -148,7 +148,7 @@ void UpdateCallerInfoUtil::UpdateAsCallerInfoFromToken(Want& want, sptr<IRemoteO
want.RemoveParam(Want::PARAM_RESV_CALLER_APP_CLONE_INDEX);
want.RemoveParam(WANT_PARAMS_APP_RESTART_FLAG);
want.RemoveParam(IS_SHELL_CALL);
want.RemoveParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST);
want.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
auto abilityRecord = Token::GetAbilityRecordByToken(asCallerSourceToken);
if (abilityRecord == nullptr) {
@@ -186,7 +186,7 @@ void UpdateCallerInfoUtil::UpdateAsCallerInfoFromCallerRecord(Want& want, sptr<I
want.RemoveParam(Want::PARAM_RESV_CALLER_APP_CLONE_INDEX);
want.RemoveParam(WANT_PARAMS_APP_RESTART_FLAG);
want.RemoveParam(IS_SHELL_CALL);
want.RemoveParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST);
want.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
auto callerRecord = Token::GetAbilityRecordByToken(callerToken);
CHECK_POINTER(callerRecord);
auto sourceInfo = callerRecord->GetCallerInfo();
@@ -229,7 +229,7 @@ bool UpdateCallerInfoUtil::UpdateAsCallerInfoFromDialog(Want& want)
want.RemoveParam(Want::PARAM_RESV_CALLER_NATIVE_NAME);
want.RemoveParam(WANT_PARAMS_APP_RESTART_FLAG);
want.RemoveParam(IS_SHELL_CALL);
want.RemoveParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST);
want.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
if (callerBundleName == "") {
want.SetParam(Want::PARAM_RESV_CALLER_NATIVE_NAME,
dialogCallerWant.GetStringParam(Want::PARAM_RESV_CALLER_NATIVE_NAME));
@@ -258,7 +258,7 @@ void UpdateCallerInfoUtil::UpdateCallerInfoFromToken(Want& want, const sptr<IRem
want.SetParam(Want::PARAM_RESV_CALLER_PID, callerPid);
want.RemoveParam(WANT_PARAMS_APP_RESTART_FLAG);
want.RemoveParam(IS_SHELL_CALL);
want.RemoveParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST);
want.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
std::string callerBundleName = abilityRecord->GetAbilityInfo().bundleName;
want.RemoveParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
+1
View File
@@ -63,6 +63,7 @@ ohos_shared_library("libappms") {
"src/cache_process_manager.cpp",
"src/child_process_record.cpp",
"src/exit_resident_process_manager.cpp",
"src/killing_process_manager.cpp",
"src/modal_system_app_freeze_uiextension.cpp",
"src/module_running_record.cpp",
"src/quick_fix_callback_with_record.cpp",
@@ -427,8 +427,6 @@ public:
*/
virtual bool IsProcessAttached(sptr<IRemoteObject> token) override;
virtual bool IsAppKilling(sptr<IRemoteObject> token) override;
virtual void SetAppExceptionCallback(sptr<IRemoteObject> callback) override;
private:
@@ -60,6 +60,7 @@
#include "irender_state_observer.h"
#include "istart_specified_ability_response.h"
#include "kia_interceptor_interface.h"
#include "kill_process_config.h"
#include "record_query_result.h"
#include "refbase.h"
#include "remote_client_manager.h"
@@ -1368,12 +1369,6 @@ public:
*/
bool IsProcessAttached(sptr<IRemoteObject> token) const;
/**
* Is a process of a ability will be killed
* @param indicates the ability
*/
bool IsAppKilling(sptr<IRemoteObject> token) const;
/**
* @brief Notify abilityms app process pre cache
* @param pid process pid.
@@ -1515,7 +1510,7 @@ private:
* @return ERR_OK, return back success, others fail.
*/
int32_t KillApplicationByUserIdLocked(const std::string &bundleName, int32_t appCloneIndex, int32_t userId,
const bool clearPageStack = false, const std::string& reason = "KillApplicationByUserIdLocked");
const KillProcessConfig &config = {});
/**
* WaitForRemoteProcessExit, Wait for the process to exit normally.
@@ -32,6 +32,7 @@
#include "bundle_info.h"
#include "configuration.h"
#include "iremote_object.h"
#include "kill_process_config.h"
#include "record_query_result.h"
#include "refbase.h"
#include "running_process_info.h"
@@ -253,7 +254,7 @@ public:
int32_t ProcessUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo);
bool ProcessExitByBundleNameAndUid(
const std::string &bundleName, const int uid, std::list<pid_t> &pids, const bool clearPageStack = false);
const std::string &bundleName, const int uid, std::list<pid_t> &pids, const KillProcessConfig &config = {});
bool ProcessExitByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::list<pid_t> &pids,
bool clearPageStack);
bool ProcessExitByTokenIdAndInstance(uint32_t accessTokenId, const std::string &instanceKey, std::list<pid_t> &pids,
@@ -0,0 +1,30 @@
/*
* 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_ABILITY_RUNTIME_KILL_PROCESS_CONFIG_H
#define OHOS_ABILITY_RUNTIME_KILL_PROCESS_CONFIG_H
#include <string>
namespace OHOS {
namespace AppExecFwk {
struct KillProcessConfig {
bool clearPageStack = false;
bool addKillingCaller = false;
std::string reason;
};
} // namespace AppExecFwk
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_KILL_PROCESS_CONFIG_H
@@ -0,0 +1,43 @@
/*
* 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_ABILITY_RUNTIME_KILLING_PROCESS_MANAGER_H
#define OHOS_ABILITY_RUNTIME_KILLING_PROCESS_MANAGER_H
#include <string>
#include <unordered_set>
#include "cpp/mutex.h"
#include "nocopyable.h"
namespace OHOS {
namespace AppExecFwk {
class KillingProcessManager {
public:
static KillingProcessManager& GetInstance();
~KillingProcessManager() = default;
bool IsCallerKilling(std::string callerKey) const;
void AddKillingCallerKey(std::string callerKey);
void RemoveKillingCallerKey(std::string callerKey);
private:
KillingProcessManager() = default;
mutable ffrt::mutex mutex_;
std::unordered_set<std::string> killingCallerKeySet_;
DISALLOW_COPY_AND_MOVE(KillingProcessManager);
};
} // namespace AppExecFwk
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_KILLING_PROCESS_MANAGER_H
@@ -752,15 +752,6 @@ bool AmsMgrScheduler::IsProcessAttached(sptr<IRemoteObject> token)
return amsMgrServiceInner_->IsProcessAttached(token);
}
bool AmsMgrScheduler::IsAppKilling(sptr<IRemoteObject> token)
{
if (!IsReady()) {
TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
return false;
}
return amsMgrServiceInner_->IsAppKilling(token);
}
void AmsMgrScheduler::SetAppExceptionCallback(sptr<IRemoteObject> callback)
{
if (!IsReady()) {
+16 -35
View File
@@ -54,6 +54,7 @@
#include "iremote_object.h"
#include "iservice_registry.h"
#include "itest_observer.h"
#include "killing_process_manager.h"
#ifdef SUPPORT_SCREEN
#include "locale_config.h"
#endif
@@ -842,19 +843,11 @@ bool AppMgrServiceInner::CheckLoadAbilityConditions(std::shared_ptr<AAFwk::Want>
TAG_LOGE(AAFwkTag::APPMGR, "abilityInfo and appInfo have diff appName");
return false;
}
bool needCheckCallerIsExist = false;
if (want) {
needCheckCallerIsExist = want->GetBoolParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST, false);
want->RemoveParam(Want::PARAMS_NEED_CHECK_CALLER_IS_EXIST);
}
if (needCheckCallerIsExist && loadParam->preToken) {
auto appRecord = GetAppRunningRecordByAbilityToken(loadParam->preToken);
if (appRecord == nullptr) {
TAG_LOGE(AAFwkTag::APPMGR, "preToken not exist");
return false;
}
if (appRecord->IsKilling()) {
TAG_LOGE(AAFwkTag::APPMGR, "app is killing");
std::string callerKey = want->GetStringParam(Want::PARAMS_REAL_CALLER_KEY);
want->RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
if (!callerKey.empty() && KillingProcessManager::GetInstance().IsCallerKilling(callerKey)) {
TAG_LOGE(AAFwkTag::APPMGR, "caller is killing");
return false;
}
}
@@ -1692,7 +1685,8 @@ int32_t AppMgrServiceInner::KillApplicationSelf(const bool clearPageStack, const
auto callingUid = IPCSkeleton::GetCallingUid();
TAG_LOGI(AAFwkTag::APPMGR, "uid value: %{public}d", callingUid);
std::list<pid_t> pids;
if (!appRunningManager_->ProcessExitByBundleNameAndUid(bundleName, callingUid, pids, clearPageStack)) {
KillProcessConfig config{clearPageStack, false, reason};
if (!appRunningManager_->ProcessExitByBundleNameAndUid(bundleName, callingUid, pids, config)) {
TAG_LOGI(AAFwkTag::APPMGR, "unstart");
return ERR_OK;
}
@@ -1783,12 +1777,12 @@ int32_t AppMgrServiceInner::KillApplicationByUserId(
return ERR_PERMISSION_DENIED;
}
return KillApplicationByUserIdLocked(bundleName, appCloneIndex, userId, clearPageStack, reason);
KillProcessConfig config{clearPageStack, true, reason};
return KillApplicationByUserIdLocked(bundleName, appCloneIndex, userId, config);
}
int32_t AppMgrServiceInner::KillApplicationByUserIdLocked(
const std::string &bundleName, int32_t appCloneIndex, const int userId,
const bool clearPageStack, const std::string& reason)
const std::string &bundleName, int32_t appCloneIndex, const int userId, const KillProcessConfig &config)
{
if (!appRunningManager_) {
TAG_LOGE(AAFwkTag::APPMGR, "appRunningManager_ null");
@@ -1811,7 +1805,7 @@ int32_t AppMgrServiceInner::KillApplicationByUserIdLocked(
TAG_LOGI(AAFwkTag::APPMGR, "uId value: %{public}d", userId);
int uid = IN_PROCESS_CALL(bundleMgrHelper->GetUidByBundleName(bundleName, userId, appCloneIndex));
TAG_LOGI(AAFwkTag::APPMGR, "uID value: %{public}d", uid);
if (!appRunningManager_->ProcessExitByBundleNameAndUid(bundleName, uid, pids, clearPageStack)) {
if (!appRunningManager_->ProcessExitByBundleNameAndUid(bundleName, uid, pids, config)) {
TAG_LOGI(AAFwkTag::APPMGR, "process corresponding package name unstart");
return result;
}
@@ -1820,7 +1814,7 @@ int32_t AppMgrServiceInner::KillApplicationByUserIdLocked(
return result;
}
for (auto iter = pids.begin(); iter != pids.end(); ++iter) {
result = KillProcessByPid(*iter, reason);
result = KillProcessByPid(*iter, config.reason);
if (result < 0) {
TAG_LOGE(AAFwkTag::APPMGR, "killApplication fail bundleName: %{public}s pid: %{public}d",
bundleName.c_str(), *iter);
@@ -4380,7 +4374,8 @@ int AppMgrServiceInner::StartUserTestProcess(
return ERR_INVALID_VALUE;
}
if (KillApplicationByUserIdLocked(bundleName, 0, userId, false, "StartUserTestProcess")) {
KillProcessConfig config{false, false, "StartUserTestProcess"};
if (KillApplicationByUserIdLocked(bundleName, 0, userId, config)) {
TAG_LOGE(AAFwkTag::APPMGR, "kill app fail");
return ERR_INVALID_VALUE;
}
@@ -4532,7 +4527,8 @@ int AppMgrServiceInner::FinishUserTest(
FinishUserTestLocked(msg, resultCode, appRecord);
int ret = KillApplicationByUserIdLocked(bundleName, 0, userTestRecord->userId, false, "FinishUserTest");
KillProcessConfig config{false, false, "FinishUserTest"};
int ret = KillApplicationByUserIdLocked(bundleName, 0, userTestRecord->userId, config);
if (ret) {
TAG_LOGE(AAFwkTag::APPMGR, "kill process fail");
return ret;
@@ -8434,21 +8430,6 @@ bool AppMgrServiceInner::IsProcessAttached(sptr<IRemoteObject> token) const
return appRecord->IsProcessAttached();
}
bool AppMgrServiceInner::IsAppKilling(sptr<IRemoteObject> token) const
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
if (IPCSkeleton::GetCallingUid() != FOUNDATION_UID) {
TAG_LOGE(AAFwkTag::APPMGR, "Not foundation call.");
return false;
}
auto appRecord = GetAppRunningRecordByAbilityToken(token);
if (appRecord == nullptr) {
TAG_LOGE(AAFwkTag::APPMGR, "abilityRecord is nullptr");
return false;
}
return appRecord->IsKilling();
}
int32_t AppMgrServiceInner::GetSupportedProcessCachePids(const std::string &bundleName,
std::vector<int32_t> &pidList)
{
+7 -2
View File
@@ -26,6 +26,7 @@
#include "freeze_util.h"
#include "hilog_tag_wrapper.h"
#include "hitrace_meter.h"
#include "killing_process_manager.h"
#include "os_account_manager_wrapper.h"
#include "perf_profile.h"
#include "parameters.h"
@@ -338,7 +339,7 @@ int32_t AppRunningManager::ProcessUpdateApplicationInfoInstalled(const Applicati
}
bool AppRunningManager::ProcessExitByBundleNameAndUid(
const std::string &bundleName, const int uid, std::list<pid_t> &pids, const bool clearPageStack)
const std::string &bundleName, const int uid, std::list<pid_t> &pids, const KillProcessConfig &config)
{
auto appRunningMap = GetAppRunningRecordMap();
for (const auto &item : appRunningMap) {
@@ -356,9 +357,13 @@ bool AppRunningManager::ProcessExitByBundleNameAndUid(
continue;
}
pids.push_back(pid);
if (clearPageStack) {
if (config.clearPageStack) {
appRecord->ScheduleClearPageStack();
}
if (config.addKillingCaller) {
std::string callerKey = std::to_string(pid) + ":" + std::to_string(appRecord->GetUid());
KillingProcessManager::GetInstance().AddKillingCallerKey(callerKey);
}
appRecord->SetKilling();
appRecord->ScheduleProcessSecurityExit();
}
@@ -0,0 +1,76 @@
/*
* 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 "killing_process_manager.h"
#include <mutex>
#include "hilog_tag_wrapper.h"
#include "task_handler_wrap.h"
namespace OHOS {
namespace AppExecFwk {
namespace {
constexpr int32_t CLEAR_CALLER_KEY_DELAY_TIME = 5 * 1000; // 5s
}
KillingProcessManager& KillingProcessManager::GetInstance()
{
static KillingProcessManager instance;
return instance;
}
bool KillingProcessManager::IsCallerKilling(std::string callerKey) const
{
std::lock_guard<ffrt::mutex> lock(mutex_);
return killingCallerKeySet_.find(callerKey) != killingCallerKeySet_.end();
}
void KillingProcessManager::AddKillingCallerKey(std::string callerKey)
{
auto taskHandler = AAFwk::TaskHandlerWrap::GetFfrtHandler();
if (taskHandler == nullptr) {
TAG_LOGE(AAFwkTag::APPMGR, "handler is null");
return;
}
if (callerKey.empty()) {
TAG_LOGE(AAFwkTag::APPMGR, "invalid callerKey");
return;
}
TAG_LOGI(AAFwkTag::APPMGR, "add caller:%{public}s", callerKey.c_str());
{
std::lock_guard<ffrt::mutex> lock(mutex_);
auto ret = killingCallerKeySet_.insert(callerKey);
if (!ret.second) {
TAG_LOGI(AAFwkTag::APPMGR, "already inserted");
}
}
auto task = [callerKey] () {
KillingProcessManager::GetInstance().RemoveKillingCallerKey(callerKey);
};
taskHandler->SubmitTask(task, "clearCallerKey", CLEAR_CALLER_KEY_DELAY_TIME);
}
void KillingProcessManager::RemoveKillingCallerKey(std::string callerKey)
{
if (callerKey.empty()) {
TAG_LOGE(AAFwkTag::APPMGR, "invalid callerKey");
return;
}
TAG_LOGI(AAFwkTag::APPMGR, "remove caller:%{public}s", callerKey.c_str());
std::lock_guard<ffrt::mutex> lock(mutex_);
killingCallerKeySet_.erase(callerKey);
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -26,6 +26,7 @@
#undef private
#include "securec.h"
#include "ability_record.h"
#include "kill_process_config.h"
using namespace OHOS::AAFwk;
@@ -82,8 +83,9 @@ void DoSomethingInterestingWithMyAPIadda(const char* data, size_t size)
ApplicationInfo appInfos;
manager->ProcessUpdateApplicationInfoInstalled(appInfos);
std::list<pid_t> pids;
bool clearPageStack = *data % ENABLE;
manager->ProcessExitByBundleNameAndUid(jsonStr, uid, pids, clearPageStack);
KillProcessConfig config;
config.clearPageStack = *data % ENABLE;
manager->ProcessExitByBundleNameAndUid(jsonStr, uid, pids, config);
int32_t userId = static_cast<int32_t>(GetU32Data(data));
manager->GetPidsByUserId(userId, pids);
manager->PrepareTerminate(token, clearMissionFlag);
@@ -43,6 +43,7 @@ ohos_unittest("AmsAbilityRunningRecordTest") {
"${ability_runtime_services_path}/appmgr/src/cache_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/child_process_record.cpp",
"${ability_runtime_services_path}/appmgr/src/exit_resident_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/render_state_observer_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/user_record_manager.cpp",
]
@@ -38,6 +38,7 @@ ohos_unittest("AmsAppLifeCycleTest") {
"${ability_runtime_services_path}/appmgr/src/app_spawn_client.cpp",
"${ability_runtime_services_path}/appmgr/src/app_state_observer_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/cache_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/module_running_record.cpp",
"${ability_runtime_services_path}/appmgr/src/remote_client_manager.cpp",
"${ability_runtime_test_path}/mock/services_appmgr_test/src/mock_bundle_manager.cpp",
@@ -39,6 +39,7 @@ ohos_unittest("AmsWorkFlowTest") {
"${ability_runtime_services_path}/appmgr/src/app_spawn_client.cpp",
"${ability_runtime_services_path}/appmgr/src/app_state_observer_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/cache_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/module_running_record.cpp",
"${ability_runtime_services_path}/appmgr/src/remote_client_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/window_focus_changed_listener.cpp",
@@ -2060,74 +2060,6 @@ HWTEST_F(AmsMgrSchedulerSecondTest, AmsMgrSchedulerSecondTest_IsProcessAttached_
EXPECT_FALSE(ret);
TAG_LOGI(AAFwkTag::TEST, "AmsMgrSchedulerSecondTest_IsProcessAttached_003 end");
}
/*
* @tc.name: AmsMgrSchedulerSecondTest_IsAppKilling_001
* @tc.desc: Test IsAppKilling
* @tc.type: FUNC
*/
HWTEST_F(AmsMgrSchedulerSecondTest, AmsMgrSchedulerSecondTest_IsAppKilling_001, TestSize.Level1)
{
TAG_LOGI(AAFwkTag::TEST, "AmsMgrSchedulerSecondTest_IsAppKilling_001 start");
auto appMgrServiceInner = std::make_shared<MockAppMgrServiceInner>();
std::shared_ptr<AmsMgrScheduler> amsMgrScheduler =
std::make_shared<AmsMgrScheduler>(appMgrServiceInner, nullptr);
/**
* @tc.steps: step1. amsMgrScheduler isReady false
* @tc.expected: step1. expect ERR_INVALID_OPERATION
*/
sptr<IRemoteObject> token = nullptr;
auto ret = amsMgrScheduler->IsAppKilling(token);
EXPECT_FALSE(ret);
TAG_LOGI(AAFwkTag::TEST, "AmsMgrSchedulerSecondTest_IsAppKilling_001 end");
}
/*
* @tc.name: AmsMgrSchedulerSecondTest_IsAppKilling_002
* @tc.desc: Test IsAppKilling
* @tc.type: FUNC
*/
HWTEST_F(AmsMgrSchedulerSecondTest, AmsMgrSchedulerSecondTest_IsAppKilling_002, TestSize.Level1)
{
TAG_LOGI(AAFwkTag::TEST, "AmsMgrSchedulerSecondTest_IsAppKilling_002 start");
auto appMgrServiceInner = std::make_shared<MockAppMgrServiceInner>();
std::shared_ptr<AmsMgrScheduler> amsMgrScheduler =
std::make_shared<AmsMgrScheduler>(appMgrServiceInner, taskHandler_);
/**
* @tc.steps: step1. amsMgrScheduler isReady false
* @tc.expected: step1. expect ERR_INVALID_OPERATION
*/
IPCSkeleton::SetCallingUid(-1);
sptr<IRemoteObject> token = nullptr;
auto ret = amsMgrScheduler->IsAppKilling(token);
EXPECT_FALSE(ret);
TAG_LOGI(AAFwkTag::TEST, "AmsMgrSchedulerSecondTest_IsAppKilling_002 end");
}
/*
* @tc.name: AmsMgrSchedulerSecondTest_IsAppKilling_003
* @tc.desc: Test IsAppKilling
* @tc.type: FUNC
*/
HWTEST_F(AmsMgrSchedulerSecondTest, AmsMgrSchedulerSecondTest_IsAppKilling_003, TestSize.Level1)
{
TAG_LOGI(AAFwkTag::TEST, "AmsMgrSchedulerSecondTest_IsAppKilling_003 start");
auto appMgrServiceInner = std::make_shared<MockAppMgrServiceInner>();
std::shared_ptr<AmsMgrScheduler> amsMgrScheduler =
std::make_shared<AmsMgrScheduler>(appMgrServiceInner, taskHandler_);
/**
* @tc.steps: step1. amsMgrScheduler isReady false
* @tc.expected: step1. expect ERR_INVALID_OPERATION
*/
IPCSkeleton::SetCallingUid(Constants::FOUNDATION_UID);
sptr<IRemoteObject> token = nullptr;
auto ret = amsMgrScheduler->IsAppKilling(token);
EXPECT_FALSE(ret);
TAG_LOGI(AAFwkTag::TEST, "AmsMgrSchedulerSecondTest_IsAppKilling_003 end");
}
} // AppExecFwk
} // OHOS
@@ -43,6 +43,7 @@ ohos_unittest("AmsRecentAppListTest") {
"${ability_runtime_services_path}/appmgr/src/cache_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/child_process_record.cpp",
"${ability_runtime_services_path}/appmgr/src/exit_resident_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/module_running_record.cpp",
"${ability_runtime_services_path}/appmgr/src/remote_client_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/render_state_observer_manager.cpp",
@@ -48,6 +48,7 @@ ohos_unittest("AmsServiceAppSpawnClientTest") {
"${ability_runtime_services_path}/appmgr/src/cache_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/child_process_record.cpp",
"${ability_runtime_services_path}/appmgr/src/exit_resident_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/render_state_observer_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/user_record_manager.cpp",
]
@@ -47,6 +47,7 @@ ohos_unittest("AmsServiceEventDriveTest") {
"${ability_runtime_services_path}/appmgr/src/cache_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/child_process_record.cpp",
"${ability_runtime_services_path}/appmgr/src/exit_resident_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/render_state_observer_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/user_record_manager.cpp",
"${ability_runtime_test_path}/mock/common/src/mock_native_token.cpp",
@@ -44,6 +44,7 @@ ohos_unittest("AmsServiceLoadAbilityProcessTest") {
"${ability_runtime_services_path}/appmgr/src/cache_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/child_process_record.cpp",
"${ability_runtime_services_path}/appmgr/src/exit_resident_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/module_running_record.cpp",
"${ability_runtime_services_path}/appmgr/src/remote_client_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/render_state_observer_manager.cpp",
@@ -45,6 +45,7 @@ ohos_unittest("AmsServiceStartupTest") {
"${ability_runtime_services_path}/appmgr/src/cache_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/child_process_record.cpp",
"${ability_runtime_services_path}/appmgr/src/exit_resident_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/render_state_observer_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/user_record_manager.cpp",
]
@@ -194,20 +194,5 @@ HWTEST_F(AppMgrClientFirstTest, CleanAbilityByUserRequest_001, TestSize.Level0)
EXPECT_TRUE(result != true);
TAG_LOGI(AAFwkTag::TEST, "CleanAbilityByUserRequest_001 end");
}
/**
* @tc.name: IsAppKilling_001
* @tc.desc: AppMgrClient test for IsAppKilling.
* @tc.type: FUNC
*/
HWTEST_F(AppMgrClientFirstTest, IsAppKilling_001, TestSize.Level0)
{
TAG_LOGI(AAFwkTag::TEST, "IsAppKilling_001 start");
auto appMgrClient = std::make_unique<AppMgrClient>();
sptr<IRemoteObject> token = GetTestAbilityToken();
auto result = appMgrClient->IsAppKilling(token);
EXPECT_TRUE(result != true);
TAG_LOGI(AAFwkTag::TEST, "IsAppKilling_001 end");
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -40,6 +40,7 @@ ohos_unittest("AMSEventHandlerTest") {
"${ability_runtime_services_path}/appmgr/src/cache_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/child_process_record.cpp",
"${ability_runtime_services_path}/appmgr/src/exit_resident_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/killing_process_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/module_running_record.cpp",
"${ability_runtime_services_path}/appmgr/src/remote_client_manager.cpp",
"${ability_runtime_services_path}/appmgr/src/render_state_observer_manager.cpp",