1 /*
2 * Copyright (c) 2021-2025 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 "bundle_installer_manager.h"
17
18 #include "app_log_tag_wrapper.h"
19 #include "bundle_hitrace_chain.h"
20 #include "bundle_memory_guard.h"
21 #include "bundle_mgr_service.h"
22 #include "datetime_ex.h"
23 #include "ipc_skeleton.h"
24 #include "parameters.h"
25 #include "xcollie_helper.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 constexpr const char* INSTALL_TASK = "Install_Task";
31 constexpr const char* UNINSTALL_TASK = "Uninstall_Task";
32 constexpr const char* RECOVER_TASK = "Recover_Task";
33 constexpr const char* THREAD_POOL_NAME = "InstallerThreadPool";
34 constexpr unsigned int TIME_OUT_SECONDS = 60 * 25;
35 constexpr int8_t MAX_TASK_NUMBER = 10;
36 constexpr int8_t RETAIL_MODE_THREAD_NUMBER = 1;
37 constexpr int8_t DELAY_INTERVAL_SECONDS = 60;
38 static std::atomic<int32_t> g_taskCounter = 0;
39 }
40
BundleInstallerManager()41 BundleInstallerManager::BundleInstallerManager()
42 {
43 if (system::GetBoolParameter(ServiceConstants::RETAIL_MODE_KEY, false)) {
44 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "RETAIL_MODE");
45 threadNum_ = RETAIL_MODE_THREAD_NUMBER;
46 }
47 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "create bundle installer manager instance");
48 }
49
~BundleInstallerManager()50 BundleInstallerManager::~BundleInstallerManager()
51 {
52 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "destroy bundle installer manager instance");
53 }
54
CreateInstallTask(const std::string & bundleFilePath,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)55 void BundleInstallerManager::CreateInstallTask(
56 const std::string &bundleFilePath, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
57 {
58 auto installer = CreateInstaller(statusReceiver);
59 if (installer == nullptr) {
60 LOG_E(BMS_TAG_INSTALLER, "create installer failed");
61 return;
62 }
63 auto traceId = HiviewDFX::HiTraceChain::GetId();
64 auto task = [installer, bundleFilePath, installParam, traceId] {
65 BUNDLE_MANAGER_TASK_CHAIN_ID(traceId);
66 BundleMemoryGuard memoryGuard;
67 int32_t timerId = XCollieHelper::SetTimer(INSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
68 EventReport::ReportDataPartitionUsageEvent();
69 installer->Install(bundleFilePath, installParam);
70 g_taskCounter--;
71 XCollieHelper::CancelTimer(timerId);
72 };
73 AddTask(task, "InstallTask path:" + bundleFilePath);
74 }
75
CreateRecoverTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)76 void BundleInstallerManager::CreateRecoverTask(
77 const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
78 {
79 auto installer = CreateInstaller(statusReceiver);
80 if (installer == nullptr) {
81 LOG_E(BMS_TAG_INSTALLER, "create installer failed");
82 return;
83 }
84 auto traceId = HiviewDFX::HiTraceChain::GetId();
85 auto task = [installer, bundleName, installParam, traceId] {
86 BUNDLE_MANAGER_TASK_CHAIN_ID(traceId);
87 BundleMemoryGuard memoryGuard;
88 int32_t timerId = XCollieHelper::SetTimer(RECOVER_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
89 installer->Recover(bundleName, installParam);
90 g_taskCounter--;
91 XCollieHelper::CancelTimer(timerId);
92 };
93 AddTask(task, "RecoverTask -n " + bundleName);
94 }
95
CreateInstallTask(const std::vector<std::string> & bundleFilePaths,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)96 void BundleInstallerManager::CreateInstallTask(const std::vector<std::string> &bundleFilePaths,
97 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
98 {
99 auto installer = CreateInstaller(statusReceiver);
100 if (installer == nullptr) {
101 LOG_E(BMS_TAG_INSTALLER, "create installer failed");
102 return;
103 }
104 auto traceId = HiviewDFX::HiTraceChain::GetId();
105 auto task = [installer, bundleFilePaths, installParam, traceId] {
106 BUNDLE_MANAGER_TASK_CHAIN_ID(traceId);
107 BundleMemoryGuard memoryGuard;
108 int32_t timerId = XCollieHelper::SetTimer(INSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
109 EventReport::ReportDataPartitionUsageEvent();
110 installer->Install(bundleFilePaths, installParam);
111 g_taskCounter--;
112 XCollieHelper::CancelTimer(timerId);
113 };
114 std::string paths;
115 for (const auto &bundleFilePath : bundleFilePaths) {
116 paths.append(bundleFilePath).append(" ");
117 }
118 AddTask(task, "InstallTask path:" + paths);
119 }
120
CreateInstallByBundleNameTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)121 void BundleInstallerManager::CreateInstallByBundleNameTask(const std::string &bundleName,
122 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
123 {
124 auto installer = CreateInstaller(statusReceiver);
125 if (installer == nullptr) {
126 LOG_E(BMS_TAG_INSTALLER, "create installer failed");
127 return;
128 }
129 auto traceId = HiviewDFX::HiTraceChain::GetId();
130 auto task = [installer, bundleName, installParam, traceId] {
131 BUNDLE_MANAGER_TASK_CHAIN_ID(traceId);
132 BundleMemoryGuard memoryGuard;
133 int32_t timerId = XCollieHelper::SetTimer(INSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
134 installer->InstallByBundleName(bundleName, installParam);
135 g_taskCounter--;
136 XCollieHelper::CancelTimer(timerId);
137 };
138 AddTask(task, "InstallTask -n " + bundleName);
139 }
140
CreateUninstallTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)141 void BundleInstallerManager::CreateUninstallTask(
142 const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
143 {
144 auto installer = CreateInstaller(statusReceiver);
145 if (installer == nullptr) {
146 LOG_E(BMS_TAG_INSTALLER, "create installer failed");
147 return;
148 }
149 auto traceId = HiviewDFX::HiTraceChain::GetId();
150 auto task = [installer, bundleName, installParam, traceId] {
151 BUNDLE_MANAGER_TASK_CHAIN_ID(traceId);
152 BundleMemoryGuard memoryGuard;
153 int32_t timerId = XCollieHelper::SetTimer(UNINSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
154 installer->Uninstall(bundleName, installParam);
155 g_taskCounter--;
156 XCollieHelper::CancelTimer(timerId);
157 };
158 AddTask(task, "UninstallTask -n " + bundleName);
159 }
160
CreateUninstallTask(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)161 void BundleInstallerManager::CreateUninstallTask(const std::string &bundleName, const std::string &modulePackage,
162 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
163 {
164 auto installer = CreateInstaller(statusReceiver);
165 if (installer == nullptr) {
166 LOG_E(BMS_TAG_INSTALLER, "create installer failed");
167 return;
168 }
169 auto traceId = HiviewDFX::HiTraceChain::GetId();
170 auto task = [installer, bundleName, modulePackage, installParam, traceId] {
171 BUNDLE_MANAGER_TASK_CHAIN_ID(traceId);
172 BundleMemoryGuard memoryGuard;
173 int32_t timerId = XCollieHelper::SetTimer(UNINSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
174 installer->Uninstall(bundleName, modulePackage, installParam);
175 g_taskCounter--;
176 XCollieHelper::CancelTimer(timerId);
177 };
178 AddTask(task, "UninstallTask -n " + bundleName);
179 }
180
CreateUninstallTask(const UninstallParam & uninstallParam,const sptr<IStatusReceiver> & statusReceive)181 void BundleInstallerManager::CreateUninstallTask(const UninstallParam &uninstallParam,
182 const sptr<IStatusReceiver> &statusReceive)
183 {
184 auto installer = CreateInstaller(statusReceive);
185 if (installer == nullptr) {
186 LOG_E(BMS_TAG_INSTALLER, "create installer failed");
187 return;
188 }
189 auto traceId = HiviewDFX::HiTraceChain::GetId();
190 auto task = [installer, uninstallParam, traceId] {
191 BUNDLE_MANAGER_TASK_CHAIN_ID(traceId);
192 BundleMemoryGuard memoryGuard;
193 int32_t timerId = XCollieHelper::SetTimer(UNINSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
194 installer->Uninstall(uninstallParam);
195 g_taskCounter--;
196 XCollieHelper::CancelTimer(timerId);
197 };
198 AddTask(task, "UninstallTask -n " + uninstallParam.bundleName);
199 }
200
CreateUninstallAndRecoverTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)201 void BundleInstallerManager::CreateUninstallAndRecoverTask(const std::string &bundleName,
202 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
203 {
204 auto installer = CreateInstaller(statusReceiver);
205 if (installer == nullptr) {
206 LOG_E(BMS_TAG_INSTALLER, "create installer failed");
207 return;
208 }
209 auto traceId = HiviewDFX::HiTraceChain::GetId();
210 auto task = [installer, bundleName, installParam, traceId] {
211 BUNDLE_MANAGER_TASK_CHAIN_ID(traceId);
212 BundleMemoryGuard memoryGuard;
213 int32_t timerId = XCollieHelper::SetTimer(UNINSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
214 installer->UninstallAndRecover(bundleName, installParam);
215 g_taskCounter--;
216 XCollieHelper::CancelTimer(timerId);
217 };
218 AddTask(task, "UninstallAndRecover -n " + bundleName);
219 }
220
CreateInstaller(const sptr<IStatusReceiver> & statusReceiver)221 std::shared_ptr<BundleInstaller> BundleInstallerManager::CreateInstaller(const sptr<IStatusReceiver> &statusReceiver)
222 {
223 int64_t installerId = GetMicroTickCount();
224 auto installer = std::make_shared<BundleInstaller>(installerId, statusReceiver);
225 installer->SetCallingUid(IPCSkeleton::GetCallingUid());
226 installer->SetCallingTokenId(IPCSkeleton::GetCallingTokenID());
227 return installer;
228 }
229
AddTask(const ThreadPoolTask & task,const std::string & taskName)230 void BundleInstallerManager::AddTask(const ThreadPoolTask &task, const std::string &taskName)
231 {
232 std::lock_guard<std::mutex> guard(mutex_);
233 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "hold mutex");
234 if (threadPool_ == nullptr) {
235 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "begin to start InstallerThreadPool");
236 threadPool_ = std::make_shared<ThreadPool>(THREAD_POOL_NAME);
237 threadPool_->Start(threadNum_);
238 threadPool_->SetMaxTaskNum(MAX_TASK_NUMBER);
239 auto delayCloseTask = std::bind(&BundleInstallerManager::DelayStopThreadPool, shared_from_this());
240 std::thread t(delayCloseTask);
241 t.detach();
242 }
243 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "add task:%{public}s", taskName.c_str());
244 g_taskCounter++;
245 threadPool_->AddTask(task);
246 }
247
DelayStopThreadPool()248 void BundleInstallerManager::DelayStopThreadPool()
249 {
250 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "DelayStopThreadPool begin");
251 BundleMemoryGuard memoryGuard;
252
253 do {
254 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "sleep for 60s runningTask %{public}d", g_taskCounter.load());
255 std::this_thread::sleep_for(std::chrono::seconds(DELAY_INTERVAL_SECONDS));
256 } while (threadPool_ != nullptr && (threadPool_->GetCurTaskNum() != 0
257 || g_taskCounter.load() != 0));
258
259 std::lock_guard<std::mutex> guard(mutex_);
260 if (threadPool_ == nullptr) {
261 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "InstallerThreadPool is null, no need to stop");
262 return;
263 }
264 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "begin to stop InstallerThreadPool");
265 threadPool_->Stop();
266 threadPool_ = nullptr;
267 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "DelayStopThreadPool end");
268 }
269
GetCurTaskNum()270 size_t BundleInstallerManager::GetCurTaskNum()
271 {
272 std::lock_guard<std::mutex> guard(mutex_);
273 if (threadPool_ == nullptr) {
274 return 0;
275 }
276
277 return threadPool_->GetCurTaskNum();
278 }
279 } // namespace AppExecFwk
280 } // namespace OHOS