blob: 3ef36993f79536ead864771dc6eb1da168fa7037 [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>
Marri Devender Rao3ed02c32022-06-28 23:12:14 -050016#include <sdeventplus/exception.hpp>
17#include <sdeventplus/source/base.hpp>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050018
Tim Leebb9366d2021-06-24 14:00:07 +080019#include <cmath>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050020#include <ctime>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050021#include <regex>
22
23namespace phosphor
24{
25namespace dump
26{
27namespace bmc
28{
29
30using namespace sdbusplus::xyz::openbmc_project::Common::Error;
31using namespace phosphor::logging;
32
Marri Devender Rao73953b82022-02-15 09:15:42 -060033bool Manager::fUserDumpInProgress = false;
34
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050035namespace internal
36{
37
38void Manager::create(Type type, std::vector<std::string> fullPaths)
39{
40 dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths);
41}
42
43} // namespace internal
44
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050045sdbusplus::message::object_path
Dhruvaraj Subhashchandranddc33662021-07-19 09:28:42 -050046 Manager::createDump(phosphor::dump::DumpCreateParams params)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050047{
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050048 if (params.size() > CREATE_DUMP_MAX_PARAMS)
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050049 {
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050050 log<level::WARNING>(
51 "BMC dump accepts not more than 2 additional parameters");
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050052 }
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050053
Marri Devender Rao73953b82022-02-15 09:15:42 -060054 if (Manager::fUserDumpInProgress == true)
55 {
56 elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
57 }
58
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050059 // Get the originator id and type from params
60 std::string originatorId;
61 originatorTypes originatorType;
62
63 phosphor::dump::extractOriginatorProperties(params, originatorId,
64 originatorType);
65
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050066 std::vector<std::string> paths;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050067 auto id = captureDump(Type::UserRequested, paths);
68
69 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050070 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050071
72 try
73 {
Claire Weinanc0ab9d42022-08-17 23:01:07 -070074 uint64_t timeStamp =
75 std::chrono::duration_cast<std::chrono::microseconds>(
76 std::chrono::system_clock::now().time_since_epoch())
77 .count();
78
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050079 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050080 id, std::make_unique<bmc::Entry>(
81 bus, objPath.c_str(), id, timeStamp, 0, std::string(),
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050082 phosphor::dump::OperationStatus::InProgress, originatorId,
83 originatorType, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050084 }
85 catch (const std::invalid_argument& e)
86 {
George Liu858fbb22021-07-01 12:25:44 +080087 log<level::ERR>(fmt::format("Error in creating dump entry, "
88 "errormsg({}), OBJECTPATH({}), ID({})",
89 e.what(), objPath.c_str(), id)
90 .c_str());
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050091 elog<InternalFailure>();
92 }
93
Marri Devender Rao73953b82022-02-15 09:15:42 -060094 Manager::fUserDumpInProgress = true;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050095 return objPath.string();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050096}
97
98uint32_t Manager::captureDump(Type type,
99 const std::vector<std::string>& fullPaths)
100{
101 // Get Dump size.
102 auto size = getAllowedSize();
103
104 pid_t pid = fork();
105
106 if (pid == 0)
107 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500108 std::filesystem::path dumpPath(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500109 auto id = std::to_string(lastEntryId + 1);
110 dumpPath /= id;
111
112 // get dreport type map entry
113 auto tempType = TypeMap.find(type);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500114 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
115 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
116 fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
117 tempType->second.c_str(), nullptr);
118
119 // dreport script execution is failed.
120 auto error = errno;
Marri Devender Rao73953b82022-02-15 09:15:42 -0600121 log<level::ERR>(fmt::format("Error occurred during dreport "
122 "function execution, errno({})",
123 error)
124 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500125 elog<InternalFailure>();
126 }
127 else if (pid > 0)
128 {
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500129 Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
130 if (type == Type::UserRequested)
131 {
132 log<level::INFO>(
133 "User initiated dump completed, resetting flag");
134 Manager::fUserDumpInProgress = false;
135 }
136 this->childPtrMap.erase(pid);
137 };
138 try
139 {
140 childPtrMap.emplace(pid,
141 std::make_unique<Child>(eventLoop.get(), pid,
142 WEXITED | WSTOPPED,
143 std::move(callback)));
144 }
145 catch (const sdeventplus::SdEventError& ex)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500146 {
147 // Failed to add to event loop
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500148 log<level::ERR>(
149 fmt::format(
150 "Error occurred during the sdeventplus::source::Child "
151 "creation ex({})",
152 ex.what())
153 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500154 elog<InternalFailure>();
155 }
156 }
157 else
158 {
159 auto error = errno;
George Liu858fbb22021-07-01 12:25:44 +0800160 log<level::ERR>(
161 fmt::format("Error occurred during fork, errno({})", error)
162 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500163 elog<InternalFailure>();
164 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500165 return ++lastEntryId;
166}
167
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500168void Manager::createEntry(const std::filesystem::path& file)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500169{
170 // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
171 static constexpr auto ID_POS = 1;
172 static constexpr auto EPOCHTIME_POS = 2;
173 std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
174
175 std::smatch match;
176 std::string name = file.filename();
177
178 if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
179 {
George Liu858fbb22021-07-01 12:25:44 +0800180 log<level::ERR>(fmt::format("Invalid Dump file name, FILENAME({})",
181 file.filename().c_str())
182 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500183 return;
184 }
185
186 auto idString = match[ID_POS];
Xie Ning56bd7972022-02-25 15:20:02 +0800187 uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500188
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500189 auto id = stoul(idString);
190
191 // If there is an existing entry update it and return.
192 auto dumpEntry = entries.find(id);
193 if (dumpEntry != entries.end())
194 {
195 dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
Xie Ning56bd7972022-02-25 15:20:02 +0800196 ->update(timestamp, std::filesystem::file_size(file), file);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500197 return;
198 }
199
200 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500201 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500202
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500203 // TODO: Get the persisted originator id & type
204 // For now, replacing it with null
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500205 try
206 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500207 entries.insert(std::make_pair(
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500208 id, std::make_unique<bmc::Entry>(
Xie Ning56bd7972022-02-25 15:20:02 +0800209 bus, objPath.c_str(), id, timestamp,
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500210 std::filesystem::file_size(file), file,
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500211 phosphor::dump::OperationStatus::Completed, std::string(),
212 originatorTypes::Internal, *this)));
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500213 }
214 catch (const std::invalid_argument& e)
215 {
George Liu858fbb22021-07-01 12:25:44 +0800216 log<level::ERR>(
Marri Devender Rao73953b82022-02-15 09:15:42 -0600217 fmt::format("Error in creating dump entry, errormsg({}), "
218 "OBJECTPATH({}), "
219 "ID({}), TIMESTAMP({}), SIZE({}), FILENAME({})",
220 e.what(), objPath.c_str(), id, timestamp,
221 std::filesystem::file_size(file),
222 file.filename().c_str())
George Liu858fbb22021-07-01 12:25:44 +0800223 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500224 return;
225 }
226}
227
228void Manager::watchCallback(const UserMap& fileInfo)
229{
230 for (const auto& i : fileInfo)
231 {
232 // For any new dump file create dump entry object
233 // and associated inotify watch.
234 if (IN_CLOSE_WRITE == i.second)
235 {
Chirag Sharma4cb07992022-05-09 04:37:22 -0500236 if (!std::filesystem::is_directory(i.first))
237 {
238 // Don't require filename to be passed, as the path
239 // of dump directory is stored in the childWatchMap
240 removeWatch(i.first.parent_path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500241
Chirag Sharma4cb07992022-05-09 04:37:22 -0500242 // dump file is written now create D-Bus entry
243 createEntry(i.first);
244 }
245 else
246 {
247 removeWatch(i.first);
248 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500249 }
250 // Start inotify watch on newly created directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500251 else if ((IN_CREATE == i.second) &&
252 std::filesystem::is_directory(i.first))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500253 {
254 auto watchObj = std::make_unique<Watch>(
255 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
256 std::bind(
257 std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
258 this, std::placeholders::_1));
259
260 childWatchMap.emplace(i.first, std::move(watchObj));
261 }
262 }
263}
264
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500265void Manager::removeWatch(const std::filesystem::path& path)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500266{
267 // Delete Watch entry from map.
268 childWatchMap.erase(path);
269}
270
271void Manager::restore()
272{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500273 std::filesystem::path dir(dumpDir);
274 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500275 {
276 return;
277 }
278
279 // Dump file path: <DUMP_PATH>/<id>/<filename>
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500280 for (const auto& p : std::filesystem::directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500281 {
282 auto idStr = p.path().filename().string();
283
284 // Consider only directory's with dump id as name.
285 // Note: As per design one file per directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500286 if ((std::filesystem::is_directory(p.path())) &&
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500287 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
288 {
Patrick Williams78e88402023-05-10 07:50:48 -0500289 lastEntryId = std::max(lastEntryId,
290 static_cast<uint32_t>(std::stoul(idStr)));
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500291 auto fileIt = std::filesystem::directory_iterator(p.path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500292 // Create dump entry d-bus object.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500293 if (fileIt != std::filesystem::end(fileIt))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500294 {
295 createEntry(fileIt->path());
296 }
297 }
298 }
299}
300
Xie Ningfc69f352022-05-17 16:06:52 +0800301size_t getDirectorySize(const std::string dir)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500302{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500303 auto size = 0;
Xie Ningfc69f352022-05-17 16:06:52 +0800304 for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500305 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500306 if (!std::filesystem::is_directory(p))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500307 {
Tim Leebb9366d2021-06-24 14:00:07 +0800308 size += std::ceil(std::filesystem::file_size(p) / 1024.0);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500309 }
310 }
Xie Ningfc69f352022-05-17 16:06:52 +0800311 return size;
312}
313
314size_t Manager::getAllowedSize()
315{
316 // Get current size of the dump directory.
317 auto size = getDirectorySize(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500318
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500319 // Set the Dump size to Maximum if the free space is greater than
320 // Dump max size otherwise return the available size.
321
322 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
323
Xie Ningfc69f352022-05-17 16:06:52 +0800324#ifdef BMC_DUMP_ROTATE_CONFIG
325 // Delete the first existing file until the space is enough
326 while (size < BMC_DUMP_MIN_SPACE_REQD)
327 {
Patrick Williams78e88402023-05-10 07:50:48 -0500328 auto delEntry = min_element(entries.begin(), entries.end(),
329 [](const auto& l, const auto& r) {
330 return l.first < r.first;
331 });
332 auto delPath = std::filesystem::path(dumpDir) /
333 std::to_string(delEntry->first);
Xie Ningfc69f352022-05-17 16:06:52 +0800334
335 size += getDirectorySize(delPath);
336
337 delEntry->second->delete_();
338 }
339#else
340 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
341 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
342
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500343 if (size < BMC_DUMP_MIN_SPACE_REQD)
344 {
345 // Reached to maximum limit
346 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
347 }
Xie Ningfc69f352022-05-17 16:06:52 +0800348#endif
349
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500350 if (size > BMC_DUMP_MAX_SIZE)
351 {
352 size = BMC_DUMP_MAX_SIZE;
353 }
354
355 return size;
356}
357
358} // namespace bmc
359} // namespace dump
360} // namespace phosphor