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