blob: 6c990992032c667a7c29ec7373c968b6a5a59fb5 [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"
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -05007#include "dump_types.hpp"
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05008#include "xyz/openbmc_project/Common/error.hpp"
9#include "xyz/openbmc_project/Dump/Create/error.hpp"
10
11#include <sys/inotify.h>
12#include <unistd.h>
13
14#include <phosphor-logging/elog-errors.hpp>
15#include <phosphor-logging/elog.hpp>
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050016#include <phosphor-logging/lg2.hpp>
Marri Devender Rao3ed02c32022-06-28 23:12:14 -050017#include <sdeventplus/exception.hpp>
18#include <sdeventplus/source/base.hpp>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050019
Tim Leebb9366d2021-06-24 14:00:07 +080020#include <cmath>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050021#include <ctime>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050022#include <regex>
23
24namespace phosphor
25{
26namespace dump
27{
28namespace bmc
29{
30
31using namespace sdbusplus::xyz::openbmc_project::Common::Error;
32using namespace phosphor::logging;
33
Marri Devender Rao73953b82022-02-15 09:15:42 -060034bool Manager::fUserDumpInProgress = false;
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050035constexpr auto BMC_DUMP = "BMC_DUMP";
Marri Devender Rao73953b82022-02-15 09:15:42 -060036
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050037namespace internal
38{
39
40void Manager::create(Type type, std::vector<std::string> fullPaths)
41{
42 dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths);
43}
44
45} // namespace internal
46
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050047sdbusplus::message::object_path
Dhruvaraj Subhashchandranddc33662021-07-19 09:28:42 -050048 Manager::createDump(phosphor::dump::DumpCreateParams params)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050049{
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050050 if (params.size() > CREATE_DUMP_MAX_PARAMS)
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050051 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050052 lg2::warning("BMC dump accepts not more than 2 additional parameters");
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050053 }
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050054
55 // Get the originator id and type from params
56 std::string originatorId;
57 originatorTypes originatorType;
58
59 phosphor::dump::extractOriginatorProperties(params, originatorId,
60 originatorType);
61
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050062 using CreateParameters =
63 sdbusplus::common::xyz::openbmc_project::dump::Create::CreateParameters;
64
65 DumpTypes dumpType = DumpTypes::USER;
66 std::string type = extractParameter<std::string>(
67 convertCreateParametersToString(CreateParameters::DumpType), params);
68 if (!type.empty())
69 {
70 dumpType = validateDumpType(type, BMC_DUMP);
71 }
72 std::string path = extractParameter<std::string>(
73 convertCreateParametersToString(CreateParameters::FilePath), params);
74
75 if ((Manager::fUserDumpInProgress == true) && (dumpType == DumpTypes::USER))
76 {
77 lg2::info("Another user initiated dump in progress");
78 elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
79 }
80
81 lg2::info("Initiating new BMC dump with type: {TYPE} path: {PATH}", "TYPE",
82 dumpTypeToString(dumpType).value(), "PATH", path);
83
84 auto id = captureDump(dumpType, path);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050085
86 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050087 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050088
89 try
90 {
Claire Weinanc0ab9d42022-08-17 23:01:07 -070091 uint64_t timeStamp =
92 std::chrono::duration_cast<std::chrono::microseconds>(
93 std::chrono::system_clock::now().time_since_epoch())
94 .count();
95
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050096 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050097 id, std::make_unique<bmc::Entry>(
98 bus, objPath.c_str(), id, timeStamp, 0, std::string(),
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050099 phosphor::dump::OperationStatus::InProgress, originatorId,
100 originatorType, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500101 }
102 catch (const std::invalid_argument& e)
103 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500104 lg2::error("Error in creating dump entry, errormsg: {ERROR}, "
105 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
106 "ERROR", e, "OBJECT_PATH", objPath, "ID", id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500107 elog<InternalFailure>();
108 }
109
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500110 if (dumpType == DumpTypes::USER)
111 {
112 Manager::fUserDumpInProgress = true;
113 }
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500114 return objPath.string();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500115}
116
117uint32_t Manager::captureDump(Type type,
118 const std::vector<std::string>& fullPaths)
119{
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500120 // get dreport type map entry
121 auto tempType = TypeMap.find(type);
122 return captureDump(stringToDumpType(tempType->second).value(),
123 fullPaths.front());
124}
125uint32_t Manager::captureDump(DumpTypes type, const std::string& path)
126{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500127 // Get Dump size.
128 auto size = getAllowedSize();
129
130 pid_t pid = fork();
131
132 if (pid == 0)
133 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500134 std::filesystem::path dumpPath(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500135 auto id = std::to_string(lastEntryId + 1);
136 dumpPath /= id;
137
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500138 auto strType = dumpTypeToString(type).value();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500139 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
140 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500141 path.empty() ? "" : path.c_str(), "-t", strType.c_str(), nullptr);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500142
143 // dreport script execution is failed.
144 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500145 lg2::error("Error occurred during dreport function execution, "
146 "errno: {ERRNO}",
147 "ERRNO", error);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500148 elog<InternalFailure>();
149 }
150 else if (pid > 0)
151 {
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500152 Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500153 if (type == DumpTypes::USER)
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500154 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500155 lg2::info("User initiated dump completed, resetting flag");
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500156 Manager::fUserDumpInProgress = false;
157 }
158 this->childPtrMap.erase(pid);
159 };
160 try
161 {
162 childPtrMap.emplace(pid,
163 std::make_unique<Child>(eventLoop.get(), pid,
164 WEXITED | WSTOPPED,
165 std::move(callback)));
166 }
167 catch (const sdeventplus::SdEventError& ex)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500168 {
169 // Failed to add to event loop
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500170 lg2::error(
171 "Error occurred during the sdeventplus::source::Child creation "
172 "ex: {ERROR}",
173 "ERROR", ex);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500174 elog<InternalFailure>();
175 }
176 }
177 else
178 {
179 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500180 lg2::error("Error occurred during fork, errno: {ERRNO}", "ERRNO",
181 error);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500182 elog<InternalFailure>();
183 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500184 return ++lastEntryId;
185}
186
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500187void Manager::createEntry(const std::filesystem::path& file)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500188{
189 // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
190 static constexpr auto ID_POS = 1;
191 static constexpr auto EPOCHTIME_POS = 2;
192 std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
193
194 std::smatch match;
195 std::string name = file.filename();
196
197 if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
198 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500199 lg2::error("Invalid Dump file name, FILENAME: {FILENAME}", "FILENAME",
200 file);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500201 return;
202 }
203
204 auto idString = match[ID_POS];
Xie Ning56bd7972022-02-25 15:20:02 +0800205 uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500206
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500207 auto id = stoul(idString);
208
209 // If there is an existing entry update it and return.
210 auto dumpEntry = entries.find(id);
211 if (dumpEntry != entries.end())
212 {
213 dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
Xie Ning56bd7972022-02-25 15:20:02 +0800214 ->update(timestamp, std::filesystem::file_size(file), file);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500215 return;
216 }
217
218 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500219 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500220
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500221 // TODO: Get the persisted originator id & type
222 // For now, replacing it with null
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500223 try
224 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500225 entries.insert(std::make_pair(
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500226 id, std::make_unique<bmc::Entry>(
Xie Ning56bd7972022-02-25 15:20:02 +0800227 bus, objPath.c_str(), id, timestamp,
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500228 std::filesystem::file_size(file), file,
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500229 phosphor::dump::OperationStatus::Completed, std::string(),
230 originatorTypes::Internal, *this)));
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500231 }
232 catch (const std::invalid_argument& e)
233 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500234 lg2::error(
235 "Error in creating dump entry, errormsg: {ERROR}, "
236 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}, TIMESTAMP: {TIMESTAMP}, "
237 "SIZE: {SIZE}, FILENAME: {FILENAME}",
238 "ERROR", e, "OBJECT_PATH", objPath, "ID", id, "TIMESTAMP",
239 timestamp, "SIZE", std::filesystem::file_size(file), "FILENAME",
240 file);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500241 return;
242 }
243}
244
245void Manager::watchCallback(const UserMap& fileInfo)
246{
247 for (const auto& i : fileInfo)
248 {
249 // For any new dump file create dump entry object
250 // and associated inotify watch.
251 if (IN_CLOSE_WRITE == i.second)
252 {
Chirag Sharma4cb07992022-05-09 04:37:22 -0500253 if (!std::filesystem::is_directory(i.first))
254 {
255 // Don't require filename to be passed, as the path
256 // of dump directory is stored in the childWatchMap
257 removeWatch(i.first.parent_path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500258
Chirag Sharma4cb07992022-05-09 04:37:22 -0500259 // dump file is written now create D-Bus entry
260 createEntry(i.first);
261 }
262 else
263 {
264 removeWatch(i.first);
265 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500266 }
267 // Start inotify watch on newly created directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500268 else if ((IN_CREATE == i.second) &&
269 std::filesystem::is_directory(i.first))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500270 {
271 auto watchObj = std::make_unique<Watch>(
272 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
273 std::bind(
274 std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
275 this, std::placeholders::_1));
276
277 childWatchMap.emplace(i.first, std::move(watchObj));
278 }
279 }
280}
281
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500282void Manager::removeWatch(const std::filesystem::path& path)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500283{
284 // Delete Watch entry from map.
285 childWatchMap.erase(path);
286}
287
288void Manager::restore()
289{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500290 std::filesystem::path dir(dumpDir);
291 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500292 {
293 return;
294 }
295
296 // Dump file path: <DUMP_PATH>/<id>/<filename>
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500297 for (const auto& p : std::filesystem::directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500298 {
299 auto idStr = p.path().filename().string();
300
301 // Consider only directory's with dump id as name.
302 // Note: As per design one file per directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500303 if ((std::filesystem::is_directory(p.path())) &&
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500304 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
305 {
Patrick Williams78e88402023-05-10 07:50:48 -0500306 lastEntryId = std::max(lastEntryId,
307 static_cast<uint32_t>(std::stoul(idStr)));
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500308 auto fileIt = std::filesystem::directory_iterator(p.path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500309 // Create dump entry d-bus object.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500310 if (fileIt != std::filesystem::end(fileIt))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500311 {
312 createEntry(fileIt->path());
313 }
314 }
315 }
316}
317
Xie Ningfc69f352022-05-17 16:06:52 +0800318size_t getDirectorySize(const std::string dir)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500319{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500320 auto size = 0;
Xie Ningfc69f352022-05-17 16:06:52 +0800321 for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500322 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500323 if (!std::filesystem::is_directory(p))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500324 {
Tim Leebb9366d2021-06-24 14:00:07 +0800325 size += std::ceil(std::filesystem::file_size(p) / 1024.0);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500326 }
327 }
Xie Ningfc69f352022-05-17 16:06:52 +0800328 return size;
329}
330
331size_t Manager::getAllowedSize()
332{
333 // Get current size of the dump directory.
334 auto size = getDirectorySize(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500335
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500336 // Set the Dump size to Maximum if the free space is greater than
337 // Dump max size otherwise return the available size.
338
339 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
340
Xie Ningfc69f352022-05-17 16:06:52 +0800341#ifdef BMC_DUMP_ROTATE_CONFIG
342 // Delete the first existing file until the space is enough
343 while (size < BMC_DUMP_MIN_SPACE_REQD)
344 {
Patrick Williams78e88402023-05-10 07:50:48 -0500345 auto delEntry = min_element(entries.begin(), entries.end(),
346 [](const auto& l, const auto& r) {
347 return l.first < r.first;
348 });
349 auto delPath = std::filesystem::path(dumpDir) /
350 std::to_string(delEntry->first);
Xie Ningfc69f352022-05-17 16:06:52 +0800351
352 size += getDirectorySize(delPath);
353
354 delEntry->second->delete_();
355 }
356#else
357 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
358 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
359
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500360 if (size < BMC_DUMP_MIN_SPACE_REQD)
361 {
362 // Reached to maximum limit
363 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
364 }
Xie Ningfc69f352022-05-17 16:06:52 +0800365#endif
366
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500367 if (size > BMC_DUMP_MAX_SIZE)
368 {
369 size = BMC_DUMP_MAX_SIZE;
370 }
371
372 return size;
373}
374
375} // namespace bmc
376} // namespace dump
377} // namespace phosphor