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