xref: /openbmc/phosphor-debug-collector/dump_manager_bmc.cpp (revision 56bd797e0a67a371e1d117023ca06eb484f67c12)
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 
10858fbb2eSGeorge Liu #include <fmt/core.h>
11fef66a95SDhruvaraj Subhashchandran #include <sys/inotify.h>
12fef66a95SDhruvaraj Subhashchandran #include <unistd.h>
13fef66a95SDhruvaraj Subhashchandran 
14fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog-errors.hpp>
15fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog.hpp>
160af74a5eSJayanth Othayoth 
17bb9366d0STim Lee #include <cmath>
180af74a5eSJayanth Othayoth #include <ctime>
19fef66a95SDhruvaraj Subhashchandran #include <regex>
20fef66a95SDhruvaraj Subhashchandran 
21fef66a95SDhruvaraj Subhashchandran namespace phosphor
22fef66a95SDhruvaraj Subhashchandran {
23fef66a95SDhruvaraj Subhashchandran namespace dump
24fef66a95SDhruvaraj Subhashchandran {
25fef66a95SDhruvaraj Subhashchandran namespace bmc
26fef66a95SDhruvaraj Subhashchandran {
27fef66a95SDhruvaraj Subhashchandran 
28fef66a95SDhruvaraj Subhashchandran using namespace sdbusplus::xyz::openbmc_project::Common::Error;
29fef66a95SDhruvaraj Subhashchandran using namespace phosphor::logging;
30fef66a95SDhruvaraj Subhashchandran 
31fef66a95SDhruvaraj Subhashchandran namespace internal
32fef66a95SDhruvaraj Subhashchandran {
33fef66a95SDhruvaraj Subhashchandran 
34fef66a95SDhruvaraj Subhashchandran void Manager::create(Type type, std::vector<std::string> fullPaths)
35fef66a95SDhruvaraj Subhashchandran {
36fef66a95SDhruvaraj Subhashchandran     dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths);
37fef66a95SDhruvaraj Subhashchandran }
38fef66a95SDhruvaraj Subhashchandran 
39fef66a95SDhruvaraj Subhashchandran } // namespace internal
40fef66a95SDhruvaraj Subhashchandran 
41969f9a59SDhruvaraj Subhashchandran sdbusplus::message::object_path
42ddc3366eSDhruvaraj Subhashchandran     Manager::createDump(phosphor::dump::DumpCreateParams params)
43fef66a95SDhruvaraj Subhashchandran {
44969f9a59SDhruvaraj Subhashchandran     if (!params.empty())
45969f9a59SDhruvaraj Subhashchandran     {
46969f9a59SDhruvaraj Subhashchandran         log<level::WARNING>("BMC dump accepts no additional parameters");
47969f9a59SDhruvaraj Subhashchandran     }
48fef66a95SDhruvaraj Subhashchandran     std::vector<std::string> paths;
496ccb50e1SDhruvaraj Subhashchandran     auto id = captureDump(Type::UserRequested, paths);
506ccb50e1SDhruvaraj Subhashchandran 
516ccb50e1SDhruvaraj Subhashchandran     // Entry Object path.
523fc6df48SJayanth Othayoth     auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
536ccb50e1SDhruvaraj Subhashchandran 
546ccb50e1SDhruvaraj Subhashchandran     try
556ccb50e1SDhruvaraj Subhashchandran     {
56a6ab806dSDhruvaraj Subhashchandran         std::time_t timeStamp = std::time(nullptr);
576ccb50e1SDhruvaraj Subhashchandran         entries.insert(std::make_pair(
58a6ab806dSDhruvaraj Subhashchandran             id, std::make_unique<bmc::Entry>(
59a6ab806dSDhruvaraj Subhashchandran                     bus, objPath.c_str(), id, timeStamp, 0, std::string(),
60a6ab806dSDhruvaraj Subhashchandran                     phosphor::dump::OperationStatus::InProgress, *this)));
616ccb50e1SDhruvaraj Subhashchandran     }
626ccb50e1SDhruvaraj Subhashchandran     catch (const std::invalid_argument& e)
636ccb50e1SDhruvaraj Subhashchandran     {
64858fbb2eSGeorge Liu         log<level::ERR>(fmt::format("Error in creating dump entry, "
65858fbb2eSGeorge Liu                                     "errormsg({}), OBJECTPATH({}), ID({})",
66858fbb2eSGeorge Liu                                     e.what(), objPath.c_str(), id)
67858fbb2eSGeorge Liu                             .c_str());
686ccb50e1SDhruvaraj Subhashchandran         elog<InternalFailure>();
696ccb50e1SDhruvaraj Subhashchandran     }
706ccb50e1SDhruvaraj Subhashchandran 
716ccb50e1SDhruvaraj Subhashchandran     return objPath.string();
72fef66a95SDhruvaraj Subhashchandran }
73fef66a95SDhruvaraj Subhashchandran 
74fef66a95SDhruvaraj Subhashchandran uint32_t Manager::captureDump(Type type,
75fef66a95SDhruvaraj Subhashchandran                               const std::vector<std::string>& fullPaths)
76fef66a95SDhruvaraj Subhashchandran {
77fef66a95SDhruvaraj Subhashchandran     // Get Dump size.
78fef66a95SDhruvaraj Subhashchandran     auto size = getAllowedSize();
79fef66a95SDhruvaraj Subhashchandran 
80fef66a95SDhruvaraj Subhashchandran     pid_t pid = fork();
81fef66a95SDhruvaraj Subhashchandran 
82fef66a95SDhruvaraj Subhashchandran     if (pid == 0)
83fef66a95SDhruvaraj Subhashchandran     {
843fc6df48SJayanth Othayoth         std::filesystem::path dumpPath(dumpDir);
85fef66a95SDhruvaraj Subhashchandran         auto id = std::to_string(lastEntryId + 1);
86fef66a95SDhruvaraj Subhashchandran         dumpPath /= id;
87fef66a95SDhruvaraj Subhashchandran 
88fef66a95SDhruvaraj Subhashchandran         // get dreport type map entry
89fef66a95SDhruvaraj Subhashchandran         auto tempType = TypeMap.find(type);
90fef66a95SDhruvaraj Subhashchandran 
91fef66a95SDhruvaraj Subhashchandran         execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
92fef66a95SDhruvaraj Subhashchandran               id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
93fef66a95SDhruvaraj Subhashchandran               fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
94fef66a95SDhruvaraj Subhashchandran               tempType->second.c_str(), nullptr);
95fef66a95SDhruvaraj Subhashchandran 
96fef66a95SDhruvaraj Subhashchandran         // dreport script execution is failed.
97fef66a95SDhruvaraj Subhashchandran         auto error = errno;
98858fbb2eSGeorge Liu         log<level::ERR>(
99858fbb2eSGeorge Liu             fmt::format(
100858fbb2eSGeorge Liu                 "Error occurred during dreport function execution, errno({})",
101858fbb2eSGeorge Liu                 error)
102858fbb2eSGeorge Liu                 .c_str());
103fef66a95SDhruvaraj Subhashchandran         elog<InternalFailure>();
104fef66a95SDhruvaraj Subhashchandran     }
105fef66a95SDhruvaraj Subhashchandran     else if (pid > 0)
106fef66a95SDhruvaraj Subhashchandran     {
107fef66a95SDhruvaraj Subhashchandran         auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid,
108fef66a95SDhruvaraj Subhashchandran                                      WEXITED | WSTOPPED, callback, nullptr);
109fef66a95SDhruvaraj Subhashchandran         if (0 > rc)
110fef66a95SDhruvaraj Subhashchandran         {
111fef66a95SDhruvaraj Subhashchandran             // Failed to add to event loop
112858fbb2eSGeorge Liu             log<level::ERR>(
113858fbb2eSGeorge Liu                 fmt::format(
114858fbb2eSGeorge Liu                     "Error occurred during the sd_event_add_child call, rc({})",
115858fbb2eSGeorge Liu                     rc)
116858fbb2eSGeorge Liu                     .c_str());
117fef66a95SDhruvaraj Subhashchandran             elog<InternalFailure>();
118fef66a95SDhruvaraj Subhashchandran         }
119fef66a95SDhruvaraj Subhashchandran     }
120fef66a95SDhruvaraj Subhashchandran     else
121fef66a95SDhruvaraj Subhashchandran     {
122fef66a95SDhruvaraj Subhashchandran         auto error = errno;
123858fbb2eSGeorge Liu         log<level::ERR>(
124858fbb2eSGeorge Liu             fmt::format("Error occurred during fork, errno({})", error)
125858fbb2eSGeorge Liu                 .c_str());
126fef66a95SDhruvaraj Subhashchandran         elog<InternalFailure>();
127fef66a95SDhruvaraj Subhashchandran     }
128fef66a95SDhruvaraj Subhashchandran 
129fef66a95SDhruvaraj Subhashchandran     return ++lastEntryId;
130fef66a95SDhruvaraj Subhashchandran }
131fef66a95SDhruvaraj Subhashchandran 
1323fc6df48SJayanth Othayoth void Manager::createEntry(const std::filesystem::path& file)
133fef66a95SDhruvaraj Subhashchandran {
134fef66a95SDhruvaraj Subhashchandran     // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
135fef66a95SDhruvaraj Subhashchandran     static constexpr auto ID_POS = 1;
136fef66a95SDhruvaraj Subhashchandran     static constexpr auto EPOCHTIME_POS = 2;
137fef66a95SDhruvaraj Subhashchandran     std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
138fef66a95SDhruvaraj Subhashchandran 
139fef66a95SDhruvaraj Subhashchandran     std::smatch match;
140fef66a95SDhruvaraj Subhashchandran     std::string name = file.filename();
141fef66a95SDhruvaraj Subhashchandran 
142fef66a95SDhruvaraj Subhashchandran     if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
143fef66a95SDhruvaraj Subhashchandran     {
144858fbb2eSGeorge Liu         log<level::ERR>(fmt::format("Invalid Dump file name, FILENAME({})",
145858fbb2eSGeorge Liu                                     file.filename().c_str())
146858fbb2eSGeorge Liu                             .c_str());
147fef66a95SDhruvaraj Subhashchandran         return;
148fef66a95SDhruvaraj Subhashchandran     }
149fef66a95SDhruvaraj Subhashchandran 
150fef66a95SDhruvaraj Subhashchandran     auto idString = match[ID_POS];
151*56bd797eSXie Ning     uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
152fef66a95SDhruvaraj Subhashchandran 
153fef66a95SDhruvaraj Subhashchandran     auto id = stoul(idString);
1546ccb50e1SDhruvaraj Subhashchandran 
1556ccb50e1SDhruvaraj Subhashchandran     // If there is an existing entry update it and return.
1566ccb50e1SDhruvaraj Subhashchandran     auto dumpEntry = entries.find(id);
1576ccb50e1SDhruvaraj Subhashchandran     if (dumpEntry != entries.end())
1586ccb50e1SDhruvaraj Subhashchandran     {
1596ccb50e1SDhruvaraj Subhashchandran         dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
160*56bd797eSXie Ning             ->update(timestamp, std::filesystem::file_size(file), file);
1616ccb50e1SDhruvaraj Subhashchandran         return;
1626ccb50e1SDhruvaraj Subhashchandran     }
1636ccb50e1SDhruvaraj Subhashchandran 
164fef66a95SDhruvaraj Subhashchandran     // Entry Object path.
1653fc6df48SJayanth Othayoth     auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
166fef66a95SDhruvaraj Subhashchandran 
1676ccb50e1SDhruvaraj Subhashchandran     try
1686ccb50e1SDhruvaraj Subhashchandran     {
169a6ab806dSDhruvaraj Subhashchandran         entries.insert(std::make_pair(
1703fc6df48SJayanth Othayoth             id, std::make_unique<bmc::Entry>(
171*56bd797eSXie Ning                     bus, objPath.c_str(), id, timestamp,
1723fc6df48SJayanth Othayoth                     std::filesystem::file_size(file), file,
1733fc6df48SJayanth Othayoth                     phosphor::dump::OperationStatus::Completed, *this)));
174fef66a95SDhruvaraj Subhashchandran     }
175fef66a95SDhruvaraj Subhashchandran     catch (const std::invalid_argument& e)
176fef66a95SDhruvaraj Subhashchandran     {
177858fbb2eSGeorge Liu         log<level::ERR>(
178858fbb2eSGeorge Liu             fmt::format(
179858fbb2eSGeorge Liu                 "Error in creating dump entry, errormsg({}), OBJECTPATH({}), "
180858fbb2eSGeorge Liu                 "ID({}), TIMESTAMP({}), SIZE({}), FILENAME({})",
181*56bd797eSXie Ning                 e.what(), objPath.c_str(), id, timestamp,
182858fbb2eSGeorge Liu                 std::filesystem::file_size(file), file.filename().c_str())
183858fbb2eSGeorge Liu                 .c_str());
184fef66a95SDhruvaraj Subhashchandran         return;
185fef66a95SDhruvaraj Subhashchandran     }
186fef66a95SDhruvaraj Subhashchandran }
187fef66a95SDhruvaraj Subhashchandran 
188fef66a95SDhruvaraj Subhashchandran void Manager::watchCallback(const UserMap& fileInfo)
189fef66a95SDhruvaraj Subhashchandran {
190fef66a95SDhruvaraj Subhashchandran     for (const auto& i : fileInfo)
191fef66a95SDhruvaraj Subhashchandran     {
192fef66a95SDhruvaraj Subhashchandran         // For any new dump file create dump entry object
193fef66a95SDhruvaraj Subhashchandran         // and associated inotify watch.
194fef66a95SDhruvaraj Subhashchandran         if (IN_CLOSE_WRITE == i.second)
195fef66a95SDhruvaraj Subhashchandran         {
196fef66a95SDhruvaraj Subhashchandran             removeWatch(i.first);
197fef66a95SDhruvaraj Subhashchandran 
198fef66a95SDhruvaraj Subhashchandran             createEntry(i.first);
199fef66a95SDhruvaraj Subhashchandran         }
200fef66a95SDhruvaraj Subhashchandran         // Start inotify watch on newly created directory.
2013fc6df48SJayanth Othayoth         else if ((IN_CREATE == i.second) &&
2023fc6df48SJayanth Othayoth                  std::filesystem::is_directory(i.first))
203fef66a95SDhruvaraj Subhashchandran         {
204fef66a95SDhruvaraj Subhashchandran             auto watchObj = std::make_unique<Watch>(
205fef66a95SDhruvaraj Subhashchandran                 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
206fef66a95SDhruvaraj Subhashchandran                 std::bind(
207fef66a95SDhruvaraj Subhashchandran                     std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
208fef66a95SDhruvaraj Subhashchandran                     this, std::placeholders::_1));
209fef66a95SDhruvaraj Subhashchandran 
210fef66a95SDhruvaraj Subhashchandran             childWatchMap.emplace(i.first, std::move(watchObj));
211fef66a95SDhruvaraj Subhashchandran         }
212fef66a95SDhruvaraj Subhashchandran     }
213fef66a95SDhruvaraj Subhashchandran }
214fef66a95SDhruvaraj Subhashchandran 
2153fc6df48SJayanth Othayoth void Manager::removeWatch(const std::filesystem::path& path)
216fef66a95SDhruvaraj Subhashchandran {
217fef66a95SDhruvaraj Subhashchandran     // Delete Watch entry from map.
218fef66a95SDhruvaraj Subhashchandran     childWatchMap.erase(path);
219fef66a95SDhruvaraj Subhashchandran }
220fef66a95SDhruvaraj Subhashchandran 
221fef66a95SDhruvaraj Subhashchandran void Manager::restore()
222fef66a95SDhruvaraj Subhashchandran {
2233fc6df48SJayanth Othayoth     std::filesystem::path dir(dumpDir);
2243fc6df48SJayanth Othayoth     if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
225fef66a95SDhruvaraj Subhashchandran     {
226fef66a95SDhruvaraj Subhashchandran         return;
227fef66a95SDhruvaraj Subhashchandran     }
228fef66a95SDhruvaraj Subhashchandran 
229fef66a95SDhruvaraj Subhashchandran     // Dump file path: <DUMP_PATH>/<id>/<filename>
2303fc6df48SJayanth Othayoth     for (const auto& p : std::filesystem::directory_iterator(dir))
231fef66a95SDhruvaraj Subhashchandran     {
232fef66a95SDhruvaraj Subhashchandran         auto idStr = p.path().filename().string();
233fef66a95SDhruvaraj Subhashchandran 
234fef66a95SDhruvaraj Subhashchandran         // Consider only directory's with dump id as name.
235fef66a95SDhruvaraj Subhashchandran         // Note: As per design one file per directory.
2363fc6df48SJayanth Othayoth         if ((std::filesystem::is_directory(p.path())) &&
237fef66a95SDhruvaraj Subhashchandran             std::all_of(idStr.begin(), idStr.end(), ::isdigit))
238fef66a95SDhruvaraj Subhashchandran         {
239fef66a95SDhruvaraj Subhashchandran             lastEntryId =
240fef66a95SDhruvaraj Subhashchandran                 std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
2413fc6df48SJayanth Othayoth             auto fileIt = std::filesystem::directory_iterator(p.path());
242fef66a95SDhruvaraj Subhashchandran             // Create dump entry d-bus object.
2433fc6df48SJayanth Othayoth             if (fileIt != std::filesystem::end(fileIt))
244fef66a95SDhruvaraj Subhashchandran             {
245fef66a95SDhruvaraj Subhashchandran                 createEntry(fileIt->path());
246fef66a95SDhruvaraj Subhashchandran             }
247fef66a95SDhruvaraj Subhashchandran         }
248fef66a95SDhruvaraj Subhashchandran     }
249fef66a95SDhruvaraj Subhashchandran }
250fef66a95SDhruvaraj Subhashchandran 
251fef66a95SDhruvaraj Subhashchandran size_t Manager::getAllowedSize()
252fef66a95SDhruvaraj Subhashchandran {
253fef66a95SDhruvaraj Subhashchandran     using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
254fef66a95SDhruvaraj Subhashchandran     using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
255fef66a95SDhruvaraj Subhashchandran 
256fef66a95SDhruvaraj Subhashchandran     auto size = 0;
257fef66a95SDhruvaraj Subhashchandran 
258fef66a95SDhruvaraj Subhashchandran     // Get current size of the dump directory.
2593fc6df48SJayanth Othayoth     for (const auto& p : std::filesystem::recursive_directory_iterator(dumpDir))
260fef66a95SDhruvaraj Subhashchandran     {
2613fc6df48SJayanth Othayoth         if (!std::filesystem::is_directory(p))
262fef66a95SDhruvaraj Subhashchandran         {
263bb9366d0STim Lee             size += std::ceil(std::filesystem::file_size(p) / 1024.0);
264fef66a95SDhruvaraj Subhashchandran         }
265fef66a95SDhruvaraj Subhashchandran     }
266fef66a95SDhruvaraj Subhashchandran 
267fef66a95SDhruvaraj Subhashchandran     // Set the Dump size to Maximum  if the free space is greater than
268fef66a95SDhruvaraj Subhashchandran     // Dump max size otherwise return the available size.
269fef66a95SDhruvaraj Subhashchandran 
270fef66a95SDhruvaraj Subhashchandran     size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
271fef66a95SDhruvaraj Subhashchandran 
272fef66a95SDhruvaraj Subhashchandran     if (size < BMC_DUMP_MIN_SPACE_REQD)
273fef66a95SDhruvaraj Subhashchandran     {
274fef66a95SDhruvaraj Subhashchandran         // Reached to maximum limit
275fef66a95SDhruvaraj Subhashchandran         elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
276fef66a95SDhruvaraj Subhashchandran     }
277fef66a95SDhruvaraj Subhashchandran     if (size > BMC_DUMP_MAX_SIZE)
278fef66a95SDhruvaraj Subhashchandran     {
279fef66a95SDhruvaraj Subhashchandran         size = BMC_DUMP_MAX_SIZE;
280fef66a95SDhruvaraj Subhashchandran     }
281fef66a95SDhruvaraj Subhashchandran 
282fef66a95SDhruvaraj Subhashchandran     return size;
283fef66a95SDhruvaraj Subhashchandran }
284fef66a95SDhruvaraj Subhashchandran 
285fef66a95SDhruvaraj Subhashchandran } // namespace bmc
286fef66a95SDhruvaraj Subhashchandran } // namespace dump
287fef66a95SDhruvaraj Subhashchandran } // namespace phosphor
288