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 "update_service.h"
17
18 #include <dlfcn.h>
19 #include <unistd.h>
20 #include <updater_sa_ipc_interface_code.h>
21
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24
25 #include "accesstoken_kit.h"
26 #include "access_manager.h"
27 #include "access_token.h"
28 #include "config_parse.h"
29 #include "dupdate_net_manager.h"
30 #include "firmware_common.h"
31 #include "firmware_manager.h"
32 #include "ipc_skeleton.h"
33 #include "module_manager.h"
34 #include "securec.h"
35 #include "startup_manager.h"
36 #include "tokenid_kit.h"
37 #include "update_log.h"
38 #include "update_service_cache.h"
39 #include "update_service_local_updater.h"
40 #include "update_service_module.h"
41 #include "update_service_restorer.h"
42 #include "update_service_util.h"
43 #include "update_system_event.h"
44
45 #ifdef UPDATE_SERVICE_ENABLE_RUN_ON_DEMAND_QOS
46 #include <sys/syscall.h>
47 #include <sys/resource.h>
48 #endif
49
50 using namespace std;
51
52 namespace OHOS {
53 namespace UpdateService {
54 constexpr const pid_t ROOT_UID = 0;
55 constexpr const pid_t EDM_UID = 3057;
REGISTER_SYSTEM_ABILITY_BY_ID(UpdateService,UPDATE_DISTRIBUTED_SERVICE_ID,true)56 REGISTER_SYSTEM_ABILITY_BY_ID(UpdateService, UPDATE_DISTRIBUTED_SERVICE_ID, true)
57
58 OHOS::sptr<UpdateService> UpdateService::updateService_ { nullptr };
59
60 #ifdef UPDATE_SERVICE_ENABLE_RUN_ON_DEMAND_QOS
61 constexpr int OPEN_SO_PRIO = -20;
62 constexpr int NORMAL_PRIO = 0;
63 #endif
64
OnRemoteDied(const wptr<IRemoteObject> & remote)65 void UpdateService::ClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
66 {
67 ENGINE_LOGI("client DeathRecipient OnRemoteDied: %{public}s", upgradeInfo_.ToString().c_str());
68 sptr<UpdateService> service = UpdateService::GetInstance();
69 int32_t funcResult = 0;
70 if (service != nullptr) {
71 service->UnregisterUpdateCallback(upgradeInfo_, funcResult);
72 }
73 }
74
ClientProxy(const UpgradeInfo & info,const sptr<IUpdateCallback> & callback)75 UpdateService::ClientProxy::ClientProxy(const UpgradeInfo &info, const sptr<IUpdateCallback> &callback)
76 : proxy_(callback)
77 {
78 ENGINE_LOGI("UpdateService::ClientProxy constructor");
79 auto clientDeathRecipient = new (std::nothrow) ClientDeathRecipient(info);
80 if (clientDeathRecipient != nullptr) {
81 deathRecipient_ = sptr<ClientDeathRecipient>(clientDeathRecipient);
82 } else {
83 ENGINE_LOGE("UpdateService::ClientProxy, new fail");
84 }
85 }
86
AddDeathRecipient()87 void UpdateService::ClientProxy::AddDeathRecipient()
88 {
89 ENGINE_LOGI("UpdateService::ClientProxy AddDeathRecipient in");
90 if (proxy_ != nullptr) {
91 auto remoteObject = proxy_->AsObject();
92 if ((remoteObject != nullptr) && (deathRecipient_ != nullptr)) {
93 remoteObject->AddDeathRecipient(deathRecipient_);
94 ENGINE_LOGI("UpdateService::ClientProxy AddDeathRecipient success");
95 }
96 }
97 }
98
RemoveDeathRecipient()99 void UpdateService::ClientProxy::RemoveDeathRecipient()
100 {
101 ENGINE_LOGI("UpdateService::ClientProxy RemoveDeathRecipient in");
102 if (proxy_ != nullptr) {
103 auto remoteObject = proxy_->AsObject();
104 if ((remoteObject != nullptr) && (deathRecipient_ != nullptr)) {
105 remoteObject->RemoveDeathRecipient(deathRecipient_);
106 ENGINE_LOGI("UpdateService::ClientProxy RemoveDeathRecipient success");
107 }
108 }
109 }
110
Get()111 sptr<IUpdateCallback> UpdateService::ClientProxy::Get()
112 {
113 return proxy_;
114 }
115
UpdateService(int32_t systemAbilityId,bool runOnCreate)116 UpdateService::UpdateService(int32_t systemAbilityId, bool runOnCreate)
117 : SystemAbility(systemAbilityId, runOnCreate)
118 {
119 updateImplMgr_ = std::make_shared<UpdateServiceImplManager>();
120 }
121
~UpdateService()122 UpdateService::~UpdateService()
123 {
124 ENGINE_LOGI("UpdateServerTest free now");
125 for (auto &iter : clientProxyMap_) {
126 iter.second.RemoveDeathRecipient();
127 }
128 }
129
GetInstance()130 sptr<UpdateService> UpdateService::GetInstance()
131 {
132 return updateService_;
133 }
134
RegisterUpdateCallback(const UpgradeInfo & info,const sptr<IUpdateCallback> & updateCallback,int32_t & funcResult)135 int32_t UpdateService::RegisterUpdateCallback(const UpgradeInfo &info, const sptr<IUpdateCallback> &updateCallback,
136 int32_t &funcResult)
137 {
138 ENGINE_LOGI("RegisterUpdateCallback");
139 UnregisterUpdateCallback(info, funcResult);
140 {
141 std::lock_guard<std::mutex> lock(clientProxyMapLock_);
142 ClientProxy clientProxy(info, updateCallback);
143 clientProxy.AddDeathRecipient();
144 clientProxyMap_.insert({info, clientProxy});
145 DelayedSingleton<AccessManager>::GetInstance()->SetRemoteIdle(clientProxyMap_.empty());
146 }
147 if (!info.IsLocal()) {
148 UpdateServiceCache::SetUpgradeInfo(info);
149 }
150 return INT_CALL_SUCCESS;
151 }
152
UnregisterUpdateCallback(const UpgradeInfo & info,int32_t & funcResult)153 int32_t UpdateService::UnregisterUpdateCallback(const UpgradeInfo &info, int32_t &funcResult)
154 {
155 ENGINE_LOGI("UnregisterUpdateCallback");
156 std::lock_guard<std::mutex> lock(clientProxyMapLock_);
157 auto iter = clientProxyMap_.find(info);
158 if (iter == clientProxyMap_.end()) {
159 return INT_CALL_SUCCESS;
160 }
161 iter->second.RemoveDeathRecipient();
162 clientProxyMap_.erase(info);
163 DelayedSingleton<AccessManager>::GetInstance()->SetRemoteIdle(clientProxyMap_.empty());
164 return INT_CALL_SUCCESS;
165 }
166
GetUpgradeCallback(const UpgradeInfo & info,int32_t & funcResult)167 sptr<IUpdateCallback> UpdateService::GetUpgradeCallback(const UpgradeInfo &info, int32_t &funcResult)
168 {
169 std::lock_guard<std::mutex> lock(clientProxyMapLock_);
170 auto iter = clientProxyMap_.find(info);
171 if (iter == clientProxyMap_.end()) {
172 return nullptr;
173 }
174 return iter->second.Get();
175 }
176
GetNewVersionInfo(const UpgradeInfo & info,NewVersionInfo & newVersionInfo,BusinessError & businessError,int32_t & funcResult)177 int32_t UpdateService::GetNewVersionInfo(const UpgradeInfo &info, NewVersionInfo &newVersionInfo,
178 BusinessError &businessError, int32_t &funcResult)
179 {
180 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
181 if (onlineUpdater == nullptr) {
182 ENGINE_LOGI("GetNewVersionInfo onlineUpdater null");
183 return INT_CALL_FAIL;
184 }
185 return onlineUpdater->GetNewVersionInfo(info, newVersionInfo, businessError);
186 }
187
GetNewVersionDescription(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & newVersionDescriptionInfo,BusinessError & businessError,int32_t & funcResult)188 int32_t UpdateService::GetNewVersionDescription(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
189 const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &newVersionDescriptionInfo,
190 BusinessError &businessError, int32_t &funcResult)
191 {
192 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
193 if (onlineUpdater == nullptr) {
194 ENGINE_LOGI("GetNewVersionDescription onlineUpdater null");
195 return INT_CALL_FAIL;
196 }
197 return onlineUpdater->GetNewVersionDescription(info, versionDigestInfo, descriptionOptions,
198 newVersionDescriptionInfo, businessError);
199 }
200
GetCurrentVersionInfo(const UpgradeInfo & info,CurrentVersionInfo & currentVersionInfo,BusinessError & businessError,int32_t & funcResult)201 int32_t UpdateService::GetCurrentVersionInfo(const UpgradeInfo &info, CurrentVersionInfo ¤tVersionInfo,
202 BusinessError &businessError, int32_t &funcResult)
203 {
204 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
205 if (onlineUpdater == nullptr) {
206 ENGINE_LOGI("GetCurrentVersionInfo onlineUpdater null");
207 return INT_CALL_FAIL;
208 }
209 return onlineUpdater->GetCurrentVersionInfo(info, currentVersionInfo, businessError);
210 }
211
GetCurrentVersionDescription(const UpgradeInfo & info,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & currentVersionDescriptionInfo,BusinessError & businessError,int32_t & funcResult)212 int32_t UpdateService::GetCurrentVersionDescription(const UpgradeInfo &info,
213 const DescriptionOptions &descriptionOptions, VersionDescriptionInfo ¤tVersionDescriptionInfo,
214 BusinessError &businessError, int32_t &funcResult)
215 {
216 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
217 if (onlineUpdater == nullptr) {
218 ENGINE_LOGI("GetCurrentVersionDescription onlineUpdater null");
219 return INT_CALL_FAIL;
220 }
221 return onlineUpdater->GetCurrentVersionDescription(info, descriptionOptions, currentVersionDescriptionInfo,
222 businessError);
223 }
224
GetTaskInfo(const UpgradeInfo & info,TaskInfo & taskInfo,BusinessError & businessError,int32_t & funcResult)225 int32_t UpdateService::GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo, BusinessError &businessError,
226 int32_t &funcResult)
227 {
228 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
229 if (onlineUpdater == nullptr) {
230 ENGINE_LOGI("GetTaskInfo onlineUpdater null");
231 return INT_CALL_FAIL;
232 }
233 return onlineUpdater->GetTaskInfo(info, taskInfo, businessError);
234 }
235
SetUpgradePolicy(const UpgradeInfo & info,const UpgradePolicy & policy,BusinessError & businessError,int32_t & funcResult)236 int32_t UpdateService::SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
237 BusinessError &businessError, int32_t &funcResult)
238 {
239 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
240 if (onlineUpdater == nullptr) {
241 ENGINE_LOGI("SetUpgradePolicy onlineUpdater null");
242 return INT_CALL_FAIL;
243 }
244 return onlineUpdater->SetUpgradePolicy(info, policy, businessError);
245 }
246
GetUpgradePolicy(const UpgradeInfo & info,UpgradePolicy & policy,BusinessError & businessError,int32_t & funcResult)247 int32_t UpdateService::GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy, BusinessError &businessError,
248 int32_t &funcResult)
249 {
250 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
251 if (onlineUpdater == nullptr) {
252 ENGINE_LOGI("GetUpgradePolicy onlineUpdater null");
253 return INT_CALL_FAIL;
254 }
255 return onlineUpdater->GetUpgradePolicy(info, policy, businessError);
256 }
257
CheckNewVersion(const UpgradeInfo & info,BusinessError & businessError,CheckResult & checkResult,int32_t & funcResult)258 int32_t UpdateService::CheckNewVersion(const UpgradeInfo &info, BusinessError &businessError, CheckResult &checkResult,
259 int32_t &funcResult)
260 {
261 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
262 if (onlineUpdater == nullptr) {
263 ENGINE_LOGI("CheckNewVersion onlineUpdater null");
264 return INT_CALL_FAIL;
265 }
266 return onlineUpdater->CheckNewVersion(info, businessError, checkResult);
267 }
268
Download(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DownloadOptions & downloadOptions,BusinessError & businessError,int32_t & funcResult)269 int32_t UpdateService::Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
270 const DownloadOptions &downloadOptions, BusinessError &businessError, int32_t &funcResult)
271 {
272 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
273 if (onlineUpdater == nullptr) {
274 ENGINE_LOGI("Download onlineUpdater null");
275 return INT_CALL_FAIL;
276 }
277 return onlineUpdater->Download(info, versionDigestInfo, downloadOptions, businessError);
278 }
279
PauseDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const PauseDownloadOptions & pauseDownloadOptions,BusinessError & businessError,int32_t & funcResult)280 int32_t UpdateService::PauseDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
281 const PauseDownloadOptions &pauseDownloadOptions, BusinessError &businessError, int32_t &funcResult)
282 {
283 ENGINE_LOGI("PauseDownload");
284 businessError.errorNum = CallResult::SUCCESS;
285 businessError.Build(CallResult::UN_SUPPORT, "PauseDownload unsupport");
286 return INT_CALL_SUCCESS;
287 }
288
ResumeDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const ResumeDownloadOptions & resumeDownloadOptions,BusinessError & businessError,int32_t & funcResult)289 int32_t UpdateService::ResumeDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
290 const ResumeDownloadOptions &resumeDownloadOptions, BusinessError &businessError, int32_t &funcResult)
291 {
292 ENGINE_LOGI("ResumeDownload allowNetwork:%{public}d", CAST_INT(resumeDownloadOptions.allowNetwork));
293 businessError.Build(CallResult::UN_SUPPORT, "ResumeDownload unsupport");
294 return INT_CALL_SUCCESS;
295 }
296
Upgrade(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const UpgradeOptions & upgradeOptions,BusinessError & businessError,int32_t & funcResult)297 int32_t UpdateService::Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
298 const UpgradeOptions &upgradeOptions, BusinessError &businessError, int32_t &funcResult)
299 {
300 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
301 if (onlineUpdater == nullptr) {
302 ENGINE_LOGI("Upgrade onlineUpdater null");
303 return INT_CALL_FAIL;
304 }
305 return onlineUpdater->Upgrade(info, versionDigestInfo, upgradeOptions, businessError);
306 }
307
ClearError(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const ClearOptions & clearOptions,BusinessError & businessError,int32_t & funcResult)308 int32_t UpdateService::ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
309 const ClearOptions &clearOptions, BusinessError &businessError, int32_t &funcResult)
310 {
311 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
312 if (onlineUpdater == nullptr) {
313 ENGINE_LOGI("ClearError onlineUpdater null");
314 return INT_CALL_FAIL;
315 }
316 return onlineUpdater->ClearError(info, versionDigestInfo, clearOptions, businessError);
317 }
318
TerminateUpgrade(const UpgradeInfo & info,BusinessError & businessError,int32_t & funcResult)319 int32_t UpdateService::TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError,
320 int32_t &funcResult)
321 {
322 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
323 if (onlineUpdater == nullptr) {
324 ENGINE_LOGI("TerminateUpgrade onlineUpdater null");
325 return INT_CALL_FAIL;
326 }
327 return onlineUpdater->TerminateUpgrade(info, businessError);
328 }
329
Cancel(const UpgradeInfo & info,int32_t service,BusinessError & businessError,int32_t & funcResult)330 int32_t UpdateService::Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError,
331 int32_t &funcResult)
332 {
333 sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
334 if (onlineUpdater == nullptr) {
335 ENGINE_LOGI("Cancel onlineUpdater null");
336 return INT_CALL_FAIL;
337 }
338 return onlineUpdater->Cancel(info, service, businessError);
339 }
340
FactoryReset(BusinessError & businessError,int32_t & funcResult)341 int32_t UpdateService::FactoryReset(BusinessError &businessError, int32_t &funcResult)
342 {
343 sptr<UpdateServiceRestorer> restorer = new UpdateServiceRestorer();
344 if (restorer == nullptr) {
345 ENGINE_LOGI("FactoryReset restorer null");
346 return INT_CALL_FAIL;
347 }
348 return restorer->FactoryReset(businessError);
349 }
350
ApplyNewVersion(const UpgradeInfo & info,const std::string & miscFile,const std::vector<std::string> & packageNames,BusinessError & businessError,int32_t & funcResult)351 int32_t UpdateService::ApplyNewVersion(const UpgradeInfo &info, const std::string &miscFile,
352 const std::vector<std::string> &packageNames, BusinessError &businessError, int32_t &funcResult)
353 {
354 sptr<UpdateServiceLocalUpdater> localUpdater = new UpdateServiceLocalUpdater();
355 if (localUpdater == nullptr) {
356 ENGINE_LOGI("FactoryReset localUpdater null");
357 return INT_CALL_FAIL;
358 }
359 return localUpdater->ApplyNewVersion(info, miscFile, packageNames, businessError);
360 }
361
VerifyUpgradePackage(const std::string & packagePath,const std::string & keyPath,BusinessError & businessError,int32_t & funcResult)362 int32_t UpdateService::VerifyUpgradePackage(const std::string &packagePath, const std::string &keyPath,
363 BusinessError &businessError, int32_t &funcResult)
364 {
365 sptr<UpdateServiceLocalUpdater> localUpdater = new UpdateServiceLocalUpdater();
366 if (localUpdater == nullptr) {
367 ENGINE_LOGI("FactoryReset localUpdater null");
368 return INT_CALL_FAIL;
369 }
370 return localUpdater->VerifyUpgradePackage(packagePath, keyPath, businessError);
371 }
372
BuildUpgradeInfoDump(const int fd,UpgradeInfo & info)373 void BuildUpgradeInfoDump(const int fd, UpgradeInfo &info)
374 {
375 dprintf(fd, "---------------------upgrade info info--------------------\n");
376 dprintf(fd, "UpgradeApp: %s\n", info.upgradeApp.c_str());
377 dprintf(fd, "vendor: %s\n", info.businessType.vendor.c_str());
378 dprintf(fd, "subType: %d\n", static_cast<int>(info.businessType.subType));
379 }
380
BuildVersionInfoDump(const int fd,const CheckResult & checkResult)381 void BuildVersionInfoDump(const int fd, const CheckResult &checkResult)
382 {
383 dprintf(fd, "---------------------version info--------------------\n");
384 dprintf(fd, "isExistNewVersion: %d\n", checkResult.isExistNewVersion);
385 if (checkResult.newVersionInfo.versionComponents.empty()) {
386 return;
387 }
388 dprintf(fd, "PackageSize: %zu\n", static_cast<size_t>(checkResult.newVersionInfo.versionComponents[0].size));
389 dprintf(fd, "ComponentType: %d\n", checkResult.newVersionInfo.versionComponents[0].componentType);
390 dprintf(fd, "UpgradeAction: %s\n", checkResult.newVersionInfo.versionComponents[0].upgradeAction.c_str());
391 dprintf(fd, "DisplayVersion: %s\n", checkResult.newVersionInfo.versionComponents[0].displayVersion.c_str());
392 dprintf(fd, "InnerVersion: %s\n", checkResult.newVersionInfo.versionComponents[0].innerVersion.c_str());
393 dprintf(fd, "Content: %s\n", checkResult.newVersionInfo.versionComponents[0].descriptionInfo.content.c_str());
394 }
395
BuildTaskInfoDump(const int fd)396 void BuildTaskInfoDump(const int fd)
397 {
398 sptr<UpdateService> service = UpdateService::GetInstance();
399 if (service == nullptr) {
400 ENGINE_LOGI("BuildTaskInfoDump no instance");
401 return;
402 }
403
404 TaskInfo taskInfo;
405 BusinessError businessError;
406 UpgradeInfo upgradeInfo;
407 int32_t funcResult = 0;
408 service->GetTaskInfo(upgradeInfo, taskInfo, businessError, funcResult);
409 if (!taskInfo.existTask) {
410 dprintf(fd, "TaskInfo is empty\n");
411 return;
412 }
413
414 dprintf(fd, "---------------------OTA status info--------------------\n");
415 dprintf(fd, "Progress: %d\n", taskInfo.taskBody.progress);
416 dprintf(fd, "UpgradeStatus: %d\n", taskInfo.taskBody.status);
417 dprintf(fd, "SubStatus: %d\n", taskInfo.taskBody.subStatus);
418 for (auto &iter : taskInfo.taskBody.errorMessages) {
419 dprintf(fd, "ErrorCode: %d\n", iter.errorCode);
420 dprintf(fd, "ErrorMsg: %s\n", iter.errorMessage.c_str());
421 }
422 }
423
DumpUpgradeCallback(const int fd)424 void UpdateService::DumpUpgradeCallback(const int fd)
425 {
426 dprintf(fd, "---------------------callback info--------------------\n");
427 std::lock_guard<std::mutex> lock(clientProxyMapLock_);
428 for (const auto &iter : clientProxyMap_) {
429 const UpgradeInfo& info = iter.first;
430 dprintf(fd, "%s\n", info.ToString().c_str());
431 }
432 }
433
Dump(int fd,const std::vector<std::u16string> & args)434 int UpdateService::Dump(int fd, const std::vector<std::u16string> &args)
435 {
436 if (!ModuleManager::GetInstance().IsModuleLoaded()) {
437 if (fd < 0) {
438 ENGINE_LOGI("HiDumper handle invalid");
439 return -1;
440 }
441
442 if (args.size() == 0) {
443 UpgradeInfo upgradeInfo = UpdateServiceCache::GetUpgradeInfo(BusinessSubType::FIRMWARE);
444 BuildUpgradeInfoDump(fd, upgradeInfo);
445 BuildTaskInfoDump(fd);
446 DumpUpgradeCallback(fd);
447 } else {
448 dprintf(fd, "input error, no parameters required\n");
449 }
450 return 0;
451 }
452
453 return ModuleManager::GetInstance().HandleDumpFunc("Dump", fd, args);
454 }
455
456 #ifdef UPDATE_SERVICE_ENABLE_RUN_ON_DEMAND_QOS
SetThreadPrio(int priority)457 void UpdateService::SetThreadPrio(int priority)
458 {
459 int tid = syscall(SYS_gettid);
460 ENGINE_LOGI("set tid: %{public}d priority:%{public}d.", tid, priority);
461 if (setpriority(PRIO_PROCESS, tid, priority) != 0) {
462 ENGINE_LOGE("set tid: %{public}d priority:%{public}d failed.", tid, priority);
463 }
464 }
465 #endif
466
OnStart(const SystemAbilityOnDemandReason & startReason)467 void UpdateService::OnStart(const SystemAbilityOnDemandReason &startReason)
468 {
469 ENGINE_LOGI("UpdaterService oh OnStart, startReason name %{public}s, id %{public}d, value %{public}s",
470 startReason.GetName().c_str(), CAST_INT(startReason.GetId()), startReason.GetValue().c_str());
471 updateService_ = this;
472 if (updateService_ == nullptr) {
473 ENGINE_LOGE("updateService_ null");
474 }
475
476 DelayedSingleton<ConfigParse>::GetInstance()->LoadConfigInfo(); // 启动读取配置信息
477 std::string libPath = DelayedSingleton<ConfigParse>::GetInstance()->GetModuleLibPath();
478 ENGINE_LOGI("GetModuleLibPath %{public}s ", libPath.c_str());
479 #ifdef UPDATE_SERVICE_ENABLE_RUN_ON_DEMAND_QOS
480 SetThreadPrio(OPEN_SO_PRIO);
481 #endif
482 ModuleManager::GetInstance().LoadModule(libPath);
483 #ifdef UPDATE_SERVICE_ENABLE_RUN_ON_DEMAND_QOS
484 SetThreadPrio(NORMAL_PRIO);
485 #endif
486
487 if (!ModuleManager::GetInstance().IsModuleLoaded()) {
488 ENGINE_LOGI("IsModuleLoaded false, init updateservice_sa");
489 DelayedSingleton<NetManager>::GetInstance()->Init();
490
491 // 动态启停流程启动
492 DelayedSingleton<StartupManager>::GetInstance()->Start();
493 }
494
495 if (Publish(this)) {
496 ENGINE_LOGI("UpdaterService OnStart publish success");
497 } else {
498 ENGINE_LOGI("UpdaterService OnStart publish fail");
499 }
500
501 if (ModuleManager::GetInstance().IsModuleLoaded()) {
502 ENGINE_LOGI("IsModuleLoaded true");
503 ModuleManager::GetInstance().HandleOnStartOnStopFunc("OnStart", startReason);
504 }
505 }
506
OnIdle(const SystemAbilityOnDemandReason & idleReason)507 int32_t UpdateService::OnIdle(const SystemAbilityOnDemandReason &idleReason)
508 {
509 ENGINE_LOGI("UpdaterService OnIdle");
510 return ModuleManager::GetInstance().HandleOnIdleFunc("OnIdle", idleReason);
511 }
512
OnStop(const SystemAbilityOnDemandReason & stopReason)513 void UpdateService::OnStop(const SystemAbilityOnDemandReason &stopReason)
514 {
515 ENGINE_LOGI("UpdaterService OnStop");
516 ModuleManager::GetInstance().HandleOnStartOnStopFunc("OnStop", stopReason);
517 }
518
CallbackEnter(uint32_t code)519 int32_t UpdateService::CallbackEnter(uint32_t code)
520 {
521 // 未加载接口扩展能力,执行UpdateService鉴权动作
522 if (!ModuleManager::GetInstance().IsModuleLoaded()) {
523 ENGINE_LOGI("CallbackEnter, extend module not loaded, code %{public}u", code);
524 return PermissionCheck(code);
525 }
526
527 // 加载了接口扩展能力, 但是当前调用接口不是拓展接口,还是执行UpdateService鉴权动作
528 if (!ModuleManager::GetInstance().IsMapFuncExist(code)) {
529 ENGINE_LOGE("CallbackEnter, code %{public}u not extended", code);
530 return PermissionCheck(code);
531 }
532
533 // 加载了接口扩展能力,并且当前调用的接口为拓展接口,则由Hook框架进行接口鉴权
534 return INT_CALL_SUCCESS;
535 }
536
PermissionCheck(uint32_t code)537 int32_t UpdateService::PermissionCheck(uint32_t code)
538 {
539 ENGINE_LOGI("UpdateService Oh PermissionCheck, code: %{public}u", code);
540 if (!IsCallerValid()) {
541 ENGINE_LOGE("UpdateService IsCallerValid false");
542 return INT_NOT_SYSTEM_APP;
543 }
544
545 if (!IsPermissionGranted(code)) {
546 ENGINE_LOGE("UpdateService code %{public}u IsPermissionGranted false", code);
547 return INT_APP_NOT_GRANTED;
548 }
549
550 if (code == CAST_UINT(UpdaterSaInterfaceCode::FACTORY_RESET)) {
551 SYS_EVENT_SYSTEM_RESET(0, UpdateSystemEvent::RESET_START);
552 }
553 return INT_CALL_SUCCESS;
554 }
555
CallbackExit(uint32_t code,int32_t result)556 int32_t UpdateService::CallbackExit(uint32_t code, int32_t result)
557 {
558 return INT_CALL_SUCCESS;
559 }
560
CallbackParcel(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)561 int32_t UpdateService::CallbackParcel(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
562 {
563 // 未加载接口扩展能力,通过IDL框架分发
564 if (!ModuleManager::GetInstance().IsModuleLoaded()) {
565 ENGINE_LOGI("CallbackParcel, extend module not loaded, code %{public}u", code);
566 return INT_CALL_SUCCESS;
567 }
568
569 // 加载了接口扩展能力, 但是当前调用接口不是拓展接口,还是通过idl框架分发
570 if (!ModuleManager::GetInstance().IsMapFuncExist(code)) {
571 ENGINE_LOGE("CallbackParcel, code %{public}u not extended", code);
572 return INT_CALL_SUCCESS;
573 }
574
575 // 加载了接口扩展能力, 并且当前调用接口为拓展接口,则通过Hook框架分发
576 int32_t ret = ModuleManager::GetInstance().HandleFunc(code, data, reply, option);
577 ENGINE_LOGE("CallbackParcel, code %{public}u extended, result %{public}d", code, ret);
578 if (ret != INT_CALL_SUCCESS) {
579 return ret;
580 }
581 return INT_CALL_FAIL;
582 }
583
IsCallerValid()584 bool UpdateService::IsCallerValid()
585 {
586 OHOS::Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
587 auto callerTokenType = OHOS::Security::AccessToken::AccessTokenKit::GetTokenType(callerToken);
588 switch (callerTokenType) {
589 case OHOS::Security::AccessToken::TypeATokenTypeEnum::TOKEN_HAP: {
590 uint64_t callerFullTokenID = IPCSkeleton::GetCallingFullTokenID();
591 // hap进程只允许系统应用调用
592 return OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(callerFullTokenID);
593 }
594 case OHOS::Security::AccessToken::TypeATokenTypeEnum::TOKEN_NATIVE: {
595 pid_t callerUid = IPCSkeleton::GetCallingUid();
596 // native进程只允许root权限和edm调用
597 return callerUid == ROOT_UID || callerUid == EDM_UID;
598 }
599 default:
600 // 其他情况调用予以禁止
601 return false;
602 }
603 }
604
IsPermissionGranted(uint32_t code)605 bool UpdateService::IsPermissionGranted(uint32_t code)
606 {
607 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
608 string permission = "ohos.permission.UPDATE_SYSTEM";
609 if (code == CAST_UINT(UpdaterSaInterfaceCode::FACTORY_RESET)) {
610 permission = "ohos.permission.FACTORY_RESET";
611 }
612 int verifyResult = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
613 bool isPermissionGranted = verifyResult == Security::AccessToken::PERMISSION_GRANTED;
614 if (!isPermissionGranted) {
615 ENGINE_LOGE("%{public}s not granted, code:%{public}u", permission.c_str(), code);
616 }
617 return isPermissionGranted;
618 }
619 } // namespace UpdateService
620 } // namespace OHOS
621