1 /*
2 * Copyright (c) 2022-2024 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_mgr_service.h"
17
18 #include <sys/stat.h>
19
20 #include "account_helper.h"
21 #ifdef CODE_SIGNATURE_ENABLE
22 #include "aot/aot_sign_data_cache_mgr.h"
23 #endif
24 #include "bundle_common_event.h"
25 #include "bundle_memory_guard.h"
26 #include "bundle_resource_helper.h"
27 #include "datetime_ex.h"
28 #include "el5_filekey_callback.h"
29 #include "el5_filekey_manager_kit.h"
30 #include "installd_client.h"
31 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
32 #include "app_control_manager_host_impl.h"
33 #endif
34 #include "perf_profile.h"
35 #include "scope_guard.h"
36 #include "system_ability_definition.h"
37 #include "system_ability_helper.h"
38 #include "xcollie_helper.h"
39
40 namespace OHOS {
41 namespace AppExecFwk {
42 namespace {
43 const int32_t BUNDLE_BROKER_SERVICE_ABILITY_ID = 0x00010500;
44 const int32_t EL5_FILEKEY_SERVICE_ABILITY_ID = 8250;
45 constexpr const char* FUN_BMS_START = "BundleMgrService::Start";
46 constexpr unsigned int BMS_START_TIME_OUT_SECONDS = 60 * 4;
47 } // namespace
48
49 const bool REGISTER_RESULT =
50 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<BundleMgrService>::GetInstance().get());
51
BundleMgrService()52 BundleMgrService::BundleMgrService() : SystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, true)
53 {
54 APP_LOGI("instance is created");
55 PerfProfile::GetInstance().SetBmsLoadStartTime(GetTickCount());
56 }
57
~BundleMgrService()58 BundleMgrService::~BundleMgrService()
59 {
60 host_ = nullptr;
61 installer_ = nullptr;
62 if (handler_) {
63 handler_.reset();
64 }
65 if (dataMgr_) {
66 dataMgr_.reset();
67 }
68 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
69 {
70 std::lock_guard<std::mutex> connectLock(bundleConnectMutex_);
71 if (!connectAbilityMgr_.empty()) {
72 connectAbilityMgr_.clear();
73 }
74 }
75 #endif
76 if (hidumpHelper_) {
77 hidumpHelper_.reset();
78 }
79 APP_LOGI("BundleMgrService instance is destroyed");
80 }
81
OnStart()82 void BundleMgrService::OnStart()
83 {
84 APP_LOGI("BundleMgrService OnStart start");
85 if (!Init()) {
86 APP_LOGE("BundleMgrService init fail");
87 return;
88 }
89
90 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
91 AddSystemAbilityListener(BUNDLE_BROKER_SERVICE_ABILITY_ID);
92 AddSystemAbilityListener(EL5_FILEKEY_SERVICE_ABILITY_ID);
93 APP_LOGI("BundleMgrService OnStart end");
94 }
95
OnStop()96 void BundleMgrService::OnStop()
97 {
98 APP_LOGI("OnStop is called");
99 SelfClean();
100 }
101
IsServiceReady() const102 bool BundleMgrService::IsServiceReady() const
103 {
104 return ready_;
105 }
106
Init()107 bool BundleMgrService::Init()
108 {
109 if (ready_) {
110 APP_LOGW("init more than one time");
111 return false;
112 }
113
114 APP_LOGI("BundleMgrService Init begin");
115 CreateBmsServiceDir();
116 InitBmsParam();
117 InitPreInstallExceptionMgr();
118 CHECK_INIT_RESULT(InitBundleMgrHost(), "Init bundleMgr fail");
119 CHECK_INIT_RESULT(InitBundleInstaller(), "Init bundleInstaller fail");
120 InitBundleDataMgr();
121 CHECK_INIT_RESULT(InitBundleUserMgr(), "Init bundleUserMgr fail");
122 CHECK_INIT_RESULT(InitVerifyManager(), "Init verifyManager fail");
123 CHECK_INIT_RESULT(InitExtendResourceManager(), "Init extendResourceManager fail");
124 CHECK_INIT_RESULT(InitBundleEventHandler(), "Init bundleEventHandler fail");
125 InitHidumpHelper();
126 InitFreeInstall();
127 CHECK_INIT_RESULT(InitDefaultApp(), "Init defaultApp fail");
128 CHECK_INIT_RESULT(InitAppControl(), "Init appControl fail");
129 CHECK_INIT_RESULT(InitQuickFixManager(), "Init quickFixManager fail");
130 CHECK_INIT_RESULT(InitOverlayManager(), "Init overlayManager fail");
131 CHECK_INIT_RESULT(InitBundleResourceMgr(), "Init BundleResourceMgr fail");
132 BundleResourceHelper::BundleSystemStateInit();
133 ready_ = true;
134 APP_LOGI("BundleMgrService Init success");
135 return true;
136 }
137
InitBmsParam()138 void BundleMgrService::InitBmsParam()
139 {
140 bmsParam_ = std::make_shared<BmsParam>();
141 }
142
InitPreInstallExceptionMgr()143 void BundleMgrService::InitPreInstallExceptionMgr()
144 {
145 preInstallExceptionMgr_ = std::make_shared<PreInstallExceptionMgr>();
146 }
147
InitBundleMgrHost()148 bool BundleMgrService::InitBundleMgrHost()
149 {
150 if (host_ == nullptr) {
151 host_ = new (std::nothrow) BundleMgrHostImpl();
152 }
153
154 return host_ != nullptr;
155 }
156
InitBundleInstaller()157 bool BundleMgrService::InitBundleInstaller()
158 {
159 if (installer_ == nullptr) {
160 installer_ = new (std::nothrow) BundleInstallerHost();
161 if (installer_ == nullptr) {
162 APP_LOGE("init installer fail");
163 return false;
164 }
165 installer_->Init();
166 }
167
168 return true;
169 }
170
InitBundleDataMgr()171 void BundleMgrService::InitBundleDataMgr()
172 {
173 if (dataMgr_ == nullptr) {
174 APP_LOGI("Create BundledataMgr");
175 dataMgr_ = std::make_shared<BundleDataMgr>();
176 dataMgr_->AddUserId(Constants::DEFAULT_USERID);
177 }
178 }
179
InitBundleUserMgr()180 bool BundleMgrService::InitBundleUserMgr()
181 {
182 if (userMgrHost_ == nullptr) {
183 userMgrHost_ = new (std::nothrow) BundleUserMgrHostImpl();
184 }
185
186 return userMgrHost_ != nullptr;
187 }
188
InitVerifyManager()189 bool BundleMgrService::InitVerifyManager()
190 {
191 if (verifyManager_ == nullptr) {
192 verifyManager_ = new (std::nothrow) VerifyManagerHostImpl();
193 }
194
195 return verifyManager_ != nullptr;
196 }
197
InitExtendResourceManager()198 bool BundleMgrService::InitExtendResourceManager()
199 {
200 if (extendResourceManager_ == nullptr) {
201 extendResourceManager_ = new (std::nothrow) ExtendResourceManagerHostImpl();
202 }
203
204 return extendResourceManager_ != nullptr;
205 }
206
InitBundleEventHandler()207 bool BundleMgrService::InitBundleEventHandler()
208 {
209 if (handler_ == nullptr) {
210 handler_ = std::make_shared<BMSEventHandler>();
211 }
212 auto task = [this]() {
213 BundleMemoryGuard memoryGuard;
214 int32_t timerId = XCollieHelper::SetRecoveryTimer(FUN_BMS_START, BMS_START_TIME_OUT_SECONDS);
215 ScopeGuard cancelTimerGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
216 handler_->BmsStartEvent();
217 };
218 ffrt::submit(task);
219 return true;
220 }
221
InitHidumpHelper()222 void BundleMgrService::InitHidumpHelper()
223 {
224 if (hidumpHelper_ == nullptr) {
225 APP_LOGI("Create hidump helper");
226 hidumpHelper_ = std::make_shared<HidumpHelper>(dataMgr_);
227 }
228 }
229
InitFreeInstall()230 void BundleMgrService::InitFreeInstall()
231 {
232 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
233 if (agingMgr_ == nullptr) {
234 APP_LOGI("Create aging manager");
235 agingMgr_ = std::make_shared<BundleAgingMgr>();
236 agingMgr_->InitAgingtTimer();
237 }
238 if (bundleDistributedManager_ == nullptr) {
239 APP_LOGI("Create bundleDistributedManager");
240 bundleDistributedManager_ = std::make_shared<BundleDistributedManager>();
241 }
242 #endif
243 }
244
InitDefaultApp()245 bool BundleMgrService::InitDefaultApp()
246 {
247 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
248 if (defaultAppHostImpl_ == nullptr) {
249 defaultAppHostImpl_ = new (std::nothrow) DefaultAppHostImpl();
250 if (defaultAppHostImpl_ == nullptr) {
251 APP_LOGE("create DefaultAppHostImpl failed");
252 return false;
253 }
254 }
255 #endif
256 return true;
257 }
258
InitAppControl()259 bool BundleMgrService::InitAppControl()
260 {
261 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
262 if (appControlManagerHostImpl_ == nullptr) {
263 appControlManagerHostImpl_ = new (std::nothrow) AppControlManagerHostImpl();
264 if (appControlManagerHostImpl_ == nullptr) {
265 APP_LOGE("create appControlManagerHostImpl failed");
266 return false;
267 }
268 }
269 #endif
270 return true;
271 }
272
InitQuickFixManager()273 bool BundleMgrService::InitQuickFixManager()
274 {
275 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
276 if (quickFixManagerHostImpl_ == nullptr) {
277 quickFixManagerHostImpl_ = new (std::nothrow) QuickFixManagerHostImpl();
278 if (quickFixManagerHostImpl_ == nullptr) {
279 APP_LOGE("create QuickFixManagerHostImpl failed");
280 return false;
281 }
282 }
283 #endif
284 return true;
285 }
286
InitOverlayManager()287 bool BundleMgrService::InitOverlayManager()
288 {
289 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
290 if (overlayManagerHostImpl_ == nullptr) {
291 overlayManagerHostImpl_ = new (std::nothrow) OverlayManagerHostImpl();
292 if (overlayManagerHostImpl_ == nullptr) {
293 APP_LOGE("create OverlayManagerHostImpl failed");
294 return false;
295 }
296 }
297 #endif
298 return true;
299 }
300
CreateBmsServiceDir()301 void BundleMgrService::CreateBmsServiceDir()
302 {
303 auto ret = InstalldClient::GetInstance()->Mkdir(
304 ServiceConstants::HAP_COPY_PATH, S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH,
305 Constants::FOUNDATION_UID, ServiceConstants::BMS_GID);
306 if (!ret) {
307 APP_LOGE("create dir failed");
308 }
309 }
310
InitBundleResourceMgr()311 bool BundleMgrService::InitBundleResourceMgr()
312 {
313 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
314 if (bundleResourceHostImpl_ == nullptr) {
315 bundleResourceHostImpl_ = new (std::nothrow) BundleResourceHostImpl();
316 if (bundleResourceHostImpl_ == nullptr) {
317 APP_LOGE("create bundleResourceHostImpl failed");
318 return false;
319 }
320 }
321 #endif
322 return true;
323 }
324
GetBundleInstaller() const325 sptr<BundleInstallerHost> BundleMgrService::GetBundleInstaller() const
326 {
327 return installer_;
328 }
329
RegisterDataMgr(std::shared_ptr<BundleDataMgr> dataMgrImpl)330 void BundleMgrService::RegisterDataMgr(std::shared_ptr<BundleDataMgr> dataMgrImpl)
331 {
332 dataMgr_ = dataMgrImpl;
333 if (dataMgr_ != nullptr) {
334 dataMgr_->AddUserId(Constants::DEFAULT_USERID);
335 }
336 }
337
GetDataMgr() const338 const std::shared_ptr<BundleDataMgr> BundleMgrService::GetDataMgr() const
339 {
340 return dataMgr_;
341 }
342
343 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
GetAgingMgr() const344 const std::shared_ptr<BundleAgingMgr> BundleMgrService::GetAgingMgr() const
345 {
346 return agingMgr_;
347 }
348
GetConnectAbility(int32_t userId)349 const std::shared_ptr<BundleConnectAbilityMgr> BundleMgrService::GetConnectAbility(int32_t userId)
350 {
351 int32_t currentUserId = userId;
352 if (currentUserId == Constants::UNSPECIFIED_USERID) {
353 currentUserId = AccountHelper::GetCurrentActiveUserId();
354 }
355 std::lock_guard<std::mutex> connectLock(bundleConnectMutex_);
356 if (connectAbilityMgr_.find(userId) == connectAbilityMgr_.end() ||
357 connectAbilityMgr_[userId] == nullptr) {
358 auto ptr = std::make_shared<BundleConnectAbilityMgr>();
359 connectAbilityMgr_[userId] = ptr;
360 }
361 return connectAbilityMgr_[userId];
362 }
363
GetBundleDistributedManager() const364 const std::shared_ptr<BundleDistributedManager> BundleMgrService::GetBundleDistributedManager() const
365 {
366 return bundleDistributedManager_;
367 }
368 #endif
369
SelfClean()370 void BundleMgrService::SelfClean()
371 {
372 if (ready_) {
373 ready_ = false;
374 if (registerToService_) {
375 registerToService_ = false;
376 }
377 }
378 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
379 agingMgr_.reset();
380 {
381 std::lock_guard<std::mutex> connectLock(bundleConnectMutex_);
382 connectAbilityMgr_.clear();
383 }
384 bundleDistributedManager_.reset();
385 #endif
386 }
387
GetBundleUserMgr() const388 sptr<BundleUserMgrHostImpl> BundleMgrService::GetBundleUserMgr() const
389 {
390 return userMgrHost_;
391 }
392
GetVerifyManager() const393 sptr<IVerifyManager> BundleMgrService::GetVerifyManager() const
394 {
395 return verifyManager_;
396 }
397
GetExtendResourceManager() const398 sptr<IExtendResourceManager> BundleMgrService::GetExtendResourceManager() const
399 {
400 return extendResourceManager_;
401 }
402
GetBmsParam() const403 const std::shared_ptr<BmsParam> BundleMgrService::GetBmsParam() const
404 {
405 return bmsParam_;
406 }
407
GetPreInstallExceptionMgr() const408 const std::shared_ptr<PreInstallExceptionMgr> BundleMgrService::GetPreInstallExceptionMgr() const
409 {
410 return preInstallExceptionMgr_;
411 }
412
413 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
GetDefaultAppProxy() const414 sptr<IDefaultApp> BundleMgrService::GetDefaultAppProxy() const
415 {
416 return defaultAppHostImpl_;
417 }
418 #endif
419
420 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
GetAppControlProxy() const421 sptr<IAppControlMgr> BundleMgrService::GetAppControlProxy() const
422 {
423 return appControlManagerHostImpl_;
424 }
425 #endif
426
427 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
GetQuickFixManagerProxy() const428 sptr<QuickFixManagerHostImpl> BundleMgrService::GetQuickFixManagerProxy() const
429 {
430 return quickFixManagerHostImpl_;
431 }
432 #endif
433
434 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
GetOverlayManagerProxy() const435 sptr<IOverlayManager> BundleMgrService::GetOverlayManagerProxy() const
436 {
437 return overlayManagerHostImpl_;
438 }
439 #endif
440
441 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
GetBundleResourceProxy() const442 sptr<IBundleResource> BundleMgrService::GetBundleResourceProxy() const
443 {
444 return bundleResourceHostImpl_;
445 }
446 #endif
447
RegisterChargeIdleListener()448 void BundleMgrService::RegisterChargeIdleListener()
449 {
450 APP_LOGI("begin to register charge idle listener");
451 EventFwk::MatchingSkills matchingSkills;
452 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CHARGE_IDLE_MODE_CHANGED);
453 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
454 chargeIdleListener_ = std::make_shared<ChargeIdleListener>(subscribeInfo);
455 (void)EventFwk::CommonEventManager::SubscribeCommonEvent(chargeIdleListener_);
456 APP_LOGI("register charge idle listener done");
457 }
458
CheckAllUser()459 void BundleMgrService::CheckAllUser()
460 {
461 if (dataMgr_ == nullptr) {
462 return;
463 }
464
465 APP_LOGI("Check all user start");
466 std::set<int32_t> userIds = dataMgr_->GetAllUser();
467 for (auto userId : userIds) {
468 if (userId == Constants::DEFAULT_USERID) {
469 continue;
470 }
471
472 bool isExists = false;
473 if (AccountHelper::IsOsAccountExists(userId, isExists) != ERR_OK) {
474 APP_LOGW("Failed query whether user(%{public}d) exists", userId);
475 continue;
476 }
477
478 if (!isExists) {
479 APP_LOGI("Query user(%{public}d) success but not complete and remove it", userId);
480 userMgrHost_->RemoveUser(userId);
481 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
482 {
483 std::lock_guard<std::mutex> connectLock(bundleConnectMutex_);
484 connectAbilityMgr_.erase(userId);
485 }
486 #endif
487 }
488 }
489 APP_LOGI("Check all user end");
490 }
491
RegisterService()492 void BundleMgrService::RegisterService()
493 {
494 if (!registerToService_) {
495 if (!SystemAbilityHelper::AddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, host_)) {
496 APP_LOGE("fail to register to system ability manager");
497 return;
498 }
499 APP_LOGI("BundleMgrService register to sam success");
500 registerToService_ = true;
501 }
502
503 PerfProfile::GetInstance().SetBmsLoadEndTime(GetTickCount());
504 PerfProfile::GetInstance().Dump();
505 }
506
NotifyBundleScanStatus()507 void BundleMgrService::NotifyBundleScanStatus()
508 {
509 APP_LOGI("PublishCommonEvent for bundle scan finished");
510 AAFwk::Want want;
511 want.SetAction(COMMON_EVENT_BUNDLE_SCAN_FINISHED);
512 EventFwk::CommonEventData commonEventData { want };
513 if (!EventFwk::CommonEventManager::PublishCommonEvent(commonEventData)) {
514 notifyBundleScanStatus = true;
515 APP_LOGE("PublishCommonEvent for bundle scan finished failed");
516 } else {
517 APP_LOGI("PublishCommonEvent for bundle scan finished succeed");
518 }
519 }
520
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)521 void BundleMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
522 {
523 APP_LOGI("OnAddSystemAbility systemAbilityId:%{public}d added", systemAbilityId);
524 if (COMMON_EVENT_SERVICE_ID == systemAbilityId && notifyBundleScanStatus) {
525 NotifyBundleScanStatus();
526 }
527 #ifdef CODE_SIGNATURE_ENABLE
528 if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
529 AOTSignDataCacheMgr::GetInstance().RegisterScreenUnlockListener();
530 }
531 #endif
532 if (BUNDLE_BROKER_SERVICE_ABILITY_ID == systemAbilityId) {
533 if (host_ != nullptr) {
534 isBrokerServiceStarted_ = true;
535 host_->SetBrokerServiceStatus(true);
536 }
537 }
538 if (EL5_FILEKEY_SERVICE_ABILITY_ID == systemAbilityId) {
539 int32_t reg = Security::AccessToken::El5FilekeyManagerKit::RegisterCallback(sptr(new El5FilekeyCallback()));
540 APP_LOGI("Register El5FilekeyCallback result: %{public}d", reg);
541 }
542 }
543
Hidump(const std::vector<std::string> & args,std::string & result) const544 bool BundleMgrService::Hidump(const std::vector<std::string> &args, std::string& result) const
545 {
546 if (hidumpHelper_ && hidumpHelper_->Dump(args, result)) {
547 return true;
548 }
549
550 APP_LOGD("HidumpHelper failed");
551 return false;
552 }
553
IsBrokerServiceStarted() const554 bool BundleMgrService::IsBrokerServiceStarted() const
555 {
556 return isBrokerServiceStarted_;
557 }
558 } // namespace AppExecFwk
559 } // namespace OHOS
560