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