1 /*
2  * Copyright (c) 2021 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 "system_ability_helper.h"
17 
18 #ifdef ABILITY_RUNTIME_ENABLE
19 #include "ability_manager_interface.h"
20 #endif
21 
22 #include "app_log_wrapper.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 #ifdef ABILITY_RUNTIME_ENABLE
31     constexpr const char* KILL_REASON = "Kill Reason: UpgradeApp";
32 #endif
33 }
GetSystemAbility(const int32_t systemAbilityId)34 sptr<IRemoteObject> SystemAbilityHelper::GetSystemAbility(const int32_t systemAbilityId)
35 {
36     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37     if (systemAbilityMgr == nullptr) {
38         APP_LOGE("fail get system ability manager to get %{public}d proxy", systemAbilityId);
39         return nullptr;
40     }
41     return systemAbilityMgr->GetSystemAbility(systemAbilityId);
42 }
43 
AddSystemAbility(const int32_t systemAbilityId, const sptr<IRemoteObject> &systemAbility)44 bool SystemAbilityHelper::AddSystemAbility(const int32_t systemAbilityId, const sptr<IRemoteObject> &systemAbility)
45 {
46     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
47     if (systemAbilityMgr && (systemAbilityMgr->AddSystemAbility(systemAbilityId, systemAbility) == 0)) {
48         return true;
49     }
50     APP_LOGE("fail register %{public}d to system ability manager", systemAbilityId);
51     return false;
52 }
53 
RemoveSystemAbility(const int32_t systemAbilityId)54 bool SystemAbilityHelper::RemoveSystemAbility(const int32_t systemAbilityId)
55 {
56     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57     if (systemAbilityMgr && (systemAbilityMgr->RemoveSystemAbility(systemAbilityId) == 0)) {
58         return true;
59     }
60     APP_LOGE("fail remove %{public}d from system ability manager", systemAbilityId);
61     return false;
62 }
63 
UninstallApp(const std::string &bundleName, int32_t uid, int32_t appIndex)64 int SystemAbilityHelper::UninstallApp(const std::string &bundleName, int32_t uid, int32_t appIndex)
65 {
66 #ifdef ABILITY_RUNTIME_ENABLE
67     sptr<AAFwk::IAbilityManager> abilityMgrProxy =
68         iface_cast<AAFwk::IAbilityManager>(SystemAbilityHelper::GetSystemAbility(ABILITY_MGR_SERVICE_ID));
69     if (abilityMgrProxy == nullptr) {
70         APP_LOGE("fail to find the app mgr service to kill application");
71         return -1;
72     }
73     std::string identity = IPCSkeleton::ResetCallingIdentity();
74     auto ret = abilityMgrProxy->UninstallApp(bundleName, uid, appIndex);
75     IPCSkeleton::SetCallingIdentity(identity);
76     return ret;
77 #else
78     return 0;
79 #endif
80 }
81 
UpgradeApp(const std::string &bundleName, int32_t uid, int32_t appIndex)82 int SystemAbilityHelper::UpgradeApp(const std::string &bundleName, int32_t uid, int32_t appIndex)
83 {
84 #ifdef ABILITY_RUNTIME_ENABLE
85     sptr<AAFwk::IAbilityManager> abilityMgrProxy =
86         iface_cast<AAFwk::IAbilityManager>(SystemAbilityHelper::GetSystemAbility(ABILITY_MGR_SERVICE_ID));
87     if (abilityMgrProxy == nullptr) {
88         APP_LOGE("fail to find the app mgr service to kill application");
89         return -1;
90     }
91     std::string identity = IPCSkeleton::ResetCallingIdentity();
92     auto ret = abilityMgrProxy->UpgradeApp(bundleName, uid, KILL_REASON, appIndex);
93     IPCSkeleton::SetCallingIdentity(identity);
94     return ret;
95 #else
96     return 0;
97 #endif
98 }
99 
UnloadSystemAbility(const int32_t systemAbilityId)100 bool SystemAbilityHelper::UnloadSystemAbility(const int32_t systemAbilityId)
101 {
102     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
103     if (systemAbilityMgr != nullptr && (systemAbilityMgr->UnloadSystemAbility(systemAbilityId) == 0)) {
104         return true;
105     }
106     APP_LOGE("fail unload %{public}d from system ability manager", systemAbilityId);
107     return false;
108 }
109 }  // namespace AppExecFwk
110 }  // namespace OHOS