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