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 "rule_cluster.h"
17
18 #include <sstream>
19 #include <sys/stat.h>
20 #include <unistd.h>
21
22 #include "logger.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_TAG("FreezeDetector");
27
FreezeRuleCluster()28 FreezeRuleCluster::FreezeRuleCluster()
29 {
30 rules_.clear();
31 }
32
~FreezeRuleCluster()33 FreezeRuleCluster::~FreezeRuleCluster()
34 {
35 rules_.clear();
36 }
37
Init()38 bool FreezeRuleCluster::Init()
39 {
40 if (access(DEFAULT_RULE_FILE.c_str(), R_OK) != 0) {
41 HIVIEW_LOGE("cannot access rule file.");
42 return false;
43 }
44
45 if (CheckFileSize(DEFAULT_RULE_FILE) == false) {
46 HIVIEW_LOGE("bad rule file size.");
47 return false;
48 }
49
50 if (ParseRuleFile(DEFAULT_RULE_FILE) == false) {
51 HIVIEW_LOGE("failed to parse rule file.");
52 return false;
53 }
54
55 if (rules_.size() == 0) {
56 HIVIEW_LOGE("no rule in rule file.");
57 return false;
58 }
59
60 return true;
61 }
62
CheckFileSize(const std::string & path)63 bool FreezeRuleCluster::CheckFileSize(const std::string& path)
64 {
65 struct stat st;
66 if (stat(path.c_str(), &st) != 0) {
67 return false;
68 }
69 if (st.st_size > MAX_FILE_SIZE) {
70 return false;
71 }
72 return true;
73 }
74
ParseRuleFile(const std::string & file)75 bool FreezeRuleCluster::ParseRuleFile(const std::string& file)
76 {
77 xmlDoc* doc = xmlReadFile(file.c_str(), nullptr, 0);
78 if (doc == nullptr) {
79 HIVIEW_LOGE("failed to read rule file.");
80 return false;
81 }
82
83 xmlNode* root = xmlDocGetRootElement(doc);
84 if (root == nullptr) {
85 HIVIEW_LOGE("failed to get root element in rule file.");
86 xmlFreeDoc(doc);
87 doc = nullptr;
88 return false;
89 }
90
91 for (xmlNode* node = root; node; node = node->next) {
92 if (node->type != XML_ELEMENT_NODE) {
93 continue;
94 }
95 if (TAG_FREEZE == std::string((char*)(node->name))) {
96 ParseTagFreeze(node);
97 break;
98 }
99 }
100
101 xmlFreeDoc(doc);
102 doc = nullptr;
103 return true;
104 }
105
ParseTagFreeze(xmlNode * tag)106 void FreezeRuleCluster::ParseTagFreeze(xmlNode* tag)
107 {
108 for (xmlNode* node = tag->children; node; node = node->next) {
109 if (TAG_RULES == std::string((char*)(node->name))) {
110 ParseTagRules(node);
111 }
112 }
113 }
114
ParseTagRules(xmlNode * tag)115 void FreezeRuleCluster::ParseTagRules(xmlNode* tag)
116 {
117 for (xmlNode* node = tag->children; node; node = node->next) {
118 if (TAG_RULE == std::string((char*)(node->name))) {
119 ParseTagRule(node);
120 }
121 }
122 }
123
ParseTagRule(xmlNode * tag)124 void FreezeRuleCluster::ParseTagRule(xmlNode* tag)
125 {
126 std::string domain = GetAttributeValue<std::string>(tag, ATTRIBUTE_DOMAIN);
127 if (domain == "") {
128 HIVIEW_LOGE("null rule attribute:domain.");
129 return;
130 }
131 std::string stringId = GetAttributeValue<std::string>(tag, ATTRIBUTE_STRINGID);
132 if (stringId == "") {
133 HIVIEW_LOGE("null rule attribute:stringid.");
134 return;
135 }
136
137 FreezeRule rule = FreezeRule(domain, stringId);
138
139 for (xmlNode* node = tag->children; node; node = node->next) {
140 if (TAG_LINKS == std::string((char*)(node->name))) {
141 ParseTagLinks(node, rule);
142 }
143 }
144
145 if (rules_.find(domain + stringId) != rules_.end()) {
146 HIVIEW_LOGE("skip duplicated rule, stringid:%{public}s.", stringId.c_str());
147 return;
148 }
149
150 rules_[domain + stringId] = rule;
151 }
152
ParseTagLinks(xmlNode * tag,FreezeRule & rule)153 void FreezeRuleCluster::ParseTagLinks(xmlNode* tag, FreezeRule& rule)
154 {
155 for (xmlNode* node = tag->children; node; node = node->next) {
156 if (TAG_EVENT == std::string((char*)(node->name))) {
157 std::string domain = GetAttributeValue<std::string>(node, ATTRIBUTE_DOMAIN);
158 if (domain == "") {
159 HIVIEW_LOGE("null event attribute:domain.");
160 return;
161 }
162 std::string stringId = GetAttributeValue<std::string>(node, ATTRIBUTE_STRINGID);
163 if (stringId == "") {
164 HIVIEW_LOGE("null event attribute:stringid.");
165 return;
166 }
167
168 long window = GetAttributeValue<long>(node, ATTRIBUTE_WINDOW);
169
170 FreezeResult result = FreezeResult(window, domain, stringId);
171 ParseTagEvent(node, result);
172 rule.AddResult(domain, stringId, result);
173
174 bool principalPoint = false;
175 if (rule.GetDomain() == domain && rule.GetStringId() == stringId) {
176 principalPoint = true;
177 }
178 if (result.GetScope() == "app") {
179 applicationPairs_[stringId] = std::pair<std::string, bool>(domain, principalPoint);
180 } else {
181 systemPairs_[stringId] = std::pair<std::string, bool>(domain, principalPoint);
182 }
183 }
184 }
185 }
186
ParseTagEvent(xmlNode * tag,FreezeResult & result)187 void FreezeRuleCluster::ParseTagEvent(xmlNode* tag, FreezeResult& result)
188 {
189 for (xmlNode* node = tag->children; node; node = node->next) {
190 if (TAG_RESULT == std::string((char*)(node->name))) {
191 ParseTagResult(node, result);
192 break;
193 }
194 }
195 }
196
ParseTagResult(xmlNode * tag,FreezeResult & result)197 void FreezeRuleCluster::ParseTagResult(xmlNode* tag, FreezeResult& result)
198 {
199 unsigned long code = GetAttributeValue<unsigned long>(tag, ATTRIBUTE_CODE);
200 std::string scope = GetAttributeValue<std::string>(tag, ATTRIBUTE_SCOPE);
201 std::string samePackage = GetAttributeValue<std::string>(tag, ATTRIBUTE_SAME_PACKAGE);
202 std::string action = GetAttributeValue<std::string>(tag, attributeAction);
203
204 result.SetId(code);
205 result.SetScope(scope);
206 result.SetSamePackage(samePackage);
207 result.SetAction(action);
208 }
209
210 template<typename T>
GetAttributeValue(xmlNode * node,const std::string & name)211 T FreezeRuleCluster::GetAttributeValue(xmlNode* node, const std::string& name)
212 {
213 xmlChar* prop = xmlGetProp(node, (xmlChar*)(name.c_str()));
214 std::string propa = "";
215 if (prop != nullptr) {
216 propa = (char*)prop;
217 }
218 std::istringstream istr(propa);
219 T value;
220 istr >> value;
221 xmlFree(prop);
222 return value;
223 }
224
GetResult(const WatchPoint & watchPoint,std::vector<FreezeResult> & list)225 bool FreezeRuleCluster::GetResult(const WatchPoint& watchPoint, std::vector<FreezeResult>& list)
226 {
227 std::string domain = watchPoint.GetDomain();
228 std::string stringId = watchPoint.GetStringId();
229 if (rules_.find(domain + stringId) == rules_.end()) {
230 return false;
231 }
232 auto map = rules_[domain + stringId].GetMap();
233 for (auto& i : map) {
234 list.push_back(i.second);
235 }
236
237 if (list.empty()) {
238 return false;
239 }
240 return true;
241 }
242
AddResult(const std::string & domain,const std::string & stringId,const FreezeResult & result)243 void FreezeRule::AddResult(const std::string& domain, const std::string& stringId, const FreezeResult& result)
244 {
245 if (results_.find(domain + stringId) != results_.end()) {
246 HIVIEW_LOGE("skip duplicated event tag, stringid:%{public}s.", stringId.c_str());
247 return;
248 }
249
250 results_[domain + stringId] = result;
251 }
252
GetResult(const std::string & domain,const std::string & stringId,FreezeResult & result)253 bool FreezeRule::GetResult(const std::string& domain, const std::string& stringId, FreezeResult& result)
254 {
255 if (results_.find(domain + stringId) == results_.end()) {
256 HIVIEW_LOGE("failed to find rule result, domain:%{public}s stringid:%{public}s.",
257 domain.c_str(), stringId.c_str());
258 return false;
259 }
260
261 result = results_[domain + stringId]; // take result back
262 return true;
263 }
264 } // namespace HiviewDFX
265 } // namespace OHOS
266