• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "sys_installer_kits_impl.h"
17 
18 #include <unistd.h>
19 
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "log/log.h"
23 #include "securec.h"
24 #include "system_ability_definition.h"
25 #include "utils.h"
26 
27 #include "isys_installer.h"
28 #include "isys_installer_callback.h"
29 #include "sys_installer_callback.h"
30 #include "sys_installer_common.h"
31 #include "sys_installer_load_callback.h"
32 #include "sys_installer_proxy.h"
33 
34 namespace OHOS {
35 namespace SysInstaller {
36 using namespace Updater;
37 constexpr int GROUP_UPDATE_AUTHORITY = 6666;
38 constexpr int LOAD_SA_TIMEOUT_MS = 3;
39 
GetInstance()40 SysInstallerKitsImpl &SysInstallerKitsImpl::GetInstance()
41 {
42     static SysInstallerKitsImpl instance;
43     return instance;
44 }
45 
ResetService(const wptr<IRemoteObject> & remote)46 void SysInstallerKitsImpl::ResetService(const wptr<IRemoteObject>& remote)
47 {
48     LOG(INFO) << "Remote is dead, reset service instance";
49 
50     std::lock_guard<std::mutex> lock(sysInstallerLock_);
51     if (sysInstaller_ != nullptr) {
52         sptr<IRemoteObject> object = sysInstaller_->AsObject();
53         if ((object != nullptr) && (remote == object)) {
54             object->RemoveDeathRecipient(deathRecipient_);
55             sysInstaller_ = nullptr;
56         }
57     }
58 }
59 
GetService()60 sptr<ISysInstaller> SysInstallerKitsImpl::GetService()
61 {
62     std::lock_guard<std::mutex> lock(sysInstallerLock_);
63     if (sysInstaller_ != nullptr) {
64         return sysInstaller_;
65     }
66 
67     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
68     if (samgr == nullptr) {
69         LOG(ERROR) << "Get samgr failed";
70         return nullptr;
71     }
72     sptr<IRemoteObject> object = samgr->GetSystemAbility(SYS_INSTALLER_DISTRIBUTED_SERVICE_ID);
73     if (object == nullptr) {
74         LOG(ERROR) << "Get update object from samgr failed";
75         return nullptr;
76     }
77 
78     if (deathRecipient_ == nullptr) {
79         deathRecipient_ = new DeathRecipient();
80     }
81 
82     if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
83         LOG(ERROR) << "Failed to add death recipient";
84     }
85 
86     LOG(INFO) << "get remote object ok";
87     sysInstaller_ = iface_cast<ISysInstaller>(object);
88     if (sysInstaller_ == nullptr) {
89         LOG(ERROR) << "account iface_cast failed";
90     }
91     return sysInstaller_;
92 }
93 
OnRemoteDied(const wptr<IRemoteObject> & remote)94 void SysInstallerKitsImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
95 {
96     SysInstallerKitsImpl::GetInstance().ResetService(remote);
97 }
98 
Init()99 int32_t SysInstallerKitsImpl::Init()
100 {
101     std::lock_guard<std::mutex> lock(sysInstallerLock_);
102     if (sysInstaller_ != nullptr) {
103         LOG(INFO) << "already init";
104         return 0;
105     }
106     (void)Utils::MkdirRecursive(SYS_LOG_DIR, 0777); // 0777 : rwxrwxrwx
107     InitUpdaterLogger("SysInstallerClient", SYS_LOG_FILE, SYS_STAGE_FILE, SYS_ERROR_FILE);
108     (void)chown(SYS_LOG_FILE, GROUP_UPDATE_AUTHORITY, GROUP_UPDATE_AUTHORITY);
109     (void)chown(SYS_STAGE_FILE, GROUP_UPDATE_AUTHORITY, GROUP_UPDATE_AUTHORITY);
110     (void)chown(SYS_ERROR_FILE, GROUP_UPDATE_AUTHORITY, GROUP_UPDATE_AUTHORITY);
111 
112     // 构造步骤1的SystemAbilityLoadCallbackStub子类的实例
113     sptr<SysInstallerLoadCallback> loadCallback_ = new SysInstallerLoadCallback();
114     // 调用LoadSystemAbility方法
115     sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
116     if (sm == nullptr) {
117         LOG(ERROR) << "GetSystemAbilityManager samgr object null";
118         return -1;
119     }
120     int32_t result = sm->LoadSystemAbility(SYS_INSTALLER_DISTRIBUTED_SERVICE_ID, loadCallback_);
121     if (result != ERR_OK) {
122         LOG(ERROR) << "systemAbilityId " << SYS_INSTALLER_DISTRIBUTED_SERVICE_ID <<
123             " load failed, result code:" << result;
124         return -1;
125     }
126 
127     std::unique_lock<std::mutex> callbackLock(serviceMutex_);
128     serviceCv_.wait_for(callbackLock, std::chrono::seconds(LOAD_SA_TIMEOUT_MS));
129     return 0;
130 }
131 
SysInstallerInit()132 int32_t SysInstallerKitsImpl::SysInstallerInit()
133 {
134     LOG(INFO) << "SysInstallerInit";
135     int ret = Init();
136     if (ret != 0) {
137         LOG(ERROR) << "Init failed";
138         return ret;
139     }
140 
141     auto updateService = GetService();
142     if (updateService == nullptr) {
143         LOG(ERROR) << "Get updateService failed";
144         return -1;
145     }
146     updateService->SysInstallerInit();
147     return 0;
148 }
149 
StartUpdatePackageZip(const std::string & pkgPath)150 int32_t SysInstallerKitsImpl::StartUpdatePackageZip(const std::string &pkgPath)
151 {
152     LOG(INFO) << "StartUpdatePackageZip";
153     auto updateService = GetService();
154     if (updateService == nullptr) {
155         LOG(ERROR) << "Get updateService failed";
156         return -1;
157     }
158     int32_t ret = updateService->StartUpdatePackageZip(pkgPath);
159     LOG(INFO) << "StartUpdatePackageZip ret:" << ret;
160     return ret;
161 }
162 
SetUpdateCallback(sptr<ISysInstallerCallbackFunc> callback)163 int32_t SysInstallerKitsImpl::SetUpdateCallback(sptr<ISysInstallerCallbackFunc> callback)
164 {
165     LOG(INFO) << "SetUpdateCallback";
166     if (callback == nullptr) {
167         LOG(ERROR) << "callback null";
168         return -1;
169     }
170 
171     auto updateService = GetService();
172     if (updateService == nullptr) {
173         LOG(ERROR) << "Get updateService failed";
174         return -1;
175     }
176 
177     if (updateCallBack_ == nullptr) {
178         updateCallBack_ = new SysInstallerCallback;
179     }
180     static_cast<SysInstallerCallback *>(updateCallBack_.GetRefPtr())->RegisterCallback(callback);
181     return updateService->SetUpdateCallback(updateCallBack_);
182 }
183 
GetUpdateStatus()184 int32_t SysInstallerKitsImpl::GetUpdateStatus()
185 {
186     LOG(INFO) << "GetUpdateStatus";
187     auto updateService = GetService();
188     if (updateService == nullptr) {
189         LOG(ERROR) << "Get updateService failed";
190         return -1;
191     }
192     return updateService->GetUpdateStatus();
193 }
194 
StartUpdateParaZip(const std::string & pkgPath,const std::string & location,const std::string & cfgDir)195 int32_t SysInstallerKitsImpl::StartUpdateParaZip(const std::string &pkgPath,
196     const std::string &location, const std::string &cfgDir)
197 {
198     LOG(INFO) << "StartUpdateParaZip";
199     auto updateService = GetService();
200     if (updateService == nullptr) {
201         LOG(ERROR) << "Get updateService failed";
202         return -1;
203     }
204     int32_t ret = updateService->StartUpdateParaZip(pkgPath, location, cfgDir);
205     LOG(INFO) << "StartUpdateParaZip ret:" << ret;
206 #ifdef UPDATER_UT
207     return -1;
208 #endif
209     return ret;
210 }
211 
StartDeleteParaZip(const std::string & location,const std::string & cfgDir)212 int32_t SysInstallerKitsImpl::StartDeleteParaZip(const std::string &location, const std::string &cfgDir)
213 {
214     LOG(INFO) << "StartDeleteParaZip";
215     auto updateService = GetService();
216     if (updateService == nullptr) {
217         LOG(ERROR) << "Get updateService failed";
218         return -1;
219     }
220     int32_t ret = updateService->StartDeleteParaZip(location, cfgDir);
221     LOG(INFO) << "StartDeleteParaZip ret:" << ret;
222 #ifdef UPDATER_UT
223     return -1;
224 #endif
225     return ret;
226 }
227 
AccDecompressAndVerifyPkg(const std::string & srcPath,const std::string & dstPath,const uint32_t type)228 int32_t SysInstallerKitsImpl::AccDecompressAndVerifyPkg(const std::string &srcPath,
229     const std::string &dstPath, const uint32_t type)
230 {
231     LOG(INFO) << "AccDecompressAndVerifyPkg";
232     auto updateService = GetService();
233     if (updateService == nullptr) {
234         LOG(ERROR) << "Get updateService failed";
235         return -1;
236     }
237     int32_t ret = updateService->AccDecompressAndVerifyPkg(srcPath, dstPath, type);
238     LOG(INFO) << "AccDecompressAndVerifyPkg ret:" << ret;
239 #ifdef UPDATER_UT
240     return -1;
241 #endif
242     return ret;
243 }
244 
AccDeleteDir(const std::string & dstPath)245 int32_t SysInstallerKitsImpl::AccDeleteDir(const std::string &dstPath)
246 {
247     LOG(INFO) << "AccDeleteDir";
248     auto updateService = GetService();
249     if (updateService == nullptr) {
250         LOG(ERROR) << "Get updateService failed";
251         return -1;
252     }
253     int32_t ret = updateService->AccDeleteDir(dstPath);
254     LOG(INFO) << "AccDeleteDir ret:" << ret;
255 #ifdef UPDATER_UT
256     return -1;
257 #endif
258     return ret;
259 }
260 
LoadServiceSuccess()261 void SysInstallerKitsImpl::LoadServiceSuccess()
262 {
263     serviceCv_.notify_all();
264 }
265 
LoadServiceFail()266 void SysInstallerKitsImpl::LoadServiceFail()
267 {
268     serviceCv_.notify_all();
269 }
270 }
271 } // namespace OHOS
272