1fef66a95SDhruvaraj Subhashchandran #include "config.h" 2fef66a95SDhruvaraj Subhashchandran 3fef66a95SDhruvaraj Subhashchandran #include "dump_manager_bmc.hpp" 4fef66a95SDhruvaraj Subhashchandran 5fef66a95SDhruvaraj Subhashchandran #include "bmc_dump_entry.hpp" 6fef66a95SDhruvaraj Subhashchandran #include "dump_internal.hpp" 7fef66a95SDhruvaraj Subhashchandran #include "xyz/openbmc_project/Common/error.hpp" 8fef66a95SDhruvaraj Subhashchandran #include "xyz/openbmc_project/Dump/Create/error.hpp" 9fef66a95SDhruvaraj Subhashchandran 10858fbb2eSGeorge Liu #include <fmt/core.h> 11fef66a95SDhruvaraj Subhashchandran #include <sys/inotify.h> 12fef66a95SDhruvaraj Subhashchandran #include <unistd.h> 13fef66a95SDhruvaraj Subhashchandran 14fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog-errors.hpp> 15fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog.hpp> 160af74a5eSJayanth Othayoth 17bb9366d0STim Lee #include <cmath> 180af74a5eSJayanth Othayoth #include <ctime> 19fef66a95SDhruvaraj Subhashchandran #include <regex> 20fef66a95SDhruvaraj Subhashchandran 21fef66a95SDhruvaraj Subhashchandran namespace phosphor 22fef66a95SDhruvaraj Subhashchandran { 23fef66a95SDhruvaraj Subhashchandran namespace dump 24fef66a95SDhruvaraj Subhashchandran { 25fef66a95SDhruvaraj Subhashchandran namespace bmc 26fef66a95SDhruvaraj Subhashchandran { 27fef66a95SDhruvaraj Subhashchandran 28fef66a95SDhruvaraj Subhashchandran using namespace sdbusplus::xyz::openbmc_project::Common::Error; 29fef66a95SDhruvaraj Subhashchandran using namespace phosphor::logging; 30fef66a95SDhruvaraj Subhashchandran 31fef66a95SDhruvaraj Subhashchandran namespace internal 32fef66a95SDhruvaraj Subhashchandran { 33fef66a95SDhruvaraj Subhashchandran 34fef66a95SDhruvaraj Subhashchandran void Manager::create(Type type, std::vector<std::string> fullPaths) 35fef66a95SDhruvaraj Subhashchandran { 36fef66a95SDhruvaraj Subhashchandran dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths); 37fef66a95SDhruvaraj Subhashchandran } 38fef66a95SDhruvaraj Subhashchandran 39fef66a95SDhruvaraj Subhashchandran } // namespace internal 40fef66a95SDhruvaraj Subhashchandran 41969f9a59SDhruvaraj Subhashchandran sdbusplus::message::object_path 42ddc3366eSDhruvaraj Subhashchandran Manager::createDump(phosphor::dump::DumpCreateParams params) 43fef66a95SDhruvaraj Subhashchandran { 44*74a1f39cSAsmitha Karunanithi if (params.size() > CREATE_DUMP_MAX_PARAMS) 45969f9a59SDhruvaraj Subhashchandran { 46*74a1f39cSAsmitha Karunanithi log<level::WARNING>( 47*74a1f39cSAsmitha Karunanithi "BMC dump accepts not more than 2 additional parameters"); 48969f9a59SDhruvaraj Subhashchandran } 49*74a1f39cSAsmitha Karunanithi 50*74a1f39cSAsmitha Karunanithi // Get the originator id and type from params 51*74a1f39cSAsmitha Karunanithi std::string originatorId; 52*74a1f39cSAsmitha Karunanithi originatorTypes originatorType; 53*74a1f39cSAsmitha Karunanithi 54*74a1f39cSAsmitha Karunanithi phosphor::dump::extractOriginatorProperties(params, originatorId, 55*74a1f39cSAsmitha Karunanithi originatorType); 56*74a1f39cSAsmitha Karunanithi 57fef66a95SDhruvaraj Subhashchandran std::vector<std::string> paths; 586ccb50e1SDhruvaraj Subhashchandran auto id = captureDump(Type::UserRequested, paths); 596ccb50e1SDhruvaraj Subhashchandran 606ccb50e1SDhruvaraj Subhashchandran // Entry Object path. 613fc6df48SJayanth Othayoth auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); 626ccb50e1SDhruvaraj Subhashchandran 636ccb50e1SDhruvaraj Subhashchandran try 646ccb50e1SDhruvaraj Subhashchandran { 65c0ab9d43SClaire Weinan uint64_t timeStamp = 66c0ab9d43SClaire Weinan std::chrono::duration_cast<std::chrono::microseconds>( 67c0ab9d43SClaire Weinan std::chrono::system_clock::now().time_since_epoch()) 68c0ab9d43SClaire Weinan .count(); 69c0ab9d43SClaire Weinan 706ccb50e1SDhruvaraj Subhashchandran entries.insert(std::make_pair( 71a6ab806dSDhruvaraj Subhashchandran id, std::make_unique<bmc::Entry>( 72a6ab806dSDhruvaraj Subhashchandran bus, objPath.c_str(), id, timeStamp, 0, std::string(), 73*74a1f39cSAsmitha Karunanithi phosphor::dump::OperationStatus::InProgress, originatorId, 74*74a1f39cSAsmitha Karunanithi originatorType, *this))); 756ccb50e1SDhruvaraj Subhashchandran } 766ccb50e1SDhruvaraj Subhashchandran catch (const std::invalid_argument& e) 776ccb50e1SDhruvaraj Subhashchandran { 78858fbb2eSGeorge Liu log<level::ERR>(fmt::format("Error in creating dump entry, " 79858fbb2eSGeorge Liu "errormsg({}), OBJECTPATH({}), ID({})", 80858fbb2eSGeorge Liu e.what(), objPath.c_str(), id) 81858fbb2eSGeorge Liu .c_str()); 826ccb50e1SDhruvaraj Subhashchandran elog<InternalFailure>(); 836ccb50e1SDhruvaraj Subhashchandran } 846ccb50e1SDhruvaraj Subhashchandran 856ccb50e1SDhruvaraj Subhashchandran return objPath.string(); 86fef66a95SDhruvaraj Subhashchandran } 87fef66a95SDhruvaraj Subhashchandran 88fef66a95SDhruvaraj Subhashchandran uint32_t Manager::captureDump(Type type, 89fef66a95SDhruvaraj Subhashchandran const std::vector<std::string>& fullPaths) 90fef66a95SDhruvaraj Subhashchandran { 91fef66a95SDhruvaraj Subhashchandran // Get Dump size. 92fef66a95SDhruvaraj Subhashchandran auto size = getAllowedSize(); 93fef66a95SDhruvaraj Subhashchandran 94fef66a95SDhruvaraj Subhashchandran pid_t pid = fork(); 95fef66a95SDhruvaraj Subhashchandran 96fef66a95SDhruvaraj Subhashchandran if (pid == 0) 97fef66a95SDhruvaraj Subhashchandran { 983fc6df48SJayanth Othayoth std::filesystem::path dumpPath(dumpDir); 99fef66a95SDhruvaraj Subhashchandran auto id = std::to_string(lastEntryId + 1); 100fef66a95SDhruvaraj Subhashchandran dumpPath /= id; 101fef66a95SDhruvaraj Subhashchandran 102fef66a95SDhruvaraj Subhashchandran // get dreport type map entry 103fef66a95SDhruvaraj Subhashchandran auto tempType = TypeMap.find(type); 104fef66a95SDhruvaraj Subhashchandran 105fef66a95SDhruvaraj Subhashchandran execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i", 106fef66a95SDhruvaraj Subhashchandran id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p", 107fef66a95SDhruvaraj Subhashchandran fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t", 108fef66a95SDhruvaraj Subhashchandran tempType->second.c_str(), nullptr); 109fef66a95SDhruvaraj Subhashchandran 110fef66a95SDhruvaraj Subhashchandran // dreport script execution is failed. 111fef66a95SDhruvaraj Subhashchandran auto error = errno; 112858fbb2eSGeorge Liu log<level::ERR>( 113858fbb2eSGeorge Liu fmt::format( 114858fbb2eSGeorge Liu "Error occurred during dreport function execution, errno({})", 115858fbb2eSGeorge Liu error) 116858fbb2eSGeorge Liu .c_str()); 117fef66a95SDhruvaraj Subhashchandran elog<InternalFailure>(); 118fef66a95SDhruvaraj Subhashchandran } 119fef66a95SDhruvaraj Subhashchandran else if (pid > 0) 120fef66a95SDhruvaraj Subhashchandran { 121fef66a95SDhruvaraj Subhashchandran auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid, 122fef66a95SDhruvaraj Subhashchandran WEXITED | WSTOPPED, callback, nullptr); 123fef66a95SDhruvaraj Subhashchandran if (0 > rc) 124fef66a95SDhruvaraj Subhashchandran { 125fef66a95SDhruvaraj Subhashchandran // Failed to add to event loop 126858fbb2eSGeorge Liu log<level::ERR>( 127858fbb2eSGeorge Liu fmt::format( 128858fbb2eSGeorge Liu "Error occurred during the sd_event_add_child call, rc({})", 129858fbb2eSGeorge Liu rc) 130858fbb2eSGeorge Liu .c_str()); 131fef66a95SDhruvaraj Subhashchandran elog<InternalFailure>(); 132fef66a95SDhruvaraj Subhashchandran } 133fef66a95SDhruvaraj Subhashchandran } 134fef66a95SDhruvaraj Subhashchandran else 135fef66a95SDhruvaraj Subhashchandran { 136fef66a95SDhruvaraj Subhashchandran auto error = errno; 137858fbb2eSGeorge Liu log<level::ERR>( 138858fbb2eSGeorge Liu fmt::format("Error occurred during fork, errno({})", error) 139858fbb2eSGeorge Liu .c_str()); 140fef66a95SDhruvaraj Subhashchandran elog<InternalFailure>(); 141fef66a95SDhruvaraj Subhashchandran } 142fef66a95SDhruvaraj Subhashchandran 143fef66a95SDhruvaraj Subhashchandran return ++lastEntryId; 144fef66a95SDhruvaraj Subhashchandran } 145fef66a95SDhruvaraj Subhashchandran 1463fc6df48SJayanth Othayoth void Manager::createEntry(const std::filesystem::path& file) 147fef66a95SDhruvaraj Subhashchandran { 148fef66a95SDhruvaraj Subhashchandran // Dump File Name format obmcdump_ID_EPOCHTIME.EXT 149fef66a95SDhruvaraj Subhashchandran static constexpr auto ID_POS = 1; 150fef66a95SDhruvaraj Subhashchandran static constexpr auto EPOCHTIME_POS = 2; 151fef66a95SDhruvaraj Subhashchandran std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)"); 152fef66a95SDhruvaraj Subhashchandran 153fef66a95SDhruvaraj Subhashchandran std::smatch match; 154fef66a95SDhruvaraj Subhashchandran std::string name = file.filename(); 155fef66a95SDhruvaraj Subhashchandran 156fef66a95SDhruvaraj Subhashchandran if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0))) 157fef66a95SDhruvaraj Subhashchandran { 158858fbb2eSGeorge Liu log<level::ERR>(fmt::format("Invalid Dump file name, FILENAME({})", 159858fbb2eSGeorge Liu file.filename().c_str()) 160858fbb2eSGeorge Liu .c_str()); 161fef66a95SDhruvaraj Subhashchandran return; 162fef66a95SDhruvaraj Subhashchandran } 163fef66a95SDhruvaraj Subhashchandran 164fef66a95SDhruvaraj Subhashchandran auto idString = match[ID_POS]; 16556bd797eSXie Ning uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000; 166fef66a95SDhruvaraj Subhashchandran 167fef66a95SDhruvaraj Subhashchandran auto id = stoul(idString); 1686ccb50e1SDhruvaraj Subhashchandran 1696ccb50e1SDhruvaraj Subhashchandran // If there is an existing entry update it and return. 1706ccb50e1SDhruvaraj Subhashchandran auto dumpEntry = entries.find(id); 1716ccb50e1SDhruvaraj Subhashchandran if (dumpEntry != entries.end()) 1726ccb50e1SDhruvaraj Subhashchandran { 1736ccb50e1SDhruvaraj Subhashchandran dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get()) 17456bd797eSXie Ning ->update(timestamp, std::filesystem::file_size(file), file); 1756ccb50e1SDhruvaraj Subhashchandran return; 1766ccb50e1SDhruvaraj Subhashchandran } 1776ccb50e1SDhruvaraj Subhashchandran 178fef66a95SDhruvaraj Subhashchandran // Entry Object path. 1793fc6df48SJayanth Othayoth auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); 180fef66a95SDhruvaraj Subhashchandran 181*74a1f39cSAsmitha Karunanithi // TODO: Get the persisted originator id & type 182*74a1f39cSAsmitha Karunanithi // For now, replacing it with null 1836ccb50e1SDhruvaraj Subhashchandran try 1846ccb50e1SDhruvaraj Subhashchandran { 185a6ab806dSDhruvaraj Subhashchandran entries.insert(std::make_pair( 1863fc6df48SJayanth Othayoth id, std::make_unique<bmc::Entry>( 18756bd797eSXie Ning bus, objPath.c_str(), id, timestamp, 1883fc6df48SJayanth Othayoth std::filesystem::file_size(file), file, 189*74a1f39cSAsmitha Karunanithi phosphor::dump::OperationStatus::Completed, std::string(), 190*74a1f39cSAsmitha Karunanithi originatorTypes::Internal, *this))); 191fef66a95SDhruvaraj Subhashchandran } 192fef66a95SDhruvaraj Subhashchandran catch (const std::invalid_argument& e) 193fef66a95SDhruvaraj Subhashchandran { 194858fbb2eSGeorge Liu log<level::ERR>( 195858fbb2eSGeorge Liu fmt::format( 196858fbb2eSGeorge Liu "Error in creating dump entry, errormsg({}), OBJECTPATH({}), " 197858fbb2eSGeorge Liu "ID({}), TIMESTAMP({}), SIZE({}), FILENAME({})", 19856bd797eSXie Ning e.what(), objPath.c_str(), id, timestamp, 199858fbb2eSGeorge Liu std::filesystem::file_size(file), file.filename().c_str()) 200858fbb2eSGeorge Liu .c_str()); 201fef66a95SDhruvaraj Subhashchandran return; 202fef66a95SDhruvaraj Subhashchandran } 203fef66a95SDhruvaraj Subhashchandran } 204fef66a95SDhruvaraj Subhashchandran 205fef66a95SDhruvaraj Subhashchandran void Manager::watchCallback(const UserMap& fileInfo) 206fef66a95SDhruvaraj Subhashchandran { 207fef66a95SDhruvaraj Subhashchandran for (const auto& i : fileInfo) 208fef66a95SDhruvaraj Subhashchandran { 209fef66a95SDhruvaraj Subhashchandran // For any new dump file create dump entry object 210fef66a95SDhruvaraj Subhashchandran // and associated inotify watch. 211fef66a95SDhruvaraj Subhashchandran if (IN_CLOSE_WRITE == i.second) 212fef66a95SDhruvaraj Subhashchandran { 213fef66a95SDhruvaraj Subhashchandran removeWatch(i.first); 214fef66a95SDhruvaraj Subhashchandran 215fef66a95SDhruvaraj Subhashchandran createEntry(i.first); 216fef66a95SDhruvaraj Subhashchandran } 217fef66a95SDhruvaraj Subhashchandran // Start inotify watch on newly created directory. 2183fc6df48SJayanth Othayoth else if ((IN_CREATE == i.second) && 2193fc6df48SJayanth Othayoth std::filesystem::is_directory(i.first)) 220fef66a95SDhruvaraj Subhashchandran { 221fef66a95SDhruvaraj Subhashchandran auto watchObj = std::make_unique<Watch>( 222fef66a95SDhruvaraj Subhashchandran eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first, 223fef66a95SDhruvaraj Subhashchandran std::bind( 224fef66a95SDhruvaraj Subhashchandran std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback), 225fef66a95SDhruvaraj Subhashchandran this, std::placeholders::_1)); 226fef66a95SDhruvaraj Subhashchandran 227fef66a95SDhruvaraj Subhashchandran childWatchMap.emplace(i.first, std::move(watchObj)); 228fef66a95SDhruvaraj Subhashchandran } 229fef66a95SDhruvaraj Subhashchandran } 230fef66a95SDhruvaraj Subhashchandran } 231fef66a95SDhruvaraj Subhashchandran 2323fc6df48SJayanth Othayoth void Manager::removeWatch(const std::filesystem::path& path) 233fef66a95SDhruvaraj Subhashchandran { 234fef66a95SDhruvaraj Subhashchandran // Delete Watch entry from map. 235fef66a95SDhruvaraj Subhashchandran childWatchMap.erase(path); 236fef66a95SDhruvaraj Subhashchandran } 237fef66a95SDhruvaraj Subhashchandran 238fef66a95SDhruvaraj Subhashchandran void Manager::restore() 239fef66a95SDhruvaraj Subhashchandran { 2403fc6df48SJayanth Othayoth std::filesystem::path dir(dumpDir); 2413fc6df48SJayanth Othayoth if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir)) 242fef66a95SDhruvaraj Subhashchandran { 243fef66a95SDhruvaraj Subhashchandran return; 244fef66a95SDhruvaraj Subhashchandran } 245fef66a95SDhruvaraj Subhashchandran 246fef66a95SDhruvaraj Subhashchandran // Dump file path: <DUMP_PATH>/<id>/<filename> 2473fc6df48SJayanth Othayoth for (const auto& p : std::filesystem::directory_iterator(dir)) 248fef66a95SDhruvaraj Subhashchandran { 249fef66a95SDhruvaraj Subhashchandran auto idStr = p.path().filename().string(); 250fef66a95SDhruvaraj Subhashchandran 251fef66a95SDhruvaraj Subhashchandran // Consider only directory's with dump id as name. 252fef66a95SDhruvaraj Subhashchandran // Note: As per design one file per directory. 2533fc6df48SJayanth Othayoth if ((std::filesystem::is_directory(p.path())) && 254fef66a95SDhruvaraj Subhashchandran std::all_of(idStr.begin(), idStr.end(), ::isdigit)) 255fef66a95SDhruvaraj Subhashchandran { 256fef66a95SDhruvaraj Subhashchandran lastEntryId = 257fef66a95SDhruvaraj Subhashchandran std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr))); 2583fc6df48SJayanth Othayoth auto fileIt = std::filesystem::directory_iterator(p.path()); 259fef66a95SDhruvaraj Subhashchandran // Create dump entry d-bus object. 2603fc6df48SJayanth Othayoth if (fileIt != std::filesystem::end(fileIt)) 261fef66a95SDhruvaraj Subhashchandran { 262fef66a95SDhruvaraj Subhashchandran createEntry(fileIt->path()); 263fef66a95SDhruvaraj Subhashchandran } 264fef66a95SDhruvaraj Subhashchandran } 265fef66a95SDhruvaraj Subhashchandran } 266fef66a95SDhruvaraj Subhashchandran } 267fef66a95SDhruvaraj Subhashchandran 268fc69f35cSXie Ning size_t getDirectorySize(const std::string dir) 269fef66a95SDhruvaraj Subhashchandran { 270fef66a95SDhruvaraj Subhashchandran auto size = 0; 271fc69f35cSXie Ning for (const auto& p : std::filesystem::recursive_directory_iterator(dir)) 272fef66a95SDhruvaraj Subhashchandran { 2733fc6df48SJayanth Othayoth if (!std::filesystem::is_directory(p)) 274fef66a95SDhruvaraj Subhashchandran { 275bb9366d0STim Lee size += std::ceil(std::filesystem::file_size(p) / 1024.0); 276fef66a95SDhruvaraj Subhashchandran } 277fef66a95SDhruvaraj Subhashchandran } 278fc69f35cSXie Ning return size; 279fc69f35cSXie Ning } 280fc69f35cSXie Ning 281fc69f35cSXie Ning size_t Manager::getAllowedSize() 282fc69f35cSXie Ning { 283fc69f35cSXie Ning // Get current size of the dump directory. 284fc69f35cSXie Ning auto size = getDirectorySize(dumpDir); 285fef66a95SDhruvaraj Subhashchandran 286fef66a95SDhruvaraj Subhashchandran // Set the Dump size to Maximum if the free space is greater than 287fef66a95SDhruvaraj Subhashchandran // Dump max size otherwise return the available size. 288fef66a95SDhruvaraj Subhashchandran 289fef66a95SDhruvaraj Subhashchandran size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size); 290fef66a95SDhruvaraj Subhashchandran 291fc69f35cSXie Ning #ifdef BMC_DUMP_ROTATE_CONFIG 292fc69f35cSXie Ning // Delete the first existing file until the space is enough 293fc69f35cSXie Ning while (size < BMC_DUMP_MIN_SPACE_REQD) 294fc69f35cSXie Ning { 295fc69f35cSXie Ning auto delEntry = min_element( 296fc69f35cSXie Ning entries.begin(), entries.end(), 297fc69f35cSXie Ning [](const auto& l, const auto& r) { return l.first < r.first; }); 298fc69f35cSXie Ning auto delPath = 299fc69f35cSXie Ning std::filesystem::path(dumpDir) / std::to_string(delEntry->first); 300fc69f35cSXie Ning 301fc69f35cSXie Ning size += getDirectorySize(delPath); 302fc69f35cSXie Ning 303fc69f35cSXie Ning delEntry->second->delete_(); 304fc69f35cSXie Ning } 305fc69f35cSXie Ning #else 306fc69f35cSXie Ning using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error; 307fc69f35cSXie Ning using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON; 308fc69f35cSXie Ning 309fef66a95SDhruvaraj Subhashchandran if (size < BMC_DUMP_MIN_SPACE_REQD) 310fef66a95SDhruvaraj Subhashchandran { 311fef66a95SDhruvaraj Subhashchandran // Reached to maximum limit 312fef66a95SDhruvaraj Subhashchandran elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps")); 313fef66a95SDhruvaraj Subhashchandran } 314fc69f35cSXie Ning #endif 315fc69f35cSXie Ning 316fef66a95SDhruvaraj Subhashchandran if (size > BMC_DUMP_MAX_SIZE) 317fef66a95SDhruvaraj Subhashchandran { 318fef66a95SDhruvaraj Subhashchandran size = BMC_DUMP_MAX_SIZE; 319fef66a95SDhruvaraj Subhashchandran } 320fef66a95SDhruvaraj Subhashchandran 321fef66a95SDhruvaraj Subhashchandran return size; 322fef66a95SDhruvaraj Subhashchandran } 323fef66a95SDhruvaraj Subhashchandran 324fef66a95SDhruvaraj Subhashchandran } // namespace bmc 325fef66a95SDhruvaraj Subhashchandran } // namespace dump 326fef66a95SDhruvaraj Subhashchandran } // namespace phosphor 327