1 /*
2 * Copyright (c) 2024 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 "module_update_consumer.h"
17 #include <vector>
18 #include "directory_ex.h"
19 #include "log/log.h"
20 #include "module_constants.h"
21 #include "module_error_code.h"
22 #include "module_update_main.h"
23 #include "module_utils.h"
24 #include "parameter.h"
25 #include "scope_guard.h"
26
27 namespace OHOS {
28 namespace SysInstaller {
29 using namespace Updater;
30
ModuleUpdateConsumer(ModuleUpdateQueue & queue,std::unordered_map<int32_t,std::string> & saIdHmpMap,volatile sig_atomic_t & exit)31 ModuleUpdateConsumer::ModuleUpdateConsumer(ModuleUpdateQueue &queue,
32 std::unordered_map<int32_t, std::string> &saIdHmpMap, volatile sig_atomic_t &exit)
33 : queue_(queue),
34 saIdHmpMap_(saIdHmpMap),
35 exit_(exit) {}
36
DoRevert(int32_t code) const37 void ModuleUpdateConsumer::DoRevert(int32_t code) const
38 {
39 std::string revertHmpName = GetCurrentHmpName();
40 if (revertHmpName.empty()) {
41 LOG(ERROR) << "hmp package revert, hmpName is empty.";
42 return;
43 }
44 Timer timer;
45 ModuleUpdateMain::GetInstance().SaveInstallerResult(revertHmpName, code, "revert", timer);
46 NotifyBmsRevert(revertHmpName, false);
47 Revert(revertHmpName, true);
48 }
49
Run()50 void ModuleUpdateConsumer::Run()
51 {
52 LOG(INFO) << "ModuleUpdateConsumer Consume";
53 ModuleErrorCode code = ModuleErrorCode::ERR_BMS_REVERT;
54 bool revertFlag = false;
55 do {
56 if (exit_ == 1 && queue_.IsEmpty()) {
57 queue_.Stop();
58 break;
59 }
60 std::pair<int32_t, std::string> saStatusPair = queue_.Pop();
61 if (saStatusPair.first == 0 && saStatusPair.second == "") {
62 LOG(INFO) << "producer and consumer stop";
63 break;
64 }
65 if (saStatusPair.first == APP_SERIAL_NUMBER) {
66 LOG(INFO) << "hmp package revert, module name=" << saStatusPair.second;
67 revertFlag = true;
68 continue;
69 }
70 int32_t saId = saStatusPair.first;
71 std::string saStatus = saStatusPair.second;
72 auto it = saIdHmpMap_.find(saId);
73 if (it == saIdHmpMap_.end() || it->second == "") {
74 LOG(ERROR) << "find hmp fail, saId=" << saId;
75 continue;
76 }
77 std::string hmpName = it->second;
78 if (strcmp(saStatus.c_str(), LOAD_FAIL) == 0 || strcmp(saStatus.c_str(), CRASH) == 0) {
79 code = ModuleErrorCode::ERR_SAMGR_REVERT;
80 LOG(INFO) << "hmp package revert, module name=" << hmpName << "; said=" << saId;
81 revertFlag = true;
82 } else if (IsHotSa(saId) && strcmp(saStatus.c_str(), UNLOAD) == 0) {
83 LOG(INFO) << "sa is unload, said=" << saId;
84 } else {
85 LOG(ERROR) << "sa status not exist, said=" << saId;
86 }
87 } while (true);
88 if (revertFlag) {
89 DoRevert(code);
90 }
91 LOG(INFO) << "consumer exit";
92 }
93 } // SysInstaller
94 } // namespace OHOS