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 "hiview_logger.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_LABEL(0xD002D01, "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 if (result.GetScope() == "sys") {
181                 systemPairs_[stringId] = std::pair<std::string, bool>(domain, principalPoint);
182             } else {
183                 sysWarningPairs_[stringId] = std::pair<std::string, bool>(domain, principalPoint);
184             }
185         }
186     }
187 }
188 
ParseTagEvent(xmlNode * tag,FreezeResult & result)189 void FreezeRuleCluster::ParseTagEvent(xmlNode* tag, FreezeResult& result)
190 {
191     for (xmlNode* node = tag->children; node; node = node->next) {
192         if (TAG_RESULT == std::string((char*)(node->name))) {
193             ParseTagResult(node, result);
194             break;
195         }
196     }
197 }
198 
ParseTagResult(xmlNode * tag,FreezeResult & result)199 void FreezeRuleCluster::ParseTagResult(xmlNode* tag, FreezeResult& result)
200 {
201     unsigned long code = GetAttributeValue<unsigned long>(tag, ATTRIBUTE_CODE);
202     std::string scope = GetAttributeValue<std::string>(tag, ATTRIBUTE_SCOPE);
203     std::string samePackage = GetAttributeValue<std::string>(tag, ATTRIBUTE_SAME_PACKAGE);
204     std::string action = GetAttributeValue<std::string>(tag, attributeAction);
205     std::string ffrt = GetAttributeValue<std::string>(tag, attributeFfrt);
206 
207     result.SetId(code);
208     result.SetScope(scope);
209     result.SetSamePackage(samePackage);
210     result.SetAction(action);
211     result.SetFfrt(ffrt);
212 }
213 
214 template<typename T>
GetAttributeValue(xmlNode * node,const std::string & name)215 T FreezeRuleCluster::GetAttributeValue(xmlNode* node, const std::string& name)
216 {
217     xmlChar* prop = xmlGetProp(node, (xmlChar*)(name.c_str()));
218     std::string propa = "";
219     if (prop != nullptr) {
220         propa = (char*)prop;
221     }
222     std::istringstream istr(propa);
223     T value;
224     istr >> value;
225     xmlFree(prop);
226     return value;
227 }
228 
GetResult(const WatchPoint & watchPoint,std::vector<FreezeResult> & list)229 bool FreezeRuleCluster::GetResult(const WatchPoint& watchPoint, std::vector<FreezeResult>& list)
230 {
231     std::string domain = watchPoint.GetDomain();
232     std::string stringId = watchPoint.GetStringId();
233     if (rules_.find(domain + stringId) == rules_.end()) {
234         return false;
235     }
236     auto map = rules_[domain + stringId].GetMap();
237     for (auto& i : map) {
238         list.push_back(i.second);
239     }
240 
241     if (list.empty()) {
242         return false;
243     }
244     return true;
245 }
246 
GetSysWarningPairs() const247 std::map<std::string, std::pair<std::string, bool>> FreezeRuleCluster::GetSysWarningPairs() const
248 {
249     return sysWarningPairs_;
250 }
251 
AddResult(const std::string & domain,const std::string & stringId,const FreezeResult & result)252 void FreezeRule::AddResult(const std::string& domain, const std::string& stringId, const FreezeResult& result)
253 {
254     if (results_.find(domain + stringId) != results_.end()) {
255         HIVIEW_LOGE("skip duplicated event tag, stringid:%{public}s.", stringId.c_str());
256         return;
257     }
258 
259     results_[domain + stringId] = result;
260 }
261 
GetResult(const std::string & domain,const std::string & stringId,FreezeResult & result)262 bool FreezeRule::GetResult(const std::string& domain, const std::string& stringId, FreezeResult& result)
263 {
264     if (results_.find(domain + stringId) == results_.end()) {
265         HIVIEW_LOGE("failed to find rule result, domain:%{public}s stringid:%{public}s.",
266             domain.c_str(), stringId.c_str());
267         return false;
268     }
269 
270     result = results_[domain + stringId]; // take result back
271     return true;
272 }
273 } // namespace HiviewDFX
274 } // namespace OHOS
275