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 #include <xyz/openbmc_project/Dump/Create/server.hpp>
9 
10 #include <filesystem>
11 #include <map>
12 
13 namespace phosphor
14 {
15 namespace dump
16 {
17 namespace bmc
18 {
19 
20 using CreateIface = sdbusplus::server::object_t<
21     sdbusplus::xyz::openbmc_project::Dump::server::Create>;
22 
23 using UserMap = phosphor::dump::inotify::UserMap;
24 
25 using Watch = phosphor::dump::inotify::Watch;
26 using ::sdeventplus::source::Child;
27 
28 /** @class Manager
29  *  @brief OpenBMC Dump  manager implementation.
30  *  @details A concrete implementation for the
31  *  xyz.openbmc_project.Dump.Create DBus API
32  */
33 class Manager :
34     virtual public CreateIface,
35     virtual public phosphor::dump::Manager
36 {
37   public:
38     Manager() = delete;
39     Manager(const Manager&) = default;
40     Manager& operator=(const Manager&) = delete;
41     Manager(Manager&&) = delete;
42     Manager& operator=(Manager&&) = delete;
43     virtual ~Manager() = default;
44 
45     /** @brief Constructor to put object onto bus at a dbus path.
46      *  @param[in] bus - Bus to attach to.
47      *  @param[in] event - Dump manager sd_event loop.
48      *  @param[in] path - Path to attach at.
49      *  @param[in] baseEntryPath - Base path for dump entry.
50      *  @param[in] filePath - Path where the dumps are stored.
51      */
52     Manager(sdbusplus::bus_t& bus, const EventPtr& event, const char* path,
53             const std::string& baseEntryPath, const char* filePath) :
54         CreateIface(bus, path),
55         phosphor::dump::Manager(bus, path, baseEntryPath),
56         eventLoop(event.get()),
57         dumpWatch(
58             eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE | IN_CREATE, EPOLLIN,
59             filePath,
60             std::bind(std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
61                       this, std::placeholders::_1)),
62         dumpDir(filePath)
63     {}
64 
65     /** @brief Implementation of dump watch call back
66      *  @param [in] fileInfo - map of file info  path:event
67      */
68     void watchCallback(const UserMap& fileInfo);
69 
70     /** @brief Construct dump d-bus objects from their persisted
71      *        representations.
72      */
73     void restore() override;
74 
75     /** @brief Implementation for CreateDump
76      *  Method to create a BMC dump entry when user requests for a new BMC dump
77      *
78      *  @return object_path - The object path of the new dump entry.
79      */
80     sdbusplus::message::object_path
81         createDump(phosphor::dump::DumpCreateParams params) override;
82 
83   private:
84     /** @brief Create Dump entry d-bus object
85      *  @param[in] fullPath - Full path of the Dump file name
86      */
87     void createEntry(const std::filesystem::path& fullPath);
88 
89     /** @brief Capture BMC Dump based on the Dump type.
90      *  @param[in] type - Type of the dump to pass to dreport
91      *  @param[in] path - An absolute path to the file
92      *             to be included as part of Dump package.
93      *  @return id - The Dump entry id number.
94      */
95     uint32_t captureDump(DumpTypes type, const std::string& path);
96 
97     /** @brief Remove specified watch object pointer from the
98      *        watch map and associated entry from the map.
99      *        @param[in] path - unique identifier of the map
100      */
101     void removeWatch(const std::filesystem::path& path);
102 
103     /** @brief Calculate per dump allowed size based on the available
104      *        size in the dump location.
105      *  @returns dump size in kilobytes.
106      */
107     size_t getAllowedSize();
108 
109     /** @brief sdbusplus Dump event loop */
110     EventPtr eventLoop;
111 
112     /** @brief Dump main watch object */
113     Watch dumpWatch;
114 
115     /** @brief Path to the dump file*/
116     std::string dumpDir;
117 
118     /** @brief Flag to reject user intiated dump if a dump is in progress*/
119     // TODO: https://github.com/openbmc/phosphor-debug-collector/issues/19
120     static bool fUserDumpInProgress;
121 
122     /** @brief Child directory path and its associated watch object map
123      *        [path:watch object]
124      */
125     std::map<std::filesystem::path, std::unique_ptr<Watch>> childWatchMap;
126 
127     /** @brief map of SDEventPlus child pointer added to event loop */
128     std::map<pid_t, std::unique_ptr<Child>> childPtrMap;
129 };
130 
131 } // namespace bmc
132 } // namespace dump
133 } // namespace phosphor
134