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 Capture BMC Dump based on the Dump type.
117      *  @param[in] type - Type of the dump to pass to dreport
118      *  @param[in] path - An absolute path to the file
119      *             to be included as part of Dump package.
120      *  @return id - The Dump entry id number.
121      */
122     uint32_t captureDump(DumpTypes type, const std::string& path);
123 
124     /** @brief Remove specified watch object pointer from the
125      *        watch map and associated entry from the map.
126      *        @param[in] path - unique identifier of the map
127      */
128     void removeWatch(const std::filesystem::path& path);
129 
130     /** @brief Calculate per dump allowed size based on the available
131      *        size in the dump location.
132      *  @returns dump size in kilobytes.
133      */
134     size_t getAllowedSize();
135 
136     /** @brief sdbusplus Dump event loop */
137     EventPtr eventLoop;
138 
139     /** @brief Dump main watch object */
140     Watch dumpWatch;
141 
142     /** @brief Path to the dump file*/
143     std::string dumpDir;
144 
145     /** @brief Flag to reject user intiated dump if a dump is in progress*/
146     // TODO: https://github.com/openbmc/phosphor-debug-collector/issues/19
147     static bool fUserDumpInProgress;
148 
149     /** @brief Child directory path and its associated watch object map
150      *        [path:watch object]
151      */
152     std::map<std::filesystem::path, std::unique_ptr<Watch>> childWatchMap;
153 
154     /** @brief map of SDEventPlus child pointer added to event loop */
155     std::map<pid_t, std::unique_ptr<Child>> childPtrMap;
156 };
157 
158 } // namespace bmc
159 } // namespace dump
160 } // namespace phosphor
161