blob: b30489bdfa31404a73f7cd82e9634dc12461f38d [file] [log] [blame]
Jayanth Othayoth224882b2017-05-04 05:46:45 -05001#pragma once
2
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -05003#include "xyz/openbmc_project/Common/OriginatedBy/server.hpp"
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -05004#include "xyz/openbmc_project/Common/Progress/server.hpp"
Jayanth Othayoth224882b2017-05-04 05:46:45 -05005#include "xyz/openbmc_project/Dump/Entry/server.hpp"
6#include "xyz/openbmc_project/Object/Delete/server.hpp"
7#include "xyz/openbmc_project/Time/EpochTime/server.hpp"
8
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -05009#include <phosphor-logging/lg2.hpp>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050010#include <sdbusplus/bus.hpp>
11#include <sdbusplus/server/object.hpp>
Dhruvaraj Subhashchandran64f8da92021-12-08 03:48:23 -060012#include <sdeventplus/event.hpp>
13#include <sdeventplus/source/event.hpp>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050014
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050015#include <filesystem>
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -050016#include <fstream>
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050017
Jayanth Othayoth224882b2017-05-04 05:46:45 -050018namespace phosphor
19{
20namespace dump
21{
22
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -050023// Current serialiation version of the class increment if there any change
24// in the serialized data members
25constexpr size_t CLASS_SERIALIZATION_VERSION = 1;
26
27// Folder to store serialized dump contents
28constexpr auto PRESERVE = ".preserve";
29
30// Binary file store the contents
31constexpr auto SERIAL_FILE = "serialized_entry.json";
32
Jayanth Othayoth224882b2017-05-04 05:46:45 -050033template <typename T>
Patrick Williams9b18bf22022-07-22 19:26:55 -050034using ServerObject = typename sdbusplus::server::object_t<T>;
Jayanth Othayoth224882b2017-05-04 05:46:45 -050035
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050036// TODO Revisit whether sdbusplus::xyz::openbmc_project::Time::server::EpochTime
37// still needed in dump entry since start time and completed time are available
38// from sdbusplus::xyz::openbmc_project::Common::server::Progress
39// #ibm-openbmc/2809
Patrick Williams9b18bf22022-07-22 19:26:55 -050040using EntryIfaces = sdbusplus::server::object_t<
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050041 sdbusplus::xyz::openbmc_project::Common::server::OriginatedBy,
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050042 sdbusplus::xyz::openbmc_project::Common::server::Progress,
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050043 sdbusplus::xyz::openbmc_project::Dump::server::Entry,
44 sdbusplus::xyz::openbmc_project::Object::server::Delete,
45 sdbusplus::xyz::openbmc_project::Time::server::EpochTime>;
Jayanth Othayoth224882b2017-05-04 05:46:45 -050046
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050047using OperationStatus =
48 sdbusplus::xyz::openbmc_project::Common::server::Progress::OperationStatus;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050049
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050050using originatorTypes = sdbusplus::xyz::openbmc_project::Common::server::
51 OriginatedBy::OriginatorTypes;
52
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050053class Manager;
54
Jayanth Othayoth224882b2017-05-04 05:46:45 -050055/** @class Entry
Dhruvaraj Subhashchandranf140f662020-01-30 00:29:01 -060056 * @brief Base Dump Entry implementation.
Jayanth Othayoth224882b2017-05-04 05:46:45 -050057 * @details A concrete implementation for the
58 * xyz.openbmc_project.Dump.Entry DBus API
59 */
60class Entry : public EntryIfaces
61{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050062 public:
63 Entry() = delete;
64 Entry(const Entry&) = delete;
65 Entry& operator=(const Entry&) = delete;
66 Entry(Entry&&) = delete;
67 Entry& operator=(Entry&&) = delete;
68 ~Entry() = default;
Jayanth Othayoth224882b2017-05-04 05:46:45 -050069
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050070 /** @brief Constructor for the Dump Entry Object
71 * @param[in] bus - Bus to attach to.
72 * @param[in] objPath - Object path to attach to
73 * @param[in] dumpId - Dump id.
74 * @param[in] timeStamp - Dump creation timestamp
75 * since the epoch.
Dhruvaraj Subhashchandran4a98e8f2020-01-29 07:11:08 -060076 * @param[in] dumpSize - Dump file size in bytes.
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050077 * @param[in] originId - Id of the originator of the dump
78 * @param[in] originType - Originator type
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050079 * @param[in] parent - The dump entry's parent.
80 */
Patrick Williams9b18bf22022-07-22 19:26:55 -050081 Entry(sdbusplus::bus_t& bus, const std::string& objPath, uint32_t dumpId,
Dhruvaraj Subhashchandran64f8da92021-12-08 03:48:23 -060082 uint64_t timeStamp, uint64_t dumpSize,
83 const std::filesystem::path& file, OperationStatus dumpStatus,
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050084 std::string originId, originatorTypes originType, Manager& parent) :
Patrick Williams73f64072022-04-01 17:04:47 -050085 EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::emit_no_signals),
Dhruvaraj Subhashchandran64f8da92021-12-08 03:48:23 -060086 parent(parent), id(dumpId), file(file)
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050087 {
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050088 originatorId(originId);
89 originatorType(originType);
90
Dhruvaraj Subhashchandran4a98e8f2020-01-29 07:11:08 -060091 size(dumpSize);
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050092 status(dumpStatus);
93
94 // If the object is created after the dump creation keep
95 // all same as timeStamp
96 // if the object created before the dump creation, update
97 // only the start time. Completed and elapsed time will
98 // be updated once the dump is completed.
99 if (dumpStatus == OperationStatus::Completed)
100 {
101 elapsed(timeStamp);
102 startTime(timeStamp);
103 completedTime(timeStamp);
104 }
105 else
106 {
107 elapsed(0);
108 startTime(timeStamp);
109 completedTime(0);
110 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500111 };
Jayanth Othayoth224882b2017-05-04 05:46:45 -0500112
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500113 /** @brief Delete this d-bus object.
114 */
115 void delete_() override;
Jayanth Othayoth224882b2017-05-04 05:46:45 -0500116
Dhruvaraj Subhashchandran4a98e8f2020-01-29 07:11:08 -0600117 /** @brief Method to initiate the offload of dump
Dhruvaraj Subhashchandran69e61522020-02-04 06:39:11 -0600118 * @param[in] uri - URI to offload dump
Dhruvaraj Subhashchandran4a98e8f2020-01-29 07:11:08 -0600119 */
Dhruvaraj Subhashchandran69e61522020-02-04 06:39:11 -0600120 void initiateOffload(std::string uri) override
Dhruvaraj Subhashchandran4a98e8f2020-01-29 07:11:08 -0600121 {
Dhruvaraj Subhashchandran69e61522020-02-04 06:39:11 -0600122 offloadUri(uri);
Dhruvaraj Subhashchandran4a98e8f2020-01-29 07:11:08 -0600123 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500124
Dhruvaraj Subhashchandran270355b2022-02-01 02:44:43 -0600125 /** @brief Returns the dump id
126 * @return the id associated with entry
127 */
128 uint32_t getDumpId()
129 {
130 return id;
131 }
132
Dhruvaraj Subhashchandran64f8da92021-12-08 03:48:23 -0600133 /** @brief Method to get the file handle of the dump
134 * @returns A Unix file descriptor to the dump file
135 * @throws sdbusplus::xyz::openbmc_project::Common::File::Error::Open on
136 * failure to open the file
137 * @throws sdbusplus::xyz::openbmc_project::Common::Error::Unavailable if
138 * the file string is empty
139 */
140 sdbusplus::message::unix_fd getFileHandle() override;
141
Dhruvaraj Subhashchandran93f06412024-06-02 05:16:51 -0500142 /**
143 * @brief Serialize the dump entry attributes to a file.
144 *
145 */
146 virtual void serialize();
147
148 /**
149 * @brief Deserialize the dump entry attributes from a file.
150 *
151 * @param[in] dumpPath - The path where the .preserve folder is located.
152 */
153 virtual void deserialize(const std::filesystem::path& dumpPath);
154
Dhruvaraj Subhashchandran4a98e8f2020-01-29 07:11:08 -0600155 protected:
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500156 /** @brief This entry's parent */
157 Manager& parent;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500158
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500159 /** @brief This entry's id */
160 uint32_t id;
Dhruvaraj Subhashchandran64f8da92021-12-08 03:48:23 -0600161
162 /** @Dump file name */
163 std::filesystem::path file;
164
165 private:
166 /** @brief Closes the file descriptor and removes the corresponding event
167 * source.
168 *
169 */
170 void closeFD()
171 {
172 if (fdCloseEventSource)
173 {
174 close(fdCloseEventSource->first);
175 fdCloseEventSource.reset();
176 }
177 }
178
179 /* @brief A pair of file descriptor and corresponding event source. */
180 std::optional<std::pair<int, std::unique_ptr<sdeventplus::source::Defer>>>
181 fdCloseEventSource;
Jayanth Othayoth224882b2017-05-04 05:46:45 -0500182};
183
184} // namespace dump
185} // namespace phosphor