blob: 9856f1ef70e7557a229d46754f5c17b1135b9946 [file] [log] [blame]
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05001#pragma once
2
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05003#include <experimental/filesystem>
4
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05005#include <sdbusplus/bus.hpp>
6#include <sdbusplus/server/object.hpp>
7#include <xyz/openbmc_project/Dump/Create/server.hpp>
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05008
9#include "xyz/openbmc_project/Dump/Internal/Create/server.hpp"
10#include "dump_entry.hpp"
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050011#include "dump_utils.hpp"
Jayanth Othayothbcb174b2017-07-02 06:29:24 -050012#include "watch.hpp"
13#include "config.h"
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050014
15namespace phosphor
16{
17namespace dump
18{
19namespace internal
20{
21
22class Manager;
23
24} // namespace internal
25
Jayanth Othayothbcb174b2017-07-02 06:29:24 -050026using UserMap = phosphor::dump::inotify::UserMap;
27
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050028using Type =
29 sdbusplus::xyz::openbmc_project::Dump::Internal::server::Create::Type;
30
31using CreateIface = sdbusplus::server::object::object<
32 sdbusplus::xyz::openbmc_project::Dump::server::Create>;
33
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050034namespace fs = std::experimental::filesystem;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050035
Jayanth Othayoth764d1b22017-07-12 19:14:02 -050036using Watch = phosphor::dump::inotify::Watch;
37
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050038/** @class Manager
39 * @brief OpenBMC Dump manager implementation.
40 * @details A concrete implementation for the
41 * xyz.openbmc_project.Dump.Create DBus API.
42 */
43class Manager : public CreateIface
44{
45 friend class internal::Manager;
46 friend class Entry;
47
48 public:
49 Manager() = delete;
50 Manager(const Manager&) = default;
51 Manager& operator=(const Manager&) = delete;
52 Manager(Manager&&) = delete;
53 Manager& operator=(Manager&&) = delete;
54 virtual ~Manager() = default;
55
56 /** @brief Constructor to put object onto bus at a dbus path.
57 * @param[in] bus - Bus to attach to.
58 * @param[in] event - Dump manager sd_event loop.
59 * @param[in] path - Path to attach at.
60 */
61 Manager(sdbusplus::bus::bus& bus,
62 const EventPtr& event, const char* path) :
63 CreateIface(bus, path),
64 bus(bus),
65 eventLoop(event.get()),
Jayanth Othayothbcb174b2017-07-02 06:29:24 -050066 lastEntryId(0),
Jayanth Othayothab7f9202017-08-02 06:24:58 -050067 activeDumpCount(0),
Jayanth Othayothbcb174b2017-07-02 06:29:24 -050068 dumpWatch(eventLoop,
69 IN_NONBLOCK,
Jayanth Othayothec608a52017-08-01 05:23:35 -050070 IN_CLOSE_WRITE | IN_CREATE,
Jayanth Othayothbcb174b2017-07-02 06:29:24 -050071 EPOLLIN,
Jayanth Othayoth2dccfe42017-07-12 18:20:40 -050072 BMC_DUMP_PATH,
Jayanth Othayothbcb174b2017-07-02 06:29:24 -050073 std::bind(
74 std::mem_fn(
75 &phosphor::dump::Manager::watchCallback),
76 this, std::placeholders::_1))
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050077 {}
78
79 /** @brief Implementation for CreateDump
80 * Method to create Dump.
81 *
82 * @return id - The Dump entry id number.
83 */
84 uint32_t createDump() override;
85
Jayanth Othayothbcb174b2017-07-02 06:29:24 -050086 /** @brief Implementation of dump watch call back
87 * @param [in] fileInfo - map of file info path:event
88 */
89 void watchCallback(const UserMap& fileInfo);
90
Jayanth Othayoth43096592017-07-20 02:17:37 -050091 /** @brief Construct dump d-bus objects from their persisted
92 * representations.
93 */
94 void restore();
95
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050096 private:
97 /** @brief Create Dump entry d-bus object
98 * @param[in] fullPath - Full path of the Dump file name
99 */
100 void createEntry(const fs::path& fullPath);
101
102 /** @brief Capture BMC Dump based on the Dump type.
103 * @param[in] type - Type of the Dump.
104 * @param[in] fullPaths - List of absolute paths to the files
105 * to be included as part of Dump package.
106 * @return id - The Dump entry id number.
107 */
108 uint32_t captureDump(
109 Type type,
110 const std::vector<std::string>& fullPaths);
111
112 /** @brief Erase specified entry d-bus object
113 *
114 * @param[in] entryId - unique identifier of the entry
115 */
116 void erase(uint32_t entryId);
117
118 /** @brief sd_event_add_child callback
119 *
120 * @param[in] s - event source
121 * @param[in] si - signal info
122 * @param[in] userdata - pointer to Watch object
123 *
124 * @returns 0 on success, -1 on fail
125 */
126 static int callback(sd_event_source* s,
127 const siginfo_t* si,
128 void* userdata)
129 {
130 //No specific action required in
131 //the sd_event_add_child callback.
132 return 0;
133 }
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500134 /** @brief Remove specified watch object pointer from the
135 * watch map and associated entry from the map.
136 * @param[in] path - unique identifier of the map
137 */
138 void removeWatch(const fs::path& path);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500139
Jayanth Othayothab7f9202017-08-02 06:24:58 -0500140 /** @brief Calculate per dump allowed size based on number of
141 * existing dumps and total dump size. Returns
142 * allowed size for a dump in kilo bytes.
143 * @returns 0 indicates no space is available in dump location
144 * non zero value indicates size of one dump.
145 */
146 size_t getAllowedSize();
147
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500148 /** @brief sdbusplus DBus bus connection. */
149 sdbusplus::bus::bus& bus;
150
151 /** @brief sdbusplus Dump event loop */
152 EventPtr eventLoop;
153
154 /** @brief Dump Entry dbus objects map based on entry id */
155 std::map<uint32_t, std::unique_ptr<Entry>> entries;
156
157 /** @brief Id of the last Dump entry */
158 uint32_t lastEntryId;
Jayanth Othayothbcb174b2017-07-02 06:29:24 -0500159
Jayanth Othayothab7f9202017-08-02 06:24:58 -0500160 /** @brief active dump count */
161 uint32_t activeDumpCount;
162
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500163 /** @brief Dump main watch object */
164 Watch dumpWatch;
165
166 /** @brief Child directory path and its associated watch object map
167 * [path:watch object]
168 */
169 std::map<fs::path, std::unique_ptr<Watch>> childWatchMap;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500170};
171
172} // namespace dump
173} // namespace phosphor