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 <thread>
20 #include <sys/stat.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include "app_log_wrapper.h"
25 #include "system_ability_definition.h"
26 #include "system_ability_helper.h"
27 #include "bundle_constants.h"
28
29 using namespace std::chrono_literals;
30
31 namespace OHOS {
32 namespace AppExecFwk {
33
InstalldService()34 InstalldService::InstalldService()
35 {
36 APP_LOGI("installd service instance is created");
37 }
38
~InstalldService()39 InstalldService::~InstalldService()
40 {
41 APP_LOGI("installd service instance is destroyed");
42 }
43
Init()44 bool InstalldService::Init()
45 {
46 if (isReady_) {
47 APP_LOGW("the installd service is already ready");
48 return false;
49 }
50 // installd service need mask 000
51 umask(Constants::INSTALLD_UMASK);
52 hostImpl_ = new (std::nothrow) InstalldHostImpl();
53 if (hostImpl_ == nullptr) {
54 APP_LOGI("InstalldHostImpl Init failed");
55 return false;
56 }
57 if (!InitDir(Constants::THIRD_PARTY_APP_INSTALL_PATH)) {
58 APP_LOGI("THIRD_PARTY_APP_INSTALL_PATH Path already exists");
59 }
60 if (!InitDir(Constants::SYSTEM_APP_INSTALL_PATH)) {
61 APP_LOGI("SYSTEM_APP_INSTALL_PATH is already exists");
62 }
63 if (!InitDir(Constants::EXTRACT_TMP_PATH)) {
64 APP_LOGI("EXTRACT_TMP_PATH is already exists");
65 }
66 if (!InitDir(Constants::HAP_COPY_PATH)) {
67 APP_LOGI("HAP_COPY_PATH is already exists");
68 }
69 return true;
70 }
71
InitDir(const std::string & path)72 bool InstalldService::InitDir(const std::string &path)
73 {
74 if (InstalldOperator::IsExistDir(path)) {
75 APP_LOGI("Path already exists");
76 return false;
77 }
78 if (!InstalldOperator::MkOwnerDir(path, true, Constants::BMS_UID, Constants::BMS_GID)) {
79 APP_LOGE("create path failed %{public}s", strerror(errno));
80 return false;
81 }
82 return true;
83 }
84
Start()85 void InstalldService::Start()
86 {
87 if (!(Init())) {
88 APP_LOGE("init fail");
89 return;
90 }
91 // add installd service to system ability manager.
92 // need to retry some times due to installd start faster than system ability manager.
93 if (!SystemAbilityHelper::AddSystemAbility(INSTALLD_SERVICE_ID, hostImpl_)) {
94 APP_LOGW("installd service fail to register into system ability manager, retry it");
95 int32_t tryTimes = 3;
96 bool isSuccess = false;
97 for (int32_t i = 0; i < tryTimes; i++) {
98 std::this_thread::sleep_for(100ms);
99 if (SystemAbilityHelper::AddSystemAbility(INSTALLD_SERVICE_ID, hostImpl_)) {
100 isSuccess = true;
101 break;
102 }
103 }
104 if (!isSuccess) {
105 APP_LOGE("installd service fail to register into system ability manager");
106 return;
107 }
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::RemoveSystemAbility(INSTALLD_SERVICE_ID);
122 isReady_ = false;
123 APP_LOGI("installd service stop successfully");
124 }
125
126 } // namespace AppExecFwk
127 } // namespace OHOS