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 "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 {
34 REGISTER_SYSTEM_ABILITY_BY_ID(InstalldService, INSTALLD_SERVICE_ID, true);
35
InstalldService(int32_t saId,bool runOnCreate)36 InstalldService::InstalldService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
37 {
38 APP_LOGI("installd service instance is created");
39 }
40
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 }
60
OnStop()61 void InstalldService::OnStop()
62 {
63 Stop();
64 APP_LOGI("installd OnStop");
65 }
66
Init()67 bool InstalldService::Init()
68 {
69 if (isReady_) {
70 APP_LOGW("the installd service is already ready");
71 return false;
72 }
73 // installd service need mask 000
74 umask(Constants::INSTALLD_UMASK);
75 hostImpl_ = new (std::nothrow) InstalldHostImpl();
76 if (hostImpl_ == nullptr) {
77 APP_LOGE("InstalldHostImpl Init failed");
78 return false;
79 }
80 if (!InitDir(Constants::HAP_COPY_PATH)) {
81 APP_LOGI("HAP_COPY_PATH is already exists");
82 }
83 return true;
84 }
85
InitDir(const std::string & path)86 bool InstalldService::InitDir(const std::string &path)
87 {
88 if (InstalldOperator::IsExistDir(path)) {
89 APP_LOGI("Path already exists");
90 return false;
91 }
92 if (!InstalldOperator::MkOwnerDir(path, true, Constants::FOUNDATION_UID, Constants::BMS_GID)) {
93 APP_LOGE("create path failed, errno : %{public}d", errno);
94 return false;
95 }
96 return true;
97 }
98
Start()99 void InstalldService::Start()
100 {
101 if (!Init()) {
102 APP_LOGE("init fail");
103 return;
104 }
105 isReady_ = true;
106 APP_LOGI("installd service start successfully");
107 }
108
Stop()109 void InstalldService::Stop()
110 {
111 if (!isReady_) {
112 APP_LOGW("the installd service is already stopped");
113 return;
114 }
115 // remove installd service from system ability manager.
116 // since we can't handle the fail case, just ignore the result.
117 SystemAbilityHelper::UnloadSystemAbility(INSTALLD_SERVICE_ID);
118 isReady_ = false;
119 APP_LOGI("installd service stop successfully");
120 }
121 } // namespace AppExecFwk
122 } // namespace OHOS