blob: 4ffcd14e2c0014f51d89ebdcccc9b91807461c52 [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
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050013#include <ctime>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050014#include <phosphor-logging/elog-errors.hpp>
15#include <phosphor-logging/elog.hpp>
16#include <regex>
17
18namespace phosphor
19{
20namespace dump
21{
22namespace bmc
23{
24
25using namespace sdbusplus::xyz::openbmc_project::Common::Error;
26using namespace phosphor::logging;
27
28namespace internal
29{
30
31void Manager::create(Type type, std::vector<std::string> fullPaths)
32{
33 dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths);
34}
35
36} // namespace internal
37
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050038sdbusplus::message::object_path
39 Manager::createDump(std::map<std::string, std::string> params)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050040{
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050041 if (!params.empty())
42 {
43 log<level::WARNING>("BMC dump accepts no additional parameters");
44 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050045 std::vector<std::string> paths;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050046 auto id = captureDump(Type::UserRequested, paths);
47
48 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050049 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050050
51 try
52 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050053 std::time_t timeStamp = std::time(nullptr);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050054 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050055 id, std::make_unique<bmc::Entry>(
56 bus, objPath.c_str(), id, timeStamp, 0, std::string(),
57 phosphor::dump::OperationStatus::InProgress, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050058 }
59 catch (const std::invalid_argument& e)
60 {
61 log<level::ERR>(e.what());
62 log<level::ERR>("Error in creating dump entry",
63 entry("OBJECTPATH=%s", objPath.c_str()),
64 entry("ID=%d", id));
65 elog<InternalFailure>();
66 }
67
68 return objPath.string();
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050069}
70
71uint32_t Manager::captureDump(Type type,
72 const std::vector<std::string>& fullPaths)
73{
74 // Get Dump size.
75 auto size = getAllowedSize();
76
77 pid_t pid = fork();
78
79 if (pid == 0)
80 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050081 std::filesystem::path dumpPath(dumpDir);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050082 auto id = std::to_string(lastEntryId + 1);
83 dumpPath /= id;
84
85 // get dreport type map entry
86 auto tempType = TypeMap.find(type);
87
88 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
89 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
90 fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
91 tempType->second.c_str(), nullptr);
92
93 // dreport script execution is failed.
94 auto error = errno;
95 log<level::ERR>("Error occurred during dreport function execution",
96 entry("ERRNO=%d", error));
97 elog<InternalFailure>();
98 }
99 else if (pid > 0)
100 {
101 auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid,
102 WEXITED | WSTOPPED, callback, nullptr);
103 if (0 > rc)
104 {
105 // Failed to add to event loop
106 log<level::ERR>("Error occurred during the sd_event_add_child call",
107 entry("RC=%d", rc));
108 elog<InternalFailure>();
109 }
110 }
111 else
112 {
113 auto error = errno;
114 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
115 elog<InternalFailure>();
116 }
117
118 return ++lastEntryId;
119}
120
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500121void Manager::createEntry(const std::filesystem::path& file)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500122{
123 // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
124 static constexpr auto ID_POS = 1;
125 static constexpr auto EPOCHTIME_POS = 2;
126 std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
127
128 std::smatch match;
129 std::string name = file.filename();
130
131 if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
132 {
133 log<level::ERR>("Invalid Dump file name",
134 entry("FILENAME=%s", file.filename().c_str()));
135 return;
136 }
137
138 auto idString = match[ID_POS];
139 auto msString = match[EPOCHTIME_POS];
140
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500141 auto id = stoul(idString);
142
143 // If there is an existing entry update it and return.
144 auto dumpEntry = entries.find(id);
145 if (dumpEntry != entries.end())
146 {
147 dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500148 ->update(stoull(msString), std::filesystem::file_size(file), file);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500149 return;
150 }
151
152 // Entry Object path.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500153 auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500154
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500155 try
156 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500157 entries.insert(std::make_pair(
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500158 id, std::make_unique<bmc::Entry>(
159 bus, objPath.c_str(), id, stoull(msString),
160 std::filesystem::file_size(file), file,
161 phosphor::dump::OperationStatus::Completed, *this)));
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500162 }
163 catch (const std::invalid_argument& e)
164 {
165 log<level::ERR>(e.what());
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500166 log<level::ERR>("Error in creating dump entry",
167 entry("OBJECTPATH=%s", objPath.c_str()),
168 entry("ID=%d", id),
169 entry("TIMESTAMP=%ull", stoull(msString)),
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500170 entry("SIZE=%d", std::filesystem::file_size(file)),
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500171 entry("FILENAME=%s", file.c_str()));
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500172 return;
173 }
174}
175
176void Manager::watchCallback(const UserMap& fileInfo)
177{
178 for (const auto& i : fileInfo)
179 {
180 // For any new dump file create dump entry object
181 // and associated inotify watch.
182 if (IN_CLOSE_WRITE == i.second)
183 {
184 removeWatch(i.first);
185
186 createEntry(i.first);
187 }
188 // Start inotify watch on newly created directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500189 else if ((IN_CREATE == i.second) &&
190 std::filesystem::is_directory(i.first))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500191 {
192 auto watchObj = std::make_unique<Watch>(
193 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
194 std::bind(
195 std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
196 this, std::placeholders::_1));
197
198 childWatchMap.emplace(i.first, std::move(watchObj));
199 }
200 }
201}
202
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500203void Manager::removeWatch(const std::filesystem::path& path)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500204{
205 // Delete Watch entry from map.
206 childWatchMap.erase(path);
207}
208
209void Manager::restore()
210{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500211 std::filesystem::path dir(dumpDir);
212 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500213 {
214 return;
215 }
216
217 // Dump file path: <DUMP_PATH>/<id>/<filename>
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500218 for (const auto& p : std::filesystem::directory_iterator(dir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500219 {
220 auto idStr = p.path().filename().string();
221
222 // Consider only directory's with dump id as name.
223 // Note: As per design one file per directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500224 if ((std::filesystem::is_directory(p.path())) &&
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500225 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
226 {
227 lastEntryId =
228 std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500229 auto fileIt = std::filesystem::directory_iterator(p.path());
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500230 // Create dump entry d-bus object.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500231 if (fileIt != std::filesystem::end(fileIt))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500232 {
233 createEntry(fileIt->path());
234 }
235 }
236 }
237}
238
239size_t Manager::getAllowedSize()
240{
241 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
242 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
243
244 auto size = 0;
245
246 // Get current size of the dump directory.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500247 for (const auto& p : std::filesystem::recursive_directory_iterator(dumpDir))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500248 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500249 if (!std::filesystem::is_directory(p))
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500250 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500251 size += std::filesystem::file_size(p);
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500252 }
253 }
254
255 // Convert size into KB
256 size = size / 1024;
257
258 // Set the Dump size to Maximum if the free space is greater than
259 // Dump max size otherwise return the available size.
260
261 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
262
263 if (size < BMC_DUMP_MIN_SPACE_REQD)
264 {
265 // Reached to maximum limit
266 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
267 }
268 if (size > BMC_DUMP_MAX_SIZE)
269 {
270 size = BMC_DUMP_MAX_SIZE;
271 }
272
273 return size;
274}
275
276} // namespace bmc
277} // namespace dump
278} // namespace phosphor