1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved.
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 #include "vk_bundle_mgr_helper.h"
16
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19 #include "../loader_hilog.h"
20 #include <securec.h>
21 #define EOK 0
22
23 extern "C" {
24 void *loader_instance_heap_calloc(const struct loader_instance *instance,
25 size_t size, VkSystemAllocationScope allocation_scope);
26 }
27 constexpr const char *DEBUG_SANDBOX_DIR = "/data/storage/el1/bundle/";
28
InitBundleInfo(char * debugHapName)29 bool InitBundleInfo(char* debugHapName)
30 {
31 if (NULL == debugHapName || '\0' == debugHapName[0]) {
32 VKHILOGE("debug_hap_name is NULL!");
33 return false;
34 }
35 std::string debugHap(debugHapName);
36 auto vkBundleMgrHelper = OHOS::DelayedSingleton<OHOS::AppExecFwk::VKBundleMgrHelper>::GetInstance();
37 if (vkBundleMgrHelper == nullptr) {
38 VKHILOGE("vkBundleMgrHelper is null!");
39 return false;
40 }
41
42 if (vkBundleMgrHelper->GetBundleInfoForSelf(
43 OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION, vkBundleMgrHelper->g_bundleInfo) ==
44 OHOS::ERR_OK) {
45 if (vkBundleMgrHelper->g_bundleInfo.name == debugHap) {
46 return true;
47 } else {
48 VKHILOGE("this hap is %{public}s, the debug hap is %{public}s",
49 vkBundleMgrHelper->g_bundleInfo.name.c_str(), debugHap.c_str());
50 }
51 } else {
52 VKHILOGE("Call GetBundleInfoForSelf func failed!");
53 }
54 return false;
55 }
56
CheckAppProvisionTypeIsDebug()57 bool CheckAppProvisionTypeIsDebug()
58 {
59 auto vkBundleMgrHelper = OHOS::DelayedSingleton<OHOS::AppExecFwk::VKBundleMgrHelper>::GetInstance();
60 if (vkBundleMgrHelper == nullptr) {
61 VKHILOGE("vkBundleMgrHelper is null!");
62 return false;
63 }
64 VKHILOGD("this hap is %{public}s, the debug hap appProvisionType is %{public}s",
65 vkBundleMgrHelper->g_bundleInfo.name.c_str(),
66 vkBundleMgrHelper->g_bundleInfo.applicationInfo.appProvisionType.c_str());
67 if (vkBundleMgrHelper->g_bundleInfo.applicationInfo.appProvisionType == "release") {
68 return false;
69 }
70 return true;
71 }
72
GetDebugLayerLibPath(const struct loader_instance * inst,VkSystemAllocationScope allocation_sacope)73 char* GetDebugLayerLibPath(const struct loader_instance *inst, VkSystemAllocationScope allocation_sacope)
74 {
75 auto vkBundleMgrHelper = OHOS::DelayedSingleton<OHOS::AppExecFwk::VKBundleMgrHelper>::GetInstance();
76 std::string pathStr(DEBUG_SANDBOX_DIR);
77 std::string appLibPath = pathStr + vkBundleMgrHelper->g_bundleInfo.applicationInfo.nativeLibraryPath + "/";
78 const char* fullPath = appLibPath.c_str();
79 size_t len = strlen(fullPath) + 1;
80 char* libPath = static_cast<char*>(loader_instance_heap_calloc(inst, len, allocation_sacope));
81 if (libPath == NULL) {
82 VKHILOGE("malloc libPath fail");
83 return NULL;
84 }
85 if (memcpy_s(libPath, len, fullPath, len) != EOK) {
86 VKHILOGE("memcpy_s libPath fail, fullPath: %{public}s", fullPath);
87 return NULL;
88 }
89 VKHILOGD("GetDebugLayerLibPath(): the libPath is %{public}s", libPath);
90 return libPath;
91 }
92
93 namespace OHOS {
94 namespace AppExecFwk {
VKBundleMgrHelper()95 VKBundleMgrHelper::VKBundleMgrHelper() {}
96
~VKBundleMgrHelper()97 VKBundleMgrHelper::~VKBundleMgrHelper()
98 {
99 if (bundleMgr_ != nullptr && bundleMgr_->AsObject() != nullptr && deathRecipient_ != nullptr) {
100 bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
101 }
102 }
103
Connect()104 sptr<AppExecFwk::IBundleMgr> VKBundleMgrHelper::Connect()
105 {
106 VKHILOGD("Call VKBundleMgrHelper::Connect");
107 std::lock_guard<std::mutex> lock(mutex_);
108 if (bundleMgr_ == nullptr) {
109 sptr<ISystemAbilityManager> systemAbilityManager =
110 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
111 if (systemAbilityManager == nullptr) {
112 VKHILOGE("Failed to get system ability manager.");
113 return nullptr;
114 }
115 sptr<IRemoteObject> remoteObject_ = systemAbilityManager->GetSystemAbility(
116 BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
117 if (remoteObject_ == nullptr || (bundleMgr_ = iface_cast<IBundleMgr>(remoteObject_)) == nullptr) {
118 VKHILOGE("Failed to get bundle mgr service remote object");
119 return nullptr;
120 }
121 std::weak_ptr<VKBundleMgrHelper> weakPtr = shared_from_this();
122 auto deathCallback = [weakPtr](const wptr<IRemoteObject> &object) {
123 auto sharedPtr = weakPtr.lock();
124 if (sharedPtr == nullptr) {
125 VKHILOGE("Bundle helper instance is nullptr");
126 return;
127 }
128 sharedPtr->OnDeath();
129 };
130 deathRecipient_ = new(std::nothrow) VKBundleMgrServiceDeathRecipient(deathCallback);
131 if (deathRecipient_ == nullptr) {
132 VKHILOGE("Failed to create death recipient ptr deathRecipient_!");
133 return nullptr;
134 }
135 if (bundleMgr_->AsObject() != nullptr) {
136 bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_);
137 }
138 }
139 return bundleMgr_;
140 }
141
OnDeath()142 void VKBundleMgrHelper::OnDeath()
143 {
144 VKHILOGD("Call VKBundleMgrHelper::OnDeath");
145 std::lock_guard<std::mutex> lock(mutex_);
146 if (bundleMgr_ == nullptr || bundleMgr_->AsObject() == nullptr) {
147 VKHILOGE("bundleMgr_ is nullptr!");
148 return;
149 }
150 bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
151 bundleMgr_ = nullptr;
152 }
153
GetBundleInfoForSelf(AppExecFwk::GetBundleInfoFlag flags,BundleInfo & bundleInfo)154 ErrCode VKBundleMgrHelper::GetBundleInfoForSelf(AppExecFwk::GetBundleInfoFlag flags, BundleInfo &bundleInfo)
155 {
156 VKHILOGD("Call VKBundleMgrHealper::GetBundleInfoForSelf.");
157 auto bundleMgr_ = Connect();
158 if (bundleMgr_ == nullptr) {
159 VKHILOGE("Failed to connect.");
160 return -1;
161 }
162 return bundleMgr_->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo);
163 }
164
VKBundleMgrServiceDeathRecipient(const std::function<void (const wptr<IRemoteObject> & object)> & deathCallback)165 VKBundleMgrServiceDeathRecipient::VKBundleMgrServiceDeathRecipient(
166 const std::function<void(const wptr<IRemoteObject>& object)>& deathCallback)
167 : deathCallback_(deathCallback) {}
168
OnRemoteDied(const wptr<IRemoteObject> & object)169 void VKBundleMgrServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
170 {
171 if (deathCallback_ != nullptr) {
172 deathCallback_(object);
173 }
174 }
175 }
176 }