blob: 66f780df4d958d62d8668740a2a9aeb1a60be44d [file] [log] [blame]
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05001#include "config.h"
2
3#include "dump_manager_bmc.hpp"
4
5#include "bmc_dump_entry.hpp"
6#include "dump_internal.hpp"
7#include "xyz/openbmc_project/Common/error.hpp"
8#include "xyz/openbmc_project/Dump/Create/error.hpp"
9
George Liu858fbb22021-07-01 12:25:44 +080010#include <fmt/core.h>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050011#include <sys/inotify.h>
12#include <unistd.h>
13
14#include <phosphor-logging/elog-errors.hpp>
15#include <phosphor-logging/elog.hpp>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050016
Tim Leebb9366d2021-06-24 14:00:07 +080017#include <cmath>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050018#include <ctime>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050019#include <regex>
20
21namespace phosphor
22{
23namespace dump
24{
25namespace bmc
26{
27
28using namespace sdbusplus::xyz::openbmc_project::Common::Error;
29using namespace phosphor::logging;
30
31namespace internal
32{
33
34void Manager::create(Type type, std::vector<std::string> fullPaths)
35{
36 dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths);
37}
38
39} // namespace internal
40
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050041sdbusplus::message::object_path
Dhruvaraj Subhashchandranddc33662021-07-19 09:28:42 -050042 Manager::createDump(phosphor::dump::DumpCreateParams params)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050043{
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050044 if (!params.empty())
45 {
46 log<level::WARNING>("BMC dump accepts no additional parameters");
47 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050048 std::vector<std::string> paths;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050049 auto id = captureDump(Type::UserRequested, paths);
50
51 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050052 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050053
54 try
55 {
Claire Weinanc0ab9d42022-08-17 23:01:07 -070056 uint64_t timeStamp =
57 std::chrono::duration_cast<std::chrono::microseconds>(
58 std::chrono::system_clock::now().time_since_epoch())
59 .count();
60
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050061 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050062 id, std::make_unique<bmc::Entry>(
63 bus, objPath.c_str(), id, timeStamp, 0, std::string(),
64 phosphor::dump::OperationStatus::InProgress, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050065 }
66 catch (const std::invalid_argument& e)
67 {
George Liu858fbb22021-07-01 12:25:44 +080068 log<level::ERR>(fmt::format("Error in creating dump entry, "
69 "errormsg({}), OBJECTPATH({}), ID({})",
70 e.what(), objPath.c_str(), id)
71 .c_str());
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050072 elog<InternalFailure>();
73 }
74
75 return objPath.string();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050076}
77
78uint32_t Manager::captureDump(Type type,
79 const std::vector<std::string>& fullPaths)
80{
81 // Get Dump size.
82 auto size = getAllowedSize();
83
84 pid_t pid = fork();
85
86 if (pid == 0)
87 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050088 std::filesystem::path dumpPath(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050089 auto id = std::to_string(lastEntryId + 1);
90 dumpPath /= id;
91
92 // get dreport type map entry
93 auto tempType = TypeMap.find(type);
94
95 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
96 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
97 fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
98 tempType->second.c_str(), nullptr);
99
100 // dreport script execution is failed.
101 auto error = errno;
George Liu858fbb22021-07-01 12:25:44 +0800102 log<level::ERR>(
103 fmt::format(
104 "Error occurred during dreport function execution, errno({})",
105 error)
106 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500107 elog<InternalFailure>();
108 }
109 else if (pid > 0)
110 {
111 auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid,
112 WEXITED | WSTOPPED, callback, nullptr);
113 if (0 > rc)
114 {
115 // Failed to add to event loop
George Liu858fbb22021-07-01 12:25:44 +0800116 log<level::ERR>(
117 fmt::format(
118 "Error occurred during the sd_event_add_child call, rc({})",
119 rc)
120 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500121 elog<InternalFailure>();
122 }
123 }
124 else
125 {
126 auto error = errno;
George Liu858fbb22021-07-01 12:25:44 +0800127 log<level::ERR>(
128 fmt::format("Error occurred during fork, errno({})", error)
129 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500130 elog<InternalFailure>();
131 }
132
133 return ++lastEntryId;
134}
135
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500136void Manager::createEntry(const std::filesystem::path& file)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500137{
138 // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
139 static constexpr auto ID_POS = 1;
140 static constexpr auto EPOCHTIME_POS = 2;
141 std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
142
143 std::smatch match;
144 std::string name = file.filename();
145
146 if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
147 {
George Liu858fbb22021-07-01 12:25:44 +0800148 log<level::ERR>(fmt::format("Invalid Dump file name, FILENAME({})",
149 file.filename().c_str())
150 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500151 return;
152 }
153
154 auto idString = match[ID_POS];
Xie Ning56bd7972022-02-25 15:20:02 +0800155 uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500156
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500157 auto id = stoul(idString);
158
159 // If there is an existing entry update it and return.
160 auto dumpEntry = entries.find(id);
161 if (dumpEntry != entries.end())
162 {
163 dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
Xie Ning56bd7972022-02-25 15:20:02 +0800164 ->update(timestamp, std::filesystem::file_size(file), file);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500165 return;
166 }
167
168 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500169 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500170
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500171 try
172 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500173 entries.insert(std::make_pair(
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500174 id, std::make_unique<bmc::Entry>(
Xie Ning56bd7972022-02-25 15:20:02 +0800175 bus, objPath.c_str(), id, timestamp,
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500176 std::filesystem::file_size(file), file,
177 phosphor::dump::OperationStatus::Completed, *this)));
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500178 }
179 catch (const std::invalid_argument& e)
180 {
George Liu858fbb22021-07-01 12:25:44 +0800181 log<level::ERR>(
182 fmt::format(
183 "Error in creating dump entry, errormsg({}), OBJECTPATH({}), "
184 "ID({}), TIMESTAMP({}), SIZE({}), FILENAME({})",
Xie Ning56bd7972022-02-25 15:20:02 +0800185 e.what(), objPath.c_str(), id, timestamp,
George Liu858fbb22021-07-01 12:25:44 +0800186 std::filesystem::file_size(file), file.filename().c_str())
187 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500188 return;
189 }
190}
191
192void Manager::watchCallback(const UserMap& fileInfo)
193{
194 for (const auto& i : fileInfo)
195 {
196 // For any new dump file create dump entry object
197 // and associated inotify watch.
198 if (IN_CLOSE_WRITE == i.second)
199 {
200 removeWatch(i.first);
201
202 createEntry(i.first);
203 }
204 // Start inotify watch on newly created directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500205 else if ((IN_CREATE == i.second) &&
206 std::filesystem::is_directory(i.first))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500207 {
208 auto watchObj = std::make_unique<Watch>(
209 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
210 std::bind(
211 std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
212 this, std::placeholders::_1));
213
214 childWatchMap.emplace(i.first, std::move(watchObj));
215 }
216 }
217}
218
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500219void Manager::removeWatch(const std::filesystem::path& path)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500220{
221 // Delete Watch entry from map.
222 childWatchMap.erase(path);
223}
224
225void Manager::restore()
226{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500227 std::filesystem::path dir(dumpDir);
228 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500229 {
230 return;
231 }
232
233 // Dump file path: <DUMP_PATH>/<id>/<filename>
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500234 for (const auto& p : std::filesystem::directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500235 {
236 auto idStr = p.path().filename().string();
237
238 // Consider only directory's with dump id as name.
239 // Note: As per design one file per directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500240 if ((std::filesystem::is_directory(p.path())) &&
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500241 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
242 {
243 lastEntryId =
244 std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500245 auto fileIt = std::filesystem::directory_iterator(p.path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500246 // Create dump entry d-bus object.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500247 if (fileIt != std::filesystem::end(fileIt))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500248 {
249 createEntry(fileIt->path());
250 }
251 }
252 }
253}
254
Xie Ningfc69f352022-05-17 16:06:52 +0800255size_t getDirectorySize(const std::string dir)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500256{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500257 auto size = 0;
Xie Ningfc69f352022-05-17 16:06:52 +0800258 for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500259 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500260 if (!std::filesystem::is_directory(p))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500261 {
Tim Leebb9366d2021-06-24 14:00:07 +0800262 size += std::ceil(std::filesystem::file_size(p) / 1024.0);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500263 }
264 }
Xie Ningfc69f352022-05-17 16:06:52 +0800265 return size;
266}
267
268size_t Manager::getAllowedSize()
269{
270 // Get current size of the dump directory.
271 auto size = getDirectorySize(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500272
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500273 // Set the Dump size to Maximum if the free space is greater than
274 // Dump max size otherwise return the available size.
275
276 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
277
Xie Ningfc69f352022-05-17 16:06:52 +0800278#ifdef BMC_DUMP_ROTATE_CONFIG
279 // Delete the first existing file until the space is enough
280 while (size < BMC_DUMP_MIN_SPACE_REQD)
281 {
282 auto delEntry = min_element(
283 entries.begin(), entries.end(),
284 [](const auto& l, const auto& r) { return l.first < r.first; });
285 auto delPath =
286 std::filesystem::path(dumpDir) / std::to_string(delEntry->first);
287
288 size += getDirectorySize(delPath);
289
290 delEntry->second->delete_();
291 }
292#else
293 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
294 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
295
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500296 if (size < BMC_DUMP_MIN_SPACE_REQD)
297 {
298 // Reached to maximum limit
299 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
300 }
Xie Ningfc69f352022-05-17 16:06:52 +0800301#endif
302
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500303 if (size > BMC_DUMP_MAX_SIZE)
304 {
305 size = BMC_DUMP_MAX_SIZE;
306 }
307
308 return size;
309}
310
311} // namespace bmc
312} // namespace dump
313} // namespace phosphor