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" |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 6 | #include "dump_types.hpp" |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 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 | |
| 13 | #include <phosphor-logging/elog-errors.hpp> |
| 14 | #include <phosphor-logging/elog.hpp> |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 15 | #include <phosphor-logging/lg2.hpp> |
Marri Devender Rao | 3ed02c3 | 2022-06-28 23:12:14 -0500 | [diff] [blame] | 16 | #include <sdeventplus/exception.hpp> |
| 17 | #include <sdeventplus/source/base.hpp> |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 18 | |
Tim Lee | bb9366d | 2021-06-24 14:00:07 +0800 | [diff] [blame] | 19 | #include <cmath> |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 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 | |
Marri Devender Rao | 73953b8 | 2022-02-15 09:15:42 -0600 | [diff] [blame] | 31 | bool Manager::fUserDumpInProgress = false; |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 32 | constexpr auto BMC_DUMP = "BMC_DUMP"; |
Marri Devender Rao | 73953b8 | 2022-02-15 09:15:42 -0600 | [diff] [blame] | 33 | |
Dhruvaraj Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 34 | sdbusplus::message::object_path |
Dhruvaraj Subhashchandran | ddc3366 | 2021-07-19 09:28:42 -0500 | [diff] [blame] | 35 | Manager::createDump(phosphor::dump::DumpCreateParams params) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 36 | { |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 37 | if (params.size() > CREATE_DUMP_MAX_PARAMS) |
Dhruvaraj Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 38 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 39 | lg2::warning("BMC dump accepts not more than 2 additional parameters"); |
Dhruvaraj Subhashchandran | 969f9a5 | 2020-10-30 01:42:39 -0500 | [diff] [blame] | 40 | } |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 41 | |
| 42 | // Get the originator id and type from params |
| 43 | std::string originatorId; |
| 44 | originatorTypes originatorType; |
| 45 | |
| 46 | phosphor::dump::extractOriginatorProperties(params, originatorId, |
| 47 | originatorType); |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 48 | using CreateParameters = |
| 49 | sdbusplus::common::xyz::openbmc_project::dump::Create::CreateParameters; |
| 50 | |
| 51 | DumpTypes dumpType = DumpTypes::USER; |
| 52 | std::string type = extractParameter<std::string>( |
| 53 | convertCreateParametersToString(CreateParameters::DumpType), params); |
| 54 | if (!type.empty()) |
| 55 | { |
| 56 | dumpType = validateDumpType(type, BMC_DUMP); |
| 57 | } |
Dhruvaraj Subhashchandran | e4350f9 | 2023-06-29 05:57:47 -0500 | [diff] [blame] | 58 | |
| 59 | if (dumpType == DumpTypes::ELOG) |
| 60 | { |
| 61 | dumpType = getErrorDumpType(params); |
| 62 | } |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 63 | std::string path = extractParameter<std::string>( |
| 64 | convertCreateParametersToString(CreateParameters::FilePath), params); |
| 65 | |
| 66 | if ((Manager::fUserDumpInProgress == true) && (dumpType == DumpTypes::USER)) |
| 67 | { |
| 68 | lg2::info("Another user initiated dump in progress"); |
| 69 | elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>(); |
| 70 | } |
| 71 | |
| 72 | lg2::info("Initiating new BMC dump with type: {TYPE} path: {PATH}", "TYPE", |
| 73 | dumpTypeToString(dumpType).value(), "PATH", path); |
| 74 | |
| 75 | auto id = captureDump(dumpType, path); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 76 | |
| 77 | // Entry Object path. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 78 | auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 79 | |
| 80 | try |
| 81 | { |
Claire Weinan | c0ab9d4 | 2022-08-17 23:01:07 -0700 | [diff] [blame] | 82 | uint64_t timeStamp = |
| 83 | std::chrono::duration_cast<std::chrono::microseconds>( |
| 84 | std::chrono::system_clock::now().time_since_epoch()) |
| 85 | .count(); |
| 86 | |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 87 | entries.insert(std::make_pair( |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 88 | id, std::make_unique<bmc::Entry>( |
| 89 | bus, objPath.c_str(), id, timeStamp, 0, std::string(), |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 90 | phosphor::dump::OperationStatus::InProgress, originatorId, |
| 91 | originatorType, *this))); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 92 | } |
| 93 | catch (const std::invalid_argument& e) |
| 94 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 95 | lg2::error("Error in creating dump entry, errormsg: {ERROR}, " |
| 96 | "OBJECTPATH: {OBJECT_PATH}, ID: {ID}", |
| 97 | "ERROR", e, "OBJECT_PATH", objPath, "ID", id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 98 | elog<InternalFailure>(); |
| 99 | } |
| 100 | |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 101 | if (dumpType == DumpTypes::USER) |
| 102 | { |
| 103 | Manager::fUserDumpInProgress = true; |
| 104 | } |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 105 | return objPath.string(); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 108 | uint32_t Manager::captureDump(DumpTypes type, const std::string& path) |
| 109 | { |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 110 | // Get Dump size. |
| 111 | auto size = getAllowedSize(); |
| 112 | |
| 113 | pid_t pid = fork(); |
| 114 | |
| 115 | if (pid == 0) |
| 116 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 117 | std::filesystem::path dumpPath(dumpDir); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 118 | auto id = std::to_string(lastEntryId + 1); |
| 119 | dumpPath /= id; |
| 120 | |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 121 | auto strType = dumpTypeToString(type).value(); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 122 | execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i", |
| 123 | id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p", |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 124 | path.empty() ? "" : path.c_str(), "-t", strType.c_str(), nullptr); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 125 | |
| 126 | // dreport script execution is failed. |
| 127 | auto error = errno; |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 128 | lg2::error("Error occurred during dreport function execution, " |
| 129 | "errno: {ERRNO}", |
| 130 | "ERRNO", error); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 131 | elog<InternalFailure>(); |
| 132 | } |
| 133 | else if (pid > 0) |
| 134 | { |
Marri Devender Rao | 3ed02c3 | 2022-06-28 23:12:14 -0500 | [diff] [blame] | 135 | Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) { |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 136 | if (type == DumpTypes::USER) |
Marri Devender Rao | 3ed02c3 | 2022-06-28 23:12:14 -0500 | [diff] [blame] | 137 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 138 | lg2::info("User initiated dump completed, resetting flag"); |
Marri Devender Rao | 3ed02c3 | 2022-06-28 23:12:14 -0500 | [diff] [blame] | 139 | Manager::fUserDumpInProgress = false; |
| 140 | } |
| 141 | this->childPtrMap.erase(pid); |
| 142 | }; |
| 143 | try |
| 144 | { |
| 145 | childPtrMap.emplace(pid, |
| 146 | std::make_unique<Child>(eventLoop.get(), pid, |
| 147 | WEXITED | WSTOPPED, |
| 148 | std::move(callback))); |
| 149 | } |
| 150 | catch (const sdeventplus::SdEventError& ex) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 151 | { |
| 152 | // Failed to add to event loop |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 153 | lg2::error( |
| 154 | "Error occurred during the sdeventplus::source::Child creation " |
| 155 | "ex: {ERROR}", |
| 156 | "ERROR", ex); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 157 | elog<InternalFailure>(); |
| 158 | } |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | auto error = errno; |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 163 | lg2::error("Error occurred during fork, errno: {ERRNO}", "ERRNO", |
| 164 | error); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 165 | elog<InternalFailure>(); |
| 166 | } |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 167 | return ++lastEntryId; |
| 168 | } |
| 169 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 170 | void Manager::createEntry(const std::filesystem::path& file) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 171 | { |
Dhruvaraj Subhashchandran | 93f0641 | 2024-06-02 05:16:51 -0500 | [diff] [blame^] | 172 | auto dumpDetails = extractDumpDetails(file); |
| 173 | if (!dumpDetails) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 174 | { |
Dhruvaraj Subhashchandran | 93f0641 | 2024-06-02 05:16:51 -0500 | [diff] [blame^] | 175 | lg2::error("Failed to extract dump details from file name: {PATH}", |
| 176 | "PATH", file); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
Dhruvaraj Subhashchandran | 93f0641 | 2024-06-02 05:16:51 -0500 | [diff] [blame^] | 180 | auto [id, timestamp, size] = *dumpDetails; |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 181 | |
| 182 | // If there is an existing entry update it and return. |
| 183 | auto dumpEntry = entries.find(id); |
| 184 | if (dumpEntry != entries.end()) |
| 185 | { |
| 186 | dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get()) |
Xie Ning | 56bd797 | 2022-02-25 15:20:02 +0800 | [diff] [blame] | 187 | ->update(timestamp, std::filesystem::file_size(file), file); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | |
| 191 | // Entry Object path. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 192 | auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); |
Dhruvaraj Subhashchandran | 6ccb50e | 2020-10-29 09:33:18 -0500 | [diff] [blame] | 193 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 194 | // TODO: Get the persisted originator id & type |
| 195 | // For now, replacing it with null |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 196 | try |
| 197 | { |
Dhruvaraj Subhashchandran | a6ab806 | 2020-10-29 15:29:10 -0500 | [diff] [blame] | 198 | entries.insert(std::make_pair( |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 199 | id, std::make_unique<bmc::Entry>( |
Xie Ning | 56bd797 | 2022-02-25 15:20:02 +0800 | [diff] [blame] | 200 | bus, objPath.c_str(), id, timestamp, |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 201 | std::filesystem::file_size(file), file, |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 202 | phosphor::dump::OperationStatus::Completed, std::string(), |
| 203 | originatorTypes::Internal, *this))); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 204 | } |
| 205 | catch (const std::invalid_argument& e) |
| 206 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 207 | lg2::error( |
| 208 | "Error in creating dump entry, errormsg: {ERROR}, " |
| 209 | "OBJECTPATH: {OBJECT_PATH}, ID: {ID}, TIMESTAMP: {TIMESTAMP}, " |
| 210 | "SIZE: {SIZE}, FILENAME: {FILENAME}", |
| 211 | "ERROR", e, "OBJECT_PATH", objPath, "ID", id, "TIMESTAMP", |
| 212 | timestamp, "SIZE", std::filesystem::file_size(file), "FILENAME", |
| 213 | file); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | void Manager::watchCallback(const UserMap& fileInfo) |
| 218 | { |
| 219 | for (const auto& i : fileInfo) |
| 220 | { |
| 221 | // For any new dump file create dump entry object |
| 222 | // and associated inotify watch. |
| 223 | if (IN_CLOSE_WRITE == i.second) |
| 224 | { |
Chirag Sharma | 4cb0799 | 2022-05-09 04:37:22 -0500 | [diff] [blame] | 225 | if (!std::filesystem::is_directory(i.first)) |
| 226 | { |
| 227 | // Don't require filename to be passed, as the path |
| 228 | // of dump directory is stored in the childWatchMap |
| 229 | removeWatch(i.first.parent_path()); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 230 | |
Chirag Sharma | 4cb0799 | 2022-05-09 04:37:22 -0500 | [diff] [blame] | 231 | // dump file is written now create D-Bus entry |
| 232 | createEntry(i.first); |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | removeWatch(i.first); |
| 237 | } |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 238 | } |
| 239 | // Start inotify watch on newly created directory. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 240 | else if ((IN_CREATE == i.second) && |
| 241 | std::filesystem::is_directory(i.first)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 242 | { |
| 243 | auto watchObj = std::make_unique<Watch>( |
| 244 | eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first, |
| 245 | std::bind( |
| 246 | std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback), |
| 247 | this, std::placeholders::_1)); |
| 248 | |
| 249 | childWatchMap.emplace(i.first, std::move(watchObj)); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 254 | void Manager::removeWatch(const std::filesystem::path& path) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 255 | { |
| 256 | // Delete Watch entry from map. |
| 257 | childWatchMap.erase(path); |
| 258 | } |
| 259 | |
| 260 | void Manager::restore() |
| 261 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 262 | std::filesystem::path dir(dumpDir); |
| 263 | if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 264 | { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | // Dump file path: <DUMP_PATH>/<id>/<filename> |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 269 | for (const auto& p : std::filesystem::directory_iterator(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 270 | { |
| 271 | auto idStr = p.path().filename().string(); |
| 272 | |
Dhruvaraj Subhashchandran | 93f0641 | 2024-06-02 05:16:51 -0500 | [diff] [blame^] | 273 | // Consider only directories with dump id as name. |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 274 | // Note: As per design one file per directory. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 275 | if ((std::filesystem::is_directory(p.path())) && |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 276 | std::all_of(idStr.begin(), idStr.end(), ::isdigit)) |
| 277 | { |
Patrick Williams | 78e8840 | 2023-05-10 07:50:48 -0500 | [diff] [blame] | 278 | lastEntryId = std::max(lastEntryId, |
| 279 | static_cast<uint32_t>(std::stoul(idStr))); |
Dhruvaraj Subhashchandran | 93f0641 | 2024-06-02 05:16:51 -0500 | [diff] [blame^] | 280 | for (const auto& file : |
| 281 | std::filesystem::directory_iterator(p.path())) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 282 | { |
Dhruvaraj Subhashchandran | 93f0641 | 2024-06-02 05:16:51 -0500 | [diff] [blame^] | 283 | // Skip .preserve directory |
| 284 | if (file.path().filename() == PRESERVE) |
| 285 | { |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | // Entry Object path. |
| 290 | auto objPath = std::filesystem::path(baseEntryPath) / idStr; |
| 291 | auto entry = Entry::deserializeEntry(bus, std::stoul(idStr), |
| 292 | objPath.string(), |
| 293 | file.path(), *this); |
| 294 | |
| 295 | if (entry != nullptr) |
| 296 | { |
| 297 | entries.insert( |
| 298 | std::make_pair(entry->getDumpId(), std::move(entry))); |
| 299 | } |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 305 | size_t getDirectorySize(const std::string dir) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 306 | { |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 307 | auto size = 0; |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 308 | for (const auto& p : std::filesystem::recursive_directory_iterator(dir)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 309 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 310 | if (!std::filesystem::is_directory(p)) |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 311 | { |
Tim Lee | bb9366d | 2021-06-24 14:00:07 +0800 | [diff] [blame] | 312 | size += std::ceil(std::filesystem::file_size(p) / 1024.0); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 313 | } |
| 314 | } |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 315 | return size; |
| 316 | } |
| 317 | |
| 318 | size_t Manager::getAllowedSize() |
| 319 | { |
| 320 | // Get current size of the dump directory. |
| 321 | auto size = getDirectorySize(dumpDir); |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 322 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 323 | // Set the Dump size to Maximum if the free space is greater than |
| 324 | // Dump max size otherwise return the available size. |
| 325 | |
| 326 | size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size); |
| 327 | |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 328 | #ifdef BMC_DUMP_ROTATE_CONFIG |
| 329 | // Delete the first existing file until the space is enough |
| 330 | while (size < BMC_DUMP_MIN_SPACE_REQD) |
| 331 | { |
Patrick Williams | e70edac | 2023-10-20 11:19:08 -0500 | [diff] [blame] | 332 | auto delEntry = min_element( |
| 333 | entries.begin(), entries.end(), |
| 334 | [](const auto& l, const auto& r) { return l.first < r.first; }); |
Patrick Williams | 78e8840 | 2023-05-10 07:50:48 -0500 | [diff] [blame] | 335 | auto delPath = std::filesystem::path(dumpDir) / |
| 336 | std::to_string(delEntry->first); |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 337 | |
| 338 | size += getDirectorySize(delPath); |
| 339 | |
| 340 | delEntry->second->delete_(); |
| 341 | } |
| 342 | #else |
| 343 | using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error; |
| 344 | using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON; |
| 345 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 346 | if (size < BMC_DUMP_MIN_SPACE_REQD) |
| 347 | { |
| 348 | // Reached to maximum limit |
| 349 | elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps")); |
| 350 | } |
Xie Ning | fc69f35 | 2022-05-17 16:06:52 +0800 | [diff] [blame] | 351 | #endif |
| 352 | |
Dhruvaraj Subhashchandran | fef66a9 | 2020-09-06 13:10:59 -0500 | [diff] [blame] | 353 | if (size > BMC_DUMP_MAX_SIZE) |
| 354 | { |
| 355 | size = BMC_DUMP_MAX_SIZE; |
| 356 | } |
| 357 | |
| 358 | return size; |
| 359 | } |
| 360 | |
| 361 | } // namespace bmc |
| 362 | } // namespace dump |
| 363 | } // namespace phosphor |