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