xref: /openbmc/phosphor-debug-collector/dump_manager_bmc.cpp (revision 3fc6df4867e3387adbea58359c17cc19fb6f5f90)
1fef66a95SDhruvaraj Subhashchandran #include "config.h"
2fef66a95SDhruvaraj Subhashchandran 
3fef66a95SDhruvaraj Subhashchandran #include "dump_manager_bmc.hpp"
4fef66a95SDhruvaraj Subhashchandran 
5fef66a95SDhruvaraj Subhashchandran #include "bmc_dump_entry.hpp"
6fef66a95SDhruvaraj Subhashchandran #include "dump_internal.hpp"
7fef66a95SDhruvaraj Subhashchandran #include "xyz/openbmc_project/Common/error.hpp"
8fef66a95SDhruvaraj Subhashchandran #include "xyz/openbmc_project/Dump/Create/error.hpp"
9fef66a95SDhruvaraj Subhashchandran 
10fef66a95SDhruvaraj Subhashchandran #include <sys/inotify.h>
11fef66a95SDhruvaraj Subhashchandran #include <unistd.h>
12fef66a95SDhruvaraj Subhashchandran 
13a6ab806dSDhruvaraj Subhashchandran #include <ctime>
14fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog-errors.hpp>
15fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog.hpp>
16fef66a95SDhruvaraj Subhashchandran #include <regex>
17fef66a95SDhruvaraj Subhashchandran 
18fef66a95SDhruvaraj Subhashchandran namespace phosphor
19fef66a95SDhruvaraj Subhashchandran {
20fef66a95SDhruvaraj Subhashchandran namespace dump
21fef66a95SDhruvaraj Subhashchandran {
22fef66a95SDhruvaraj Subhashchandran namespace bmc
23fef66a95SDhruvaraj Subhashchandran {
24fef66a95SDhruvaraj Subhashchandran 
25fef66a95SDhruvaraj Subhashchandran using namespace sdbusplus::xyz::openbmc_project::Common::Error;
26fef66a95SDhruvaraj Subhashchandran using namespace phosphor::logging;
27fef66a95SDhruvaraj Subhashchandran 
28fef66a95SDhruvaraj Subhashchandran namespace internal
29fef66a95SDhruvaraj Subhashchandran {
30fef66a95SDhruvaraj Subhashchandran 
31fef66a95SDhruvaraj Subhashchandran void Manager::create(Type type, std::vector<std::string> fullPaths)
32fef66a95SDhruvaraj Subhashchandran {
33fef66a95SDhruvaraj Subhashchandran     dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths);
34fef66a95SDhruvaraj Subhashchandran }
35fef66a95SDhruvaraj Subhashchandran 
36fef66a95SDhruvaraj Subhashchandran } // namespace internal
37fef66a95SDhruvaraj Subhashchandran 
38969f9a59SDhruvaraj Subhashchandran sdbusplus::message::object_path
39969f9a59SDhruvaraj Subhashchandran     Manager::createDump(std::map<std::string, std::string> params)
40fef66a95SDhruvaraj Subhashchandran {
41969f9a59SDhruvaraj Subhashchandran     if (!params.empty())
42969f9a59SDhruvaraj Subhashchandran     {
43969f9a59SDhruvaraj Subhashchandran         log<level::WARNING>("BMC dump accepts no additional parameters");
44969f9a59SDhruvaraj Subhashchandran     }
45fef66a95SDhruvaraj Subhashchandran     std::vector<std::string> paths;
466ccb50e1SDhruvaraj Subhashchandran     auto id = captureDump(Type::UserRequested, paths);
476ccb50e1SDhruvaraj Subhashchandran 
486ccb50e1SDhruvaraj Subhashchandran     // Entry Object path.
49*3fc6df48SJayanth Othayoth     auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
506ccb50e1SDhruvaraj Subhashchandran 
516ccb50e1SDhruvaraj Subhashchandran     try
526ccb50e1SDhruvaraj Subhashchandran     {
53a6ab806dSDhruvaraj Subhashchandran         std::time_t timeStamp = std::time(nullptr);
546ccb50e1SDhruvaraj Subhashchandran         entries.insert(std::make_pair(
55a6ab806dSDhruvaraj Subhashchandran             id, std::make_unique<bmc::Entry>(
56a6ab806dSDhruvaraj Subhashchandran                     bus, objPath.c_str(), id, timeStamp, 0, std::string(),
57a6ab806dSDhruvaraj Subhashchandran                     phosphor::dump::OperationStatus::InProgress, *this)));
586ccb50e1SDhruvaraj Subhashchandran     }
596ccb50e1SDhruvaraj Subhashchandran     catch (const std::invalid_argument& e)
606ccb50e1SDhruvaraj Subhashchandran     {
616ccb50e1SDhruvaraj Subhashchandran         log<level::ERR>(e.what());
626ccb50e1SDhruvaraj Subhashchandran         log<level::ERR>("Error in creating dump entry",
636ccb50e1SDhruvaraj Subhashchandran                         entry("OBJECTPATH=%s", objPath.c_str()),
646ccb50e1SDhruvaraj Subhashchandran                         entry("ID=%d", id));
656ccb50e1SDhruvaraj Subhashchandran         elog<InternalFailure>();
666ccb50e1SDhruvaraj Subhashchandran     }
676ccb50e1SDhruvaraj Subhashchandran 
686ccb50e1SDhruvaraj Subhashchandran     return objPath.string();
69fef66a95SDhruvaraj Subhashchandran }
70fef66a95SDhruvaraj Subhashchandran 
71fef66a95SDhruvaraj Subhashchandran uint32_t Manager::captureDump(Type type,
72fef66a95SDhruvaraj Subhashchandran                               const std::vector<std::string>& fullPaths)
73fef66a95SDhruvaraj Subhashchandran {
74fef66a95SDhruvaraj Subhashchandran     // Get Dump size.
75fef66a95SDhruvaraj Subhashchandran     auto size = getAllowedSize();
76fef66a95SDhruvaraj Subhashchandran 
77fef66a95SDhruvaraj Subhashchandran     pid_t pid = fork();
78fef66a95SDhruvaraj Subhashchandran 
79fef66a95SDhruvaraj Subhashchandran     if (pid == 0)
80fef66a95SDhruvaraj Subhashchandran     {
81*3fc6df48SJayanth Othayoth         std::filesystem::path dumpPath(dumpDir);
82fef66a95SDhruvaraj Subhashchandran         auto id = std::to_string(lastEntryId + 1);
83fef66a95SDhruvaraj Subhashchandran         dumpPath /= id;
84fef66a95SDhruvaraj Subhashchandran 
85fef66a95SDhruvaraj Subhashchandran         // get dreport type map entry
86fef66a95SDhruvaraj Subhashchandran         auto tempType = TypeMap.find(type);
87fef66a95SDhruvaraj Subhashchandran 
88fef66a95SDhruvaraj Subhashchandran         execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
89fef66a95SDhruvaraj Subhashchandran               id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
90fef66a95SDhruvaraj Subhashchandran               fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
91fef66a95SDhruvaraj Subhashchandran               tempType->second.c_str(), nullptr);
92fef66a95SDhruvaraj Subhashchandran 
93fef66a95SDhruvaraj Subhashchandran         // dreport script execution is failed.
94fef66a95SDhruvaraj Subhashchandran         auto error = errno;
95fef66a95SDhruvaraj Subhashchandran         log<level::ERR>("Error occurred during dreport function execution",
96fef66a95SDhruvaraj Subhashchandran                         entry("ERRNO=%d", error));
97fef66a95SDhruvaraj Subhashchandran         elog<InternalFailure>();
98fef66a95SDhruvaraj Subhashchandran     }
99fef66a95SDhruvaraj Subhashchandran     else if (pid > 0)
100fef66a95SDhruvaraj Subhashchandran     {
101fef66a95SDhruvaraj Subhashchandran         auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid,
102fef66a95SDhruvaraj Subhashchandran                                      WEXITED | WSTOPPED, callback, nullptr);
103fef66a95SDhruvaraj Subhashchandran         if (0 > rc)
104fef66a95SDhruvaraj Subhashchandran         {
105fef66a95SDhruvaraj Subhashchandran             // Failed to add to event loop
106fef66a95SDhruvaraj Subhashchandran             log<level::ERR>("Error occurred during the sd_event_add_child call",
107fef66a95SDhruvaraj Subhashchandran                             entry("RC=%d", rc));
108fef66a95SDhruvaraj Subhashchandran             elog<InternalFailure>();
109fef66a95SDhruvaraj Subhashchandran         }
110fef66a95SDhruvaraj Subhashchandran     }
111fef66a95SDhruvaraj Subhashchandran     else
112fef66a95SDhruvaraj Subhashchandran     {
113fef66a95SDhruvaraj Subhashchandran         auto error = errno;
114fef66a95SDhruvaraj Subhashchandran         log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
115fef66a95SDhruvaraj Subhashchandran         elog<InternalFailure>();
116fef66a95SDhruvaraj Subhashchandran     }
117fef66a95SDhruvaraj Subhashchandran 
118fef66a95SDhruvaraj Subhashchandran     return ++lastEntryId;
119fef66a95SDhruvaraj Subhashchandran }
120fef66a95SDhruvaraj Subhashchandran 
121*3fc6df48SJayanth Othayoth void Manager::createEntry(const std::filesystem::path& file)
122fef66a95SDhruvaraj Subhashchandran {
123fef66a95SDhruvaraj Subhashchandran     // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
124fef66a95SDhruvaraj Subhashchandran     static constexpr auto ID_POS = 1;
125fef66a95SDhruvaraj Subhashchandran     static constexpr auto EPOCHTIME_POS = 2;
126fef66a95SDhruvaraj Subhashchandran     std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
127fef66a95SDhruvaraj Subhashchandran 
128fef66a95SDhruvaraj Subhashchandran     std::smatch match;
129fef66a95SDhruvaraj Subhashchandran     std::string name = file.filename();
130fef66a95SDhruvaraj Subhashchandran 
131fef66a95SDhruvaraj Subhashchandran     if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
132fef66a95SDhruvaraj Subhashchandran     {
133fef66a95SDhruvaraj Subhashchandran         log<level::ERR>("Invalid Dump file name",
134fef66a95SDhruvaraj Subhashchandran                         entry("FILENAME=%s", file.filename().c_str()));
135fef66a95SDhruvaraj Subhashchandran         return;
136fef66a95SDhruvaraj Subhashchandran     }
137fef66a95SDhruvaraj Subhashchandran 
138fef66a95SDhruvaraj Subhashchandran     auto idString = match[ID_POS];
139fef66a95SDhruvaraj Subhashchandran     auto msString = match[EPOCHTIME_POS];
140fef66a95SDhruvaraj Subhashchandran 
141fef66a95SDhruvaraj Subhashchandran     auto id = stoul(idString);
1426ccb50e1SDhruvaraj Subhashchandran 
1436ccb50e1SDhruvaraj Subhashchandran     // If there is an existing entry update it and return.
1446ccb50e1SDhruvaraj Subhashchandran     auto dumpEntry = entries.find(id);
1456ccb50e1SDhruvaraj Subhashchandran     if (dumpEntry != entries.end())
1466ccb50e1SDhruvaraj Subhashchandran     {
1476ccb50e1SDhruvaraj Subhashchandran         dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
148*3fc6df48SJayanth Othayoth             ->update(stoull(msString), std::filesystem::file_size(file), file);
1496ccb50e1SDhruvaraj Subhashchandran         return;
1506ccb50e1SDhruvaraj Subhashchandran     }
1516ccb50e1SDhruvaraj Subhashchandran 
152fef66a95SDhruvaraj Subhashchandran     // Entry Object path.
153*3fc6df48SJayanth Othayoth     auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
154fef66a95SDhruvaraj Subhashchandran 
1556ccb50e1SDhruvaraj Subhashchandran     try
1566ccb50e1SDhruvaraj Subhashchandran     {
157a6ab806dSDhruvaraj Subhashchandran         entries.insert(std::make_pair(
158*3fc6df48SJayanth Othayoth             id, std::make_unique<bmc::Entry>(
159*3fc6df48SJayanth Othayoth                     bus, objPath.c_str(), id, stoull(msString),
160*3fc6df48SJayanth Othayoth                     std::filesystem::file_size(file), file,
161*3fc6df48SJayanth Othayoth                     phosphor::dump::OperationStatus::Completed, *this)));
162fef66a95SDhruvaraj Subhashchandran     }
163fef66a95SDhruvaraj Subhashchandran     catch (const std::invalid_argument& e)
164fef66a95SDhruvaraj Subhashchandran     {
165fef66a95SDhruvaraj Subhashchandran         log<level::ERR>(e.what());
1666ccb50e1SDhruvaraj Subhashchandran         log<level::ERR>("Error in creating dump entry",
1676ccb50e1SDhruvaraj Subhashchandran                         entry("OBJECTPATH=%s", objPath.c_str()),
1686ccb50e1SDhruvaraj Subhashchandran                         entry("ID=%d", id),
1696ccb50e1SDhruvaraj Subhashchandran                         entry("TIMESTAMP=%ull", stoull(msString)),
170*3fc6df48SJayanth Othayoth                         entry("SIZE=%d", std::filesystem::file_size(file)),
1716ccb50e1SDhruvaraj Subhashchandran                         entry("FILENAME=%s", file.c_str()));
172fef66a95SDhruvaraj Subhashchandran         return;
173fef66a95SDhruvaraj Subhashchandran     }
174fef66a95SDhruvaraj Subhashchandran }
175fef66a95SDhruvaraj Subhashchandran 
176fef66a95SDhruvaraj Subhashchandran void Manager::watchCallback(const UserMap& fileInfo)
177fef66a95SDhruvaraj Subhashchandran {
178fef66a95SDhruvaraj Subhashchandran     for (const auto& i : fileInfo)
179fef66a95SDhruvaraj Subhashchandran     {
180fef66a95SDhruvaraj Subhashchandran         // For any new dump file create dump entry object
181fef66a95SDhruvaraj Subhashchandran         // and associated inotify watch.
182fef66a95SDhruvaraj Subhashchandran         if (IN_CLOSE_WRITE == i.second)
183fef66a95SDhruvaraj Subhashchandran         {
184fef66a95SDhruvaraj Subhashchandran             removeWatch(i.first);
185fef66a95SDhruvaraj Subhashchandran 
186fef66a95SDhruvaraj Subhashchandran             createEntry(i.first);
187fef66a95SDhruvaraj Subhashchandran         }
188fef66a95SDhruvaraj Subhashchandran         // Start inotify watch on newly created directory.
189*3fc6df48SJayanth Othayoth         else if ((IN_CREATE == i.second) &&
190*3fc6df48SJayanth Othayoth                  std::filesystem::is_directory(i.first))
191fef66a95SDhruvaraj Subhashchandran         {
192fef66a95SDhruvaraj Subhashchandran             auto watchObj = std::make_unique<Watch>(
193fef66a95SDhruvaraj Subhashchandran                 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
194fef66a95SDhruvaraj Subhashchandran                 std::bind(
195fef66a95SDhruvaraj Subhashchandran                     std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
196fef66a95SDhruvaraj Subhashchandran                     this, std::placeholders::_1));
197fef66a95SDhruvaraj Subhashchandran 
198fef66a95SDhruvaraj Subhashchandran             childWatchMap.emplace(i.first, std::move(watchObj));
199fef66a95SDhruvaraj Subhashchandran         }
200fef66a95SDhruvaraj Subhashchandran     }
201fef66a95SDhruvaraj Subhashchandran }
202fef66a95SDhruvaraj Subhashchandran 
203*3fc6df48SJayanth Othayoth void Manager::removeWatch(const std::filesystem::path& path)
204fef66a95SDhruvaraj Subhashchandran {
205fef66a95SDhruvaraj Subhashchandran     // Delete Watch entry from map.
206fef66a95SDhruvaraj Subhashchandran     childWatchMap.erase(path);
207fef66a95SDhruvaraj Subhashchandran }
208fef66a95SDhruvaraj Subhashchandran 
209fef66a95SDhruvaraj Subhashchandran void Manager::restore()
210fef66a95SDhruvaraj Subhashchandran {
211*3fc6df48SJayanth Othayoth     std::filesystem::path dir(dumpDir);
212*3fc6df48SJayanth Othayoth     if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
213fef66a95SDhruvaraj Subhashchandran     {
214fef66a95SDhruvaraj Subhashchandran         return;
215fef66a95SDhruvaraj Subhashchandran     }
216fef66a95SDhruvaraj Subhashchandran 
217fef66a95SDhruvaraj Subhashchandran     // Dump file path: <DUMP_PATH>/<id>/<filename>
218*3fc6df48SJayanth Othayoth     for (const auto& p : std::filesystem::directory_iterator(dir))
219fef66a95SDhruvaraj Subhashchandran     {
220fef66a95SDhruvaraj Subhashchandran         auto idStr = p.path().filename().string();
221fef66a95SDhruvaraj Subhashchandran 
222fef66a95SDhruvaraj Subhashchandran         // Consider only directory's with dump id as name.
223fef66a95SDhruvaraj Subhashchandran         // Note: As per design one file per directory.
224*3fc6df48SJayanth Othayoth         if ((std::filesystem::is_directory(p.path())) &&
225fef66a95SDhruvaraj Subhashchandran             std::all_of(idStr.begin(), idStr.end(), ::isdigit))
226fef66a95SDhruvaraj Subhashchandran         {
227fef66a95SDhruvaraj Subhashchandran             lastEntryId =
228fef66a95SDhruvaraj Subhashchandran                 std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
229*3fc6df48SJayanth Othayoth             auto fileIt = std::filesystem::directory_iterator(p.path());
230fef66a95SDhruvaraj Subhashchandran             // Create dump entry d-bus object.
231*3fc6df48SJayanth Othayoth             if (fileIt != std::filesystem::end(fileIt))
232fef66a95SDhruvaraj Subhashchandran             {
233fef66a95SDhruvaraj Subhashchandran                 createEntry(fileIt->path());
234fef66a95SDhruvaraj Subhashchandran             }
235fef66a95SDhruvaraj Subhashchandran         }
236fef66a95SDhruvaraj Subhashchandran     }
237fef66a95SDhruvaraj Subhashchandran }
238fef66a95SDhruvaraj Subhashchandran 
239fef66a95SDhruvaraj Subhashchandran size_t Manager::getAllowedSize()
240fef66a95SDhruvaraj Subhashchandran {
241fef66a95SDhruvaraj Subhashchandran     using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
242fef66a95SDhruvaraj Subhashchandran     using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
243fef66a95SDhruvaraj Subhashchandran 
244fef66a95SDhruvaraj Subhashchandran     auto size = 0;
245fef66a95SDhruvaraj Subhashchandran 
246fef66a95SDhruvaraj Subhashchandran     // Get current size of the dump directory.
247*3fc6df48SJayanth Othayoth     for (const auto& p : std::filesystem::recursive_directory_iterator(dumpDir))
248fef66a95SDhruvaraj Subhashchandran     {
249*3fc6df48SJayanth Othayoth         if (!std::filesystem::is_directory(p))
250fef66a95SDhruvaraj Subhashchandran         {
251*3fc6df48SJayanth Othayoth             size += std::filesystem::file_size(p);
252fef66a95SDhruvaraj Subhashchandran         }
253fef66a95SDhruvaraj Subhashchandran     }
254fef66a95SDhruvaraj Subhashchandran 
255fef66a95SDhruvaraj Subhashchandran     // Convert size into KB
256fef66a95SDhruvaraj Subhashchandran     size = size / 1024;
257fef66a95SDhruvaraj Subhashchandran 
258fef66a95SDhruvaraj Subhashchandran     // Set the Dump size to Maximum  if the free space is greater than
259fef66a95SDhruvaraj Subhashchandran     // Dump max size otherwise return the available size.
260fef66a95SDhruvaraj Subhashchandran 
261fef66a95SDhruvaraj Subhashchandran     size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
262fef66a95SDhruvaraj Subhashchandran 
263fef66a95SDhruvaraj Subhashchandran     if (size < BMC_DUMP_MIN_SPACE_REQD)
264fef66a95SDhruvaraj Subhashchandran     {
265fef66a95SDhruvaraj Subhashchandran         // Reached to maximum limit
266fef66a95SDhruvaraj Subhashchandran         elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
267fef66a95SDhruvaraj Subhashchandran     }
268fef66a95SDhruvaraj Subhashchandran     if (size > BMC_DUMP_MAX_SIZE)
269fef66a95SDhruvaraj Subhashchandran     {
270fef66a95SDhruvaraj Subhashchandran         size = BMC_DUMP_MAX_SIZE;
271fef66a95SDhruvaraj Subhashchandran     }
272fef66a95SDhruvaraj Subhashchandran 
273fef66a95SDhruvaraj Subhashchandran     return size;
274fef66a95SDhruvaraj Subhashchandran }
275fef66a95SDhruvaraj Subhashchandran 
276fef66a95SDhruvaraj Subhashchandran } // namespace bmc
277fef66a95SDhruvaraj Subhashchandran } // namespace dump
278fef66a95SDhruvaraj Subhashchandran } // namespace phosphor
279