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