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