blob: 21fca8ab1dd9bc063b4f7d8e159147ea3893ef79 [file] [log] [blame]
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05001#include "config.h"
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05002
3#include "dump_manager.hpp"
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05004
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05005#include "dump_internal.hpp"
6#include "xyz/openbmc_project/Common/error.hpp"
Jayanth Othayothab7f9202017-08-02 06:24:58 -05007#include "xyz/openbmc_project/Dump/Create/error.hpp"
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05008
9#include <sys/inotify.h>
10#include <unistd.h>
11
12#include <phosphor-logging/elog-errors.hpp>
13#include <phosphor-logging/elog.hpp>
14#include <regex>
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050015
16namespace phosphor
17{
18namespace dump
19{
20
21using namespace sdbusplus::xyz::openbmc_project::Common::Error;
22using namespace phosphor::logging;
23
24namespace internal
25{
26
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050027void Manager::create(Type type, std::vector<std::string> fullPaths)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050028{
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050029 dumpMgr.phosphor::dump::Manager::captureDump(type, fullPaths);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050030}
31
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050032} // namespace internal
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050033
34uint32_t Manager::createDump()
35{
36 std::vector<std::string> paths;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050037 return captureDump(Type::UserRequested, paths);
38}
39
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050040uint32_t Manager::captureDump(Type type,
41 const std::vector<std::string>& fullPaths)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050042{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050043 // Get Dump size.
Jayanth Othayoth9c155582017-08-09 07:47:45 -050044 auto size = getAllowedSize();
45
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050046 pid_t pid = fork();
47
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050048 if (pid == 0)
49 {
Jayanth Othayothf9009a22017-07-12 19:40:34 -050050 fs::path dumpPath(BMC_DUMP_PATH);
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050051 auto id = std::to_string(lastEntryId + 1);
Jayanth Othayoth9c155582017-08-09 07:47:45 -050052 dumpPath /= id;
Jayanth Othayothf9009a22017-07-12 19:40:34 -050053
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050054 // get dreport type map entry
Marri Devender Rao0deb2872018-11-12 07:45:54 -060055 auto tempType = TypeMap.find(type);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050056
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050057 execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
58 id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
59 fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
60 tempType->second.c_str(), nullptr);
Jayanth Othayoth9c155582017-08-09 07:47:45 -050061
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050062 // dreport script execution is failed.
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050063 auto error = errno;
Jayanth Othayoth9c155582017-08-09 07:47:45 -050064 log<level::ERR>("Error occurred during dreport function execution",
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050065 entry("ERRNO=%d", error));
66 elog<InternalFailure>();
67 }
68 else if (pid > 0)
69 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050070 auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid,
71 WEXITED | WSTOPPED, callback, nullptr);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050072 if (0 > rc)
73 {
74 // Failed to add to event loop
75 log<level::ERR>("Error occurred during the sd_event_add_child call",
Gunnar Mills11eaab72017-10-19 16:07:31 -050076 entry("RC=%d", rc));
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050077 elog<InternalFailure>();
78 }
79 }
80 else
81 {
82 auto error = errno;
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050083 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050084 elog<InternalFailure>();
85 }
86
87 return ++lastEntryId;
88}
89
90void Manager::createEntry(const fs::path& file)
91{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050092 // Dump File Name format obmcdump_ID_EPOCHTIME.EXT
93 static constexpr auto ID_POS = 1;
94 static constexpr auto EPOCHTIME_POS = 2;
Jayanth Othayoth481bb3d2017-07-13 03:33:34 -050095 std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050096
Jayanth Othayoth481bb3d2017-07-13 03:33:34 -050097 std::smatch match;
98 std::string name = file.filename();
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050099
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500100 if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
Jayanth Othayoth481bb3d2017-07-13 03:33:34 -0500101 {
102 log<level::ERR>("Invalid Dump file name",
Joseph Reynoldsaf487622018-05-10 15:54:16 -0500103 entry("FILENAME=%s", file.filename().c_str()));
Jayanth Othayoth481bb3d2017-07-13 03:33:34 -0500104 return;
105 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500106
Jayanth Othayoth481bb3d2017-07-13 03:33:34 -0500107 auto idString = match[ID_POS];
108 auto msString = match[EPOCHTIME_POS];
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500109
Jayanth Othayoth481bb3d2017-07-13 03:33:34 -0500110 try
111 {
112 auto id = stoul(idString);
113 // Entry Object path.
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500114 auto objPath = fs::path(OBJ_ENTRY) / std::to_string(id);
Jayanth Othayoth481bb3d2017-07-13 03:33:34 -0500115
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500116 entries.insert(std::make_pair(
117 id,
118 std::make_unique<Entry>(bus, objPath.c_str(), id, stoull(msString),
119 fs::file_size(file), file, *this)));
Jayanth Othayoth481bb3d2017-07-13 03:33:34 -0500120 }
121 catch (const std::invalid_argument& e)
122 {
123 log<level::ERR>(e.what());
124 return;
125 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500126}
127
128void Manager::erase(uint32_t entryId)
129{
130 entries.erase(entryId);
131}
132
Nagaraju Goruganti3c899a42017-09-12 06:14:46 -0500133void Manager::deleteAll()
134{
135 auto iter = entries.begin();
136 while (iter != entries.end())
137 {
138 auto& entry = iter->second;
139 entry->delete_();
140 ++iter;
141 }
142}
143
Jayanth Othayothbcb174b2017-07-02 06:29:24 -0500144void Manager::watchCallback(const UserMap& fileInfo)
145{
146 for (const auto& i : fileInfo)
147 {
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500148 // For any new dump file create dump entry object
149 // and associated inotify watch.
Jayanth Othayothbcb174b2017-07-02 06:29:24 -0500150 if (IN_CLOSE_WRITE == i.second)
151 {
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500152 removeWatch(i.first);
153
Jayanth Othayothbcb174b2017-07-02 06:29:24 -0500154 createEntry(i.first);
155 }
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500156 // Start inotify watch on newly created directory.
157 else if ((IN_CREATE == i.second) && fs::is_directory(i.first))
158 {
159 auto watchObj = std::make_unique<Watch>(
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500160 eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
161 std::bind(std::mem_fn(&phosphor::dump::Manager::watchCallback),
162 this, std::placeholders::_1));
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500163
Jayanth Othayothab7f9202017-08-02 06:24:58 -0500164 childWatchMap.emplace(i.first, std::move(watchObj));
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500165 }
Jayanth Othayothbcb174b2017-07-02 06:29:24 -0500166 }
167}
168
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500169void Manager::removeWatch(const fs::path& path)
170{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500171 // Delete Watch entry from map.
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500172 childWatchMap.erase(path);
173}
174
Jayanth Othayoth43096592017-07-20 02:17:37 -0500175void Manager::restore()
176{
177 fs::path dir(BMC_DUMP_PATH);
178 if (!fs::exists(dir) || fs::is_empty(dir))
179 {
180 return;
181 }
182
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500183 // Dump file path: <BMC_DUMP_PATH>/<id>/<filename>
Jayanth Othayoth43096592017-07-20 02:17:37 -0500184 for (const auto& p : fs::directory_iterator(dir))
185 {
186 auto idStr = p.path().filename().string();
187
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500188 // Consider only directory's with dump id as name.
189 // Note: As per design one file per directory.
Jayanth Othayoth43096592017-07-20 02:17:37 -0500190 if ((fs::is_directory(p.path())) &&
191 std::all_of(idStr.begin(), idStr.end(), ::isdigit))
192 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500193 lastEntryId =
194 std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
Jayanth Othayoth43096592017-07-20 02:17:37 -0500195 auto fileIt = fs::directory_iterator(p.path());
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500196 // Create dump entry d-bus object.
Jayanth Othayoth43096592017-07-20 02:17:37 -0500197 if (fileIt != fs::end(fileIt))
198 {
199 createEntry(fileIt->path());
200 }
201 }
202 }
203}
204
Jayanth Othayothab7f9202017-08-02 06:24:58 -0500205size_t Manager::getAllowedSize()
206{
207 using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
208 using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
209
210 auto size = 0;
211
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500212 // Get current size of the dump directory.
Jayanth Othayoth104f57c2017-08-09 06:19:32 -0500213 for (const auto& p : fs::recursive_directory_iterator(BMC_DUMP_PATH))
Jayanth Othayothab7f9202017-08-02 06:24:58 -0500214 {
Jayanth Othayoth104f57c2017-08-09 06:19:32 -0500215 if (!fs::is_directory(p))
216 {
217 size += fs::file_size(p);
218 }
Jayanth Othayothab7f9202017-08-02 06:24:58 -0500219 }
Jayanth Othayoth104f57c2017-08-09 06:19:32 -0500220
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500221 // Convert size into KB
Jayanth Othayoth104f57c2017-08-09 06:19:32 -0500222 size = size / 1024;
223
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500224 // Set the Dump size to Maximum if the free space is greater than
225 // Dump max size otherwise return the available size.
Jayanth Othayoth104f57c2017-08-09 06:19:32 -0500226
227 size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
228
229 if (size < BMC_DUMP_MIN_SPACE_REQD)
Jayanth Othayothab7f9202017-08-02 06:24:58 -0500230 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500231 // Reached to maximum limit
Jayanth Othayothab7f9202017-08-02 06:24:58 -0500232 elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
233 }
Jayanth Othayoth104f57c2017-08-09 06:19:32 -0500234 if (size > BMC_DUMP_MAX_SIZE)
235 {
236 size = BMC_DUMP_MAX_SIZE;
237 }
238
Jayanth Othayothab7f9202017-08-02 06:24:58 -0500239 return size;
240}
241
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500242} // namespace dump
243} // namespace phosphor