• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::THIRD_PARTY_APP_INSTALL_PATH)) {
57         APP_LOGI("THIRD_PARTY_APP_INSTALL_PATH Path already exists");
58     }
59     if (!InitDir(Constants::SYSTEM_APP_INSTALL_PATH)) {
60         APP_LOGI("SYSTEM_APP_INSTALL_PATH is already exists");
61     }
62     if (!InitDir(Constants::EXTRACT_TMP_PATH)) {
63         APP_LOGI("EXTRACT_TMP_PATH is already exists");
64     }
65     if (!InitDir(Constants::HAP_COPY_PATH)) {
66         APP_LOGI("HAP_COPY_PATH is already exists");
67     }
68     return true;
69 }
70 
InitDir(const std::string & path)71 bool InstalldService::InitDir(const std::string &path)
72 {
73     if (InstalldOperator::IsExistDir(path)) {
74         APP_LOGI("Path already exists");
75         return false;
76     }
77     if (!InstalldOperator::MkOwnerDir(path, true, Constants::BMS_UID, Constants::BMS_GID)) {
78         APP_LOGE("create path failed, errno : %{public}d", errno);
79         return false;
80     }
81     return true;
82 }
83 
Start()84 void InstalldService::Start()
85 {
86     if (!(Init())) {
87         APP_LOGE("init fail");
88         return;
89     }
90     // add installd service to system ability manager.
91     // need to retry some times due to installd start faster than system ability manager.
92     if (!SystemAbilityHelper::AddSystemAbility(INSTALLD_SERVICE_ID, hostImpl_)) {
93         APP_LOGW("installd service fail to register into system ability manager, retry it");
94         int32_t tryTimes = 3;
95         bool isSuccess = false;
96         for (int32_t i = 0; i < tryTimes; i++) {
97             std::this_thread::sleep_for(100ms);
98             if (SystemAbilityHelper::AddSystemAbility(INSTALLD_SERVICE_ID, hostImpl_)) {
99                 isSuccess = true;
100                 break;
101             }
102         }
103         if (!isSuccess) {
104             APP_LOGE("installd service fail to register into system ability manager");
105             return;
106         }
107     }
108     isReady_ = true;
109     APP_LOGI("installd service start successfully");
110 }
111 
Stop()112 void InstalldService::Stop()
113 {
114     if (!isReady_) {
115         APP_LOGW("the installd service is already stopped");
116         return;
117     }
118     // remove installd service from system ability manager.
119     // since we can't handle the fail case, just ignore the result.
120     SystemAbilityHelper::RemoveSystemAbility(INSTALLD_SERVICE_ID);
121     isReady_ = false;
122     APP_LOGI("installd service stop successfully");
123 }
124 }  // namespace AppExecFwk
125 }  // namespace OHOS