Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 1 | #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 Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 13 | #include <ctime> |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 14 | #include <phosphor-logging/elog-errors.hpp> |
| 15 | #include <phosphor-logging/elog.hpp> |
| 16 | #include <regex> |
| 17 | |
| 18 | namespace phosphor |
| 19 | { |
| 20 | namespace dump |
| 21 | { |
| 22 | namespace bmc |
| 23 | { |
| 24 | |
| 25 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 26 | using namespace phosphor::logging; |
| 27 | |
| 28 | namespace internal |
| 29 | { |
| 30 | |
| 31 | void 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 Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 38 | sdbusplus::message::object_path |
| 39 | Manager::createDump(std::map<std::string, std::string> params) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 40 | { |
Dhruvaraj Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 41 | if (!params.empty()) |
| 42 | { |
| 43 | log<level::WARNING>("BMC dump accepts no additional parameters"); |
| 44 | } |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 45 | std::vector<std::string> paths; |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 46 | auto id = captureDump(Type::UserRequested, paths); |
| 47 | |
| 48 | // Entry Object path. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 49 | auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 50 | |
| 51 | try |
| 52 | { |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 53 | std::time_t timeStamp = std::time(nullptr); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 54 | entries.insert(std::make_pair( |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 55 | id, std::make_unique<bmc::Entry>( |
| 56 | bus, objPath.c_str(), id, timeStamp, 0, std::string(), |
| 57 | phosphor::dump::OperationStatus::InProgress, *this))); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 58 | } |
| 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 Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | uint32_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 Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 81 | std::filesystem::path dumpPath(dumpDir); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 82 | 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 Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 121 | void Manager::createEntry(const std::filesystem::path& file) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 122 | { |
| 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 Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 141 | 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 Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 148 | ->update(stoull(msString), std::filesystem::file_size(file), file); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
| 152 | // Entry Object path. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 153 | auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 154 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 155 | try |
| 156 | { |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 157 | entries.insert(std::make_pair( |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 158 | 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 Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 162 | } |
| 163 | catch (const std::invalid_argument& e) |
| 164 | { |
| 165 | log<level::ERR>(e.what()); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 166 | 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 Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 170 | entry("SIZE=%d", std::filesystem::file_size(file)), |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 171 | entry("FILENAME=%s", file.c_str())); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void 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 Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 189 | else if ((IN_CREATE == i.second) && |
| 190 | std::filesystem::is_directory(i.first)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 191 | { |
| 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 Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 203 | void Manager::removeWatch(const std::filesystem::path& path) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 204 | { |
| 205 | // Delete Watch entry from map. |
| 206 | childWatchMap.erase(path); |
| 207 | } |
| 208 | |
| 209 | void Manager::restore() |
| 210 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 211 | std::filesystem::path dir(dumpDir); |
| 212 | if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 213 | { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | // Dump file path: <DUMP_PATH>/<id>/<filename> |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 218 | for (const auto& p : std::filesystem::directory_iterator(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 219 | { |
| 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 Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 224 | if ((std::filesystem::is_directory(p.path())) && |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 225 | std::all_of(idStr.begin(), idStr.end(), ::isdigit)) |
| 226 | { |
| 227 | lastEntryId = |
| 228 | std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr))); |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 229 | auto fileIt = std::filesystem::directory_iterator(p.path()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 230 | // Create dump entry d-bus object. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 231 | if (fileIt != std::filesystem::end(fileIt)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 232 | { |
| 233 | createEntry(fileIt->path()); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | size_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 Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 247 | for (const auto& p : std::filesystem::recursive_directory_iterator(dumpDir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 248 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 249 | if (!std::filesystem::is_directory(p)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 250 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame^] | 251 | size += std::filesystem::file_size(p); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 252 | } |
| 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 |