blob: 8161f410beac35c7d322f865fca337024cd147b4 [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
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;
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 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050050 lg2::warning("BMC dump accepts not more than 2 additional parameters");
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050051 }
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050052
Marri Devender Rao73953b82022-02-15 09:15:42 -060053 if (Manager::fUserDumpInProgress == true)
54 {
55 elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
56 }
57
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050058 // Get the originator id and type from params
59 std::string originatorId;
60 originatorTypes originatorType;
61
62 phosphor::dump::extractOriginatorProperties(params, originatorId,
63 originatorType);
64
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050065 std::vector<std::string> paths;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050066 auto id = captureDump(Type::UserRequested, paths);
67
68 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050069 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050070
71 try
72 {
Claire Weinanc0ab9d42022-08-17 23:01:07 -070073 uint64_t timeStamp =
74 std::chrono::duration_cast<std::chrono::microseconds>(
75 std::chrono::system_clock::now().time_since_epoch())
76 .count();
77
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050078 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050079 id, std::make_unique<bmc::Entry>(
80 bus, objPath.c_str(), id, timeStamp, 0, std::string(),
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050081 phosphor::dump::OperationStatus::InProgress, originatorId,
82 originatorType, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050083 }
84 catch (const std::invalid_argument& e)
85 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050086 lg2::error("Error in creating dump entry, errormsg: {ERROR}, "
87 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
88 "ERROR", e, "OBJECT_PATH", objPath, "ID", id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050089 elog<InternalFailure>();
90 }
91
Marri Devender Rao73953b82022-02-15 09:15:42 -060092 Manager::fUserDumpInProgress = true;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050093 return objPath.string();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050094}
95
96uint32_t Manager::captureDump(Type type,
97 const std::vector<std::string>& fullPaths)
98{
99 // Get Dump size.
100 auto size = getAllowedSize();
101
102 pid_t pid = fork();
103
104 if (pid == 0)
105 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500106 std::filesystem::path dumpPath(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500107 auto id = std::to_string(lastEntryId + 1);
108 dumpPath /= id;
109
110 // get dreport type map entry
111 auto tempType = TypeMap.find(type);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500112 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
113 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
114 fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
115 tempType->second.c_str(), nullptr);
116
117 // dreport script execution is failed.
118 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500119 lg2::error("Error occurred during dreport function execution, "
120 "errno: {ERRNO}",
121 "ERRNO", error);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500122 elog<InternalFailure>();
123 }
124 else if (pid > 0)
125 {
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500126 Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
127 if (type == Type::UserRequested)
128 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500129 lg2::info("User initiated dump completed, resetting flag");
Marri Devender Rao3ed02c32022-06-28 23:12:14 -0500130 Manager::fUserDumpInProgress = false;
131 }
132 this->childPtrMap.erase(pid);
133 };
134 try
135 {
136 childPtrMap.emplace(pid,
137 std::make_unique<Child>(eventLoop.get(), pid,
138 WEXITED | WSTOPPED,
139 std::move(callback)));
140 }
141 catch (const sdeventplus::SdEventError& ex)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500142 {
143 // Failed to add to event loop
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500144 lg2::error(
145 "Error occurred during the sdeventplus::source::Child creation "
146 "ex: {ERROR}",
147 "ERROR", ex);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500148 elog<InternalFailure>();
149 }
150 }
151 else
152 {
153 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500154 lg2::error("Error occurred during fork, errno: {ERRNO}", "ERRNO",
155 error);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500156 elog<InternalFailure>();
157 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500158 return ++lastEntryId;
159}
160
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500161void Manager::createEntry(const std::filesystem::path& file)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500162{
163 // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
164 static constexpr auto ID_POS = 1;
165 static constexpr auto EPOCHTIME_POS = 2;
166 std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
167
168 std::smatch match;
169 std::string name = file.filename();
170
171 if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
172 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500173 lg2::error("Invalid Dump file name, FILENAME: {FILENAME}", "FILENAME",
174 file);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500175 return;
176 }
177
178 auto idString = match[ID_POS];
Xie Ning56bd7972022-02-25 15:20:02 +0800179 uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500180
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500181 auto id = stoul(idString);
182
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 return;
216 }
217}
218
219void Manager::watchCallback(const UserMap& fileInfo)
220{
221 for (const auto& i : fileInfo)
222 {
223 // For any new dump file create dump entry object
224 // and associated inotify watch.
225 if (IN_CLOSE_WRITE == i.second)
226 {
Chirag Sharma4cb07992022-05-09 04:37:22 -0500227 if (!std::filesystem::is_directory(i.first))
228 {
229 // Don't require filename to be passed, as the path
230 // of dump directory is stored in the childWatchMap
231 removeWatch(i.first.parent_path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500232
Chirag Sharma4cb07992022-05-09 04:37:22 -0500233 // dump file is written now create D-Bus entry
234 createEntry(i.first);
235 }
236 else
237 {
238 removeWatch(i.first);
239 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500240 }
241 // Start inotify watch on newly created directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500242 else if ((IN_CREATE == i.second) &&
243 std::filesystem::is_directory(i.first))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500244 {
245 auto watchObj = std::make_unique<Watch>(
246 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
247 std::bind(
248 std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
249 this, std::placeholders::_1));
250
251 childWatchMap.emplace(i.first, std::move(watchObj));
252 }
253 }
254}
255
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500256void Manager::removeWatch(const std::filesystem::path& path)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500257{
258 // Delete Watch entry from map.
259 childWatchMap.erase(path);
260}
261
262void Manager::restore()
263{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500264 std::filesystem::path dir(dumpDir);
265 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500266 {
267 return;
268 }
269
270 // Dump file path: <DUMP_PATH>/<id>/<filename>
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500271 for (const auto& p : std::filesystem::directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500272 {
273 auto idStr = p.path().filename().string();
274
275 // Consider only directory's with dump id as name.
276 // Note: As per design one file per directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500277 if ((std::filesystem::is_directory(p.path())) &&
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500278 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
279 {
Patrick Williams78e88402023-05-10 07:50:48 -0500280 lastEntryId = std::max(lastEntryId,
281 static_cast<uint32_t>(std::stoul(idStr)));
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500282 auto fileIt = std::filesystem::directory_iterator(p.path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500283 // Create dump entry d-bus object.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500284 if (fileIt != std::filesystem::end(fileIt))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500285 {
286 createEntry(fileIt->path());
287 }
288 }
289 }
290}
291
Xie Ningfc69f352022-05-17 16:06:52 +0800292size_t getDirectorySize(const std::string dir)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500293{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500294 auto size = 0;
Xie Ningfc69f352022-05-17 16:06:52 +0800295 for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500296 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500297 if (!std::filesystem::is_directory(p))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500298 {
Tim Leebb9366d2021-06-24 14:00:07 +0800299 size += std::ceil(std::filesystem::file_size(p) / 1024.0);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500300 }
301 }
Xie Ningfc69f352022-05-17 16:06:52 +0800302 return size;
303}
304
305size_t Manager::getAllowedSize()
306{
307 // Get current size of the dump directory.
308 auto size = getDirectorySize(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500309
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500310 // Set the Dump size to Maximum if the free space is greater than
311 // Dump max size otherwise return the available size.
312
313 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
314
Xie Ningfc69f352022-05-17 16:06:52 +0800315#ifdef BMC_DUMP_ROTATE_CONFIG
316 // Delete the first existing file until the space is enough
317 while (size < BMC_DUMP_MIN_SPACE_REQD)
318 {
Patrick Williams78e88402023-05-10 07:50:48 -0500319 auto delEntry = min_element(entries.begin(), entries.end(),
320 [](const auto& l, const auto& r) {
321 return l.first < r.first;
322 });
323 auto delPath = std::filesystem::path(dumpDir) /
324 std::to_string(delEntry->first);
Xie Ningfc69f352022-05-17 16:06:52 +0800325
326 size += getDirectorySize(delPath);
327
328 delEntry->second->delete_();
329 }
330#else
331 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
332 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
333
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500334 if (size < BMC_DUMP_MIN_SPACE_REQD)
335 {
336 // Reached to maximum limit
337 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
338 }
Xie Ningfc69f352022-05-17 16:06:52 +0800339#endif
340
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500341 if (size > BMC_DUMP_MAX_SIZE)
342 {
343 size = BMC_DUMP_MAX_SIZE;
344 }
345
346 return size;
347}
348
349} // namespace bmc
350} // namespace dump
351} // namespace phosphor