1 /*
2 * Copyright (c) 2023 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 "sanitizerd_monitor.h"
17
18 #include <cerrno>
19 #include <climits>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 #include <sys/inotify.h>
24 #include <sys/ioctl.h>
25 #include <unistd.h>
26 #include "securec.h"
27
28 #include "sanitizerd_log.h"
29 #include "logger.h"
30
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace {
34 constexpr uint32_t NOTIFY_MASK = IN_CLOSE_WRITE | IN_MOVED_TO;
35 constexpr int EVENT_BUF_LEN = 512;
36 DEFINE_LOG_TAG("Faultlogger");
37 }
38
ReadNotify(std::string * sfilename,int nfd)39 int SanitizerdMonitor::ReadNotify(std::string *sfilename, int nfd)
40 {
41 size_t res;
42 char filename[PATH_MAX];
43 char eventBuf[EVENT_BUF_LEN];
44 int eventSize;
45 int eventPos = 0;
46 int ret = 1;
47 struct inotify_event *event = nullptr;
48 std::string strSanLogPath;
49 SanitizerdType type;
50
51 res = read(nfd, eventBuf, sizeof(eventBuf));
52 if (res < sizeof(*event)) {
53 HIVIEW_LOGI("could not get notify events, %s\n", strerror(errno));
54 return ret;
55 }
56
57 while (res >= sizeof(*event)) {
58 event = reinterpret_cast<struct inotify_event*>(eventBuf + eventPos);
59 if (event->len) {
60 if (strcpy_s(filename, PATH_MAX, event->name) != EOK) {
61 HIVIEW_LOGI("try to copy the file name error, continue");
62 }
63
64 // Check the full path of log file.
65 if (event->mask & NOTIFY_MASK) {
66 *sfilename = filename;
67
68 if (event->wd == gAsanWd) {
69 strSanLogPath = std::string(ASAN_LOG_PATH);
70 type = ASAN_LOG_RPT;
71 }
72
73 ret = 0;
74 std::string strFileName(filename);
75 std::string fullPath = strSanLogPath + "/" + strFileName;
76
77 HIVIEW_LOGI("recv filename is:[%{public}s]\n", fullPath.c_str());
78 if (gCallback != nullptr) {
79 gCallback(type, strFileName);
80 }
81 }
82 }
83 eventSize = sizeof(*event) + event->len;
84 res -= eventSize;
85 eventPos += eventSize;
86 }
87 return ret;
88 }
89
Init(SANITIZERD_NOTIFY_CALLBACK pcb)90 int SanitizerdMonitor::Init(SANITIZERD_NOTIFY_CALLBACK pcb)
91 {
92 const std::string asanLogPath = std::string(ASAN_LOG_PATH);
93 gNfds = 1;
94 gUfds = reinterpret_cast<pollfd*>(calloc(1, sizeof(gUfds[0])));
95 gUfds[0].fd = inotify_init();
96 if (gUfds[0].fd < 0) {
97 HIVIEW_LOGI("inotify_init failed: %{public}d-%{public}s.", gUfds[0].fd, strerror(errno));
98 Uninit();
99 return 1;
100 }
101
102 gUfds[0].events = POLLIN;
103 gAsanWd = inotify_add_watch(gUfds[0].fd, asanLogPath.c_str(), NOTIFY_MASK);
104 if (gAsanWd < 0) {
105 HIVIEW_LOGI("add watch %{public}s failed: %{public}d-%{public}s.", asanLogPath.c_str(), gUfds[0].fd,
106 strerror(errno));
107 Uninit();
108 return 1;
109 } else {
110 HIVIEW_LOGI("add watch %{public}s successfully: %{public}d.", asanLogPath.c_str(), gUfds[0].fd);
111 }
112
113 gCallback = pcb;
114 return 0;
115 }
116
Uninit()117 void SanitizerdMonitor::Uninit()
118 {
119 for (int i = 0; i < gNfds; i++) {
120 if (gUfds[i].fd >= 0) {
121 close(gUfds[i].fd);
122 }
123 }
124
125 if (gUfds != nullptr) {
126 free(gUfds);
127 gUfds = nullptr;
128 }
129 gNfds = 0;
130 gAsanWd = -1;
131 }
132
RunMonitor(std::string * filename,int timeout)133 int SanitizerdMonitor::RunMonitor(std::string *filename, int timeout)
134 {
135 int pollres;
136
137 while (true) {
138 pollres = poll(gUfds, gNfds, timeout);
139 if (pollres == 0) {
140 return 1;
141 }
142
143 if (gUfds[0].revents & POLLIN) {
144 ReadNotify(filename, gUfds[0].fd);
145 }
146 }
147 return 0;
148 }
149 } // namespace HiviewDFX
150 } // namespace OHOS
151