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 #include "event_source_example.h"
16
17 #include <fstream>
18 #include <iostream>
19 #include <string>
20
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <sys/epoll.h>
24 #include <sys/inotify.h>
25
26 #include "event_loop.h"
27 #include "plugin_factory.h"
28
29 namespace OHOS {
30 namespace HiviewDFX {
31 REGISTER(EventSourceExample);
32 std::set<std::string> EventSourceExample::count = std::set<std::string>();
EventSourceExample()33 EventSourceExample::EventSourceExample() : inotifyFd_(0)
34 {
35 printf("EventSourceExample::EventSourceExample()\n");
36 count.insert("EventSourceExample");
37 }
38
~EventSourceExample()39 EventSourceExample::~EventSourceExample()
40 {
41 printf("EventSourceExample::~EventSourceExample()\n");
42 count.erase("EventSourceExample");
43 }
44
OnLoad()45 void EventSourceExample::OnLoad()
46 {
47 printf("EventSourceExample::OnLoad.\n");
48
49 int isCreate = ::mkdir(SYSTEM_FAULT_LOG_PATH.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
50 if (!isCreate) {
51 printf("create path:%s \n", SYSTEM_FAULT_LOG_PATH.c_str());
52 }
53
54 CreateWatchFile(SYSTEM_FAULT_LOG_PATH + "/aaa");
55 CreateWatchFile(SYSTEM_FAULT_LOG_PATH + "/bbb");
56 CreateWatchFile(SYSTEM_FAULT_LOG_PATH + "/ccc");
57
58 CreateWatchFile(SYSTEM_FAULT_LOG_PATH + "/testaa");
59 CreateWatchFile(SYSTEM_FAULT_LOG_PATH + "/testbb");
60 CreateWatchFile(SYSTEM_FAULT_LOG_PATH + "/testcc");
61 }
62
CreateWatchFile(const std::string & path)63 void EventSourceExample::CreateWatchFile(const std::string& path)
64 {
65 std::ofstream file(path);
66 if (!file.good()) {
67 printf("Fail to create watch file:%s.\n", path.c_str());
68 return;
69 }
70 file << "" << std::endl;
71 file.close();
72 }
73
OnUnload()74 void EventSourceExample::OnUnload()
75 {
76 printf("EventSourceExample::OnUnload.\n");
77 }
78
StartEventSource()79 void EventSourceExample::StartEventSource()
80 {
81 printf("EventSourceExample::StartEventSource.\n");
82 GetWorkLoop()->AddFileDescriptorEventCallback("EventSourceExample",
83 std::static_pointer_cast<EventSourceExample>(shared_from_this()));
84 }
85
OnFileDescriptorEvent(int fd,int type)86 bool EventSourceExample::OnFileDescriptorEvent(int fd, int type)
87 {
88 printf("EventSourceExample::OnEvent fd:%d, type:%d, inotifyFd_:%d.\n", fd, type, inotifyFd_);
89 const int bufSize = 2048;
90 char buffer[bufSize] = {0};
91 char *offset = nullptr;
92 struct inotify_event *event = nullptr;
93 if (inotifyFd_ < 0) {
94 printf("EventSourceExample Invalid inotify fd:%d", inotifyFd_);
95 return false;
96 }
97
98 int len = read(inotifyFd_, buffer, bufSize);
99 if (len < 0) {
100 printf("EventSourceExample failed to read event");
101 return false;
102 }
103
104 offset = buffer;
105 event = (struct inotify_event *)buffer;
106 while ((reinterpret_cast<char *>(event) - buffer) < len) {
107 for (const auto &it : fileMap_) {
108 if (it.second != event->wd) {
109 continue;
110 }
111
112 if (event->name[event->len - 1] != '\0') {
113 event->name[event->len - 1] = '\0';
114 }
115 std::string filePath = it.first + "/" + std::string(event->name);
116 printf("handle file event in %s \n", filePath.c_str());
117 std::ifstream fileS(filePath);
118 if (!fileS) {
119 continue;
120 }
121 std::string fileStr;
122 fileS >> fileStr;
123 if (fileStr.empty()) {
124 printf("fileStr.empty()\n");
125 continue;
126 }
127 fileS.close();
128 CreateAndPublishEvent(filePath);
129 }
130 auto tmpLen = sizeof(struct inotify_event) + event->len;
131 event = (struct inotify_event *)(offset + tmpLen);
132 offset += tmpLen;
133 }
134 return true;
135 }
136
GetPollFd()137 int32_t EventSourceExample::GetPollFd()
138 {
139 printf("EventSourceExample::GetPollFd.\n");
140 if (inotifyFd_ > 0) {
141 return inotifyFd_;
142 }
143
144 inotifyFd_ = inotify_init();
145 if (inotifyFd_ == -1) {
146 printf("failed to init inotify: %s.\n", strerror(errno));
147 return -1;
148 }
149
150 int wd = inotify_add_watch(inotifyFd_, SYSTEM_FAULT_LOG_PATH.c_str(), IN_CLOSE_WRITE | IN_MOVED_TO);
151 if (wd < 0) {
152 printf("failed to add watch entry : %s(%s).\n", strerror(errno), SYSTEM_FAULT_LOG_PATH.c_str());
153 close(inotifyFd_);
154 inotifyFd_ = -1;
155 return -1;
156 }
157
158 printf("GetPollFd %d \n", inotifyFd_);
159 fileMap_[SYSTEM_FAULT_LOG_PATH] = wd;
160 return inotifyFd_;
161 }
162
GetPollType()163 int32_t EventSourceExample::GetPollType()
164 {
165 printf("EventSourceExample::GetPollType.\n");
166 return EPOLLIN;
167 }
168
CreateAndPublishEvent(const std::string & file)169 void EventSourceExample::CreateAndPublishEvent(const std::string &file)
170 {
171 // create a pipeline event
172 auto event = std::make_shared<EventSourceExampleEvent>(file, static_cast<PipelineEventProducer *>(this));
173
174 // add general information
175 const int demoBufSize = 128;
176 auto bufPtr = reinterpret_cast<char *>(malloc(demoBufSize));
177 if (bufPtr == nullptr) {
178 return;
179 }
180 event->data_ = bufPtr;
181 event->addon_ = file;
182 event->isPipeline_ = true;
183 // add special information
184 event->messageType_ = Event::MessageType::FAULT_EVENT;
185 if (file == (SYSTEM_FAULT_LOG_PATH + "/aaa")) {
186 event->eventId_ = PIPELINE_EVENT_ID_AAA;
187 } else if (file == (SYSTEM_FAULT_LOG_PATH + "/bbb")) {
188 event->eventId_ = PIPELINE_EVENT_ID_BBB;
189 } else if (file == (SYSTEM_FAULT_LOG_PATH + "/ccc")) {
190 event->eventId_ = PIPELINE_EVENT_ID_CCC;
191 event->SetValue("Pipeline", "Repack");
192 } else if (file == (SYSTEM_FAULT_LOG_PATH + "/testaa")) {
193 event->eventId_ = PIPELINE_EVENT_ID_TAA;
194 event->eventName_ = "testaa";
195 } else if (file == (SYSTEM_FAULT_LOG_PATH + "/testbb")) {
196 event->eventId_ = 0;
197 event->eventName_ = "testbb";
198 } else if (file == (SYSTEM_FAULT_LOG_PATH + "/testcc")) {
199 event->eventId_ = 0;
200 event->eventName_ = "testcc";
201 } else {
202 return;
203 }
204 PublishPipelineEvent(std::dynamic_pointer_cast<PipelineEvent>(event));
205 }
206
Recycle(PipelineEvent * event)207 void EventSourceExample::Recycle(PipelineEvent *event)
208 {
209 printf("EventSourceExample::Recycle.\n");
210 auto eventPtr = static_cast<EventSourceExampleEvent *>(event);
211 if (eventPtr == nullptr || eventPtr->data_ == nullptr) {
212 return;
213 }
214 free(eventPtr->data_);
215 eventPtr->data_ = nullptr;
216 printf("Recycle event:%s.\n", eventPtr->addon_.c_str());
217 }
218
PauseDispatch(std::weak_ptr<Plugin> plugin)219 void EventSourceExample::PauseDispatch(std::weak_ptr<Plugin> plugin)
220 {
221 auto requester = plugin.lock();
222 if (requester != nullptr) {
223 printf("process pause dispatch event from plugin:%s.\n", requester->GetName().c_str());
224 }
225 }
226 } // namespace HiviewDFX
227 } // namespace OHOS
228