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