blob: d5dd987171030a1d691495e9fe7aadcdb1b835ae [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{
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050044 if (params.size() > CREATE_DUMP_MAX_PARAMS)
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050045 {
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050046 log<level::WARNING>(
47 "BMC dump accepts not more than 2 additional parameters");
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050048 }
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050049
50 // Get the originator id and type from params
51 std::string originatorId;
52 originatorTypes originatorType;
53
54 phosphor::dump::extractOriginatorProperties(params, originatorId,
55 originatorType);
56
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050057 std::vector<std::string> paths;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050058 auto id = captureDump(Type::UserRequested, paths);
59
60 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050061 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050062
63 try
64 {
Claire Weinanc0ab9d42022-08-17 23:01:07 -070065 uint64_t timeStamp =
66 std::chrono::duration_cast<std::chrono::microseconds>(
67 std::chrono::system_clock::now().time_since_epoch())
68 .count();
69
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050070 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050071 id, std::make_unique<bmc::Entry>(
72 bus, objPath.c_str(), id, timeStamp, 0, std::string(),
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050073 phosphor::dump::OperationStatus::InProgress, originatorId,
74 originatorType, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050075 }
76 catch (const std::invalid_argument& e)
77 {
George Liu858fbb22021-07-01 12:25:44 +080078 log<level::ERR>(fmt::format("Error in creating dump entry, "
79 "errormsg({}), OBJECTPATH({}), ID({})",
80 e.what(), objPath.c_str(), id)
81 .c_str());
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050082 elog<InternalFailure>();
83 }
84
85 return objPath.string();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050086}
87
88uint32_t Manager::captureDump(Type type,
89 const std::vector<std::string>& fullPaths)
90{
91 // Get Dump size.
92 auto size = getAllowedSize();
93
94 pid_t pid = fork();
95
96 if (pid == 0)
97 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050098 std::filesystem::path dumpPath(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050099 auto id = std::to_string(lastEntryId + 1);
100 dumpPath /= id;
101
102 // get dreport type map entry
103 auto tempType = TypeMap.find(type);
104
105 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
106 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
107 fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
108 tempType->second.c_str(), nullptr);
109
110 // dreport script execution is failed.
111 auto error = errno;
George Liu858fbb22021-07-01 12:25:44 +0800112 log<level::ERR>(
113 fmt::format(
114 "Error occurred during dreport function execution, errno({})",
115 error)
116 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500117 elog<InternalFailure>();
118 }
119 else if (pid > 0)
120 {
121 auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid,
122 WEXITED | WSTOPPED, callback, nullptr);
123 if (0 > rc)
124 {
125 // Failed to add to event loop
George Liu858fbb22021-07-01 12:25:44 +0800126 log<level::ERR>(
127 fmt::format(
128 "Error occurred during the sd_event_add_child call, rc({})",
129 rc)
130 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500131 elog<InternalFailure>();
132 }
133 }
134 else
135 {
136 auto error = errno;
George Liu858fbb22021-07-01 12:25:44 +0800137 log<level::ERR>(
138 fmt::format("Error occurred during fork, errno({})", error)
139 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500140 elog<InternalFailure>();
141 }
142
143 return ++lastEntryId;
144}
145
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500146void Manager::createEntry(const std::filesystem::path& file)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500147{
148 // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
149 static constexpr auto ID_POS = 1;
150 static constexpr auto EPOCHTIME_POS = 2;
151 std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
152
153 std::smatch match;
154 std::string name = file.filename();
155
156 if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
157 {
George Liu858fbb22021-07-01 12:25:44 +0800158 log<level::ERR>(fmt::format("Invalid Dump file name, FILENAME({})",
159 file.filename().c_str())
160 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500161 return;
162 }
163
164 auto idString = match[ID_POS];
Xie Ning56bd7972022-02-25 15:20:02 +0800165 uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500166
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500167 auto id = stoul(idString);
168
169 // If there is an existing entry update it and return.
170 auto dumpEntry = entries.find(id);
171 if (dumpEntry != entries.end())
172 {
173 dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
Xie Ning56bd7972022-02-25 15:20:02 +0800174 ->update(timestamp, std::filesystem::file_size(file), file);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500175 return;
176 }
177
178 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500179 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500180
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500181 // TODO: Get the persisted originator id & type
182 // For now, replacing it with null
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500183 try
184 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500185 entries.insert(std::make_pair(
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500186 id, std::make_unique<bmc::Entry>(
Xie Ning56bd7972022-02-25 15:20:02 +0800187 bus, objPath.c_str(), id, timestamp,
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500188 std::filesystem::file_size(file), file,
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500189 phosphor::dump::OperationStatus::Completed, std::string(),
190 originatorTypes::Internal, *this)));
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500191 }
192 catch (const std::invalid_argument& e)
193 {
George Liu858fbb22021-07-01 12:25:44 +0800194 log<level::ERR>(
195 fmt::format(
196 "Error in creating dump entry, errormsg({}), OBJECTPATH({}), "
197 "ID({}), TIMESTAMP({}), SIZE({}), FILENAME({})",
Xie Ning56bd7972022-02-25 15:20:02 +0800198 e.what(), objPath.c_str(), id, timestamp,
George Liu858fbb22021-07-01 12:25:44 +0800199 std::filesystem::file_size(file), file.filename().c_str())
200 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500201 return;
202 }
203}
204
205void Manager::watchCallback(const UserMap& fileInfo)
206{
207 for (const auto& i : fileInfo)
208 {
209 // For any new dump file create dump entry object
210 // and associated inotify watch.
211 if (IN_CLOSE_WRITE == i.second)
212 {
213 removeWatch(i.first);
214
215 createEntry(i.first);
216 }
217 // Start inotify watch on newly created directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500218 else if ((IN_CREATE == i.second) &&
219 std::filesystem::is_directory(i.first))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500220 {
221 auto watchObj = std::make_unique<Watch>(
222 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
223 std::bind(
224 std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
225 this, std::placeholders::_1));
226
227 childWatchMap.emplace(i.first, std::move(watchObj));
228 }
229 }
230}
231
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500232void Manager::removeWatch(const std::filesystem::path& path)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500233{
234 // Delete Watch entry from map.
235 childWatchMap.erase(path);
236}
237
238void Manager::restore()
239{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500240 std::filesystem::path dir(dumpDir);
241 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500242 {
243 return;
244 }
245
246 // Dump file path: <DUMP_PATH>/<id>/<filename>
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500247 for (const auto& p : std::filesystem::directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500248 {
249 auto idStr = p.path().filename().string();
250
251 // Consider only directory's with dump id as name.
252 // Note: As per design one file per directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500253 if ((std::filesystem::is_directory(p.path())) &&
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500254 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
255 {
256 lastEntryId =
257 std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500258 auto fileIt = std::filesystem::directory_iterator(p.path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500259 // Create dump entry d-bus object.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500260 if (fileIt != std::filesystem::end(fileIt))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500261 {
262 createEntry(fileIt->path());
263 }
264 }
265 }
266}
267
Xie Ningfc69f352022-05-17 16:06:52 +0800268size_t getDirectorySize(const std::string dir)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500269{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500270 auto size = 0;
Xie Ningfc69f352022-05-17 16:06:52 +0800271 for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500272 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500273 if (!std::filesystem::is_directory(p))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500274 {
Tim Leebb9366d2021-06-24 14:00:07 +0800275 size += std::ceil(std::filesystem::file_size(p) / 1024.0);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500276 }
277 }
Xie Ningfc69f352022-05-17 16:06:52 +0800278 return size;
279}
280
281size_t Manager::getAllowedSize()
282{
283 // Get current size of the dump directory.
284 auto size = getDirectorySize(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500285
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500286 // Set the Dump size to Maximum if the free space is greater than
287 // Dump max size otherwise return the available size.
288
289 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
290
Xie Ningfc69f352022-05-17 16:06:52 +0800291#ifdef BMC_DUMP_ROTATE_CONFIG
292 // Delete the first existing file until the space is enough
293 while (size < BMC_DUMP_MIN_SPACE_REQD)
294 {
295 auto delEntry = min_element(
296 entries.begin(), entries.end(),
297 [](const auto& l, const auto& r) { return l.first < r.first; });
298 auto delPath =
299 std::filesystem::path(dumpDir) / std::to_string(delEntry->first);
300
301 size += getDirectorySize(delPath);
302
303 delEntry->second->delete_();
304 }
305#else
306 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
307 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
308
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500309 if (size < BMC_DUMP_MIN_SPACE_REQD)
310 {
311 // Reached to maximum limit
312 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
313 }
Xie Ningfc69f352022-05-17 16:06:52 +0800314#endif
315
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500316 if (size > BMC_DUMP_MAX_SIZE)
317 {
318 size = BMC_DUMP_MAX_SIZE;
319 }
320
321 return size;
322}
323
324} // namespace bmc
325} // namespace dump
326} // namespace phosphor