blob: 68d0759799e693e13b97f153ddc69709976e5149 [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"
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -05006#include "dump_types.hpp"
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05007#include "xyz/openbmc_project/Common/error.hpp"
8#include "xyz/openbmc_project/Dump/Create/error.hpp"
9
10#include <sys/inotify.h>
11#include <unistd.h>
12
13#include <phosphor-logging/elog-errors.hpp>
14#include <phosphor-logging/elog.hpp>
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050015#include <phosphor-logging/lg2.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;
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050034constexpr auto BMC_DUMP = "BMC_DUMP";
Marri Devender Rao73953b82022-02-15 09:15:42 -060035
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050036sdbusplus::message::object_path
Dhruvaraj Subhashchandranddc33662021-07-19 09:28:42 -050037 Manager::createDump(phosphor::dump::DumpCreateParams params)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050038{
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050039 if (params.size() > CREATE_DUMP_MAX_PARAMS)
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050040 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050041 lg2::warning("BMC dump accepts not more than 2 additional parameters");
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050042 }
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050043
44 // Get the originator id and type from params
45 std::string originatorId;
46 originatorTypes originatorType;
47
48 phosphor::dump::extractOriginatorProperties(params, originatorId,
49 originatorType);
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050050 using CreateParameters =
51 sdbusplus::common::xyz::openbmc_project::dump::Create::CreateParameters;
52
53 DumpTypes dumpType = DumpTypes::USER;
54 std::string type = extractParameter<std::string>(
55 convertCreateParametersToString(CreateParameters::DumpType), params);
56 if (!type.empty())
57 {
58 dumpType = validateDumpType(type, BMC_DUMP);
59 }
Dhruvaraj Subhashchandrane4350f92023-06-29 05:57:47 -050060
61 if (dumpType == DumpTypes::ELOG)
62 {
63 dumpType = getErrorDumpType(params);
64 }
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050065 std::string path = extractParameter<std::string>(
66 convertCreateParametersToString(CreateParameters::FilePath), params);
67
68 if ((Manager::fUserDumpInProgress == true) && (dumpType == DumpTypes::USER))
69 {
70 lg2::info("Another user initiated dump in progress");
71 elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
72 }
73
74 lg2::info("Initiating new BMC dump with type: {TYPE} path: {PATH}", "TYPE",
75 dumpTypeToString(dumpType).value(), "PATH", path);
76
77 auto id = captureDump(dumpType, path);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050078
79 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050080 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050081
82 try
83 {
Claire Weinanc0ab9d42022-08-17 23:01:07 -070084 uint64_t timeStamp =
85 std::chrono::duration_cast<std::chrono::microseconds>(
86 std::chrono::system_clock::now().time_since_epoch())
87 .count();
88
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050089 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050090 id, std::make_unique<bmc::Entry>(
91 bus, objPath.c_str(), id, timeStamp, 0, std::string(),
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050092 phosphor::dump::OperationStatus::InProgress, originatorId,
93 originatorType, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050094 }
95 catch (const std::invalid_argument& e)
96 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050097 lg2::error("Error in creating dump entry, errormsg: {ERROR}, "
98 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
99 "ERROR", e, "OBJECT_PATH", objPath, "ID", id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500100 elog<InternalFailure>();
101 }
102
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500103 if (dumpType == DumpTypes::USER)
104 {
105 Manager::fUserDumpInProgress = true;
106 }
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500107 return objPath.string();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500108}
109
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500110uint32_t Manager::captureDump(DumpTypes type, const std::string& path)
111{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500112 // Get Dump size.
113 auto size = getAllowedSize();
114
115 pid_t pid = fork();
116
117 if (pid == 0)
118 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500119 std::filesystem::path dumpPath(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500120 auto id = std::to_string(lastEntryId + 1);
121 dumpPath /= id;
122
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500123 auto strType = dumpTypeToString(type).value();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500124 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
125 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500126 path.empty() ? "" : path.c_str(), "-t", strType.c_str(), nullptr);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500127
128 // dreport script execution is failed.
129 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500130 lg2::error("Error occurred during dreport function execution, "
131 "errno: {ERRNO}",
132 "ERRNO", error);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500133 elog<InternalFailure>();
134 }
135 else if (pid > 0)
136 {
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500137 Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500138 if (type == DumpTypes::USER)
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500139 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500140 lg2::info("User initiated dump completed, resetting flag");
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500141 Manager::fUserDumpInProgress = false;
142 }
143 this->childPtrMap.erase(pid);
144 };
145 try
146 {
147 childPtrMap.emplace(pid,
148 std::make_unique<Child>(eventLoop.get(), pid,
149 WEXITED | WSTOPPED,
150 std::move(callback)));
151 }
152 catch (const sdeventplus::SdEventError& ex)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500153 {
154 // Failed to add to event loop
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500155 lg2::error(
156 "Error occurred during the sdeventplus::source::Child creation "
157 "ex: {ERROR}",
158 "ERROR", ex);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500159 elog<InternalFailure>();
160 }
161 }
162 else
163 {
164 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500165 lg2::error("Error occurred during fork, errno: {ERRNO}", "ERRNO",
166 error);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500167 elog<InternalFailure>();
168 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500169 return ++lastEntryId;
170}
171
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500172void Manager::createEntry(const std::filesystem::path& file)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500173{
174 // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
175 static constexpr auto ID_POS = 1;
176 static constexpr auto EPOCHTIME_POS = 2;
177 std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
178
179 std::smatch match;
180 std::string name = file.filename();
181
182 if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
183 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500184 lg2::error("Invalid Dump file name, FILENAME: {FILENAME}", "FILENAME",
185 file);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500186 return;
187 }
188
189 auto idString = match[ID_POS];
Xie Ning56bd7972022-02-25 15:20:02 +0800190 uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500191
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500192 auto id = stoul(idString);
193
194 // If there is an existing entry update it and return.
195 auto dumpEntry = entries.find(id);
196 if (dumpEntry != entries.end())
197 {
198 dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
Xie Ning56bd7972022-02-25 15:20:02 +0800199 ->update(timestamp, std::filesystem::file_size(file), file);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500200 return;
201 }
202
203 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500204 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500205
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500206 // TODO: Get the persisted originator id & type
207 // For now, replacing it with null
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500208 try
209 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500210 entries.insert(std::make_pair(
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500211 id, std::make_unique<bmc::Entry>(
Xie Ning56bd7972022-02-25 15:20:02 +0800212 bus, objPath.c_str(), id, timestamp,
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500213 std::filesystem::file_size(file), file,
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500214 phosphor::dump::OperationStatus::Completed, std::string(),
215 originatorTypes::Internal, *this)));
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500216 }
217 catch (const std::invalid_argument& e)
218 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500219 lg2::error(
220 "Error in creating dump entry, errormsg: {ERROR}, "
221 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}, TIMESTAMP: {TIMESTAMP}, "
222 "SIZE: {SIZE}, FILENAME: {FILENAME}",
223 "ERROR", e, "OBJECT_PATH", objPath, "ID", id, "TIMESTAMP",
224 timestamp, "SIZE", std::filesystem::file_size(file), "FILENAME",
225 file);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500226 return;
227 }
228}
229
230void Manager::watchCallback(const UserMap& fileInfo)
231{
232 for (const auto& i : fileInfo)
233 {
234 // For any new dump file create dump entry object
235 // and associated inotify watch.
236 if (IN_CLOSE_WRITE == i.second)
237 {
Chirag Sharma4cb07992022-05-09 04:37:22 -0500238 if (!std::filesystem::is_directory(i.first))
239 {
240 // Don't require filename to be passed, as the path
241 // of dump directory is stored in the childWatchMap
242 removeWatch(i.first.parent_path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500243
Chirag Sharma4cb07992022-05-09 04:37:22 -0500244 // dump file is written now create D-Bus entry
245 createEntry(i.first);
246 }
247 else
248 {
249 removeWatch(i.first);
250 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500251 }
252 // Start inotify watch on newly created directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500253 else if ((IN_CREATE == i.second) &&
254 std::filesystem::is_directory(i.first))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500255 {
256 auto watchObj = std::make_unique<Watch>(
257 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
258 std::bind(
259 std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
260 this, std::placeholders::_1));
261
262 childWatchMap.emplace(i.first, std::move(watchObj));
263 }
264 }
265}
266
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500267void Manager::removeWatch(const std::filesystem::path& path)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500268{
269 // Delete Watch entry from map.
270 childWatchMap.erase(path);
271}
272
273void Manager::restore()
274{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500275 std::filesystem::path dir(dumpDir);
276 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500277 {
278 return;
279 }
280
281 // Dump file path: <DUMP_PATH>/<id>/<filename>
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500282 for (const auto& p : std::filesystem::directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500283 {
284 auto idStr = p.path().filename().string();
285
286 // Consider only directory's with dump id as name.
287 // Note: As per design one file per directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500288 if ((std::filesystem::is_directory(p.path())) &&
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500289 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
290 {
Patrick Williams78e88402023-05-10 07:50:48 -0500291 lastEntryId = std::max(lastEntryId,
292 static_cast<uint32_t>(std::stoul(idStr)));
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500293 auto fileIt = std::filesystem::directory_iterator(p.path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500294 // Create dump entry d-bus object.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500295 if (fileIt != std::filesystem::end(fileIt))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500296 {
297 createEntry(fileIt->path());
298 }
299 }
300 }
301}
302
Xie Ningfc69f352022-05-17 16:06:52 +0800303size_t getDirectorySize(const std::string dir)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500304{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500305 auto size = 0;
Xie Ningfc69f352022-05-17 16:06:52 +0800306 for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500307 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500308 if (!std::filesystem::is_directory(p))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500309 {
Tim Leebb9366d2021-06-24 14:00:07 +0800310 size += std::ceil(std::filesystem::file_size(p) / 1024.0);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500311 }
312 }
Xie Ningfc69f352022-05-17 16:06:52 +0800313 return size;
314}
315
316size_t Manager::getAllowedSize()
317{
318 // Get current size of the dump directory.
319 auto size = getDirectorySize(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500320
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500321 // Set the Dump size to Maximum if the free space is greater than
322 // Dump max size otherwise return the available size.
323
324 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
325
Xie Ningfc69f352022-05-17 16:06:52 +0800326#ifdef BMC_DUMP_ROTATE_CONFIG
327 // Delete the first existing file until the space is enough
328 while (size < BMC_DUMP_MIN_SPACE_REQD)
329 {
Patrick Williams78e88402023-05-10 07:50:48 -0500330 auto delEntry = min_element(entries.begin(), entries.end(),
331 [](const auto& l, const auto& r) {
332 return l.first < r.first;
333 });
334 auto delPath = std::filesystem::path(dumpDir) /
335 std::to_string(delEntry->first);
Xie Ningfc69f352022-05-17 16:06:52 +0800336
337 size += getDirectorySize(delPath);
338
339 delEntry->second->delete_();
340 }
341#else
342 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
343 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
344
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500345 if (size < BMC_DUMP_MIN_SPACE_REQD)
346 {
347 // Reached to maximum limit
348 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
349 }
Xie Ningfc69f352022-05-17 16:06:52 +0800350#endif
351
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500352 if (size > BMC_DUMP_MAX_SIZE)
353 {
354 size = BMC_DUMP_MAX_SIZE;
355 }
356
357 return size;
358}
359
360} // namespace bmc
361} // namespace dump
362} // namespace phosphor