• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       moduleNameSet_(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(bootValue, BOOT_SUCCESS_VALUE) != 0 &&
67                 (strcmp(saStatus, LOAD_FAIL) == 0 || strcmp(saStatus, CRASH) == 0)) {
68                 queue_.Put(saStatusPair);
69             }
70             SetParameter(attr.c_str(), "");
71         }
72         if (strcpy_s(saStatus, PARAM_VALUE_SIZE, "") != EOK) {
73             LOG(ERROR) << "fail to strcpy saStatus";
74             return;
75         }
76     }
77 }
78 
AddAbnormalApp()79 void ModuleUpdateProducer::AddAbnormalApp()
80 {
81     char appInstallRes[PARAM_VALUE_SIZE] = "";
82     std::size_t resLength = strlen(BMS_INSTALL_FAIL);
83     for (const auto &moduleName : moduleNameSet_) {
84         std::string attr = std::string(BMS_RESULT_PREFIX) + "." + moduleName;
85         if (GetParameter(attr.c_str(), "", appInstallRes, PARAM_VALUE_SIZE) < 0) {
86             LOG(ERROR) << "failed to get parameter " << attr;
87             continue;
88         }
89         if (strncmp(appInstallRes, BMS_INSTALL_FAIL, resLength) != 0) {
90             (void)memset_s(appInstallRes, PARAM_VALUE_SIZE, 0, PARAM_VALUE_SIZE);
91             continue;
92         }
93         std::pair<int32_t, std::string> appResultPair = std::make_pair(APP_SERIAL_NUMBER, moduleName);
94         queue_.Put(appResultPair);
95         (void)memset_s(appInstallRes, PARAM_VALUE_SIZE, 0, PARAM_VALUE_SIZE);
96     }
97 }
98 
Run()99 void ModuleUpdateProducer::Run()
100 {
101     LOG(INFO) << "ModuleUpdateProducer Produce";
102     char saValue[PARAM_VALUE_SIZE] = "";
103     int ret = GetParameter(SA_START, "", saValue, PARAM_VALUE_SIZE);
104     if (ret < 0) {
105         LOG(ERROR) << "failed to get parameter " << SA_START;
106         return;
107     }
108     char appValue[PARAM_VALUE_SIZE] = "";
109     if (GetParameter(BMS_START_INSTALL, "", appValue, PARAM_VALUE_SIZE) < 0) {
110         LOG(ERROR) << "failed to get parameter " << BMS_START_INSTALL;
111         return;
112     }
113     do {
114         if (exit_ == 1) {
115             LOG(INFO) << "producer and consumer stop";
116             queue_.Stop();
117             break;
118         }
119         SetParameter(SA_START, SA_NORMAL);
120         SetParameter(BMS_START_INSTALL, NOTIFY_BMS_REVERT);
121         if (strcmp(saValue, SA_ABNORMAL) == 0) {
122             AddAbnormalSa();
123         }
124         // bms write all hmp install result at once
125         if (strcmp(appValue, BMS_REVERT) == 0) {
126             AddAbnormalApp();
127         }
128         (void)memset_s(saValue, PARAM_VALUE_SIZE, 0, PARAM_VALUE_SIZE);
129         (void)memset_s(appValue, PARAM_VALUE_SIZE, 0, PARAM_VALUE_SIZE);
130         exit_ = 1;
131     } while (true);
132     LOG(INFO) << "producer exit";
133 }
134 } // SysInstaller
135 } // namespace OHOS