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