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 10fef66a95SDhruvaraj Subhashchandran #include <sys/inotify.h> 11fef66a95SDhruvaraj Subhashchandran #include <unistd.h> 12fef66a95SDhruvaraj Subhashchandran 13fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog-errors.hpp> 14fef66a95SDhruvaraj Subhashchandran #include <phosphor-logging/elog.hpp> 150af74a5eSJayanth Othayoth 16*bb9366d0STim Lee #include <cmath> 170af74a5eSJayanth Othayoth #include <ctime> 18fef66a95SDhruvaraj Subhashchandran #include <regex> 19fef66a95SDhruvaraj Subhashchandran 20fef66a95SDhruvaraj Subhashchandran namespace phosphor 21fef66a95SDhruvaraj Subhashchandran { 22fef66a95SDhruvaraj Subhashchandran namespace dump 23fef66a95SDhruvaraj Subhashchandran { 24fef66a95SDhruvaraj Subhashchandran namespace bmc 25fef66a95SDhruvaraj Subhashchandran { 26fef66a95SDhruvaraj Subhashchandran 27fef66a95SDhruvaraj Subhashchandran using namespace sdbusplus::xyz::openbmc_project::Common::Error; 28fef66a95SDhruvaraj Subhashchandran using namespace phosphor::logging; 29fef66a95SDhruvaraj Subhashchandran 30fef66a95SDhruvaraj Subhashchandran namespace internal 31fef66a95SDhruvaraj Subhashchandran { 32fef66a95SDhruvaraj Subhashchandran 33fef66a95SDhruvaraj Subhashchandran void Manager::create(Type type, std::vector<std::string> fullPaths) 34fef66a95SDhruvaraj Subhashchandran { 35fef66a95SDhruvaraj Subhashchandran dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths); 36fef66a95SDhruvaraj Subhashchandran } 37fef66a95SDhruvaraj Subhashchandran 38fef66a95SDhruvaraj Subhashchandran } // namespace internal 39fef66a95SDhruvaraj Subhashchandran 40969f9a59SDhruvaraj Subhashchandran sdbusplus::message::object_path 41969f9a59SDhruvaraj Subhashchandran Manager::createDump(std::map<std::string, std::string> params) 42fef66a95SDhruvaraj Subhashchandran { 43969f9a59SDhruvaraj Subhashchandran if (!params.empty()) 44969f9a59SDhruvaraj Subhashchandran { 45969f9a59SDhruvaraj Subhashchandran log<level::WARNING>("BMC dump accepts no additional parameters"); 46969f9a59SDhruvaraj Subhashchandran } 47fef66a95SDhruvaraj Subhashchandran std::vector<std::string> paths; 486ccb50e1SDhruvaraj Subhashchandran auto id = captureDump(Type::UserRequested, paths); 496ccb50e1SDhruvaraj Subhashchandran 506ccb50e1SDhruvaraj Subhashchandran // Entry Object path. 513fc6df48SJayanth Othayoth auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); 526ccb50e1SDhruvaraj Subhashchandran 536ccb50e1SDhruvaraj Subhashchandran try 546ccb50e1SDhruvaraj Subhashchandran { 55a6ab806dSDhruvaraj Subhashchandran std::time_t timeStamp = std::time(nullptr); 566ccb50e1SDhruvaraj Subhashchandran entries.insert(std::make_pair( 57a6ab806dSDhruvaraj Subhashchandran id, std::make_unique<bmc::Entry>( 58a6ab806dSDhruvaraj Subhashchandran bus, objPath.c_str(), id, timeStamp, 0, std::string(), 59a6ab806dSDhruvaraj Subhashchandran phosphor::dump::OperationStatus::InProgress, *this))); 606ccb50e1SDhruvaraj Subhashchandran } 616ccb50e1SDhruvaraj Subhashchandran catch (const std::invalid_argument& e) 626ccb50e1SDhruvaraj Subhashchandran { 636ccb50e1SDhruvaraj Subhashchandran log<level::ERR>(e.what()); 646ccb50e1SDhruvaraj Subhashchandran log<level::ERR>("Error in creating dump entry", 656ccb50e1SDhruvaraj Subhashchandran entry("OBJECTPATH=%s", objPath.c_str()), 666ccb50e1SDhruvaraj Subhashchandran entry("ID=%d", id)); 676ccb50e1SDhruvaraj Subhashchandran elog<InternalFailure>(); 686ccb50e1SDhruvaraj Subhashchandran } 696ccb50e1SDhruvaraj Subhashchandran 706ccb50e1SDhruvaraj Subhashchandran return objPath.string(); 71fef66a95SDhruvaraj Subhashchandran } 72fef66a95SDhruvaraj Subhashchandran 73fef66a95SDhruvaraj Subhashchandran uint32_t Manager::captureDump(Type type, 74fef66a95SDhruvaraj Subhashchandran const std::vector<std::string>& fullPaths) 75fef66a95SDhruvaraj Subhashchandran { 76fef66a95SDhruvaraj Subhashchandran // Get Dump size. 77fef66a95SDhruvaraj Subhashchandran auto size = getAllowedSize(); 78fef66a95SDhruvaraj Subhashchandran 79fef66a95SDhruvaraj Subhashchandran pid_t pid = fork(); 80fef66a95SDhruvaraj Subhashchandran 81fef66a95SDhruvaraj Subhashchandran if (pid == 0) 82fef66a95SDhruvaraj Subhashchandran { 833fc6df48SJayanth Othayoth std::filesystem::path dumpPath(dumpDir); 84fef66a95SDhruvaraj Subhashchandran auto id = std::to_string(lastEntryId + 1); 85fef66a95SDhruvaraj Subhashchandran dumpPath /= id; 86fef66a95SDhruvaraj Subhashchandran 87fef66a95SDhruvaraj Subhashchandran // get dreport type map entry 88fef66a95SDhruvaraj Subhashchandran auto tempType = TypeMap.find(type); 89fef66a95SDhruvaraj Subhashchandran 90fef66a95SDhruvaraj Subhashchandran execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i", 91fef66a95SDhruvaraj Subhashchandran id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p", 92fef66a95SDhruvaraj Subhashchandran fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t", 93fef66a95SDhruvaraj Subhashchandran tempType->second.c_str(), nullptr); 94fef66a95SDhruvaraj Subhashchandran 95fef66a95SDhruvaraj Subhashchandran // dreport script execution is failed. 96fef66a95SDhruvaraj Subhashchandran auto error = errno; 97fef66a95SDhruvaraj Subhashchandran log<level::ERR>("Error occurred during dreport function execution", 98fef66a95SDhruvaraj Subhashchandran entry("ERRNO=%d", error)); 99fef66a95SDhruvaraj Subhashchandran elog<InternalFailure>(); 100fef66a95SDhruvaraj Subhashchandran } 101fef66a95SDhruvaraj Subhashchandran else if (pid > 0) 102fef66a95SDhruvaraj Subhashchandran { 103fef66a95SDhruvaraj Subhashchandran auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid, 104fef66a95SDhruvaraj Subhashchandran WEXITED | WSTOPPED, callback, nullptr); 105fef66a95SDhruvaraj Subhashchandran if (0 > rc) 106fef66a95SDhruvaraj Subhashchandran { 107fef66a95SDhruvaraj Subhashchandran // Failed to add to event loop 108fef66a95SDhruvaraj Subhashchandran log<level::ERR>("Error occurred during the sd_event_add_child call", 109fef66a95SDhruvaraj Subhashchandran entry("RC=%d", rc)); 110fef66a95SDhruvaraj Subhashchandran elog<InternalFailure>(); 111fef66a95SDhruvaraj Subhashchandran } 112fef66a95SDhruvaraj Subhashchandran } 113fef66a95SDhruvaraj Subhashchandran else 114fef66a95SDhruvaraj Subhashchandran { 115fef66a95SDhruvaraj Subhashchandran auto error = errno; 116fef66a95SDhruvaraj Subhashchandran log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); 117fef66a95SDhruvaraj Subhashchandran elog<InternalFailure>(); 118fef66a95SDhruvaraj Subhashchandran } 119fef66a95SDhruvaraj Subhashchandran 120fef66a95SDhruvaraj Subhashchandran return ++lastEntryId; 121fef66a95SDhruvaraj Subhashchandran } 122fef66a95SDhruvaraj Subhashchandran 1233fc6df48SJayanth Othayoth void Manager::createEntry(const std::filesystem::path& file) 124fef66a95SDhruvaraj Subhashchandran { 125fef66a95SDhruvaraj Subhashchandran // Dump File Name format obmcdump_ID_EPOCHTIME.EXT 126fef66a95SDhruvaraj Subhashchandran static constexpr auto ID_POS = 1; 127fef66a95SDhruvaraj Subhashchandran static constexpr auto EPOCHTIME_POS = 2; 128fef66a95SDhruvaraj Subhashchandran std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)"); 129fef66a95SDhruvaraj Subhashchandran 130fef66a95SDhruvaraj Subhashchandran std::smatch match; 131fef66a95SDhruvaraj Subhashchandran std::string name = file.filename(); 132fef66a95SDhruvaraj Subhashchandran 133fef66a95SDhruvaraj Subhashchandran if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0))) 134fef66a95SDhruvaraj Subhashchandran { 135fef66a95SDhruvaraj Subhashchandran log<level::ERR>("Invalid Dump file name", 136fef66a95SDhruvaraj Subhashchandran entry("FILENAME=%s", file.filename().c_str())); 137fef66a95SDhruvaraj Subhashchandran return; 138fef66a95SDhruvaraj Subhashchandran } 139fef66a95SDhruvaraj Subhashchandran 140fef66a95SDhruvaraj Subhashchandran auto idString = match[ID_POS]; 141fef66a95SDhruvaraj Subhashchandran auto msString = match[EPOCHTIME_POS]; 142fef66a95SDhruvaraj Subhashchandran 143fef66a95SDhruvaraj Subhashchandran auto id = stoul(idString); 1446ccb50e1SDhruvaraj Subhashchandran 1456ccb50e1SDhruvaraj Subhashchandran // If there is an existing entry update it and return. 1466ccb50e1SDhruvaraj Subhashchandran auto dumpEntry = entries.find(id); 1476ccb50e1SDhruvaraj Subhashchandran if (dumpEntry != entries.end()) 1486ccb50e1SDhruvaraj Subhashchandran { 1496ccb50e1SDhruvaraj Subhashchandran dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get()) 1503fc6df48SJayanth Othayoth ->update(stoull(msString), std::filesystem::file_size(file), file); 1516ccb50e1SDhruvaraj Subhashchandran return; 1526ccb50e1SDhruvaraj Subhashchandran } 1536ccb50e1SDhruvaraj Subhashchandran 154fef66a95SDhruvaraj Subhashchandran // Entry Object path. 1553fc6df48SJayanth Othayoth auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id); 156fef66a95SDhruvaraj Subhashchandran 1576ccb50e1SDhruvaraj Subhashchandran try 1586ccb50e1SDhruvaraj Subhashchandran { 159a6ab806dSDhruvaraj Subhashchandran entries.insert(std::make_pair( 1603fc6df48SJayanth Othayoth id, std::make_unique<bmc::Entry>( 1613fc6df48SJayanth Othayoth bus, objPath.c_str(), id, stoull(msString), 1623fc6df48SJayanth Othayoth std::filesystem::file_size(file), file, 1633fc6df48SJayanth Othayoth phosphor::dump::OperationStatus::Completed, *this))); 164fef66a95SDhruvaraj Subhashchandran } 165fef66a95SDhruvaraj Subhashchandran catch (const std::invalid_argument& e) 166fef66a95SDhruvaraj Subhashchandran { 167fef66a95SDhruvaraj Subhashchandran log<level::ERR>(e.what()); 1686ccb50e1SDhruvaraj Subhashchandran log<level::ERR>("Error in creating dump entry", 1696ccb50e1SDhruvaraj Subhashchandran entry("OBJECTPATH=%s", objPath.c_str()), 1706ccb50e1SDhruvaraj Subhashchandran entry("ID=%d", id), 1716ccb50e1SDhruvaraj Subhashchandran entry("TIMESTAMP=%ull", stoull(msString)), 1723fc6df48SJayanth Othayoth entry("SIZE=%d", std::filesystem::file_size(file)), 1736ccb50e1SDhruvaraj Subhashchandran entry("FILENAME=%s", file.c_str())); 174fef66a95SDhruvaraj Subhashchandran return; 175fef66a95SDhruvaraj Subhashchandran } 176fef66a95SDhruvaraj Subhashchandran } 177fef66a95SDhruvaraj Subhashchandran 178fef66a95SDhruvaraj Subhashchandran void Manager::watchCallback(const UserMap& fileInfo) 179fef66a95SDhruvaraj Subhashchandran { 180fef66a95SDhruvaraj Subhashchandran for (const auto& i : fileInfo) 181fef66a95SDhruvaraj Subhashchandran { 182fef66a95SDhruvaraj Subhashchandran // For any new dump file create dump entry object 183fef66a95SDhruvaraj Subhashchandran // and associated inotify watch. 184fef66a95SDhruvaraj Subhashchandran if (IN_CLOSE_WRITE == i.second) 185fef66a95SDhruvaraj Subhashchandran { 186fef66a95SDhruvaraj Subhashchandran removeWatch(i.first); 187fef66a95SDhruvaraj Subhashchandran 188fef66a95SDhruvaraj Subhashchandran createEntry(i.first); 189fef66a95SDhruvaraj Subhashchandran } 190fef66a95SDhruvaraj Subhashchandran // Start inotify watch on newly created directory. 1913fc6df48SJayanth Othayoth else if ((IN_CREATE == i.second) && 1923fc6df48SJayanth Othayoth std::filesystem::is_directory(i.first)) 193fef66a95SDhruvaraj Subhashchandran { 194fef66a95SDhruvaraj Subhashchandran auto watchObj = std::make_unique<Watch>( 195fef66a95SDhruvaraj Subhashchandran eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first, 196fef66a95SDhruvaraj Subhashchandran std::bind( 197fef66a95SDhruvaraj Subhashchandran std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback), 198fef66a95SDhruvaraj Subhashchandran this, std::placeholders::_1)); 199fef66a95SDhruvaraj Subhashchandran 200fef66a95SDhruvaraj Subhashchandran childWatchMap.emplace(i.first, std::move(watchObj)); 201fef66a95SDhruvaraj Subhashchandran } 202fef66a95SDhruvaraj Subhashchandran } 203fef66a95SDhruvaraj Subhashchandran } 204fef66a95SDhruvaraj Subhashchandran 2053fc6df48SJayanth Othayoth void Manager::removeWatch(const std::filesystem::path& path) 206fef66a95SDhruvaraj Subhashchandran { 207fef66a95SDhruvaraj Subhashchandran // Delete Watch entry from map. 208fef66a95SDhruvaraj Subhashchandran childWatchMap.erase(path); 209fef66a95SDhruvaraj Subhashchandran } 210fef66a95SDhruvaraj Subhashchandran 211fef66a95SDhruvaraj Subhashchandran void Manager::restore() 212fef66a95SDhruvaraj Subhashchandran { 2133fc6df48SJayanth Othayoth std::filesystem::path dir(dumpDir); 2143fc6df48SJayanth Othayoth if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir)) 215fef66a95SDhruvaraj Subhashchandran { 216fef66a95SDhruvaraj Subhashchandran return; 217fef66a95SDhruvaraj Subhashchandran } 218fef66a95SDhruvaraj Subhashchandran 219fef66a95SDhruvaraj Subhashchandran // Dump file path: <DUMP_PATH>/<id>/<filename> 2203fc6df48SJayanth Othayoth for (const auto& p : std::filesystem::directory_iterator(dir)) 221fef66a95SDhruvaraj Subhashchandran { 222fef66a95SDhruvaraj Subhashchandran auto idStr = p.path().filename().string(); 223fef66a95SDhruvaraj Subhashchandran 224fef66a95SDhruvaraj Subhashchandran // Consider only directory's with dump id as name. 225fef66a95SDhruvaraj Subhashchandran // Note: As per design one file per directory. 2263fc6df48SJayanth Othayoth if ((std::filesystem::is_directory(p.path())) && 227fef66a95SDhruvaraj Subhashchandran std::all_of(idStr.begin(), idStr.end(), ::isdigit)) 228fef66a95SDhruvaraj Subhashchandran { 229fef66a95SDhruvaraj Subhashchandran lastEntryId = 230fef66a95SDhruvaraj Subhashchandran std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr))); 2313fc6df48SJayanth Othayoth auto fileIt = std::filesystem::directory_iterator(p.path()); 232fef66a95SDhruvaraj Subhashchandran // Create dump entry d-bus object. 2333fc6df48SJayanth Othayoth if (fileIt != std::filesystem::end(fileIt)) 234fef66a95SDhruvaraj Subhashchandran { 235fef66a95SDhruvaraj Subhashchandran createEntry(fileIt->path()); 236fef66a95SDhruvaraj Subhashchandran } 237fef66a95SDhruvaraj Subhashchandran } 238fef66a95SDhruvaraj Subhashchandran } 239fef66a95SDhruvaraj Subhashchandran } 240fef66a95SDhruvaraj Subhashchandran 241fef66a95SDhruvaraj Subhashchandran size_t Manager::getAllowedSize() 242fef66a95SDhruvaraj Subhashchandran { 243fef66a95SDhruvaraj Subhashchandran using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error; 244fef66a95SDhruvaraj Subhashchandran using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON; 245fef66a95SDhruvaraj Subhashchandran 246fef66a95SDhruvaraj Subhashchandran auto size = 0; 247fef66a95SDhruvaraj Subhashchandran 248fef66a95SDhruvaraj Subhashchandran // Get current size of the dump directory. 2493fc6df48SJayanth Othayoth for (const auto& p : std::filesystem::recursive_directory_iterator(dumpDir)) 250fef66a95SDhruvaraj Subhashchandran { 2513fc6df48SJayanth Othayoth if (!std::filesystem::is_directory(p)) 252fef66a95SDhruvaraj Subhashchandran { 253*bb9366d0STim Lee size += std::ceil(std::filesystem::file_size(p) / 1024.0); 254fef66a95SDhruvaraj Subhashchandran } 255fef66a95SDhruvaraj Subhashchandran } 256fef66a95SDhruvaraj Subhashchandran 257fef66a95SDhruvaraj Subhashchandran // Set the Dump size to Maximum if the free space is greater than 258fef66a95SDhruvaraj Subhashchandran // Dump max size otherwise return the available size. 259fef66a95SDhruvaraj Subhashchandran 260fef66a95SDhruvaraj Subhashchandran size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size); 261fef66a95SDhruvaraj Subhashchandran 262fef66a95SDhruvaraj Subhashchandran if (size < BMC_DUMP_MIN_SPACE_REQD) 263fef66a95SDhruvaraj Subhashchandran { 264fef66a95SDhruvaraj Subhashchandran // Reached to maximum limit 265fef66a95SDhruvaraj Subhashchandran elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps")); 266fef66a95SDhruvaraj Subhashchandran } 267fef66a95SDhruvaraj Subhashchandran if (size > BMC_DUMP_MAX_SIZE) 268fef66a95SDhruvaraj Subhashchandran { 269fef66a95SDhruvaraj Subhashchandran size = BMC_DUMP_MAX_SIZE; 270fef66a95SDhruvaraj Subhashchandran } 271fef66a95SDhruvaraj Subhashchandran 272fef66a95SDhruvaraj Subhashchandran return size; 273fef66a95SDhruvaraj Subhashchandran } 274fef66a95SDhruvaraj Subhashchandran 275fef66a95SDhruvaraj Subhashchandran } // namespace bmc 276fef66a95SDhruvaraj Subhashchandran } // namespace dump 277fef66a95SDhruvaraj Subhashchandran } // namespace phosphor 278