Dhruvaraj Subhashchandran | 830a6d4 | 2021-05-05 15:47:57 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "dump_manager.hpp" |
| 4 | #include "dump_utils.hpp" |
| 5 | #include "watch.hpp" |
| 6 | |
| 7 | #include <sdeventplus/source/child.hpp> |
| 8 | |
| 9 | #include <filesystem> |
| 10 | #include <map> |
Dhruvaraj Subhashchandran | 830a6d4 | 2021-05-05 15:47:57 -0500 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace dump |
| 14 | { |
| 15 | namespace bmc_stored |
| 16 | { |
Dhruvaraj Subhashchandran | 4a95984 | 2021-09-30 02:55:52 -0500 | [diff] [blame] | 17 | using namespace phosphor::logging; |
Dhruvaraj Subhashchandran | 830a6d4 | 2021-05-05 15:47:57 -0500 | [diff] [blame] | 18 | using UserMap = phosphor::dump::inotify::UserMap; |
| 19 | |
| 20 | using Watch = phosphor::dump::inotify::Watch; |
| 21 | using ::sdeventplus::source::Child; |
| 22 | |
| 23 | /** @class Manager |
| 24 | * @brief Manager base class for locally stored dumps. |
| 25 | */ |
| 26 | class Manager : public phosphor::dump::Manager |
| 27 | { |
| 28 | public: |
| 29 | Manager() = delete; |
| 30 | Manager(const Manager&) = default; |
| 31 | Manager& operator=(const Manager&) = delete; |
| 32 | Manager(Manager&&) = delete; |
| 33 | Manager& operator=(Manager&&) = delete; |
| 34 | virtual ~Manager() = default; |
| 35 | |
| 36 | /** @brief Constructor to put object onto bus at a dbus path. |
| 37 | * @param[in] bus - Bus to attach to. |
| 38 | * @param[in] event - Dump manager sd_event loop. |
| 39 | * @param[in] path - Path to attach at. |
| 40 | * @param[in] baseEntryPath - Base path for dump entry. |
| 41 | * @param[in] filePath - Path where the dumps are stored. |
| 42 | * @param[in] dumpFilenameFormat - Format of dump filename in regex |
| 43 | * @param[in] maxDumpSize - Maximum size of the dump |
| 44 | * @param[in] minDumpSize - Minimum possible size of a usable dump. |
| 45 | * @param[in] allocatedSize - Total size allocated for the dumps |
| 46 | */ |
| 47 | Manager(sdbusplus::bus::bus& bus, const EventPtr& event, const char* path, |
| 48 | const std::string& baseEntryPath, const char* filePath, |
| 49 | const std::string dumpFilenameFormat, const uint64_t maxDumpSize, |
| 50 | const uint64_t minDumpSize, const uint64_t allocatedSize) : |
| 51 | phosphor::dump::Manager(bus, path, baseEntryPath), |
| 52 | eventLoop(event.get()), dumpDir(filePath), |
| 53 | dumpWatch( |
| 54 | eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE | IN_CREATE, EPOLLIN, |
| 55 | filePath, |
| 56 | std::bind(std::mem_fn( |
| 57 | &phosphor::dump::bmc_stored::Manager::watchCallback), |
| 58 | this, std::placeholders::_1)), |
| 59 | dumpFilenameFormat(dumpFilenameFormat), maxDumpSize(maxDumpSize), |
| 60 | minDumpSize(minDumpSize), allocatedSize(allocatedSize) |
| 61 | {} |
| 62 | |
| 63 | /** @brief Implementation of dump watch call back |
| 64 | * @param [in] fileInfo - map of file info path:event |
| 65 | */ |
| 66 | void watchCallback(const UserMap& fileInfo); |
| 67 | |
| 68 | /** @brief Construct dump d-bus objects from their persisted |
| 69 | * representations. |
| 70 | */ |
| 71 | void restore() override; |
| 72 | |
| 73 | /** @brief sdbusplus Dump event loop */ |
| 74 | EventPtr eventLoop; |
| 75 | |
| 76 | protected: |
| 77 | /** @brief Calculate per dump allowed size based on the available |
| 78 | * size in the dump location. |
| 79 | * @returns dump size in kilobytes. |
| 80 | */ |
| 81 | size_t getAllowedSize(); |
| 82 | |
| 83 | /** @brief Create a Dump Entry Object |
| 84 | * @param[in] id - Id of the dump |
| 85 | * @param[in] objPath - Object path to attach to |
| 86 | * @param[in] timeStamp - Dump creation timestamp |
| 87 | * since the epoch. |
| 88 | * @param[in] fileSize - Dump file size in bytes. |
| 89 | * @param[in] file - Name of dump file. |
| 90 | * @param[in] status - status of the dump. |
| 91 | * @param[in] parent - The dump entry's parent. |
| 92 | * @param[in] originatorId - Id of the originator of the dump |
| 93 | * @param[in] originatorType - Originator type |
| 94 | */ |
| 95 | virtual void createEntry(const uint32_t id, const std::string objPath, |
| 96 | const uint64_t ms, uint64_t fileSize, |
| 97 | const std::filesystem::path& file, |
| 98 | phosphor::dump::OperationStatus status, |
| 99 | std::string originatorId, |
| 100 | originatorTypes originatorType) = 0; |
| 101 | |
| 102 | /** @brief Path to the dump file*/ |
| 103 | std::string dumpDir; |
| 104 | |
| 105 | /** @brief map of SDEventPlus child pointer added to event loop */ |
| 106 | std::map<pid_t, std::unique_ptr<Child>> childPtrMap; |
| 107 | |
| 108 | private: |
| 109 | /** @brief Create Dump entry d-bus object |
| 110 | * @param[in] fullPath - Full path of the Dump file name |
| 111 | */ |
| 112 | void createEntry(const std::filesystem::path& fullPath); |
| 113 | |
| 114 | /** @brief Remove specified watch object pointer from the |
| 115 | * watch map and associated entry from the map. |
| 116 | * @param[in] path - unique identifier of the map |
| 117 | */ |
| 118 | void removeWatch(const std::filesystem::path& path); |
| 119 | |
| 120 | /** @brief Dump main watch object */ |
| 121 | Watch dumpWatch; |
| 122 | |
| 123 | /** @brief Child directory path and its associated watch object map |
| 124 | * [path:watch object] |
| 125 | */ |
| 126 | std::map<std::filesystem::path, std::unique_ptr<Watch>> childWatchMap; |
| 127 | |
| 128 | /** @brief A regex based format for the filename */ |
| 129 | std::string dumpFilenameFormat; |
| 130 | |
| 131 | /** @brief Maximum possible size of a dump */ |
| 132 | uint64_t maxDumpSize; |
| 133 | |
| 134 | /** @brief Minimum viable dump size */ |
| 135 | uint64_t minDumpSize; |
| 136 | |
| 137 | /** @brief The total size allocated for a kind of dump */ |
| 138 | uint64_t allocatedSize; |
| 139 | }; |
| 140 | |
| 141 | } // namespace bmc_stored |
| 142 | } // namespace dump |
| 143 | } // namespace phosphor |