1 /*
2 * Copyright (c) 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 "installd/installd_service.h"
17
18 #include <chrono>
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <thread>
23
24 #include "app_log_wrapper.h"
25 #include "bundle_constants.h"
26 #include "installd/installd_operator.h"
27 #include "system_ability_definition.h"
28 #include "system_ability_helper.h"
29
30 using namespace std::chrono_literals;
31
32 namespace OHOS {
33 namespace AppExecFwk {
InstalldService()34 InstalldService::InstalldService() : SystemAbility(INSTALLD_SERVICE_ID, true)
35 {
36 APP_LOGI("installd service instance is created");
37 }
38
~InstalldService()39 InstalldService::~InstalldService()
40 {
41 APP_LOGI("installd service instance is destroyed");
42 }
43
OnStart()44 void InstalldService::OnStart()
45 {
46 APP_LOGI("installd OnStart");
47 Start();
48 if (!Publish(hostImpl_)) {
49 APP_LOGE("Publish failed");
50 }
51 }
52
OnStop()53 void InstalldService::OnStop()
54 {
55 Stop();
56 APP_LOGI("installd OnStop");
57 }
58
Init()59 bool InstalldService::Init()
60 {
61 if (isReady_) {
62 APP_LOGW("the installd service is already ready");
63 return false;
64 }
65 // installd service need mask 000
66 umask(Constants::INSTALLD_UMASK);
67 hostImpl_ = new (std::nothrow) InstalldHostImpl();
68 if (hostImpl_ == nullptr) {
69 APP_LOGE("InstalldHostImpl Init failed");
70 return false;
71 }
72 if (!InitDir(Constants::HAP_COPY_PATH)) {
73 APP_LOGI("HAP_COPY_PATH is already exists");
74 }
75 return true;
76 }
77
InitDir(const std::string & path)78 bool InstalldService::InitDir(const std::string &path)
79 {
80 if (InstalldOperator::IsExistDir(path)) {
81 APP_LOGI("Path already exists");
82 return false;
83 }
84 if (!InstalldOperator::MkOwnerDir(path, true, Constants::FOUNDATION_UID, Constants::BMS_GID)) {
85 APP_LOGE("create path failed, errno : %{public}d", errno);
86 return false;
87 }
88 return true;
89 }
90
Start()91 void InstalldService::Start()
92 {
93 if (!(Init())) {
94 APP_LOGE("init fail");
95 return;
96 }
97 // add installd service to system ability manager.
98 // need to retry some times due to installd start faster than system ability manager.
99 if (!SystemAbilityHelper::AddSystemAbility(INSTALLD_SERVICE_ID, hostImpl_)) {
100 APP_LOGE("installd service fail to register into system ability manager");
101 return;
102 }
103 isReady_ = true;
104 APP_LOGI("installd service start successfully");
105 }
106
Stop()107 void InstalldService::Stop()
108 {
109 if (!isReady_) {
110 APP_LOGW("the installd service is already stopped");
111 return;
112 }
113 // remove installd service from system ability manager.
114 // since we can't handle the fail case, just ignore the result.
115 SystemAbilityHelper::RemoveSystemAbility(INSTALLD_SERVICE_ID);
116 isReady_ = false;
117 APP_LOGI("installd service stop successfully");
118 }
119 } // namespace AppExecFwk
120 } // namespace OHOS
121