• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ability_manager_helper.h"
17 
18 #include "bundle_mgr_service.h"
19 #include "system_ability_definition.h"
20 #include "system_ability_helper.h"
21 
22 #include "app_log_tag_wrapper.h"
23 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
24 #include "ability_manager_client.h"
25 #include "app_mgr_interface.h"
26 #include "running_process_info.h"
27 #endif
28 
29 namespace OHOS {
30 namespace AppExecFwk {
UninstallApplicationProcesses(const std::string & bundleName,const int uid,bool isUpgradeApp,int32_t appIndex)31 bool AbilityManagerHelper::UninstallApplicationProcesses(
32     const std::string &bundleName, const int uid, bool isUpgradeApp, int32_t appIndex)
33 {
34 #ifdef ABILITY_RUNTIME_ENABLE
35     LOG_NOFUNC_I(BMS_TAG_INSTALLER, "begin UninstallApplicationProcesses -n %{public}s, -up %{public}d, -u %{public}d,"
36         "-i %{public}d", bundleName.c_str(), isUpgradeApp, uid, appIndex);
37     int ret = 0;
38     if (isUpgradeApp) {
39         ret = SystemAbilityHelper::UpgradeApp(bundleName, uid, appIndex);
40     } else {
41         ret = SystemAbilityHelper::UninstallApp(bundleName, uid, appIndex);
42     }
43     if (ret != 0) {
44         APP_LOGE("kill application process failed uid: %{public}d, appIndex: %{public}d", uid, appIndex);
45         return false;
46     }
47     LOG_NOFUNC_I(BMS_TAG_INSTALLER, "UninstallApplicationProcesses end");
48     return true;
49 #else
50     LOG_NOFUNC_I(BMS_TAG_INSTALLER, "ABILITY_RUNTIME_ENABLE is false");
51     return true;
52 #endif
53 }
54 
IsRunning(const std::string & bundleName)55 int32_t AbilityManagerHelper::IsRunning(const std::string &bundleName)
56 {
57 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
58     LOG_NOFUNC_I(BMS_TAG_DEFAULT, "IsRunning check %{public}s", bundleName.c_str());
59     sptr<IAppMgr> appMgrProxy =
60         iface_cast<IAppMgr>(SystemAbilityHelper::GetSystemAbility(APP_MGR_SERVICE_ID));
61     if (appMgrProxy == nullptr) {
62         APP_LOGE("fail to find the app mgr service to check app is running");
63         return FAILED;
64     }
65 
66     std::vector<RunningProcessInfo> runningList;
67     int result = appMgrProxy->GetAllRunningProcesses(runningList);
68     if (result != ERR_OK) {
69         APP_LOGE("GetAllRunningProcesses failed");
70         return FAILED;
71     }
72     LOG_NOFUNC_I(BMS_TAG_DEFAULT, "size %{public}zu", runningList.size());
73 
74     for (const auto &info : runningList) {
75         auto res = std::any_of(info.bundleNames.begin(), info.bundleNames.end(),
76             [bundleName](const auto &bundleNameInRunningProcessInfo) {
77                 return bundleNameInRunningProcessInfo == bundleName;
78             });
79         if (res) {
80             return RUNNING;
81         }
82     }
83 
84     return NOT_RUNNING;
85 #else
86     APP_LOGI("BUNDLE_FRAMEWORK_FREE_INSTALL is false");
87     return FAILED;
88 #endif
89 }
90 
QueryRunningSharedBundles(const pid_t pid,std::map<std::string,uint32_t> & shareBundles)91 int32_t AbilityManagerHelper::QueryRunningSharedBundles(
92     const pid_t pid, std::map<std::string, uint32_t> &shareBundles)
93 {
94 #ifdef ABILITY_RUNTIME_ENABLE
95     sptr<IAppMgr> appMgrProxy =
96         iface_cast<IAppMgr>(SystemAbilityHelper::GetSystemAbility(APP_MGR_SERVICE_ID));
97     if (appMgrProxy == nullptr) {
98         APP_LOGE("fail to find the app mgr service");
99         return FAILED;
100     }
101     std::string identity = IPCSkeleton::ResetCallingIdentity();
102     int32_t ret = appMgrProxy->QueryRunningSharedBundles(pid, shareBundles);
103     IPCSkeleton::SetCallingIdentity(identity);
104     return ret;
105 #else
106     APP_LOGI("ABILITY_RUNTIME_ENABLE is false");
107     return FAILED;
108 #endif
109 }
110 }  // namespace AppExecFwk
111 }  // namespace OHOS
112