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 "log/log.h"
17 #include "securec.h"
18 #include "string_ex.h"
19 #include <sys/stat.h>
20 #include <sys/statfs.h>
21 #include "module_constants.h"
22 #include "module_ipc_helper.h"
23 #include "module_update_producer.h"
24 #include "module_utils.h"
25 #include "parameter.h"
26
27 namespace OHOS {
28 namespace SysInstaller {
29 using namespace Updater;
30 namespace {
31 static const std::vector<std::string> SA_STATUS_VEC {UNLOAD, LOAD_FAIL, CRASH};
32 constexpr int32_t PARAM_VALUE_SIZE = 10;
33 constexpr const char *BOOT_COMPLETE_PARAM = "bootevent.boot.completed";
34 constexpr const char *BOOT_SUCCESS_VALUE = "true";
35 }
36
ModuleUpdateProducer(ModuleUpdateQueue & queue,std::unordered_map<int32_t,std::string> & saIdHmpMap,std::unordered_set<std::string> & hmpSet,volatile sig_atomic_t & exit)37 ModuleUpdateProducer::ModuleUpdateProducer(ModuleUpdateQueue &queue,
38 std::unordered_map<int32_t, std::string> &saIdHmpMap,
39 std::unordered_set<std::string> &hmpSet,
40 volatile sig_atomic_t &exit)
41 : queue_(queue),
42 saIdHmpMap_(saIdHmpMap),
43 hmpNameSet_(hmpSet),
44 exit_(exit) {}
45
AddAbnormalSa()46 void ModuleUpdateProducer::AddAbnormalSa()
47 {
48 char saStatus[PARAM_VALUE_SIZE] = "";
49 for (auto it : saIdHmpMap_) {
50 int32_t saId = it.first;
51 std::string attr = std::string(SA_START_PREFIX) + "." + std::to_string(saId);
52 int ret = GetParameter(attr.c_str(), "", saStatus, PARAM_VALUE_SIZE);
53 if (ret < 0) {
54 LOG(ERROR) << "failed to get parameter " << attr;
55 continue;
56 }
57 if (find(SA_STATUS_VEC.begin(), SA_STATUS_VEC.end(), saStatus) != SA_STATUS_VEC.end()) {
58 LOG(INFO) << "add abnormal sa=" << saId << "; status=" << saStatus;
59 std::pair<int32_t, std::string> saStatusPair = std::make_pair(saId, saStatus);
60 char bootValue[PARAM_VALUE_SIZE] = "";
61 int bootVal = GetParameter(BOOT_COMPLETE_PARAM, "", bootValue, PARAM_VALUE_SIZE);
62 if (bootVal < 0) {
63 LOG(ERROR) << "Failed to get parameter " << BOOT_COMPLETE_PARAM;
64 continue;
65 }
66 if (strcmp(saStatus, UNLOAD) != 0 || (strcmp(saStatus, UNLOAD) == 0 &&
67 strcmp(bootValue, BOOT_SUCCESS_VALUE) == 0 &&
68 IsHotSa(saId))) {
69 queue_.Put(saStatusPair);
70 }
71 SetParameter(attr.c_str(), "");
72 }
73 if (strcpy_s(saStatus, PARAM_VALUE_SIZE, "") != EOK) {
74 LOG(ERROR) << "fail to strcpy saStatus";
75 return;
76 }
77 }
78 }
79
AddAbnormalApp()80 void ModuleUpdateProducer::AddAbnormalApp()
81 {
82 char appInstallRes[PARAM_VALUE_SIZE] = "";
83 std::size_t resLength = strlen(BMS_INSTALL_FAIL);
84 for (const auto &hmpName : hmpNameSet_) {
85 std::string attr = std::string(BMS_RESULT_PREFIX) + "." + hmpName;
86 if (GetParameter(attr.c_str(), "", appInstallRes, PARAM_VALUE_SIZE) < 0) {
87 LOG(ERROR) << "failed to get parameter " << attr;
88 continue;
89 }
90 if (strncmp(appInstallRes, BMS_INSTALL_FAIL, resLength) != 0) {
91 (void)memset_s(appInstallRes, PARAM_VALUE_SIZE, 0, PARAM_VALUE_SIZE);
92 continue;
93 }
94 std::pair<int32_t, std::string> appResultPair = std::make_pair(APP_SERIAL_NUMBER, hmpName);
95 queue_.Put(appResultPair);
96 (void)memset_s(appInstallRes, PARAM_VALUE_SIZE, 0, PARAM_VALUE_SIZE);
97 }
98 }
99
Run()100 void ModuleUpdateProducer::Run()
101 {
102 LOG(INFO) << "ModuleUpdateProducer Produce";
103 char saValue[PARAM_VALUE_SIZE] = "";
104 int ret = GetParameter(SA_START, "", saValue, PARAM_VALUE_SIZE);
105 if (ret < 0) {
106 LOG(ERROR) << "failed to get parameter " << SA_START;
107 return;
108 }
109 char appValue[PARAM_VALUE_SIZE] = "";
110 if (GetParameter(BMS_START_INSTALL, "", appValue, PARAM_VALUE_SIZE) < 0) {
111 LOG(ERROR) << "failed to get parameter " << BMS_START_INSTALL;
112 return;
113 }
114 do {
115 if (exit_ == 1) {
116 LOG(INFO) << "producer and consumer stop";
117 queue_.Stop();
118 break;
119 }
120 if (strcmp(saValue, SA_ABNORMAL) == 0) {
121 SetParameter(SA_START, SA_NORMAL);
122 AddAbnormalSa();
123 }
124 // bms write all hmp install result at once
125 if (strcmp(appValue, BMS_REVERT) == 0) {
126 SetParameter(BMS_START_INSTALL, NOTIFY_BMS_REVERT);
127 AddAbnormalApp();
128 }
129 (void)memset_s(saValue, PARAM_VALUE_SIZE, 0, PARAM_VALUE_SIZE);
130 (void)memset_s(appValue, PARAM_VALUE_SIZE, 0, PARAM_VALUE_SIZE);
131 if (GetParameter(SA_START, "", saValue, PARAM_VALUE_SIZE) < 0 ||
132 GetParameter(BMS_START_INSTALL, "", appValue, PARAM_VALUE_SIZE) < 0) {
133 exit_ = 1;
134 }
135 } while (true);
136 LOG(INFO) << "producer exit";
137 }
138 } // SysInstaller
139 } // namespace OHOS