1 #pragma once 2 #include "dump_manager.hpp" 3 4 #include <systemd/sd-event.h> 5 #include <unistd.h> 6 7 #include <phosphor-logging/elog-errors.hpp> 8 #include <phosphor-logging/elog.hpp> 9 #include <phosphor-logging/lg2.hpp> 10 #include <sdbusplus/bus.hpp> 11 #include <xyz/openbmc_project/Common/error.hpp> 12 #include <xyz/openbmc_project/Dump/Create/server.hpp> 13 #include <xyz/openbmc_project/State/Boot/Progress/server.hpp> 14 15 #include <memory> 16 17 namespace phosphor 18 { 19 namespace dump 20 { 21 22 using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server:: 23 Progress::ProgressStages; 24 25 using namespace phosphor::logging; 26 using namespace sdbusplus::xyz::openbmc_project::Common::Error; 27 28 /* Need a custom deleter for freeing up sd_event */ 29 struct EventDeleter 30 { 31 void operator()(sd_event* event) const 32 { 33 event = sd_event_unref(event); 34 } 35 }; 36 using EventPtr = std::unique_ptr<sd_event, EventDeleter>; 37 38 /** @struct CustomFd 39 * 40 * RAII wrapper for file descriptor. 41 */ 42 struct CustomFd 43 { 44 private: 45 /** @brief File descriptor */ 46 int fd = -1; 47 48 public: 49 CustomFd() = delete; 50 CustomFd(const CustomFd&) = delete; 51 CustomFd& operator=(const CustomFd&) = delete; 52 CustomFd(CustomFd&&) = delete; 53 CustomFd& operator=(CustomFd&&) = delete; 54 55 /** @brief Saves File descriptor and uses it to do file operation 56 * 57 * @param[in] fd - File descriptor 58 */ 59 CustomFd(int fd) : fd(fd) {} 60 61 ~CustomFd() 62 { 63 if (fd >= 0) 64 { 65 close(fd); 66 } 67 } 68 69 int operator()() const 70 { 71 return fd; 72 } 73 }; 74 75 /** 76 * @brief Get the bus service 77 * 78 * @param[in] bus - Bus to attach to. 79 * @param[in] path - D-Bus path name. 80 * @param[in] interface - D-Bus interface name. 81 * @return the bus service as a string 82 **/ 83 std::string getService(sdbusplus::bus_t& bus, const std::string& path, 84 const std::string& interface); 85 86 /** 87 * @brief Get the host boot progress stage 88 * 89 * @return BootProgress on success 90 * Throw exception on failure 91 * 92 */ 93 BootProgress getBootProgress(); 94 95 /** 96 * @brief Check whether host is running 97 * 98 * @return true if the host running else false. 99 * Throw exception on failure. 100 */ 101 bool isHostRunning(); 102 103 inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params, 104 std::string& originatorId, 105 originatorTypes& originatorType) 106 { 107 using InvalidArgument = 108 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; 109 using Argument = xyz::openbmc_project::Common::InvalidArgument; 110 using CreateParametersXYZ = 111 sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters; 112 113 auto iter = params.find( 114 sdbusplus::xyz::openbmc_project::Dump::server::Create:: 115 convertCreateParametersToString(CreateParametersXYZ::OriginatorId)); 116 if (iter == params.end()) 117 { 118 lg2::info("OriginatorId is not provided"); 119 } 120 else 121 { 122 try 123 { 124 originatorId = std::get<std::string>(iter->second); 125 } 126 catch (const std::bad_variant_access& e) 127 { 128 // Exception will be raised if the input is not string 129 lg2::error("An invalid originatorId passed. It should be a string, " 130 "errormsg: {ERROR}", 131 "ERROR", e); 132 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"), 133 Argument::ARGUMENT_VALUE("INVALID INPUT")); 134 } 135 } 136 137 iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create:: 138 convertCreateParametersToString( 139 CreateParametersXYZ::OriginatorType)); 140 if (iter == params.end()) 141 { 142 lg2::info("OriginatorType is not provided. Replacing the string " 143 "with the default value"); 144 originatorType = originatorTypes::Internal; 145 } 146 else 147 { 148 try 149 { 150 std::string type = std::get<std::string>(iter->second); 151 originatorType = sdbusplus::xyz::openbmc_project::Common::server:: 152 OriginatedBy::convertOriginatorTypesFromString(type); 153 } 154 catch (const std::bad_variant_access& e) 155 { 156 // Exception will be raised if the input is not string 157 lg2::error("An invalid originatorType passed, errormsg: {ERROR}", 158 "ERROR", e); 159 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"), 160 Argument::ARGUMENT_VALUE("INVALID INPUT")); 161 } 162 } 163 } 164 165 } // namespace dump 166 } // namespace phosphor 167