1 /*
2 * Copyright (c) 2025 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 "event_socket_factory.h"
17
18 #include "def.h"
19 #include "hisysevent.h"
20 #include "hilog/log.h"
21 #include "raw_data_base_def.h"
22
23 #include <algorithm>
24 #include <list>
25 #include <string>
26
27 #undef LOG_DOMAIN
28 #define LOG_DOMAIN 0xD002D08
29
30 #undef LOG_TAG
31 #define LOG_TAG "EVENT_SOCKET_FACTORY"
32
33 namespace OHOS {
34 namespace HiviewDFX {
35 namespace {
36 struct sockaddr_un normalAddr = {
37 .sun_family = AF_UNIX,
38 .sun_path = "/dev/unix/socket/hisysevent",
39 };
40
41 struct sockaddr_un higherPriorityAddr = {
42 .sun_family = AF_UNIX,
43 .sun_path = "/dev/unix/socket/hisysevent_fast",
44 };
45
IsHigherPriorityEventName(const std::list<std::string> & events,const std::string & name)46 inline bool IsHigherPriorityEventName(const std::list<std::string>& events, const std::string& name)
47 {
48 auto iter = std::find(events.begin(), events.end(), name);
49 return iter != events.end();
50 }
51
IsHigherPriorityAafwkEvent(const std::string & name)52 bool IsHigherPriorityAafwkEvent(const std::string& name)
53 {
54 static std::list<std::string> events {
55 "APP_INPUT_BLOCK", "BUSSINESS_THREAD_BLOCK_3S", "BUSSINESS_THREAD_BLOCK_6S",
56 "LIFECYCLE_HALF_TIMEOUT", "LIFECYCLE_TIME_OUT", "THREAD_BLOCK_3S", "THREAD_BLOCK_6S" };
57 return IsHigherPriorityEventName(events, name);
58 }
59
IsHigerPriorityAceEvent(const std::string & name)60 bool IsHigerPriorityAceEvent(const std::string& name)
61 {
62 static std::list<std::string> events {
63 "UI_BLOCK_3S", "UI_BLOCK_6S", "UI_BLOCK_RECOVERED" };
64 return IsHigherPriorityEventName(events, name);
65 }
66
IsHigerPriorityFrameworkEvent(const std::string & name)67 bool IsHigerPriorityFrameworkEvent(const std::string& name)
68 {
69 static std::list<std::string> events {
70 "IPC_FULL", "SERVICE_BLOCK", "SERVICE_TIMEOUT", "SERVICE_WARNING" };
71 return IsHigherPriorityEventName(events, name);
72 }
73
IsHigherPriorityEvent(const std::string & domain,const std::string & name,int type)74 bool IsHigherPriorityEvent(const std::string& domain, const std::string& name, int type)
75 {
76 if (domain == "AAFWK") {
77 return IsHigherPriorityAafwkEvent(name);
78 } else if (domain == "ACE") {
79 return IsHigerPriorityAceEvent(name);
80 } else if (domain == "FRAMEWORK") {
81 return IsHigerPriorityFrameworkEvent(name);
82 } else if (domain == "RELIABILITY") {
83 return type == HiSysEvent::EventType::FAULT;
84 } else if (domain == "GRAPHIC") {
85 return name == "NO_DRAW";
86 } else if (domain == "MULTIMODALINPUT") {
87 return name == "TARGET_POINTER_EVENT_FAILURE";
88 } else if (domain == "POWER") {
89 return name == "SCREEN_ON_TIMEOUT";
90 } else if (domain == "WINDOWMANAGER") {
91 return name == "NO_FOCUS_WINDOW";
92 } else if (domain == "SCHEDULE_EXT") {
93 return name == "SYSTEM_LOAD_LEVEL_CHANGED";
94 } else {
95 return false;
96 }
97 }
98
ParseEventInfo(RawData & data,std::string & domain,std::string & name,int & type)99 void ParseEventInfo(RawData& data, std::string& domain, std::string& name, int& type)
100 {
101 if (size_t len = data.GetDataLength(); len < sizeof(int32_t) + sizeof(struct HiSysEventHeader)) {
102 HILOG_WARN(LOG_CORE, "length[%{public}zu] of data is invalid", len);
103 return;
104 }
105 auto originalData = data.GetData();
106 struct HiSysEventHeader header = *(reinterpret_cast<struct HiSysEventHeader*>(originalData + sizeof(int32_t)));
107 domain = std::string(header.domain);
108 name = std::string(header.name);
109 type = static_cast<int>(header.type) + 1; // transform type to HiSysEvent::EventType
110 }
111 }
112
GetEventSocket(RawData & data)113 EventSocket& EventSocketFactory::GetEventSocket(RawData& data)
114 {
115 std::string domain;
116 std::string name;
117 int type = HiSysEvent::EventType::FAULT;
118 ParseEventInfo(data, domain, name, type);
119 return IsHigherPriorityEvent(domain, name, type) ? higherPriorityAddr : normalAddr;
120 }
121 }
122 }
123