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