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