1 /*
2 * Copyright (c) 2022 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 #include "hisysevent_adapter.h"
16
17 #include <string>
18
19 #include "def.h"
20
21 // hisysevent report 500 times per 5s
22 #define HISYSEVENT_PERIOD 5
23 #define HISYSEVENT_THRESHOLD 500
24 #include "hisysevent.h"
25 #include "sam_log.h"
26
27 namespace OHOS {
28 using namespace OHOS::HiviewDFX;
29 namespace {
30 constexpr const char* ADD_SYSTEMABILITY_FAIL = "ADD_SYSTEMABILITY_FAIL";
31 constexpr const char* CALLER_UID = "CALLER_UID";
32 constexpr const char* SAID = "SAID";
33 constexpr const char* COUNT = "COUNT";
34 constexpr const char* FILE_NAME = "FILE_NAME";
35 constexpr const char* GETSA__TAG = "GETSA_FREQUENCY";
36
37 constexpr const char* REASON = "REASON";
38 constexpr const char* ONDEMAND_SA_LOAD_FAIL = "ONDEMAND_SA_LOAD_FAIL";
39 constexpr const char* ONDEMAND_SA_LOAD = "ONDEMAND_SA_LOAD";
40 constexpr const char* EVENT = "EVENT";
41 constexpr const char* SA_CRASH = "SA_CRASH";
42 constexpr const char* ONDEMAND_SA_UNLOAD = "ONDEMAND_SA_UNLOAD";
43 constexpr const char* SA_UNLOAD_FAIL = "SA_UNLOAD_FAIL";
44 constexpr const char* SA_LOAD_DURATION = "SA_LOAD_DURATION";
45 constexpr const char* SA_UNLOAD_DURATION = "SA_UNLOAD_DURATION";
46 constexpr const char* SA_MAIN_EXIT = "SA_MAIN_EXIT";
47 constexpr const char* PROCESS_START_FAIL = "PROCESS_START_FAIL";
48 constexpr const char* PROCESS_STOP_FAIL = "PROCESS_STOP_FAIL";
49 constexpr const char* PROCESS_START_DURATION = "PROCESS_START_DURATION";
50 constexpr const char* PROCESS_STOP_DURATION = "PROCESS_STOP_DURATION";
51 constexpr const char* PROCESS_NAME = "PROCESS_NAME";
52 constexpr const char* PID = "PID";
53 constexpr const char* UID = "UID";
54 constexpr const char* DURATION = "DURATION";
55 constexpr const char* KEY_STAGE = "KEY_STAGE";
56 constexpr int32_t CONTAINER_SA_MIN = 0x00010500; //66816
57 constexpr int32_t CONTAINER_SA_MAX = 0x0001055f; //66911
58 }
59
IsInCrashWhiteList(int32_t saId)60 static bool IsInCrashWhiteList(int32_t saId)
61 {
62 std::vector<int> whiteList = { 1205, 1213, 1215, 9999, 65537, 65830,
63 65850, 65888, 69930, 131071, 345135 };
64 for (auto sa : whiteList) {
65 if (saId == sa) {
66 return true;
67 }
68 }
69 if (saId >= CONTAINER_SA_MIN && saId <= CONTAINER_SA_MAX) {
70 return true;
71 }
72 return false;
73 }
74
ReportSaCrash(int32_t saId)75 void ReportSaCrash(int32_t saId)
76 {
77 if (IsInCrashWhiteList(saId)) {
78 HILOGD("report SA:%{public}d is in crash whitelist.", saId);
79 return;
80 }
81 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
82 SA_CRASH,
83 HiSysEvent::EventType::FAULT,
84 SAID, saId);
85 if (ret != 0) {
86 HILOGE("report sa crash failed! SA:%{public}d, ret:%{public}d.", saId, ret);
87 }
88 }
89
ReportSaUnLoadFail(int32_t saId,int32_t pid,int32_t uid,const std::string & reason)90 void ReportSaUnLoadFail(int32_t saId, int32_t pid, int32_t uid, const std::string& reason)
91 {
92 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
93 SA_UNLOAD_FAIL,
94 HiSysEvent::EventType::FAULT,
95 SAID, saId,
96 PID, pid,
97 UID, uid,
98 REASON, reason);
99 if (ret != 0) {
100 HILOGE("report sa unload fail event failed! SA:%{public}d, ret %{public}d.", saId, ret);
101 }
102 }
103
ReportSaDuration(const std::string & eventName,int32_t saId,int32_t keyStage,int64_t duration)104 static void ReportSaDuration(const std::string& eventName, int32_t saId, int32_t keyStage, int64_t duration)
105 {
106 if (duration <= 0) {
107 return;
108 }
109 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
110 eventName,
111 HiSysEvent::EventType::BEHAVIOR,
112 SAID, saId,
113 KEY_STAGE, keyStage,
114 DURATION, duration);
115 if (ret != 0) {
116 HILOGE("report event:%{public}s failed! SA:%{public}d, ret:%{public}d.",
117 eventName.c_str(), saId, ret);
118 }
119 }
120
ReportSaMainExit(const std::string & reason)121 void ReportSaMainExit(const std::string& reason)
122 {
123 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
124 SA_MAIN_EXIT,
125 HiSysEvent::EventType::FAULT,
126 REASON, reason);
127 if (ret != 0) {
128 HILOGE("report sa main exit event failed! ret:%{public}d.", ret);
129 }
130 }
131
ReportSaLoadDuration(int32_t saId,int32_t keyStage,int64_t duration)132 void ReportSaLoadDuration(int32_t saId, int32_t keyStage, int64_t duration)
133 {
134 ReportSaDuration(SA_LOAD_DURATION, saId, keyStage, duration);
135 }
136
ReportSaUnLoadDuration(int32_t saId,int32_t keyStage,int64_t duration)137 void ReportSaUnLoadDuration(int32_t saId, int32_t keyStage, int64_t duration)
138 {
139 ReportSaDuration(SA_UNLOAD_DURATION, saId, keyStage, duration);
140 }
141
ReportProcessDuration(const std::string & eventName,const std::string & processName,int32_t pid,int32_t uid,int64_t duration)142 static void ReportProcessDuration(const std::string& eventName, const std::string& processName,
143 int32_t pid, int32_t uid, int64_t duration)
144 {
145 if (duration <= 0) {
146 return;
147 }
148 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
149 eventName,
150 HiSysEvent::EventType::BEHAVIOR,
151 PROCESS_NAME, processName,
152 PID, pid,
153 UID, uid,
154 DURATION, duration);
155 if (ret != 0) {
156 HILOGE("report event:%{public}s failed! process:%{public}s, ret:%{public}d.",
157 eventName.c_str(), processName.c_str(), ret);
158 }
159 }
160
ReportProcessStartDuration(const std::string & processName,int32_t pid,int32_t uid,int64_t duration)161 void ReportProcessStartDuration(const std::string& processName, int32_t pid, int32_t uid, int64_t duration)
162 {
163 ReportProcessDuration(PROCESS_START_DURATION, processName, pid, uid, duration);
164 }
165
ReportProcessStopDuration(const std::string & processName,int32_t pid,int32_t uid,int64_t duration)166 void ReportProcessStopDuration(const std::string& processName, int32_t pid, int32_t uid, int64_t duration)
167 {
168 ReportProcessDuration(PROCESS_STOP_DURATION, processName, pid, uid, duration);
169 }
170
ReportProcessFail(const std::string & eventName,const std::string & processName,int32_t pid,int32_t uid,const std::string & reason)171 static void ReportProcessFail(const std::string& eventName, const std::string& processName,
172 int32_t pid, int32_t uid, const std::string& reason)
173 {
174 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
175 eventName,
176 HiSysEvent::EventType::FAULT,
177 PROCESS_NAME, processName,
178 PID, pid,
179 UID, uid,
180 REASON, reason);
181 if (ret != 0) {
182 HILOGE("report event:%{public}s failed! process:%{public}s, ret:%{public}d.",
183 eventName.c_str(), processName.c_str(), ret);
184 }
185 }
186
ReportProcessStartFail(const std::string & processName,int32_t pid,int32_t uid,const std::string & reason)187 void ReportProcessStartFail(const std::string& processName, int32_t pid, int32_t uid, const std::string& reason)
188 {
189 ReportProcessFail(PROCESS_START_FAIL, processName, pid, uid, reason);
190 }
191
ReportProcessStopFail(const std::string & processName,int32_t pid,int32_t uid,const std::string & reason)192 void ReportProcessStopFail(const std::string& processName, int32_t pid, int32_t uid, const std::string& reason)
193 {
194 ReportProcessFail(PROCESS_STOP_FAIL, processName, pid, uid, reason);
195 }
196
ReportSamgrSaLoadFail(int32_t said,int32_t pid,int32_t uid,const std::string & reason)197 void ReportSamgrSaLoadFail(int32_t said, int32_t pid, int32_t uid, const std::string& reason)
198 {
199 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
200 ONDEMAND_SA_LOAD_FAIL,
201 HiSysEvent::EventType::FAULT,
202 SAID, said,
203 PID, pid,
204 UID, uid,
205 REASON, reason);
206 if (ret != 0) {
207 HILOGE("hisysevent report samgr sa load fail event failed! ret %{public}d.", ret);
208 }
209 }
210
ReportSamgrSaLoad(int32_t said,int32_t pid,int32_t uid,int32_t eventId)211 void ReportSamgrSaLoad(int32_t said, int32_t pid, int32_t uid, int32_t eventId)
212 {
213 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
214 ONDEMAND_SA_LOAD,
215 HiSysEvent::EventType::BEHAVIOR,
216 SAID, said,
217 PID, pid,
218 UID, uid,
219 EVENT, eventId);
220 if (ret != 0) {
221 HILOGE("hisysevent report samgr sa load event failed! ret %{public}d.", ret);
222 }
223 }
224
ReportSamgrSaUnload(int32_t said,int32_t pid,int32_t uid,int32_t eventId)225 void ReportSamgrSaUnload(int32_t said, int32_t pid, int32_t uid, int32_t eventId)
226 {
227 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
228 ONDEMAND_SA_UNLOAD,
229 HiSysEvent::EventType::BEHAVIOR,
230 SAID, said,
231 PID, pid,
232 UID, uid,
233 EVENT, eventId);
234 if (ret != 0) {
235 HILOGE("hisysevent report samgr sa unload event failed! ret %{public}d.", ret);
236 }
237 }
238
IsOpenSoWhiteList(int32_t saId)239 static bool IsOpenSoWhiteList(int32_t saId)
240 {
241 std::vector<int> whiteList = { 4606 };
242 for (auto sa : whiteList) {
243 if (saId == sa) {
244 return true;
245 }
246 }
247 return false;
248 }
249
ReportAddSystemAbilityFailed(int32_t said,int32_t pid,int32_t uid,const std::string & filaName)250 void ReportAddSystemAbilityFailed(int32_t said, int32_t pid, int32_t uid, const std::string& filaName)
251 {
252 if (IsOpenSoWhiteList(said)) {
253 HILOGD("report SA:%{public}d is open so whitelist.", said);
254 return;
255 }
256 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
257 ADD_SYSTEMABILITY_FAIL,
258 HiSysEvent::EventType::FAULT,
259 SAID, said,
260 PID, pid,
261 UID, uid,
262 FILE_NAME, filaName);
263 if (ret != 0) {
264 HILOGE("hisysevent report add system ability event failed! ret %{public}d.", ret);
265 }
266 }
267
ReportGetSAFrequency(uint32_t callerUid,uint32_t said,int32_t count)268 void ReportGetSAFrequency(uint32_t callerUid, uint32_t said, int32_t count)
269 {
270 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
271 GETSA__TAG,
272 HiSysEvent::EventType::STATISTIC,
273 CALLER_UID, callerUid,
274 SAID, said,
275 COUNT, count);
276 if (ret != 0) {
277 HILOGD("hisysevent report get sa frequency failed! ret %{public}d.", ret);
278 }
279 }
280
WatchDogSendEvent(int32_t pid,uint32_t uid,const std::string & sendMsg,const std::string & eventName)281 void WatchDogSendEvent(int32_t pid, uint32_t uid, const std::string& sendMsg,
282 const std::string& eventName)
283 {
284 int ret = HiSysEventWrite(HiSysEvent::Domain::SAMGR,
285 eventName,
286 HiSysEvent::EventType::FAULT,
287 "PID", pid,
288 "UID", uid,
289 "MSG", sendMsg);
290 if (ret != 0) {
291 HILOGE("hisysevent report watchdog failed! ret %{public}d.", ret);
292 }
293 }
294 } // OHOS
295