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