1 /*
2 * Copyright (c) 2021-2025 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_tag_wrapper.h"
25 #include "bundle_constants.h"
26 #include "bundle_resource/bundle_resource_constants.h"
27 #include "bundle_service_constants.h"
28 #include "installd/installd_operator.h"
29 #include "mem_mgr_client.h"
30 #include "system_ability_definition.h"
31 #include "system_ability_helper.h"
32
33 #ifdef DFX_SIGDUMP_HANDLER_ENABLE
34 #include "dfx_sigdump_handler.h"
35 #endif
36
37 using namespace std::chrono_literals;
38
39 namespace OHOS {
40 namespace AppExecFwk {
41 namespace {
42 constexpr uint8_t INSTALLD_UMASK = 0000;
43 constexpr uint8_t SA_SERVICE = 1;
44 constexpr uint8_t SA_START = 1;
45 constexpr uint8_t SA_DIED = 0;
46 }
47 REGISTER_SYSTEM_ABILITY_BY_ID(InstalldService, INSTALLD_SERVICE_ID, true);
48
InstalldService(int32_t saId,bool runOnCreate)49 InstalldService::InstalldService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
50 {
51 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "installd service instance created");
52 }
53
InstalldService()54 InstalldService::InstalldService() : SystemAbility(INSTALLD_SERVICE_ID, true)
55 {
56 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "installd service instance created");
57 }
58
~InstalldService()59 InstalldService::~InstalldService()
60 {
61 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "installd service instance destroyed");
62 }
63
OnStart()64 void InstalldService::OnStart()
65 {
66 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "installd OnStart");
67 InstalldOperator::AddDeleteDfx(Constants::BUNDLE_CODE_DIR);
68 Start();
69 if (!Publish(hostImpl_)) {
70 LOG_E(BMS_TAG_INSTALLD, "Publish failed");
71 }
72 #ifdef DFX_SIGDUMP_HANDLER_ENABLE
73 InitSigDumpHandler();
74 #endif
75 }
76
OnStop()77 void InstalldService::OnStop()
78 {
79 Stop();
80 #ifdef DFX_SIGDUMP_HANDLER_ENABLE
81 DeinitSigDumpHandler();
82 #endif
83 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "installd OnStop");
84 }
85
Init()86 bool InstalldService::Init()
87 {
88 if (isReady_) {
89 LOG_NOFUNC_W(BMS_TAG_INSTALLD, "the installd service is already ready");
90 return false;
91 }
92 // installd service need mask 000
93 umask(INSTALLD_UMASK);
94 hostImpl_ = new (std::nothrow) InstalldHostImpl();
95 if (hostImpl_ == nullptr) {
96 LOG_NOFUNC_E(BMS_TAG_INSTALLD, "InstalldHostImpl Init failed");
97 return false;
98 }
99 if (!InitDir(ServiceConstants::HAP_COPY_PATH)) {
100 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "HAP_COPY_PATH is already exists");
101 }
102 if (!InitDir(BundleResourceConstants::BUNDLE_RESOURCE_RDB_PATH)) {
103 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "BUNDLE_RESOURCE_RDB_PATH is already exists");
104 }
105 return true;
106 }
107
InitDir(const std::string & path)108 bool InstalldService::InitDir(const std::string &path)
109 {
110 if (InstalldOperator::IsExistDir(path)) {
111 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "Path already exists");
112 return false;
113 }
114 if (!InstalldOperator::MkOwnerDir(path, true, Constants::FOUNDATION_UID, ServiceConstants::BMS_GID)) {
115 LOG_NOFUNC_E(BMS_TAG_INSTALLD, "create path failed errno:%{public}d", errno);
116 return false;
117 }
118 return true;
119 }
120
Start()121 void InstalldService::Start()
122 {
123 if (!Init()) {
124 LOG_NOFUNC_E(BMS_TAG_INSTALLD, "init fail");
125 return;
126 }
127 isReady_ = true;
128 AddSystemAbilityListener(MEMORY_MANAGER_SA_ID);
129 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "installd service start successfully");
130 }
131
Stop()132 void InstalldService::Stop()
133 {
134 if (!isReady_) {
135 LOG_NOFUNC_W(BMS_TAG_INSTALLD, "the installd service is already stopped");
136 return;
137 }
138 isReady_ = false;
139 Memory::MemMgrClient::GetInstance().NotifyProcessStatus(
140 getpid(), SA_SERVICE, SA_DIED, INSTALLD_SERVICE_ID);
141 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "installd service stop successfully");
142 }
143
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)144 void InstalldService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
145 {
146 LOG_NOFUNC_I(BMS_TAG_INSTALLD, "OnAddSystemAbility: %{public}d", systemAbilityId);
147 if (MEMORY_MANAGER_SA_ID == systemAbilityId) {
148 auto ret = Memory::MemMgrClient::GetInstance().NotifyProcessStatus(
149 getpid(), SA_SERVICE, SA_START, INSTALLD_SERVICE_ID);
150 if (ret == 0 && hostImpl_ != nullptr) {
151 hostImpl_->SetMemMgrStatus(true);
152 }
153 }
154 }
155 } // namespace AppExecFwk
156 } // namespace OHOS