forked from ibm-openbmc/phosphor-debug-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_utils.hpp
184 lines (164 loc) · 4.59 KB
/
dump_utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#pragma once
#include <fmt/format.h>
#include <systemd/sd-event.h>
#include <unistd.h>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
#include <xyz/openbmc_project/State/Host/server.hpp>
#include <memory>
namespace phosphor
{
namespace dump
{
using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
Progress::ProgressStages;
using HostState =
sdbusplus::xyz::openbmc_project::State::server::Host::HostState;
/* Need a custom deleter for freeing up sd_event */
struct EventDeleter
{
void operator()(sd_event* event) const
{
event = sd_event_unref(event);
}
};
using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
/** @struct CustomFd
*
* RAII wrapper for file descriptor.
*/
struct CustomFd
{
private:
/** @brief File descriptor */
int fd = -1;
public:
CustomFd() = delete;
CustomFd(const CustomFd&) = delete;
CustomFd& operator=(const CustomFd&) = delete;
CustomFd(CustomFd&&) = delete;
CustomFd& operator=(CustomFd&&) = delete;
/** @brief Saves File descriptor and uses it to do file operation
*
* @param[in] fd - File descriptor
*/
CustomFd(int fd) : fd(fd)
{}
~CustomFd()
{
if (fd >= 0)
{
close(fd);
}
}
int operator()() const
{
return fd;
}
};
/**
* @brief Get the bus service
*
* @param[in] bus - Bus to attach to.
* @param[in] path - D-Bus path name.
* @param[in] interface - D-Bus interface name.
* @return the bus service as a string
**/
std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
const std::string& interface);
/**
* @brief Get the host state
*
* @return HostState on success
* Throw exception on failure
*
*/
HostState getHostState();
/**
* @brief Get the host boot progress stage
*
* @return BootProgress on success
* Throw exception on failure
*
*/
BootProgress getBootProgress();
/**
* @brief Get the host state value
*
* @param[in] intf - Interface to get the value
* @param[in] objPath - Object path of the service
* @param[in] state - State name to get
*
* @return The state value on success
* Throw exception on failure
*/
std::string getStateValue(std::string intf, std::string objPath,
std::string state);
/**
* @brief Check whether host is running
*
* @return true if the host running else false.
* Throw exception on failure.
*/
bool isHostRunning();
/**
* @brief Check whether host is quiesced
*
* @return true if the host is quiesced else false.
* Throw exception on failure.
*/
bool isHostQuiesced();
/**
* @brief Read property value from the specified object and interface
* @param[in] bus D-Bus handle
* @param[in] service service which has implemented the interface
* @param[in] object object having has implemented the interface
* @param[in] intf interface having the property
* @param[in] prop name of the property to read
* @return property value
*/
template <typename T>
T readDBusProperty(sdbusplus::bus::bus& bus, const std::string& service,
const std::string& object, const std::string& intf,
const std::string& prop)
{
using ::phosphor::logging::level;
using ::phosphor::logging::log;
T retVal{};
try
{
auto properties =
bus.new_method_call(service.c_str(), object.c_str(),
"org.freedesktop.DBus.Properties", "Get");
properties.append(intf);
properties.append(prop);
auto result = bus.call(properties);
result.read(retVal);
}
catch (const std::exception& ex)
{
log<level::ERR>(
fmt::format("Failed to get the property ({}) interface ({}) "
"object path ({}) error ({}) ",
prop.c_str(), intf.c_str(), object.c_str(), ex.what())
.c_str());
throw;
}
return retVal;
}
/**
* @brief Create a new PEL message for dump Delete/Offload
*
* @param[in] dumpFilePath - Deleted dump file path/name
* @param[in] dumpFileType - Deleted dump file type (BMC/Resource/System)
* @param[in] dumpId - The dump ID
* @param[in] pelSev - PEL severity (Informational by default)
* @param[in] errIntf - D-Bus interface name.
* @return Returns void
**/
void createPEL(const std::string& dumpFilePath, const std::string& dumpFileType,
const int dumpId, const std::string& pelSev,
const std::string& errIntf);
} // namespace dump
} // namespace phosphor