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