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 #include "dmesg_catcher.h"
16
17 #include <string>
18 #include <sys/klog.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23
24 #include "hiview_logger.h"
25 #include "log_catcher_utils.h"
26 #include "common_utils.h"
27 #include "file_util.h"
28 #include "time_util.h"
29 #include "securec.h"
30 namespace OHOS {
31 namespace HiviewDFX {
32 DEFINE_LOG_LABEL(0xD002D01, "EventLogger-DmesgCatcher");
33 namespace {
34 constexpr int SYSLOG_ACTION_READ_ALL = 3;
35 constexpr int SYSLOG_ACTION_SIZE_BUFFER = 10;
36 constexpr mode_t DEFAULT_LOG_FILE_MODE = 0644;
37 }
DmesgCatcher()38 DmesgCatcher::DmesgCatcher() : EventLogCatcher()
39 {
40 event_ = nullptr;
41 name_ = "DmesgCatcher";
42 }
43
Initialize(const std::string & packageNam __UNUSED,int isWriteNewFile __UNUSED,int intParam)44 bool DmesgCatcher::Initialize(const std::string& packageNam __UNUSED,
45 int isWriteNewFile __UNUSED, int intParam)
46 {
47 isWriteNewFile_ = isWriteNewFile;
48 needWriteSysrq_ = intParam;
49 return true;
50 }
51
Init(std::shared_ptr<SysEvent> event)52 bool DmesgCatcher::Init(std::shared_ptr<SysEvent> event)
53 {
54 event_ = event;
55 return true;
56 }
57
DumpDmesgLog(int fd)58 bool DmesgCatcher::DumpDmesgLog(int fd)
59 {
60 if (fd < 0) {
61 return false;
62 }
63 int size = klogctl(SYSLOG_ACTION_SIZE_BUFFER, 0, 0);
64 if (size <= 0) {
65 return false;
66 }
67 char *data = (char *)malloc(size + 1);
68 if (data == nullptr) {
69 return false;
70 }
71
72 memset_s(data, size + 1, 0, size + 1);
73 int readSize = klogctl(SYSLOG_ACTION_READ_ALL, data, size);
74 if (readSize < 0) {
75 free(data);
76 return false;
77 }
78 bool res = FileUtil::SaveStringToFd(fd, data);
79 free(data);
80 return res;
81 }
82
WriteSysrq()83 bool DmesgCatcher::WriteSysrq()
84 {
85 FILE* file = fopen("/proc/sysrq-trigger", "w");
86 if (file == nullptr) {
87 HIVIEW_LOGE("Can't read sysrq,errno: %{public}d", errno);
88 return false;
89 }
90 fwrite("w", 1, 1, file);
91 fflush(file);
92
93 fwrite("l", 1, 1, file);
94 sleep(1);
95 fclose(file);
96 return true;
97 }
98
DmesgSaveTofile()99 std::string DmesgCatcher::DmesgSaveTofile()
100 {
101 auto logTime = TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC;
102 std::string sysrqTime = TimeUtil::TimestampFormatToDate(logTime, "%Y%m%d%H%M%S");
103 std::string fullPath = FULL_DIR + "sysrq-" + sysrqTime + ".log";
104
105 if (FileUtil::FileExists(fullPath)) {
106 HIVIEW_LOGW("filename: %{public}s is existed, direct use.", fullPath.c_str());
107 return fullPath;
108 }
109
110 auto fd = open(fullPath.c_str(), O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_LOG_FILE_MODE);
111 if (fd < 0) {
112 HIVIEW_LOGI("Fail to create %s.", fullPath.c_str());
113 return "";
114 }
115 bool dumpRet = DumpDmesgLog(fd);
116 close(fd);
117
118 if (!dumpRet) {
119 return "";
120 }
121 if (event_ != nullptr) {
122 event_->SetEventValue("SYSRQ_TIME", sysrqTime);
123 }
124 return fullPath;
125 }
126
Catch(int fd,int jsonFd)127 int DmesgCatcher::Catch(int fd, int jsonFd)
128 {
129 if (needWriteSysrq_ && !WriteSysrq()) {
130 return 0;
131 }
132
133 description_ = needWriteSysrq_ ? "\nSysrqCatcher -- " : "DmesgCatcher -- ";
134
135 auto originSize = GetFdSize(fd);
136 if (isWriteNewFile_) {
137 std::string fullPath = DmesgSaveTofile();
138 if (fullPath.empty()) {
139 return 0;
140 }
141 description_ += "fullPath:" + fullPath + "\n";
142 FileUtil::SaveStringToFd(fd, description_);
143 } else {
144 description_ += "\n";
145 FileUtil::SaveStringToFd(fd, description_);
146 DumpDmesgLog(fd);
147 }
148 logSize_ = GetFdSize(fd) - originSize;
149 return logSize_;
150 }
151 } // namespace HiviewDFX
152 } // namespace OHOS
153