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 | |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 10 | #include <fmt/core.h> |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 11 | #include <sys/inotify.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #include <phosphor-logging/elog-errors.hpp> |
| 15 | #include <phosphor-logging/elog.hpp> |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 16 | |
Tim Lee | bb9366d | 2021-06-24 14:00:07 +0800 | [diff] [blame] | 17 | #include <cmath> |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 18 | #include <ctime> |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 19 | #include <regex> |
| 20 | |
| 21 | namespace phosphor |
| 22 | { |
| 23 | namespace dump |
| 24 | { |
| 25 | namespace bmc |
| 26 | { |
| 27 | |
| 28 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 29 | using namespace phosphor::logging; |
| 30 | |
| 31 | namespace internal |
| 32 | { |
| 33 | |
| 34 | void Manager::create(Type type, std::vector<std::string> fullPaths) |
| 35 | { |
| 36 | dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths); |
| 37 | } |
| 38 | |
| 39 | } // namespace internal |
| 40 | |
Dhruvaraj Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 41 | sdbusplus::message::object_path |
Dhruvaraj Subhashchandran | ddc3366 | 2021-07-19 09:28:42 -0500 | [diff] [blame] | 42 | Manager::createDump(phosphor::dump::DumpCreateParams params) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 43 | { |
Dhruvaraj Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 44 | if (!params.empty()) |
| 45 | { |
| 46 | log<level::WARNING>("BMC dump accepts no additional parameters"); |
| 47 | } |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 48 | std::vector<std::string> paths; |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 49 | auto id = captureDump(Type::UserRequested, paths); |
| 50 | |
| 51 | // Entry Object path. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 52 | auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 53 | |
| 54 | try |
| 55 | { |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 56 | std::time_t timeStamp = std::time(nullptr); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 57 | entries.insert(std::make_pair( |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 58 | id, std::make_unique<bmc::Entry>( |
| 59 | bus, objPath.c_str(), id, timeStamp, 0, std::string(), |
| 60 | phosphor::dump::OperationStatus::InProgress, *this))); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 61 | } |
| 62 | catch (const std::invalid_argument& e) |
| 63 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 64 | log<level::ERR>(fmt::format("Error in creating dump entry, " |
| 65 | "errormsg({}), OBJECTPATH({}), ID({})", |
| 66 | e.what(), objPath.c_str(), id) |
| 67 | .c_str()); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 68 | elog<InternalFailure>(); |
| 69 | } |
| 70 | |
| 71 | return objPath.string(); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | uint32_t Manager::captureDump(Type type, |
| 75 | const std::vector<std::string>& fullPaths) |
| 76 | { |
| 77 | // Get Dump size. |
| 78 | auto size = getAllowedSize(); |
| 79 | |
| 80 | pid_t pid = fork(); |
| 81 | |
| 82 | if (pid == 0) |
| 83 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 84 | std::filesystem::path dumpPath(dumpDir); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 85 | auto id = std::to_string(lastEntryId + 1); |
| 86 | dumpPath /= id; |
| 87 | |
| 88 | // get dreport type map entry |
| 89 | auto tempType = TypeMap.find(type); |
| 90 | |
| 91 | execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i", |
| 92 | id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p", |
| 93 | fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t", |
| 94 | tempType->second.c_str(), nullptr); |
| 95 | |
| 96 | // dreport script execution is failed. |
| 97 | auto error = errno; |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 98 | log<level::ERR>( |
| 99 | fmt::format( |
| 100 | "Error occurred during dreport function execution, errno({})", |
| 101 | error) |
| 102 | .c_str()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 103 | elog<InternalFailure>(); |
| 104 | } |
| 105 | else if (pid > 0) |
| 106 | { |
| 107 | auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid, |
| 108 | WEXITED | WSTOPPED, callback, nullptr); |
| 109 | if (0 > rc) |
| 110 | { |
| 111 | // Failed to add to event loop |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 112 | log<level::ERR>( |
| 113 | fmt::format( |
| 114 | "Error occurred during the sd_event_add_child call, rc({})", |
| 115 | rc) |
| 116 | .c_str()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 117 | elog<InternalFailure>(); |
| 118 | } |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | auto error = errno; |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 123 | log<level::ERR>( |
| 124 | fmt::format("Error occurred during fork, errno({})", error) |
| 125 | .c_str()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 126 | elog<InternalFailure>(); |
| 127 | } |
| 128 | |
| 129 | return ++lastEntryId; |
| 130 | } |
| 131 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 132 | void Manager::createEntry(const std::filesystem::path& file) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 133 | { |
| 134 | // Dump File Name format obmcdump_ID_EPOCHTIME.EXT |
| 135 | static constexpr auto ID_POS = 1; |
| 136 | static constexpr auto EPOCHTIME_POS = 2; |
| 137 | std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)"); |
| 138 | |
| 139 | std::smatch match; |
| 140 | std::string name = file.filename(); |
| 141 | |
| 142 | if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0))) |
| 143 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 144 | log<level::ERR>(fmt::format("Invalid Dump file name, FILENAME({})", |
| 145 | file.filename().c_str()) |
| 146 | .c_str()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
| 150 | auto idString = match[ID_POS]; |
Xie Ning | 56bd797 | 2022-02-25 15:20:02 +0800 | [diff] [blame] | 151 | uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000; |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 152 | |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 153 | auto id = stoul(idString); |
| 154 | |
| 155 | // If there is an existing entry update it and return. |
| 156 | auto dumpEntry = entries.find(id); |
| 157 | if (dumpEntry != entries.end()) |
| 158 | { |
| 159 | dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get()) |
Xie Ning | 56bd797 | 2022-02-25 15:20:02 +0800 | [diff] [blame] | 160 | ->update(timestamp, std::filesystem::file_size(file), file); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
| 164 | // Entry Object path. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 165 | auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 166 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 167 | try |
| 168 | { |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 169 | entries.insert(std::make_pair( |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 170 | id, std::make_unique<bmc::Entry>( |
Xie Ning | 56bd797 | 2022-02-25 15:20:02 +0800 | [diff] [blame] | 171 | bus, objPath.c_str(), id, timestamp, |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 172 | std::filesystem::file_size(file), file, |
| 173 | phosphor::dump::OperationStatus::Completed, *this))); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 174 | } |
| 175 | catch (const std::invalid_argument& e) |
| 176 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 177 | log<level::ERR>( |
| 178 | fmt::format( |
| 179 | "Error in creating dump entry, errormsg({}), OBJECTPATH({}), " |
| 180 | "ID({}), TIMESTAMP({}), SIZE({}), FILENAME({})", |
Xie Ning | 56bd797 | 2022-02-25 15:20:02 +0800 | [diff] [blame] | 181 | e.what(), objPath.c_str(), id, timestamp, |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 182 | std::filesystem::file_size(file), file.filename().c_str()) |
| 183 | .c_str()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void Manager::watchCallback(const UserMap& fileInfo) |
| 189 | { |
| 190 | for (const auto& i : fileInfo) |
| 191 | { |
| 192 | // For any new dump file create dump entry object |
| 193 | // and associated inotify watch. |
| 194 | if (IN_CLOSE_WRITE == i.second) |
| 195 | { |
| 196 | removeWatch(i.first); |
| 197 | |
| 198 | createEntry(i.first); |
| 199 | } |
| 200 | // Start inotify watch on newly created directory. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 201 | else if ((IN_CREATE == i.second) && |
| 202 | std::filesystem::is_directory(i.first)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 203 | { |
| 204 | auto watchObj = std::make_unique<Watch>( |
| 205 | eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first, |
| 206 | std::bind( |
| 207 | std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback), |
| 208 | this, std::placeholders::_1)); |
| 209 | |
| 210 | childWatchMap.emplace(i.first, std::move(watchObj)); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 215 | void Manager::removeWatch(const std::filesystem::path& path) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 216 | { |
| 217 | // Delete Watch entry from map. |
| 218 | childWatchMap.erase(path); |
| 219 | } |
| 220 | |
| 221 | void Manager::restore() |
| 222 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 223 | std::filesystem::path dir(dumpDir); |
| 224 | if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 225 | { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | // Dump file path: <DUMP_PATH>/<id>/<filename> |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 230 | for (const auto& p : std::filesystem::directory_iterator(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 231 | { |
| 232 | auto idStr = p.path().filename().string(); |
| 233 | |
| 234 | // Consider only directory's with dump id as name. |
| 235 | // Note: As per design one file per directory. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 236 | if ((std::filesystem::is_directory(p.path())) && |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 237 | std::all_of(idStr.begin(), idStr.end(), ::isdigit)) |
| 238 | { |
| 239 | lastEntryId = |
| 240 | std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr))); |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 241 | auto fileIt = std::filesystem::directory_iterator(p.path()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 242 | // Create dump entry d-bus object. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 243 | if (fileIt != std::filesystem::end(fileIt)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 244 | { |
| 245 | createEntry(fileIt->path()); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | size_t Manager::getAllowedSize() |
| 252 | { |
| 253 | using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error; |
| 254 | using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON; |
| 255 | |
| 256 | auto size = 0; |
| 257 | |
| 258 | // Get current size of the dump directory. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 259 | for (const auto& p : std::filesystem::recursive_directory_iterator(dumpDir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 260 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 261 | if (!std::filesystem::is_directory(p)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 262 | { |
Tim Lee | bb9366d | 2021-06-24 14:00:07 +0800 | [diff] [blame] | 263 | size += std::ceil(std::filesystem::file_size(p) / 1024.0); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 267 | // Set the Dump size to Maximum if the free space is greater than |
| 268 | // Dump max size otherwise return the available size. |
| 269 | |
| 270 | size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size); |
| 271 | |
| 272 | if (size < BMC_DUMP_MIN_SPACE_REQD) |
| 273 | { |
| 274 | // Reached to maximum limit |
| 275 | elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps")); |
| 276 | } |
| 277 | if (size > BMC_DUMP_MAX_SIZE) |
| 278 | { |
| 279 | size = BMC_DUMP_MAX_SIZE; |
| 280 | } |
| 281 | |
| 282 | return size; |
| 283 | } |
| 284 | |
| 285 | } // namespace bmc |
| 286 | } // namespace dump |
| 287 | } // namespace phosphor |