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 "faultlog_manager.h"
16
17 #include <cstdint>
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <vector>
22
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "constants.h"
29 #include "defines.h"
30 #include "file_util.h"
31 #include "log_store_ex.h"
32 #include "logger.h"
33 #include "time_util.h"
34
35 #include "faultlog_database.h"
36 #include "faultlog_formatter.h"
37 #include "faultlog_info.h"
38 #include "faultlog_util.h"
39
40 namespace OHOS {
41 namespace HiviewDFX {
42 using namespace FaultLogger;
43 DEFINE_LOG_TAG("FaultLogManager");
CreateLogFileFilter(time_t time,int32_t id,int32_t faultLogType,const std::string & module)44 LogStoreEx::LogFileFilter CreateLogFileFilter(time_t time, int32_t id, int32_t faultLogType, const std::string& module)
45 {
46 LogStoreEx::LogFileFilter filter = [time, id, faultLogType, module](const LogFile &file) {
47 FaultLogInfo info = ExtractInfoFromFileName(file.name_);
48 if (info.time <= time) {
49 return false;
50 }
51
52 if ((id != -1) && (info.id != id)) {
53 return false;
54 }
55
56 if ((faultLogType != 0) && (info.faultLogType != faultLogType)) {
57 return false;
58 }
59
60 if ((!module.empty()) && (info.module != module)) {
61 return false;
62 }
63 return true;
64 };
65 return filter;
66 }
67
~FaultLogManager()68 FaultLogManager::~FaultLogManager()
69 {
70 if (faultLogDb_ != nullptr) {
71 delete faultLogDb_;
72 faultLogDb_ = nullptr;
73 }
74 }
75
CreateTempFaultLogFile(time_t time,int32_t id,int32_t faultType,const std::string & module) const76 int32_t FaultLogManager::CreateTempFaultLogFile(time_t time, int32_t id, int32_t faultType,
77 const std::string &module) const
78 {
79 FaultLogInfo info;
80 info.time = time;
81 info.id = id;
82 info.faultLogType = faultType;
83 info.module = module;
84 auto fileName = GetFaultLogName(info);
85 return store_->CreateLogFile(fileName);
86 }
87
Init()88 void FaultLogManager::Init()
89 {
90 store_ = std::make_unique<LogStoreEx>(FaultLogger::DEFAULT_FAULTLOG_FOLDER, true);
91 LogStoreEx::LogFileComparator comparator = [](const LogFile &lhs, const LogFile &rhs) {
92 FaultLogInfo lhsInfo = ExtractInfoFromFileName(lhs.name_);
93 FaultLogInfo rhsInfo = ExtractInfoFromFileName(rhs.name_);
94 return lhsInfo.time > rhsInfo.time;
95 };
96 store_->SetLogFileComparator(comparator);
97 store_->Init();
98 faultLogDb_ = new FaultLogDatabase();
99 }
100
SaveFaultLogToFile(FaultLogInfo & info) const101 std::string FaultLogManager::SaveFaultLogToFile(FaultLogInfo &info) const
102 {
103 auto fileName = GetFaultLogName(info);
104 auto fd = store_->CreateLogFile(fileName);
105 if (fd < 0) {
106 return "";
107 }
108
109 FaultLogger::WriteFaultLogToFile(fd, info.faultLogType, info.sectionMap);
110 FaultLogger::WriteLogToFile(fd, info.logPath);
111 close(fd);
112 store_->ClearSameLogFilesIfNeeded(CreateLogFileFilter(0, info.id, 0, info.module), MAX_FAULT_LOG_PER_HAP);
113 info.logPath = std::string(FaultLogger::DEFAULT_FAULTLOG_FOLDER) + fileName;
114 HIVIEW_LOGI("create log %{public}s", fileName.c_str());
115 return fileName;
116 }
117
GetFaultInfoList(const std::string & module,int32_t id,int32_t faultType,int32_t maxNum) const118 std::list<FaultLogInfo> FaultLogManager::GetFaultInfoList(const std::string& module,
119 int32_t id, int32_t faultType, int32_t maxNum) const
120 {
121 std::list<FaultLogInfo> ret;
122 if (faultLogDb_ != nullptr) {
123 ret = faultLogDb_->GetFaultInfoList(module, id, faultType, maxNum);
124 HIVIEW_LOGI("Find %{public}zu fault records for uid:%{public}d type:%{public}d",
125 ret.size(), id, faultType);
126 }
127 return ret;
128 }
129
SaveFaultInfoToRawDb(FaultLogInfo & info) const130 void FaultLogManager::SaveFaultInfoToRawDb(FaultLogInfo& info) const
131 {
132 if (faultLogDb_ != nullptr) {
133 faultLogDb_->SaveFaultLogInfo(info);
134 }
135 }
136
ReduceLogFileListSize(std::list<std::string> & infoVec,int32_t maxNum) const137 void FaultLogManager::ReduceLogFileListSize(std::list<std::string> &infoVec, int32_t maxNum) const
138 {
139 if ((maxNum < 0) || (infoVec.size() <= static_cast<uint32_t>(maxNum))) {
140 return;
141 }
142
143 auto begin = infoVec.begin();
144 std::advance(begin, maxNum);
145 infoVec.erase(begin, infoVec.end());
146 }
147
GetFaultLogFileList(const std::string & module,time_t time,int32_t id,int32_t faultType,int32_t maxNum) const148 std::list<std::string> FaultLogManager::GetFaultLogFileList(const std::string &module, time_t time, int32_t id,
149 int32_t faultType, int32_t maxNum) const
150 {
151 LogStoreEx::LogFileFilter filter = CreateLogFileFilter(time, id, faultType, module);
152 auto vec = store_->GetLogFiles(filter);
153 std::list<std::string> ret;
154 std::transform(vec.begin(), vec.end(), std::back_inserter(ret), [](const LogFile &file) { return file.path_; });
155 ReduceLogFileListSize(ret, maxNum);
156 return ret;
157 }
158
GetFaultLogContent(const std::string & name,std::string & content) const159 bool FaultLogManager::GetFaultLogContent(const std::string &name, std::string &content) const
160 {
161 auto path = std::string(FaultLogger::DEFAULT_FAULTLOG_FOLDER) + name;
162 return FileUtil::LoadStringFromFile(path, content);
163 }
164
IsProcessedFault(int32_t pid,int32_t uid,int32_t faultType)165 bool FaultLogManager::IsProcessedFault(int32_t pid, int32_t uid, int32_t faultType)
166 {
167 if (faultLogDb_ == nullptr) {
168 return false;
169 }
170
171 return faultLogDb_->IsFaultExist(pid, uid, faultType);
172 }
173 } // namespace HiviewDFX
174 } // namespace OHOS
175