blob: 3741f83ce52bb6f4e9fe8bd15210fa726d1f9b05 [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>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050020
21namespace phosphor
22{
23namespace dump
24{
25namespace bmc
26{
27
28using namespace sdbusplus::xyz::openbmc_project::Common::Error;
29using namespace phosphor::logging;
30
Marri Devender Rao73953b82022-02-15 09:15:42 -060031bool Manager::fUserDumpInProgress = false;
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050032constexpr auto BMC_DUMP = "BMC_DUMP";
Marri Devender Rao73953b82022-02-15 09:15:42 -060033
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050034sdbusplus::message::object_path
Dhruvaraj Subhashchandranddc33662021-07-19 09:28:42 -050035 Manager::createDump(phosphor::dump::DumpCreateParams params)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050036{
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050037 if (params.size() > CREATE_DUMP_MAX_PARAMS)
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050038 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050039 lg2::warning("BMC dump accepts not more than 2 additional parameters");
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050040 }
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050041
42 // Get the originator id and type from params
43 std::string originatorId;
44 originatorTypes originatorType;
45
46 phosphor::dump::extractOriginatorProperties(params, originatorId,
47 originatorType);
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050048 using CreateParameters =
49 sdbusplus::common::xyz::openbmc_project::dump::Create::CreateParameters;
50
51 DumpTypes dumpType = DumpTypes::USER;
52 std::string type = extractParameter<std::string>(
53 convertCreateParametersToString(CreateParameters::DumpType), params);
54 if (!type.empty())
55 {
56 dumpType = validateDumpType(type, BMC_DUMP);
57 }
Dhruvaraj Subhashchandrane4350f92023-06-29 05:57:47 -050058
59 if (dumpType == DumpTypes::ELOG)
60 {
61 dumpType = getErrorDumpType(params);
62 }
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050063 std::string path = extractParameter<std::string>(
64 convertCreateParametersToString(CreateParameters::FilePath), params);
65
66 if ((Manager::fUserDumpInProgress == true) && (dumpType == DumpTypes::USER))
67 {
68 lg2::info("Another user initiated dump in progress");
69 elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
70 }
71
72 lg2::info("Initiating new BMC dump with type: {TYPE} path: {PATH}", "TYPE",
Jayanth Othayoth418d4602024-11-25 22:17:03 -060073 dumpTypeToString(dumpType).value_or("unknown").c_str(), "PATH",
74 path);
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050075
76 auto id = captureDump(dumpType, path);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050077
78 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050079 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050080
81 try
82 {
Claire Weinanc0ab9d42022-08-17 23:01:07 -070083 uint64_t timeStamp =
84 std::chrono::duration_cast<std::chrono::microseconds>(
85 std::chrono::system_clock::now().time_since_epoch())
86 .count();
87
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050088 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050089 id, std::make_unique<bmc::Entry>(
90 bus, objPath.c_str(), id, timeStamp, 0, std::string(),
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050091 phosphor::dump::OperationStatus::InProgress, originatorId,
92 originatorType, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050093 }
94 catch (const std::invalid_argument& e)
95 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050096 lg2::error("Error in creating dump entry, errormsg: {ERROR}, "
97 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
98 "ERROR", e, "OBJECT_PATH", objPath, "ID", id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050099 elog<InternalFailure>();
100 }
101
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500102 if (dumpType == DumpTypes::USER)
103 {
104 Manager::fUserDumpInProgress = true;
105 }
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500106 return objPath.string();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500107}
108
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500109uint32_t Manager::captureDump(DumpTypes type, const std::string& path)
110{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500111 // Get Dump size.
112 auto size = getAllowedSize();
113
114 pid_t pid = fork();
115
116 if (pid == 0)
117 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500118 std::filesystem::path dumpPath(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500119 auto id = std::to_string(lastEntryId + 1);
120 dumpPath /= id;
121
Jayanth Othayoth418d4602024-11-25 22:17:03 -0600122 auto strType = dumpTypeToString(type).value_or("unknown");
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500123 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
124 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500125 path.empty() ? "" : path.c_str(), "-t", strType.c_str(), nullptr);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500126
127 // dreport script execution is failed.
128 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500129 lg2::error("Error occurred during dreport function execution, "
130 "errno: {ERRNO}",
131 "ERRNO", error);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500132 elog<InternalFailure>();
133 }
134 else if (pid > 0)
135 {
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500136 Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500137 if (type == DumpTypes::USER)
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500138 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500139 lg2::info("User initiated dump completed, resetting flag");
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500140 Manager::fUserDumpInProgress = false;
141 }
142 this->childPtrMap.erase(pid);
143 };
144 try
145 {
146 childPtrMap.emplace(pid,
147 std::make_unique<Child>(eventLoop.get(), pid,
148 WEXITED | WSTOPPED,
149 std::move(callback)));
150 }
151 catch (const sdeventplus::SdEventError& ex)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500152 {
153 // Failed to add to event loop
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500154 lg2::error(
155 "Error occurred during the sdeventplus::source::Child creation "
156 "ex: {ERROR}",
157 "ERROR", ex);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500158 elog<InternalFailure>();
159 }
160 }
161 else
162 {
163 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500164 lg2::error("Error occurred during fork, errno: {ERRNO}", "ERRNO",
165 error);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500166 elog<InternalFailure>();
167 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500168 return ++lastEntryId;
169}
170
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500171void Manager::createEntry(const std::filesystem::path& file)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500172{
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -0500173 auto dumpDetails = extractDumpDetails(file);
174 if (!dumpDetails)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500175 {
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -0500176 lg2::error("Failed to extract dump details from file name: {PATH}",
177 "PATH", file);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500178 return;
179 }
180
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -0500181 auto [id, timestamp, size] = *dumpDetails;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500182
183 // If there is an existing entry update it and return.
184 auto dumpEntry = entries.find(id);
185 if (dumpEntry != entries.end())
186 {
187 dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
Xie Ning56bd7972022-02-25 15:20:02 +0800188 ->update(timestamp, std::filesystem::file_size(file), file);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500189 return;
190 }
191
192 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500193 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500194
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500195 // TODO: Get the persisted originator id & type
196 // For now, replacing it with null
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500197 try
198 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500199 entries.insert(std::make_pair(
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500200 id, std::make_unique<bmc::Entry>(
Xie Ning56bd7972022-02-25 15:20:02 +0800201 bus, objPath.c_str(), id, timestamp,
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500202 std::filesystem::file_size(file), file,
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500203 phosphor::dump::OperationStatus::Completed, std::string(),
204 originatorTypes::Internal, *this)));
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500205 }
206 catch (const std::invalid_argument& e)
207 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500208 lg2::error(
209 "Error in creating dump entry, errormsg: {ERROR}, "
210 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}, TIMESTAMP: {TIMESTAMP}, "
211 "SIZE: {SIZE}, FILENAME: {FILENAME}",
212 "ERROR", e, "OBJECT_PATH", objPath, "ID", id, "TIMESTAMP",
213 timestamp, "SIZE", std::filesystem::file_size(file), "FILENAME",
214 file);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500215 }
216}
217
218void Manager::watchCallback(const UserMap& fileInfo)
219{
220 for (const auto& i : fileInfo)
221 {
222 // For any new dump file create dump entry object
223 // and associated inotify watch.
224 if (IN_CLOSE_WRITE == i.second)
225 {
Chirag Sharma4cb07992022-05-09 04:37:22 -0500226 if (!std::filesystem::is_directory(i.first))
227 {
228 // Don't require filename to be passed, as the path
229 // of dump directory is stored in the childWatchMap
230 removeWatch(i.first.parent_path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500231
Chirag Sharma4cb07992022-05-09 04:37:22 -0500232 // dump file is written now create D-Bus entry
233 createEntry(i.first);
234 }
235 else
236 {
237 removeWatch(i.first);
238 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500239 }
240 // Start inotify watch on newly created directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500241 else if ((IN_CREATE == i.second) &&
242 std::filesystem::is_directory(i.first))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500243 {
244 auto watchObj = std::make_unique<Watch>(
245 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
246 std::bind(
247 std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
248 this, std::placeholders::_1));
249
250 childWatchMap.emplace(i.first, std::move(watchObj));
251 }
252 }
253}
254
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500255void Manager::removeWatch(const std::filesystem::path& path)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500256{
257 // Delete Watch entry from map.
258 childWatchMap.erase(path);
259}
260
261void Manager::restore()
262{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500263 std::filesystem::path dir(dumpDir);
264 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500265 {
266 return;
267 }
268
269 // Dump file path: <DUMP_PATH>/<id>/<filename>
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500270 for (const auto& p : std::filesystem::directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500271 {
272 auto idStr = p.path().filename().string();
273
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -0500274 // Consider only directories with dump id as name.
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500275 // Note: As per design one file per directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500276 if ((std::filesystem::is_directory(p.path())) &&
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500277 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
278 {
Patrick Williams973b2912024-08-16 15:20:50 -0400279 lastEntryId =
280 std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -0500281 for (const auto& file :
282 std::filesystem::directory_iterator(p.path()))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500283 {
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -0500284 // Skip .preserve directory
285 if (file.path().filename() == PRESERVE)
286 {
287 continue;
288 }
289
290 // Entry Object path.
291 auto objPath = std::filesystem::path(baseEntryPath) / idStr;
Patrick Williams973b2912024-08-16 15:20:50 -0400292 auto entry = Entry::deserializeEntry(
293 bus, std::stoul(idStr), objPath.string(), file.path(),
294 *this);
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -0500295
296 if (entry != nullptr)
297 {
298 entries.insert(
299 std::make_pair(entry->getDumpId(), std::move(entry)));
300 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500301 }
302 }
303 }
304}
305
Xie Ningfc69f352022-05-17 16:06:52 +0800306size_t getDirectorySize(const std::string dir)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500307{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500308 auto size = 0;
Xie Ningfc69f352022-05-17 16:06:52 +0800309 for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500310 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500311 if (!std::filesystem::is_directory(p))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500312 {
Jayanth Othayoth17ba8762024-11-25 10:43:24 -0600313 std::uintmax_t fileSize = std::filesystem::file_size(p);
314 size += std::ceil(static_cast<double>(fileSize) / 1024.0);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500315 }
316 }
Xie Ningfc69f352022-05-17 16:06:52 +0800317 return size;
318}
319
320size_t Manager::getAllowedSize()
321{
322 // Get current size of the dump directory.
323 auto size = getDirectorySize(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500324
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500325 // Set the Dump size to Maximum if the free space is greater than
326 // Dump max size otherwise return the available size.
327
328 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
329
Xie Ningfc69f352022-05-17 16:06:52 +0800330#ifdef BMC_DUMP_ROTATE_CONFIG
331 // Delete the first existing file until the space is enough
332 while (size < BMC_DUMP_MIN_SPACE_REQD)
333 {
Patrick Williamse70edac2023-10-20 11:19:08 -0500334 auto delEntry = min_element(
335 entries.begin(), entries.end(),
336 [](const auto& l, const auto& r) { return l.first < r.first; });
Patrick Williams78e88402023-05-10 07:50:48 -0500337 auto delPath = std::filesystem::path(dumpDir) /
338 std::to_string(delEntry->first);
Xie Ningfc69f352022-05-17 16:06:52 +0800339
340 size += getDirectorySize(delPath);
341
342 delEntry->second->delete_();
343 }
344#else
345 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
346 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
347
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500348 if (size < BMC_DUMP_MIN_SPACE_REQD)
349 {
350 // Reached to maximum limit
351 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
352 }
Xie Ningfc69f352022-05-17 16:06:52 +0800353#endif
354
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500355 if (size > BMC_DUMP_MAX_SIZE)
356 {
357 size = BMC_DUMP_MAX_SIZE;
358 }
359
360 return size;
361}
362
363} // namespace bmc
364} // namespace dump
365} // namespace phosphor