Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 1 | #pragma once |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 2 | #include "dump_manager.hpp" |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame^] | 3 | #include "dump_types.hpp" |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 4 | |
Jayanth Othayoth | d02153c | 2017-07-02 22:29:42 -0500 | [diff] [blame] | 5 | #include <systemd/sd-event.h> |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/elog.hpp> |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
| 13 | #include <xyz/openbmc_project/Dump/Create/server.hpp> |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 14 | #include <xyz/openbmc_project/State/Boot/Progress/server.hpp> |
Dhruvaraj Subhashchandran | 3a25e5b | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 15 | #include <xyz/openbmc_project/State/Host/server.hpp> |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 16 | |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 19 | namespace phosphor |
| 20 | { |
| 21 | namespace dump |
| 22 | { |
| 23 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 24 | using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server:: |
| 25 | Progress::ProgressStages; |
Dhruvaraj Subhashchandran | 3a25e5b | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 26 | using HostState = |
| 27 | sdbusplus::xyz::openbmc_project::State::server::Host::HostState; |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 28 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 29 | using namespace phosphor::logging; |
| 30 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 31 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 32 | /* Need a custom deleter for freeing up sd_event */ |
| 33 | struct EventDeleter |
| 34 | { |
| 35 | void operator()(sd_event* event) const |
| 36 | { |
| 37 | event = sd_event_unref(event); |
| 38 | } |
| 39 | }; |
| 40 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 41 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 42 | /** @struct CustomFd |
| 43 | * |
| 44 | * RAII wrapper for file descriptor. |
| 45 | */ |
| 46 | struct CustomFd |
| 47 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 48 | private: |
| 49 | /** @brief File descriptor */ |
| 50 | int fd = -1; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 51 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 52 | public: |
| 53 | CustomFd() = delete; |
| 54 | CustomFd(const CustomFd&) = delete; |
| 55 | CustomFd& operator=(const CustomFd&) = delete; |
| 56 | CustomFd(CustomFd&&) = delete; |
| 57 | CustomFd& operator=(CustomFd&&) = delete; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 58 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 59 | /** @brief Saves File descriptor and uses it to do file operation |
| 60 | * |
| 61 | * @param[in] fd - File descriptor |
| 62 | */ |
Patrick Williams | 78e8840 | 2023-05-10 07:50:48 -0500 | [diff] [blame] | 63 | CustomFd(int fd) : fd(fd) {} |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 64 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 65 | ~CustomFd() |
| 66 | { |
| 67 | if (fd >= 0) |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 68 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 69 | close(fd); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 70 | } |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 71 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 72 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 73 | int operator()() const |
| 74 | { |
| 75 | return fd; |
| 76 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 79 | /** |
| 80 | * @brief Get the bus service |
| 81 | * |
| 82 | * @param[in] bus - Bus to attach to. |
| 83 | * @param[in] path - D-Bus path name. |
| 84 | * @param[in] interface - D-Bus interface name. |
| 85 | * @return the bus service as a string |
Dhruvaraj Subhashchandran | 3a25e5b | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 86 | * |
| 87 | * @throws sdbusplus::exception::SdBusError - If any D-Bus error occurs during |
| 88 | * the call. |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 89 | **/ |
Patrick Williams | 9b18bf2 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 90 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 91 | const std::string& interface); |
| 92 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 93 | /** |
Dhruvaraj Subhashchandran | 3a25e5b | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 94 | * @brief Read property value from the specified object and interface |
| 95 | * @param[in] bus D-Bus handle |
| 96 | * @param[in] service service which has implemented the interface |
| 97 | * @param[in] object object having has implemented the interface |
| 98 | * @param[in] intf interface having the property |
| 99 | * @param[in] prop name of the property to read |
| 100 | * @throws sdbusplus::exception::SdBusError if an error occurs in the dbus call |
| 101 | * @return property value |
| 102 | */ |
| 103 | template <typename T> |
| 104 | T readDBusProperty(sdbusplus::bus_t& bus, const std::string& service, |
| 105 | const std::string& object, const std::string& intf, |
| 106 | const std::string& prop) |
| 107 | { |
| 108 | T retVal{}; |
| 109 | try |
| 110 | { |
| 111 | auto properties = bus.new_method_call(service.c_str(), object.c_str(), |
| 112 | "org.freedesktop.DBus.Properties", |
| 113 | "Get"); |
| 114 | properties.append(intf); |
| 115 | properties.append(prop); |
| 116 | auto result = bus.call(properties); |
| 117 | result.read(retVal); |
| 118 | } |
| 119 | catch (const std::exception& ex) |
| 120 | { |
| 121 | lg2::error( |
| 122 | "Failed to get the property: {PROPERTY} interface: {INTERFACE} " |
| 123 | "object path: {OBJECT_PATH} error: {ERROR} ", |
| 124 | "PROPERTY", prop, "INTERFACE", intf, "OBJECT_PATH", object, "ERROR", |
| 125 | ex); |
| 126 | throw; |
| 127 | } |
| 128 | return retVal; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @brief Get the state value |
| 133 | * |
| 134 | * @param[in] intf - Interface to get the value |
| 135 | * @param[in] objPath - Object path of the service |
| 136 | * @param[in] state - State name to get |
| 137 | * |
| 138 | * @return The state value as type T on successful retrieval. |
| 139 | * |
| 140 | * @throws sdbusplus::exception for D-Bus failures and std::bad_variant_access |
| 141 | * for invalid value |
| 142 | */ |
| 143 | template <typename T> |
| 144 | T getStateValue(const std::string& intf, const std::string& objPath, |
| 145 | const std::string& state) |
| 146 | { |
| 147 | try |
| 148 | { |
| 149 | auto bus = sdbusplus::bus::new_default(); |
| 150 | auto service = getService(bus, objPath, intf); |
| 151 | return std::get<T>(readDBusProperty<std::variant<T>>( |
| 152 | bus, service, objPath, intf, state)); |
| 153 | } |
| 154 | catch (const sdbusplus::exception_t& e) |
| 155 | { |
| 156 | lg2::error( |
| 157 | "D-Bus call exception, OBJPATH: {OBJPATH}, " |
| 158 | "INTERFACE: {INTERFACE}, PROPERTY: {PROPERTY}, error: {ERROR}", |
| 159 | "OBJPATH", objPath, "INTERFACE", intf, "PROPERTY", state, "ERROR", |
| 160 | e); |
| 161 | throw; |
| 162 | } |
| 163 | catch (const std::bad_variant_access& e) |
| 164 | { |
| 165 | lg2::error("Exception raised while read state: {STATE} property " |
| 166 | "value, OBJPATH: {OBJPATH}, INTERFACE: {INTERFACE}, " |
| 167 | "error: {ERROR}", |
| 168 | "STATE", state, "OBJPATH", objPath, "INTERFACE", intf, |
| 169 | "ERROR", e); |
| 170 | throw; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @brief Get the host state |
| 176 | * |
| 177 | * @return HostState on success |
| 178 | * |
| 179 | * @throws std::runtime_error - If getting the state property fails |
| 180 | */ |
| 181 | inline HostState getHostState() |
| 182 | { |
| 183 | constexpr auto hostStateInterface = "xyz.openbmc_project.State.Host"; |
| 184 | // TODO Need to change host instance if multiple instead "0" |
| 185 | constexpr auto hostStateObjPath = "/xyz/openbmc_project/state/host0"; |
| 186 | return getStateValue<HostState>(hostStateInterface, hostStateObjPath, |
| 187 | "CurrentHostState"); |
| 188 | } |
| 189 | |
| 190 | /** |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 191 | * @brief Get the host boot progress stage |
| 192 | * |
| 193 | * @return BootProgress on success |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 194 | * |
Dhruvaraj Subhashchandran | 3a25e5b | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 195 | * @throws std::runtime_error - If getting the state property fails |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 196 | */ |
Dhruvaraj Subhashchandran | 3a25e5b | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 197 | inline BootProgress getBootProgress() |
| 198 | { |
| 199 | constexpr auto bootProgressInterface = |
| 200 | "xyz.openbmc_project.State.Boot.Progress"; |
| 201 | // TODO Need to change host instance if multiple instead "0" |
| 202 | constexpr auto hostStateObjPath = "/xyz/openbmc_project/state/host0"; |
| 203 | return getStateValue<BootProgress>(bootProgressInterface, hostStateObjPath, |
| 204 | "BootProgress"); |
| 205 | } |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 206 | |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 207 | /** |
| 208 | * @brief Check whether host is running |
| 209 | * |
| 210 | * @return true if the host running else false. |
Dhruvaraj Subhashchandran | 3a25e5b | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 211 | * |
| 212 | * @throws std::runtime_error - If getting the boot progress failed |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 213 | */ |
Dhruvaraj Subhashchandran | 3a25e5b | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 214 | inline bool isHostRunning() |
| 215 | { |
| 216 | // TODO #ibm-openbmc/dev/2858 Revisit the method for finding whether host |
| 217 | // is running. |
| 218 | BootProgress bootProgressStatus = getBootProgress(); |
| 219 | if ((bootProgressStatus == BootProgress::SystemInitComplete) || |
| 220 | (bootProgressStatus == BootProgress::SystemSetup) || |
| 221 | (bootProgressStatus == BootProgress::OSStart) || |
| 222 | (bootProgressStatus == BootProgress::OSRunning) || |
| 223 | (bootProgressStatus == BootProgress::PCIInit)) |
| 224 | { |
| 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | } |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 229 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 230 | inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params, |
| 231 | std::string& originatorId, |
| 232 | originatorTypes& originatorType) |
| 233 | { |
| 234 | using InvalidArgument = |
| 235 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 236 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 237 | using CreateParametersXYZ = |
| 238 | sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters; |
| 239 | |
| 240 | auto iter = params.find( |
| 241 | sdbusplus::xyz::openbmc_project::Dump::server::Create:: |
| 242 | convertCreateParametersToString(CreateParametersXYZ::OriginatorId)); |
| 243 | if (iter == params.end()) |
| 244 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 245 | lg2::info("OriginatorId is not provided"); |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 246 | } |
| 247 | else |
| 248 | { |
| 249 | try |
| 250 | { |
| 251 | originatorId = std::get<std::string>(iter->second); |
| 252 | } |
| 253 | catch (const std::bad_variant_access& e) |
| 254 | { |
| 255 | // Exception will be raised if the input is not string |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 256 | lg2::error("An invalid originatorId passed. It should be a string, " |
| 257 | "errormsg: {ERROR}", |
| 258 | "ERROR", e); |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 259 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"), |
| 260 | Argument::ARGUMENT_VALUE("INVALID INPUT")); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create:: |
| 265 | convertCreateParametersToString( |
| 266 | CreateParametersXYZ::OriginatorType)); |
| 267 | if (iter == params.end()) |
| 268 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 269 | lg2::info("OriginatorType is not provided. Replacing the string " |
| 270 | "with the default value"); |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 271 | originatorType = originatorTypes::Internal; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | try |
| 276 | { |
| 277 | std::string type = std::get<std::string>(iter->second); |
| 278 | originatorType = sdbusplus::xyz::openbmc_project::Common::server:: |
| 279 | OriginatedBy::convertOriginatorTypesFromString(type); |
| 280 | } |
| 281 | catch (const std::bad_variant_access& e) |
| 282 | { |
| 283 | // Exception will be raised if the input is not string |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 284 | lg2::error("An invalid originatorType passed, errormsg: {ERROR}", |
| 285 | "ERROR", e); |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 286 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"), |
| 287 | Argument::ARGUMENT_VALUE("INVALID INPUT")); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
Dhruvaraj Subhashchandran | 3a25e5b | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 292 | /** |
| 293 | * @brief Check whether host is quiesced |
| 294 | * |
| 295 | * @return true if the host is quiesced else false. |
| 296 | * |
| 297 | * @throws std::runtime_error - If getting the state failed |
| 298 | */ |
| 299 | inline bool isHostQuiesced() |
| 300 | { |
| 301 | return (getHostState() == HostState::Quiesced); |
| 302 | } |
| 303 | |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame^] | 304 | /** @brief Extract the dump create parameters |
| 305 | * @param[in] key - The name of the parameter |
| 306 | * @param[in] params - The map of parameters passed as input |
| 307 | * |
| 308 | * @return On success, a std::optional containing the value of the parameter |
| 309 | * (of type T). On failure (key not found in the map or the value is not of type |
| 310 | * T), returns an empty std::optional. |
| 311 | */ |
| 312 | template <typename T> |
| 313 | T extractParameter(const std::string& key, |
| 314 | phosphor::dump::DumpCreateParams& params) |
| 315 | { |
| 316 | using InvalidArgument = |
| 317 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 318 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 319 | |
| 320 | auto it = params.find(key); |
| 321 | if (it != params.end()) |
| 322 | { |
| 323 | const auto& [foundKey, variantValue] = *it; |
| 324 | if (std::holds_alternative<T>(variantValue)) |
| 325 | { |
| 326 | return std::get<T>(variantValue); |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | lg2::error("An invalid input passed for key: {KEY}", "KEY", key); |
| 331 | elog<InvalidArgument>(Argument::ARGUMENT_NAME(key.c_str()), |
| 332 | Argument::ARGUMENT_VALUE("INVALID INPUT")); |
| 333 | } |
| 334 | } |
| 335 | return T{}; |
| 336 | } |
| 337 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 338 | } // namespace dump |
| 339 | } // namespace phosphor |