1 #pragma once
2 
3 #include "xyz/openbmc_project/Common/OriginatedBy/server.hpp"
4 #include "xyz/openbmc_project/Common/Progress/server.hpp"
5 #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 
9 #include <phosphor-logging/lg2.hpp>
10 #include <sdbusplus/bus.hpp>
11 #include <sdbusplus/server/object.hpp>
12 #include <sdeventplus/event.hpp>
13 #include <sdeventplus/source/event.hpp>
14 
15 #include <filesystem>
16 #include <fstream>
17 
18 namespace phosphor
19 {
20 namespace dump
21 {
22 
23 // Current serialiation version of the class increment if there any change
24 // in the serialized data members
25 constexpr size_t CLASS_SERIALIZATION_VERSION = 1;
26 
27 // Folder to store serialized dump contents
28 constexpr auto PRESERVE = ".preserve";
29 
30 // Binary file store the contents
31 constexpr auto SERIAL_FILE = "serialized_entry.json";
32 
33 template <typename T>
34 using ServerObject = typename sdbusplus::server::object_t<T>;
35 
36 // 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
40 using EntryIfaces = sdbusplus::server::object_t<
41     sdbusplus::xyz::openbmc_project::Common::server::OriginatedBy,
42     sdbusplus::xyz::openbmc_project::Common::server::Progress,
43     sdbusplus::xyz::openbmc_project::Dump::server::Entry,
44     sdbusplus::xyz::openbmc_project::Object::server::Delete,
45     sdbusplus::xyz::openbmc_project::Time::server::EpochTime>;
46 
47 using OperationStatus =
48     sdbusplus::xyz::openbmc_project::Common::server::Progress::OperationStatus;
49 
50 using originatorTypes = sdbusplus::xyz::openbmc_project::Common::server::
51     OriginatedBy::OriginatorTypes;
52 
53 class Manager;
54 
55 /** @class Entry
56  *  @brief Base Dump Entry implementation.
57  *  @details A concrete implementation for the
58  *  xyz.openbmc_project.Dump.Entry DBus API
59  */
60 class Entry : public EntryIfaces
61 {
62   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;
69 
70     /** @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.
76      *  @param[in] dumpSize - Dump file size in bytes.
77      *  @param[in] originId - Id of the originator of the dump
78      *  @param[in] originType - Originator type
79      *  @param[in] parent - The dump entry's parent.
80      */
Entry(sdbusplus::bus_t & bus,const std::string & objPath,uint32_t dumpId,uint64_t timeStamp,uint64_t dumpSize,const std::filesystem::path & file,OperationStatus dumpStatus,std::string originId,originatorTypes originType,Manager & parent)81     Entry(sdbusplus::bus_t& bus, const std::string& objPath, uint32_t dumpId,
82           uint64_t timeStamp, uint64_t dumpSize,
83           const std::filesystem::path& file, OperationStatus dumpStatus,
84           std::string originId, originatorTypes originType, Manager& parent) :
85         EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::emit_no_signals),
86         parent(parent), id(dumpId), file(file)
87     {
88         originatorId(originId);
89         originatorType(originType);
90 
91         size(dumpSize);
92         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         }
111     };
112 
113     /** @brief Delete this d-bus object.
114      */
115     void delete_() override;
116 
117     /** @brief Method to initiate the offload of dump
118      *  @param[in] uri - URI to offload dump
119      */
initiateOffload(std::string uri)120     void initiateOffload(std::string uri) override
121     {
122         offloadUri(uri);
123     }
124 
125     /** @brief Returns the dump id
126      *  @return the id associated with entry
127      */
getDumpId()128     uint32_t getDumpId()
129     {
130         return id;
131     }
132 
133     /** @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 
142     /**
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 
155   protected:
156     /** @brief This entry's parent */
157     Manager& parent;
158 
159     /** @brief This entry's id */
160     uint32_t id;
161 
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      */
closeFD()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;
182 };
183 
184 } // namespace dump
185 } // namespace phosphor
186