• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "common_event_data.h"
28 #include "common_event_manager.h"
29 #include "common_event_support.h"
30 #include "datetime_ex.h"
31 #include "ffrt.h"
32 #include "installd_client.h"
33 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
34 #include "app_control_manager_host_impl.h"
35 #endif
36 #include "perf_profile.h"
37 #include "system_ability_definition.h"
38 #include "system_ability_helper.h"
39 #include "want.h"
40 #ifdef HICOLLIE_ENABLE
41 #include "xcollie/watchdog.h"
42 #endif
43 
44 namespace OHOS {
45 namespace AppExecFwk {
46 namespace {
47 const int32_t BUNDLE_BROKER_SERVICE_ABILITY_ID = 0x00010500;
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     if (connectAbilityMgr_ != nullptr) {
71         connectAbilityMgr_.reset();
72     }
73 #endif
74     if (hidumpHelper_) {
75         hidumpHelper_.reset();
76     }
77     APP_LOGI("BundleMgrService instance is destroyed");
78 }
79 
OnStart()80 void BundleMgrService::OnStart()
81 {
82     APP_LOGI("BundleMgrService OnStart start");
83     if (!Init()) {
84         APP_LOGE("BundleMgrService init fail");
85         return;
86     }
87 
88     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
89     AddSystemAbilityListener(BUNDLE_BROKER_SERVICE_ABILITY_ID);
90     APP_LOGI("BundleMgrService OnStart end");
91 }
92 
OnStop()93 void BundleMgrService::OnStop()
94 {
95     APP_LOGI("OnStop is called");
96     SelfClean();
97 }
98 
IsServiceReady() const99 bool BundleMgrService::IsServiceReady() const
100 {
101     return ready_;
102 }
103 
Init()104 bool BundleMgrService::Init()
105 {
106     if (ready_) {
107         APP_LOGW("init more than one time");
108         return false;
109     }
110 
111     APP_LOGI("BundleMgrService Init begin");
112     CreateBmsServiceDir();
113     InitBmsParam();
114     CHECK_INIT_RESULT(InitBundleMgrHost(), "Init bundleMgr fail");
115     CHECK_INIT_RESULT(InitBundleInstaller(), "Init bundleInstaller fail");
116     InitBundleDataMgr();
117     CHECK_INIT_RESULT(InitBundleUserMgr(), "Init bundleUserMgr fail");
118     CHECK_INIT_RESULT(InitBundleEventHandler(), "Init bundleEventHandler fail");
119     InitHidumpHelper();
120     InitFreeInstall();
121     CHECK_INIT_RESULT(InitDefaultApp(), "Init defaultApp fail");
122     CHECK_INIT_RESULT(InitAppControl(), "Init appControl fail");
123     CHECK_INIT_RESULT(InitQuickFixManager(), "Init quickFixManager fail");
124     CHECK_INIT_RESULT(InitOverlayManager(), "Init overlayManager fail");
125     ready_ = true;
126     APP_LOGI("BundleMgrService Init success");
127     return true;
128 }
129 
InitBmsParam()130 void BundleMgrService::InitBmsParam()
131 {
132     bmsParam_ = std::make_shared<BmsParam>();
133 }
134 
InitBundleMgrHost()135 bool BundleMgrService::InitBundleMgrHost()
136 {
137     if (host_ == nullptr) {
138         host_ = new (std::nothrow) BundleMgrHostImpl();
139     }
140 
141     return host_ != nullptr;
142 }
143 
InitBundleInstaller()144 bool BundleMgrService::InitBundleInstaller()
145 {
146     if (installer_ == nullptr) {
147         installer_ = new (std::nothrow) BundleInstallerHost();
148         if (installer_ == nullptr || !installer_->Init()) {
149             APP_LOGE("init installer fail");
150             return false;
151         }
152     }
153 
154     return true;
155 }
156 
InitBundleDataMgr()157 void BundleMgrService::InitBundleDataMgr()
158 {
159     if (dataMgr_ == nullptr) {
160         APP_LOGI("Create BundledataMgr");
161         dataMgr_ = std::make_shared<BundleDataMgr>();
162         dataMgr_->AddUserId(Constants::DEFAULT_USERID);
163     }
164 }
165 
InitBundleUserMgr()166 bool BundleMgrService::InitBundleUserMgr()
167 {
168     if (userMgrHost_ == nullptr) {
169         userMgrHost_ = new (std::nothrow) BundleUserMgrHostImpl();
170     }
171 
172     return userMgrHost_ != nullptr;
173 }
174 
InitBundleEventHandler()175 bool BundleMgrService::InitBundleEventHandler()
176 {
177     if (handler_ == nullptr) {
178         handler_ = std::make_shared<BMSEventHandler>();
179     }
180     auto task = [this]() {
181         BundleMemoryGuard memoryGuard;
182         handler_->BmsStartEvent();
183     };
184     ffrt::submit(task);
185     return true;
186 }
187 
InitHidumpHelper()188 void BundleMgrService::InitHidumpHelper()
189 {
190     if (hidumpHelper_ == nullptr) {
191         APP_LOGI("Create hidump helper");
192         hidumpHelper_ = std::make_shared<HidumpHelper>(dataMgr_);
193     }
194 }
195 
InitFreeInstall()196 void BundleMgrService::InitFreeInstall()
197 {
198 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
199     if (agingMgr_ == nullptr) {
200         APP_LOGI("Create aging manager");
201         agingMgr_ = std::make_shared<BundleAgingMgr>();
202         agingMgr_->InitAgingtTimer();
203     }
204     if (connectAbilityMgr_ == nullptr) {
205         APP_LOGI("Create BundleConnectAbility");
206         connectAbilityMgr_ = std::make_shared<BundleConnectAbilityMgr>();
207     }
208     if (bundleDistributedManager_ == nullptr) {
209         APP_LOGI("Create bundleDistributedManager");
210         bundleDistributedManager_ = std::make_shared<BundleDistributedManager>();
211     }
212 #endif
213 }
214 
InitDefaultApp()215 bool BundleMgrService::InitDefaultApp()
216 {
217 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
218     if (defaultAppHostImpl_ == nullptr) {
219         defaultAppHostImpl_ = new (std::nothrow) DefaultAppHostImpl();
220         if (defaultAppHostImpl_ == nullptr) {
221             APP_LOGE("create DefaultAppHostImpl failed.");
222             return false;
223         }
224     }
225 #endif
226     return true;
227 }
228 
InitAppControl()229 bool BundleMgrService::InitAppControl()
230 {
231 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
232     if (appControlManagerHostImpl_ == nullptr) {
233         appControlManagerHostImpl_ = new (std::nothrow) AppControlManagerHostImpl();
234         if (appControlManagerHostImpl_ == nullptr) {
235             APP_LOGE("create appControlManagerHostImpl failed.");
236             return false;
237         }
238     }
239 #endif
240     return true;
241 }
242 
InitQuickFixManager()243 bool BundleMgrService::InitQuickFixManager()
244 {
245 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
246     if (quickFixManagerHostImpl_ == nullptr) {
247         quickFixManagerHostImpl_ = new (std::nothrow) QuickFixManagerHostImpl();
248         if (quickFixManagerHostImpl_ == nullptr) {
249             APP_LOGE("create QuickFixManagerHostImpl failed.");
250             return false;
251         }
252     }
253 #endif
254     return true;
255 }
256 
InitOverlayManager()257 bool BundleMgrService::InitOverlayManager()
258 {
259 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
260     if (overlayManagerHostImpl_ == nullptr) {
261         overlayManagerHostImpl_ = new (std::nothrow) OverlayManagerHostImpl();
262         if (overlayManagerHostImpl_ == nullptr) {
263             APP_LOGE("create OverlayManagerHostImpl failed.");
264             return false;
265         }
266     }
267 #endif
268     return true;
269 }
270 
CreateBmsServiceDir()271 void BundleMgrService::CreateBmsServiceDir()
272 {
273     auto ret = InstalldClient::GetInstance()->Mkdir(
274         Constants::HAP_COPY_PATH, S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH,
275         Constants::FOUNDATION_UID, Constants::BMS_GID);
276     if (!ret) {
277         APP_LOGE("create dir failed");
278     }
279 }
280 
GetBundleInstaller() const281 sptr<IBundleInstaller> BundleMgrService::GetBundleInstaller() const
282 {
283     return installer_;
284 }
285 
RegisterDataMgr(std::shared_ptr<BundleDataMgr> dataMgrImpl)286 void BundleMgrService::RegisterDataMgr(std::shared_ptr<BundleDataMgr> dataMgrImpl)
287 {
288     dataMgr_ = dataMgrImpl;
289     if (dataMgr_ != nullptr) {
290         dataMgr_->AddUserId(Constants::DEFAULT_USERID);
291     }
292 }
293 
GetDataMgr() const294 const std::shared_ptr<BundleDataMgr> BundleMgrService::GetDataMgr() const
295 {
296     return dataMgr_;
297 }
298 
299 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
GetAgingMgr() const300 const std::shared_ptr<BundleAgingMgr> BundleMgrService::GetAgingMgr() const
301 {
302     return agingMgr_;
303 }
304 
GetConnectAbility() const305 const std::shared_ptr<BundleConnectAbilityMgr> BundleMgrService::GetConnectAbility() const
306 {
307     return connectAbilityMgr_;
308 }
309 
GetBundleDistributedManager() const310 const std::shared_ptr<BundleDistributedManager> BundleMgrService::GetBundleDistributedManager() const
311 {
312     return bundleDistributedManager_;
313 }
314 #endif
315 
SelfClean()316 void BundleMgrService::SelfClean()
317 {
318     if (ready_) {
319         ready_ = false;
320         if (registerToService_) {
321             registerToService_ = false;
322         }
323     }
324     aotLoopTask_.reset();
325 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
326     agingMgr_.reset();
327     connectAbilityMgr_.reset();
328     bundleDistributedManager_.reset();
329 #endif
330 }
331 
GetBundleUserMgr() const332 sptr<BundleUserMgrHostImpl> BundleMgrService::GetBundleUserMgr() const
333 {
334     return userMgrHost_;
335 }
336 
GetBmsParam() const337 const std::shared_ptr<BmsParam> BundleMgrService::GetBmsParam() const
338 {
339     return bmsParam_;
340 }
341 
342 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
GetDefaultAppProxy() const343 sptr<IDefaultApp> BundleMgrService::GetDefaultAppProxy() const
344 {
345     return defaultAppHostImpl_;
346 }
347 #endif
348 
349 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
GetAppControlProxy() const350 sptr<IAppControlMgr> BundleMgrService::GetAppControlProxy() const
351 {
352     return appControlManagerHostImpl_;
353 }
354 #endif
355 
356 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
GetQuickFixManagerProxy() const357 sptr<QuickFixManagerHostImpl> BundleMgrService::GetQuickFixManagerProxy() const
358 {
359     return quickFixManagerHostImpl_;
360 }
361 #endif
362 
363 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
GetOverlayManagerProxy() const364 sptr<IOverlayManager> BundleMgrService::GetOverlayManagerProxy() const
365 {
366     return overlayManagerHostImpl_;
367 }
368 #endif
369 
GetAOTLoopTask() const370 std::shared_ptr<AOTLoopTask> BundleMgrService::GetAOTLoopTask() const
371 {
372     return aotLoopTask_;
373 }
374 
CheckAllUser()375 void BundleMgrService::CheckAllUser()
376 {
377     if (dataMgr_ == nullptr) {
378         return;
379     }
380 
381     APP_LOGD("Check all user start.");
382     std::set<int32_t> userIds = dataMgr_->GetAllUser();
383     for (auto userId : userIds) {
384         if (userId == Constants::DEFAULT_USERID) {
385             continue;
386         }
387 
388         bool isExists = false;
389         if (AccountHelper::IsOsAccountExists(userId, isExists) != ERR_OK) {
390             APP_LOGE("Failed to query whether the user(%{public}d) exists.", userId);
391             continue;
392         }
393 
394         if (!isExists) {
395             userMgrHost_->RemoveUser(userId);
396         }
397     }
398     APP_LOGD("Check all user end");
399 }
400 
RegisterService()401 void BundleMgrService::RegisterService()
402 {
403     if (!registerToService_) {
404         if (!SystemAbilityHelper::AddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, host_)) {
405             APP_LOGE("fail to register to system ability manager");
406             return;
407         }
408         APP_LOGI("BundleMgrService register to sam success");
409         registerToService_ = true;
410     }
411 
412     PerfProfile::GetInstance().SetBmsLoadEndTime(GetTickCount());
413     PerfProfile::GetInstance().Dump();
414 }
415 
NotifyBundleScanStatus()416 void BundleMgrService::NotifyBundleScanStatus()
417 {
418     APP_LOGD("PublishCommonEvent for bundle scan finished");
419     AAFwk::Want want;
420     want.SetAction(COMMON_EVENT_BUNDLE_SCAN_FINISHED);
421     EventFwk::CommonEventData commonEventData { want };
422     if (!EventFwk::CommonEventManager::PublishCommonEvent(commonEventData)) {
423         notifyBundleScanStatus = true;
424         APP_LOGE("PublishCommonEvent for bundle scan finished failed.");
425     } else {
426         APP_LOGD("PublishCommonEvent for bundle scan finished succeed.");
427     }
428 }
429 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)430 void BundleMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
431 {
432     APP_LOGI("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
433     if (COMMON_EVENT_SERVICE_ID == systemAbilityId && notifyBundleScanStatus) {
434         NotifyBundleScanStatus();
435     }
436     if (BUNDLE_BROKER_SERVICE_ABILITY_ID == systemAbilityId) {
437         if (host_ != nullptr) {
438             isBrokerServiceStarted_ = true;
439             host_->SetBrokerServiceStatus(true);
440         }
441     }
442 }
443 
Hidump(const std::vector<std::string> & args,std::string & result) const444 bool BundleMgrService::Hidump(const std::vector<std::string> &args, std::string& result) const
445 {
446     if (hidumpHelper_ && hidumpHelper_->Dump(args, result)) {
447         return true;
448     }
449 
450     APP_LOGD("HidumpHelper failed");
451     return false;
452 }
453 
IsBrokerServiceStarted() const454 bool BundleMgrService::IsBrokerServiceStarted() const
455 {
456     return isBrokerServiceStarted_;
457 }
458 }  // namespace AppExecFwk
459 }  // namespace OHOS
460