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 #include <sys/stat.h>
20
21 #include "if_system_ability_manager.h"
22 #include "iservice_registry.h"
23 #include "log/log.h"
24 #include "securec.h"
25 #include "system_ability_definition.h"
26 #include "utils.h"
27
28 #include "isys_installer.h"
29 #include "isys_installer_callback.h"
30 #include "sys_installer_callback.h"
31 #include "sys_installer_common.h"
32 #include "sys_installer_load_callback.h"
33 #include "sys_installer_proxy.h"
34 #include "buffer_info_parcel.h"
35
36 namespace OHOS {
37 namespace SysInstaller {
38 using namespace Updater;
39 using namespace Updater::Utils;
40 using namespace Utils;
41 constexpr int LOAD_SA_TIMEOUT_MS = 3;
42
GetInstance()43 SysInstallerKitsImpl &SysInstallerKitsImpl::GetInstance()
44 {
45 static SysInstallerKitsImpl instance;
46 return instance;
47 }
48
ResetService(const wptr<IRemoteObject> & remote)49 void SysInstallerKitsImpl::ResetService(const wptr<IRemoteObject>& remote)
50 {
51 LOG(INFO) << "Remote is dead, reset service instance";
52
53 std::lock_guard<std::mutex> lock(sysInstallerLock_);
54 if (sysInstaller_ != nullptr) {
55 sptr<IRemoteObject> object = sysInstaller_->AsObject();
56 if ((object != nullptr) && (remote == object)) {
57 object->RemoveDeathRecipient(deathRecipient_);
58 sysInstaller_ = nullptr;
59 }
60 }
61 }
62
GetService()63 sptr<ISysInstaller> SysInstallerKitsImpl::GetService()
64 {
65 std::lock_guard<std::mutex> lock(sysInstallerLock_);
66 if (sysInstaller_ != nullptr) {
67 return sysInstaller_;
68 }
69
70 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
71 if (samgr == nullptr) {
72 LOG(ERROR) << "Get samgr failed";
73 return nullptr;
74 }
75 sptr<IRemoteObject> object = samgr->GetSystemAbility(SYS_INSTALLER_DISTRIBUTED_SERVICE_ID);
76 if (object == nullptr) {
77 LOG(ERROR) << "Get update object from samgr failed";
78 return nullptr;
79 }
80
81 if (deathRecipient_ == nullptr) {
82 deathRecipient_ = new DeathRecipient();
83 }
84
85 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
86 LOG(ERROR) << "Failed to add death recipient";
87 }
88
89 LOG(INFO) << "get remote object ok";
90 sysInstaller_ = iface_cast<ISysInstaller>(object);
91 if (sysInstaller_ == nullptr) {
92 LOG(ERROR) << "account iface_cast failed";
93 }
94 return sysInstaller_;
95 }
96
OnRemoteDied(const wptr<IRemoteObject> & remote)97 void SysInstallerKitsImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
98 {
99 SysInstallerKitsImpl::GetInstance().ResetService(remote);
100 }
101
Init()102 int32_t SysInstallerKitsImpl::Init()
103 {
104 std::lock_guard<std::mutex> lock(sysInstallerLock_);
105 if (sysInstaller_ != nullptr) {
106 LOG(INFO) << "already init";
107 return 0;
108 }
109 (void)Utils::MkdirRecursive(SYS_LOG_DIR, 0777); // 0777 : rwxrwxrwx
110 InitUpdaterLogger("SysInstallerClient", SYS_LOG_FILE, SYS_STAGE_FILE, SYS_ERROR_FILE);
111 mode_t mode = 0664; // 0664 : -rw-rw-r--
112 (void)chown(SYS_LOG_FILE, USER_ROOT_AUTHORITY, GROUP_ROOT_AUTHORITY);
113 (void)chown(SYS_STAGE_FILE, USER_ROOT_AUTHORITY, GROUP_ROOT_AUTHORITY);
114 (void)chown(SYS_ERROR_FILE, USER_ROOT_AUTHORITY, GROUP_ROOT_AUTHORITY);
115 (void)chmod(SYS_LOG_FILE, mode);
116 (void)chmod(SYS_STAGE_FILE, mode);
117 (void)chmod(SYS_ERROR_FILE, mode);
118
119 // 构造步骤1的SystemAbilityLoadCallbackStub子类的实例
120 sptr<SysInstallerLoadCallback> loadCallback_ = new SysInstallerLoadCallback();
121 // 调用LoadSystemAbility方法
122 sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
123 if (sm == nullptr) {
124 LOG(ERROR) << "GetSystemAbilityManager samgr object null";
125 return -1;
126 }
127 int32_t result = sm->LoadSystemAbility(SYS_INSTALLER_DISTRIBUTED_SERVICE_ID, loadCallback_);
128 if (result != ERR_OK) {
129 LOG(ERROR) << "systemAbilityId " << SYS_INSTALLER_DISTRIBUTED_SERVICE_ID <<
130 " load failed, result code:" << result;
131 return -1;
132 }
133
134 std::unique_lock<std::mutex> callbackLock(serviceMutex_);
135 serviceCv_.wait_for(callbackLock, std::chrono::seconds(LOAD_SA_TIMEOUT_MS));
136 return 0;
137 }
138
SysInstallerInit(const std::string & taskId,bool bStreamUpgrade)139 int32_t SysInstallerKitsImpl::SysInstallerInit(const std::string &taskId, bool bStreamUpgrade)
140 {
141 LOG(INFO) << "SysInstallerInit";
142 int ret = Init();
143 if (ret != 0) {
144 LOG(ERROR) << "Init failed";
145 return ret;
146 }
147
148 auto updateService = GetService();
149 if (updateService == nullptr) {
150 LOG(ERROR) << "Get updateService failed";
151 return -1;
152 }
153 updateService->SysInstallerInit(taskId, bStreamUpgrade);
154 return 0;
155 }
156
StartUpdatePackageZip(const std::string & taskId,const std::string & pkgPath)157 int32_t SysInstallerKitsImpl::StartUpdatePackageZip(const std::string &taskId, const std::string &pkgPath)
158 {
159 LOG(INFO) << "StartUpdatePackageZip";
160 auto updateService = GetService();
161 if (updateService == nullptr) {
162 LOG(ERROR) << "Get updateService failed";
163 return -1;
164 }
165 int32_t ret = updateService->StartUpdatePackageZip(taskId, pkgPath);
166 LOG(INFO) << "StartUpdatePackageZip ret:" << ret;
167 return ret;
168 }
169
StartStreamUpdate()170 int32_t SysInstallerKitsImpl::StartStreamUpdate()
171 {
172 LOG(INFO) << "StartStreamUpdate";
173 auto updateService = GetService();
174 if (updateService == nullptr) {
175 LOG(ERROR) << "Get updateService failed";
176 return -1;
177 }
178 int32_t ret = updateService->StartStreamUpdate();
179 LOG(INFO) << "StartStreamUpdate ret:" << ret;
180 return ret;
181 }
182
StopStreamUpdate()183 int32_t SysInstallerKitsImpl::StopStreamUpdate()
184 {
185 LOG(INFO) << "StopStreamUpdate";
186 auto updateService = GetService();
187 if (updateService == nullptr) {
188 LOG(ERROR) << "Get updateService failed";
189 return -1;
190 }
191 int32_t ret = updateService->StopStreamUpdate();
192 LOG(INFO) << "StopStreamUpdate ret:" << ret;
193 return ret;
194 }
195
ProcessStreamData(const uint8_t * buffer,uint32_t size)196 int32_t SysInstallerKitsImpl::ProcessStreamData(const uint8_t *buffer, uint32_t size)
197 {
198 LOG(INFO) << "ProcessStreamData";
199 auto updateService = GetService();
200 BufferInfoParcel bufferParcel;
201 if (updateService == nullptr) {
202 LOG(ERROR) << "Get updateService failed";
203 return -1;
204 }
205 bufferParcel.bufferInfo.buffer = buffer;
206 bufferParcel.bufferInfo.size = size;
207 int32_t ret = updateService->ProcessStreamData(bufferParcel);
208 LOG(INFO) << "ProcessStreamData ret:" << ret;
209 return ret;
210 }
211
SetUpdateCallback(const std::string & taskId,sptr<ISysInstallerCallbackFunc> callback)212 int32_t SysInstallerKitsImpl::SetUpdateCallback(const std::string &taskId, sptr<ISysInstallerCallbackFunc> callback)
213 {
214 LOG(INFO) << "SetUpdateCallback";
215 if (callback == nullptr) {
216 LOG(ERROR) << "callback null";
217 return -1;
218 }
219
220 auto updateService = GetService();
221 if (updateService == nullptr) {
222 LOG(ERROR) << "Get updateService failed";
223 return -1;
224 }
225
226 sptr<ISysInstallerCallback> updateCallBack = sptr<SysInstallerCallback>::MakeSptr(callback);
227 if (updateCallBack == nullptr) {
228 LOG(ERROR) << "updateCallBack nullptr";
229 return -1;
230 }
231
232 return updateService->SetUpdateCallback(taskId, updateCallBack);
233 }
234
GetUpdateStatus(const std::string & taskId)235 int32_t SysInstallerKitsImpl::GetUpdateStatus(const std::string &taskId)
236 {
237 LOG(INFO) << "GetUpdateStatus";
238 auto updateService = GetService();
239 if (updateService == nullptr) {
240 LOG(ERROR) << "Get updateService failed";
241 return -1;
242 }
243 return updateService->GetUpdateStatus(taskId);
244 }
245
StartUpdateParaZip(const std::string & taskId,const std::string & pkgPath,const std::string & location,const std::string & cfgDir)246 int32_t SysInstallerKitsImpl::StartUpdateParaZip(const std::string &taskId, const std::string &pkgPath,
247 const std::string &location, const std::string &cfgDir)
248 {
249 LOG(INFO) << "StartUpdateParaZip";
250 auto updateService = GetService();
251 if (updateService == nullptr) {
252 LOG(ERROR) << "Get updateService failed";
253 return -1;
254 }
255 int32_t ret = updateService->StartUpdateParaZip(taskId, pkgPath, location, cfgDir);
256 LOG(INFO) << "StartUpdateParaZip ret:" << ret;
257 #ifdef UPDATER_UT
258 return -1;
259 #else
260 return ret;
261 #endif
262 }
263
StartDeleteParaZip(const std::string & taskId,const std::string & location,const std::string & cfgDir)264 int32_t SysInstallerKitsImpl::StartDeleteParaZip(const std::string &taskId, const std::string &location,
265 const std::string &cfgDir)
266 {
267 LOG(INFO) << "StartDeleteParaZip";
268 auto updateService = GetService();
269 if (updateService == nullptr) {
270 LOG(ERROR) << "Get updateService failed";
271 return -1;
272 }
273 int32_t ret = updateService->StartDeleteParaZip(taskId, location, cfgDir);
274 LOG(INFO) << "StartDeleteParaZip ret:" << ret;
275 #ifdef UPDATER_UT
276 return -1;
277 #else
278 return ret;
279 #endif
280 }
281
AccDecompressAndVerifyPkg(const std::string & taskId,const std::string & srcPath,const std::string & dstPath,const uint32_t type)282 int32_t SysInstallerKitsImpl::AccDecompressAndVerifyPkg(const std::string &taskId, const std::string &srcPath,
283 const std::string &dstPath, const uint32_t type)
284 {
285 LOG(INFO) << "AccDecompressAndVerifyPkg";
286 auto updateService = GetService();
287 if (updateService == nullptr) {
288 LOG(ERROR) << "Get updateService failed";
289 return -1;
290 }
291 int32_t ret = updateService->AccDecompressAndVerifyPkg(taskId, srcPath, dstPath, type);
292 LOG(INFO) << "AccDecompressAndVerifyPkg ret:" << ret;
293 #ifdef UPDATER_UT
294 return -1;
295 #else
296 return ret;
297 #endif
298 }
299
AccDeleteDir(const std::string & taskId,const std::string & dstPath)300 int32_t SysInstallerKitsImpl::AccDeleteDir(const std::string &taskId, const std::string &dstPath)
301 {
302 LOG(INFO) << "AccDeleteDir";
303 auto updateService = GetService();
304 if (updateService == nullptr) {
305 LOG(ERROR) << "Get updateService failed";
306 return -1;
307 }
308 int32_t ret = updateService->AccDeleteDir(taskId, dstPath);
309 LOG(INFO) << "AccDeleteDir ret:" << ret;
310 #ifdef UPDATER_UT
311 return -1;
312 #else
313 return ret;
314 #endif
315 }
316
CancelUpdateVabPackageZip(const std::string & taskId)317 int32_t SysInstallerKitsImpl::CancelUpdateVabPackageZip(const std::string &taskId)
318 {
319 LOG(INFO) << "CancelUpdateVabPackageZip";
320 auto updateService = GetService();
321 if (updateService == nullptr) {
322 LOG(ERROR) << "Get updateService failed";
323 return -1;
324 }
325 int32_t ret = updateService->CancelUpdateVabPackageZip(taskId);
326 LOG(INFO) << "CancelUpdateVabPackageZip ret:" << ret;
327 return ret;
328 }
329
StartUpdateVabPackageZip(const std::string & taskId,const std::vector<std::string> & pkgPath)330 int32_t SysInstallerKitsImpl::StartUpdateVabPackageZip(const std::string &taskId,
331 const std::vector<std::string> &pkgPath)
332 {
333 LOG(INFO) << "StartUpdateVabPackageZip";
334 auto updateService = GetService();
335 if (updateService == nullptr) {
336 LOG(ERROR) << "Get updateService failed";
337 return -1;
338 }
339 int32_t ret = updateService->StartUpdateVabPackageZip(taskId, pkgPath);
340 LOG(INFO) << "StartUpdateVabPackageZip ret:" << ret;
341 #ifdef UPDATER_UT
342 return -1;
343 #else
344 return ret;
345 #endif
346 }
347
CreateVabSnapshotCowImg(const std::unordered_map<std::string,uint64_t> & partitionInfo)348 int32_t SysInstallerKitsImpl::CreateVabSnapshotCowImg(const std::unordered_map<std::string, uint64_t> &partitionInfo)
349 {
350 LOG(INFO) << "CreateVabSnapshotCowImg";
351 auto updateService = GetService();
352 if (updateService == nullptr) {
353 LOG(ERROR) << "Get updateService failed";
354 return -1;
355 }
356 int32_t ret = updateService->CreateVabSnapshotCowImg(partitionInfo);
357 LOG(INFO) << "CreateVabSnapshotCowImg ret:" << ret;
358 #ifdef UPDATER_UT
359 return -1;
360 #else
361 return ret;
362 #endif
363 }
364
StartVabMerge(const std::string & taskId)365 int32_t SysInstallerKitsImpl::StartVabMerge(const std::string &taskId)
366 {
367 LOG(INFO) << "StartVabMerge";
368 auto updateService = GetService();
369 if (updateService == nullptr) {
370 LOG(ERROR) << "Get updateService failed";
371 return -1;
372 }
373 int32_t ret = updateService->StartVabMerge(taskId);
374 LOG(INFO) << "StartVabMerge ret:" << ret;
375 #ifdef UPDATER_UT
376 return -1;
377 #else
378 return ret;
379 #endif
380 }
381
ClearVabMetadataAndCow()382 int32_t SysInstallerKitsImpl::ClearVabMetadataAndCow()
383 {
384 LOG(INFO) << "ClearVabMetadataAndCow";
385 auto updateService = GetService();
386 if (updateService == nullptr) {
387 LOG(ERROR) << "Get updateService failed";
388 return -1;
389 }
390 int32_t ret = updateService->ClearVabMetadataAndCow();
391 LOG(INFO) << "ClearVabMetadataAndCow ret:" << ret;
392 #ifdef UPDATER_UT
393 return -1;
394 #else
395 return ret;
396 #endif
397 }
398
VabUpdateActive()399 int32_t SysInstallerKitsImpl::VabUpdateActive()
400 {
401 LOG(INFO) << "VabUpdateActive";
402 auto updateService = GetService();
403 if (updateService == nullptr) {
404 LOG(ERROR) << "Get updateService failed";
405 return -1;
406 }
407 int32_t ret = updateService->VabUpdateActive();
408 LOG(INFO) << "VabUpdateActive ret:" << ret;
409 #ifdef UPDATER_UT
410 return -1;
411 #else
412 return ret;
413 #endif
414 }
415
GetMetadataResult(const std::string & action,bool & result)416 int32_t SysInstallerKitsImpl::GetMetadataResult(const std::string &action, bool &result)
417 {
418 LOG(INFO) << "GetMetadataResult";
419 auto updateService = GetService();
420 if (updateService == nullptr) {
421 LOG(ERROR) << "Get updateService failed";
422 return -1;
423 }
424 int32_t ret = updateService->GetMetadataResult(action, result);
425 LOG(INFO) << "GetMetadataResult ret:" << ret;
426 #ifdef UPDATER_UT
427 return -1;
428 #else
429 return ret;
430 #endif
431 }
432
GetUpdateResult(const std::string & taskId,const std::string & taskType,const std::string & resultType)433 std::string SysInstallerKitsImpl::GetUpdateResult(const std::string &taskId, const std::string &taskType,
434 const std::string &resultType)
435 {
436 LOG(INFO) << "GetUpdateResult";
437 auto updateService = GetService();
438 if (updateService == nullptr) {
439 LOG(ERROR) << "Get updateService failed";
440 return std::string("");
441 }
442 std::string updateResult;
443 int32_t ret = updateService->GetUpdateResult(taskId, taskType, resultType, updateResult);
444 if (ret != 0) {
445 return std::string("");
446 }
447 return updateResult;
448 }
449
InstallCloudRom(const std::string & taskId,InstallMode installMode,const std::vector<FeatureInfo> & featureInfos,RebootStatus rebootStatus)450 int32_t SysInstallerKitsImpl::InstallCloudRom(const std::string &taskId,
451 InstallMode installMode, const std::vector<FeatureInfo> &featureInfos, RebootStatus rebootStatus)
452 {
453 LOG(INFO) << "InstallCloudRom";
454 auto updateService = GetService();
455 if (updateService == nullptr) {
456 LOG(ERROR) << "Get updateService failed";
457 return -1;
458 }
459 return updateService->InstallCloudRom(taskId, installMode, featureInfos, rebootStatus);
460 }
461
UninstallCloudRom(const std::string & taskId,const std::vector<FeatureInfo> & featureInfos,RebootStatus rebootStatus)462 int32_t SysInstallerKitsImpl::UninstallCloudRom(const std::string &taskId,
463 const std::vector<FeatureInfo> &featureInfos, RebootStatus rebootStatus)
464 {
465 LOG(INFO) << "UninstallCloudRom";
466 auto updateService = GetService();
467 if (updateService == nullptr) {
468 LOG(ERROR) << "Get updateService failed";
469 return -1;
470 }
471 return updateService->UninstallCloudRom(taskId, featureInfos, rebootStatus);
472 }
473
GetFeatureStatus(const std::vector<FeatureInfo> & featureInfos,std::vector<FeatureStatus> & statusInfos)474 int32_t SysInstallerKitsImpl::GetFeatureStatus(const std::vector<FeatureInfo> &featureInfos,
475 std::vector<FeatureStatus> &statusInfos)
476 {
477 LOG(INFO) << "GetFeatureStatus";
478 auto updateService = GetService();
479 if (updateService == nullptr) {
480 LOG(ERROR) << "Get updateService failed";
481 return -1;
482 }
483 return updateService->GetFeatureStatus(featureInfos, statusInfos);
484 }
485
GetAllFeatureStatus(const std::string & baseVersion,std::vector<FeatureStatus> & statusInfos)486 int32_t SysInstallerKitsImpl::GetAllFeatureStatus(const std::string &baseVersion,
487 std::vector<FeatureStatus> &statusInfos)
488 {
489 LOG(INFO) << "GetAllFeatureStatus";
490 auto updateService = GetService();
491 if (updateService == nullptr) {
492 LOG(ERROR) << "Get updateService failed";
493 return -1;
494 }
495 return updateService->GetAllFeatureStatus(baseVersion, statusInfos);
496 }
497
ClearCloudRom(const std::string & baseVersion,const std::string & featureName)498 int32_t SysInstallerKitsImpl::ClearCloudRom(const std::string &baseVersion,
499 const std::string &featureName)
500 {
501 LOG(INFO) << "ClearCloudRom";
502 auto updateService = GetService();
503 if (updateService == nullptr) {
504 LOG(ERROR) << "Get updateService failed";
505 return -1;
506 }
507 return updateService->ClearCloudRom(baseVersion, featureName);
508 }
509
ExitSysInstaller()510 int32_t SysInstallerKitsImpl::ExitSysInstaller()
511 {
512 LOG(INFO) << "ExitSysInstaller";
513 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
514 if (samgr == nullptr) {
515 LOG(ERROR) << "Get samgr failed";
516 return -1;
517 }
518 bool isExist = false;
519 sptr<IRemoteObject> object = samgr->CheckSystemAbility(SYS_INSTALLER_DISTRIBUTED_SERVICE_ID, isExist);
520 if (!isExist) {
521 LOG(ERROR) << "sys_installer not exist";
522 return 0;
523 }
524
525 auto updateService = GetService();
526 if (updateService == nullptr) {
527 LOG(ERROR) << "Get updateService failed";
528 return -1;
529 }
530 int32_t ret = updateService->ExitSysInstaller();
531 LOG(INFO) << "ExitSysInstaller ret:" << ret;
532 #ifdef UPDATER_UT
533 return -1;
534 #else
535 return ret;
536 #endif
537 }
538
StartAbSync()539 int32_t SysInstallerKitsImpl::StartAbSync()
540 {
541 LOG(INFO) << "StartAbSync";
542 auto updateService = GetService();
543 if (updateService == nullptr) {
544 LOG(ERROR) << "Get updateService failed";
545 return -1;
546 }
547 int32_t ret = updateService->StartAbSync();
548 LOG(INFO) << "StartAbSync ret:" << ret;
549 #ifdef UPDATER_UT
550 return -1;
551 #else
552 return ret;
553 #endif
554 }
555
SetCpuAffinity(const std::string & taskId,unsigned int reservedCores)556 int32_t SysInstallerKitsImpl::SetCpuAffinity(const std::string &taskId, unsigned int reservedCores)
557 {
558 LOG(INFO) << "SetCpuAffinity taskId:" << taskId << ", reservedCores:" << reservedCores;
559 auto updateService = GetService();
560 if (updateService == nullptr) {
561 LOG(ERROR) << "Get updateService failed";
562 return -1;
563 }
564 uint32_t reservedCpus = reservedCores;
565 int32_t ret = updateService->SetCpuAffinity(taskId, reservedCpus);
566 LOG(INFO) << "SetCpuAffinity ret:" << ret;
567 return ret;
568 }
569
LoadServiceSuccess()570 void SysInstallerKitsImpl::LoadServiceSuccess()
571 {
572 serviceCv_.notify_all();
573 }
574
LoadServiceFail()575 void SysInstallerKitsImpl::LoadServiceFail()
576 {
577 serviceCv_.notify_all();
578 }
579 }
580 } // namespace OHOS
581