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