blob: 26adf67e9121b5ba30693ed1ee464d0b467ba05a [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>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050016
Tim Leebb9366d2021-06-24 14:00:07 +080017#include <cmath>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050018#include <ctime>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050019#include <regex>
20
21namespace phosphor
22{
23namespace dump
24{
25namespace bmc
26{
27
28using namespace sdbusplus::xyz::openbmc_project::Common::Error;
29using namespace phosphor::logging;
30
31namespace internal
32{
33
34void Manager::create(Type type, std::vector<std::string> fullPaths)
35{
36 dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths);
37}
38
39} // namespace internal
40
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050041sdbusplus::message::object_path
Dhruvaraj Subhashchandranddc33662021-07-19 09:28:42 -050042 Manager::createDump(phosphor::dump::DumpCreateParams params)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050043{
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050044 if (!params.empty())
45 {
46 log<level::WARNING>("BMC dump accepts no additional parameters");
47 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050048 std::vector<std::string> paths;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050049 auto id = captureDump(Type::UserRequested, paths);
50
51 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050052 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050053
54 try
55 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050056 std::time_t timeStamp = std::time(nullptr);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050057 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050058 id, std::make_unique<bmc::Entry>(
59 bus, objPath.c_str(), id, timeStamp, 0, std::string(),
60 phosphor::dump::OperationStatus::InProgress, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050061 }
62 catch (const std::invalid_argument& e)
63 {
George Liu858fbb22021-07-01 12:25:44 +080064 log<level::ERR>(fmt::format("Error in creating dump entry, "
65 "errormsg({}), OBJECTPATH({}), ID({})",
66 e.what(), objPath.c_str(), id)
67 .c_str());
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050068 elog<InternalFailure>();
69 }
70
71 return objPath.string();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050072}
73
74uint32_t Manager::captureDump(Type type,
75 const std::vector<std::string>& fullPaths)
76{
77 // Get Dump size.
78 auto size = getAllowedSize();
79
80 pid_t pid = fork();
81
82 if (pid == 0)
83 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050084 std::filesystem::path dumpPath(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050085 auto id = std::to_string(lastEntryId + 1);
86 dumpPath /= id;
87
88 // get dreport type map entry
89 auto tempType = TypeMap.find(type);
90
91 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
92 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
93 fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
94 tempType->second.c_str(), nullptr);
95
96 // dreport script execution is failed.
97 auto error = errno;
George Liu858fbb22021-07-01 12:25:44 +080098 log<level::ERR>(
99 fmt::format(
100 "Error occurred during dreport function execution, errno({})",
101 error)
102 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500103 elog<InternalFailure>();
104 }
105 else if (pid > 0)
106 {
107 auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid,
108 WEXITED | WSTOPPED, callback, nullptr);
109 if (0 > rc)
110 {
111 // Failed to add to event loop
George Liu858fbb22021-07-01 12:25:44 +0800112 log<level::ERR>(
113 fmt::format(
114 "Error occurred during the sd_event_add_child call, rc({})",
115 rc)
116 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500117 elog<InternalFailure>();
118 }
119 }
120 else
121 {
122 auto error = errno;
George Liu858fbb22021-07-01 12:25:44 +0800123 log<level::ERR>(
124 fmt::format("Error occurred during fork, errno({})", error)
125 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500126 elog<InternalFailure>();
127 }
128
129 return ++lastEntryId;
130}
131
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500132void Manager::createEntry(const std::filesystem::path& file)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500133{
134 // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
135 static constexpr auto ID_POS = 1;
136 static constexpr auto EPOCHTIME_POS = 2;
137 std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
138
139 std::smatch match;
140 std::string name = file.filename();
141
142 if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
143 {
George Liu858fbb22021-07-01 12:25:44 +0800144 log<level::ERR>(fmt::format("Invalid Dump file name, FILENAME({})",
145 file.filename().c_str())
146 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500147 return;
148 }
149
150 auto idString = match[ID_POS];
Xie Ning56bd7972022-02-25 15:20:02 +0800151 uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500152
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500153 auto id = stoul(idString);
154
155 // If there is an existing entry update it and return.
156 auto dumpEntry = entries.find(id);
157 if (dumpEntry != entries.end())
158 {
159 dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
Xie Ning56bd7972022-02-25 15:20:02 +0800160 ->update(timestamp, std::filesystem::file_size(file), file);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500161 return;
162 }
163
164 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500165 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500166
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500167 try
168 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500169 entries.insert(std::make_pair(
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500170 id, std::make_unique<bmc::Entry>(
Xie Ning56bd7972022-02-25 15:20:02 +0800171 bus, objPath.c_str(), id, timestamp,
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500172 std::filesystem::file_size(file), file,
173 phosphor::dump::OperationStatus::Completed, *this)));
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500174 }
175 catch (const std::invalid_argument& e)
176 {
George Liu858fbb22021-07-01 12:25:44 +0800177 log<level::ERR>(
178 fmt::format(
179 "Error in creating dump entry, errormsg({}), OBJECTPATH({}), "
180 "ID({}), TIMESTAMP({}), SIZE({}), FILENAME({})",
Xie Ning56bd7972022-02-25 15:20:02 +0800181 e.what(), objPath.c_str(), id, timestamp,
George Liu858fbb22021-07-01 12:25:44 +0800182 std::filesystem::file_size(file), file.filename().c_str())
183 .c_str());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500184 return;
185 }
186}
187
188void Manager::watchCallback(const UserMap& fileInfo)
189{
190 for (const auto& i : fileInfo)
191 {
192 // For any new dump file create dump entry object
193 // and associated inotify watch.
194 if (IN_CLOSE_WRITE == i.second)
195 {
196 removeWatch(i.first);
197
198 createEntry(i.first);
199 }
200 // Start inotify watch on newly created directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500201 else if ((IN_CREATE == i.second) &&
202 std::filesystem::is_directory(i.first))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500203 {
204 auto watchObj = std::make_unique<Watch>(
205 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
206 std::bind(
207 std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
208 this, std::placeholders::_1));
209
210 childWatchMap.emplace(i.first, std::move(watchObj));
211 }
212 }
213}
214
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500215void Manager::removeWatch(const std::filesystem::path& path)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500216{
217 // Delete Watch entry from map.
218 childWatchMap.erase(path);
219}
220
221void Manager::restore()
222{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500223 std::filesystem::path dir(dumpDir);
224 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500225 {
226 return;
227 }
228
229 // Dump file path: <DUMP_PATH>/<id>/<filename>
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500230 for (const auto& p : std::filesystem::directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500231 {
232 auto idStr = p.path().filename().string();
233
234 // Consider only directory's with dump id as name.
235 // Note: As per design one file per directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500236 if ((std::filesystem::is_directory(p.path())) &&
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500237 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
238 {
239 lastEntryId =
240 std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500241 auto fileIt = std::filesystem::directory_iterator(p.path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500242 // Create dump entry d-bus object.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500243 if (fileIt != std::filesystem::end(fileIt))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500244 {
245 createEntry(fileIt->path());
246 }
247 }
248 }
249}
250
251size_t Manager::getAllowedSize()
252{
253 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
254 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
255
256 auto size = 0;
257
258 // Get current size of the dump directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500259 for (const auto& p : std::filesystem::recursive_directory_iterator(dumpDir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500260 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500261 if (!std::filesystem::is_directory(p))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500262 {
Tim Leebb9366d2021-06-24 14:00:07 +0800263 size += std::ceil(std::filesystem::file_size(p) / 1024.0);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500264 }
265 }
266
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500267 // Set the Dump size to Maximum if the free space is greater than
268 // Dump max size otherwise return the available size.
269
270 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
271
272 if (size < BMC_DUMP_MIN_SPACE_REQD)
273 {
274 // Reached to maximum limit
275 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
276 }
277 if (size > BMC_DUMP_MAX_SIZE)
278 {
279 size = BMC_DUMP_MAX_SIZE;
280 }
281
282 return size;
283}
284
285} // namespace bmc
286} // namespace dump
287} // namespace phosphor