Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 3 | #include <string> |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 4 | #include <functional> |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 5 | #include <experimental/filesystem> |
| 6 | #include <cereal/access.hpp> |
Vishwanatha Subbanna | 0a83873 | 2017-10-05 12:43:19 +0530 | [diff] [blame] | 7 | #include <cereal/cereal.hpp> |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/log.hpp> |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 10 | #include <xyz/openbmc_project/State/Boot/Progress/server.hpp> |
| 11 | #include <xyz/openbmc_project/Control/Boot/RebootAttempts/server.hpp> |
| 12 | #include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp> |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 13 | #include "xyz/openbmc_project/State/Host/server.hpp" |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 14 | #include "settings.hpp" |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 15 | #include "config.h" |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace state |
| 20 | { |
| 21 | namespace manager |
| 22 | { |
| 23 | |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 24 | using HostInherit = sdbusplus::server::object::object< |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 25 | sdbusplus::xyz::openbmc_project::State::server::Host, |
| 26 | sdbusplus::xyz::openbmc_project::State::Boot::server::Progress, |
| 27 | sdbusplus::xyz::openbmc_project::Control::Boot::server::RebootAttempts, |
| 28 | sdbusplus::xyz::openbmc_project::State::OperatingSystem::server::Status>; |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 29 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 30 | using namespace phosphor::logging; |
| 31 | |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 32 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 33 | namespace fs = std::experimental::filesystem; |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 34 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 35 | /** @class Host |
| 36 | * @brief OpenBMC host state management implementation. |
| 37 | * @details A concrete implementation for xyz.openbmc_project.State.Host |
| 38 | * DBus API. |
| 39 | */ |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 40 | class Host : public HostInherit |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 41 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 42 | public: |
| 43 | /** @brief Constructs Host State Manager |
| 44 | * |
| 45 | * @note This constructor passes 'true' to the base class in order to |
| 46 | * defer dbus object registration until we can run |
| 47 | * determineInitialState() and set our properties |
| 48 | * |
| 49 | * @param[in] bus - The Dbus bus object |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 50 | * @param[in] objPath - The Dbus object path |
| 51 | */ |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 52 | Host(sdbusplus::bus::bus& bus, const char* objPath) : |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 53 | HostInherit(bus, objPath, true), bus(bus), |
Andrew Geissler | e4039a8 | 2020-02-11 15:51:59 -0600 | [diff] [blame] | 54 | systemdSignalJobRemoved( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 55 | bus, |
| 56 | sdbusRule::type::signal() + sdbusRule::member("JobRemoved") + |
| 57 | sdbusRule::path("/org/freedesktop/systemd1") + |
| 58 | sdbusRule::interface("org.freedesktop.systemd1.Manager"), |
Andrew Geissler | e4039a8 | 2020-02-11 15:51:59 -0600 | [diff] [blame] | 59 | std::bind(std::mem_fn(&Host::sysStateChangeJobRemoved), this, |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 60 | std::placeholders::_1)), |
Andrew Geissler | 47b9612 | 2020-02-11 16:13:13 -0600 | [diff] [blame] | 61 | systemdSignalJobNew( |
| 62 | bus, |
| 63 | sdbusRule::type::signal() + sdbusRule::member("JobNew") + |
| 64 | sdbusRule::path("/org/freedesktop/systemd1") + |
| 65 | sdbusRule::interface("org.freedesktop.systemd1.Manager"), |
| 66 | std::bind(std::mem_fn(&Host::sysStateChangeJobNew), this, |
| 67 | std::placeholders::_1)), |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 68 | settings(bus) |
| 69 | { |
| 70 | // Enable systemd signals |
| 71 | subscribeToSystemdSignals(); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 72 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 73 | // Will throw exception on fail |
| 74 | determineInitialState(); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 75 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 76 | attemptsLeft(BOOT_COUNT_MAX_ALLOWED); |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 77 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 78 | // We deferred this until we could get our property correct |
| 79 | this->emit_object_added(); |
| 80 | } |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 81 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 82 | /** @brief Set value of HostTransition */ |
| 83 | Transition requestedHostTransition(Transition value) override; |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 84 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 85 | /** @brief Set Value for boot progress */ |
| 86 | ProgressStages bootProgress(ProgressStages value) override; |
Dhruvaraj Subhashchandran | 4e6534f | 2017-09-19 06:13:20 -0500 | [diff] [blame] | 87 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 88 | /** @brief Set Value for Operating System Status */ |
| 89 | OSStatus operatingSystemState(OSStatus value) override; |
Dhruvaraj Subhashchandran | 4e6534f | 2017-09-19 06:13:20 -0500 | [diff] [blame] | 90 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 91 | /** @brief Set value of CurrentHostState */ |
| 92 | HostState currentHostState(HostState value) override; |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 93 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 94 | /** |
| 95 | * @brief Set host reboot count to default |
| 96 | * |
| 97 | * OpenBMC software controls the number of allowed reboot attempts so |
| 98 | * any external set request of this property will be overridden by |
| 99 | * this function and set to the default. |
| 100 | * |
| 101 | * The only code responsible for decrementing the boot count resides |
| 102 | * within this process and that will use the sub class interface |
| 103 | * directly |
| 104 | * |
| 105 | * @param[in] value - Reboot count value, will be ignored |
| 106 | * |
| 107 | * @return Default number of reboot attempts left |
| 108 | */ |
| 109 | uint32_t attemptsLeft(uint32_t value) override |
| 110 | { |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 111 | // value is ignored in this implementation |
| 112 | (void)(value); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 113 | log<level::DEBUG>("External request to reset reboot count"); |
| 114 | return (sdbusplus::xyz::openbmc_project::Control::Boot::server:: |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 115 | RebootAttempts::attemptsLeft(BOOT_COUNT_MAX_ALLOWED)); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 116 | } |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 117 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 118 | private: |
| 119 | /** |
| 120 | * @brief subscribe to the systemd signals |
| 121 | * |
| 122 | * This object needs to capture when it's systemd targets complete |
| 123 | * so it can keep it's state updated |
| 124 | * |
| 125 | **/ |
| 126 | void subscribeToSystemdSignals(); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 127 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 128 | /** |
| 129 | * @brief Determine initial host state and set internally |
| 130 | * |
| 131 | * @return Will throw exceptions on failure |
| 132 | **/ |
| 133 | void determineInitialState(); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 134 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 135 | /** @brief Execute the transition request |
| 136 | * |
| 137 | * This function assumes the state has been validated and the host |
| 138 | * is in an appropriate state for the transition to be started. |
| 139 | * |
| 140 | * @param[in] tranReq - Transition requested |
| 141 | */ |
| 142 | void executeTransition(Transition tranReq); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 143 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 144 | /** |
| 145 | * @brief Determine if target is active |
| 146 | * |
| 147 | * This function determines if the target is active and |
| 148 | * helps prevent misleading log recorded states. |
| 149 | * |
| 150 | * @param[in] target - Target string to check on |
| 151 | * |
| 152 | * @return boolean corresponding to state active |
| 153 | **/ |
| 154 | bool stateActive(const std::string& target); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 155 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 156 | /** |
| 157 | * @brief Determine if auto reboot flag is set |
| 158 | * |
| 159 | * @return boolean corresponding to current auto_reboot setting |
| 160 | **/ |
| 161 | bool isAutoReboot(); |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 162 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 163 | /** @brief Check if systemd state change is relevant to this object |
| 164 | * |
| 165 | * Instance specific interface to handle the detected systemd state |
| 166 | * change |
| 167 | * |
| 168 | * @param[in] msg - Data associated with subscribed signal |
| 169 | * |
| 170 | */ |
Andrew Geissler | e4039a8 | 2020-02-11 15:51:59 -0600 | [diff] [blame] | 171 | void sysStateChangeJobRemoved(sdbusplus::message::message& msg); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 172 | |
Andrew Geissler | 47b9612 | 2020-02-11 16:13:13 -0600 | [diff] [blame] | 173 | /** @brief Check if JobNew systemd signal is relevant to this object |
| 174 | * |
| 175 | * In certain instances phosphor-state-manager needs to monitor for the |
| 176 | * entry into a systemd target. This function will be used for these cases. |
| 177 | * |
| 178 | * Instance specific interface to handle the detected systemd state |
| 179 | * change |
| 180 | * |
| 181 | * @param[in] msg - Data associated with subscribed signal |
| 182 | * |
| 183 | */ |
| 184 | void sysStateChangeJobNew(sdbusplus::message::message& msg); |
| 185 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 186 | /** @brief Decrement reboot count |
| 187 | * |
| 188 | * This is used internally to this application to decrement the boot |
| 189 | * count on each boot attempt. The host will use the external |
| 190 | * attemptsLeft() interface to reset the count when a boot is successful |
| 191 | * |
| 192 | * @return number of reboot count attempts left |
| 193 | */ |
| 194 | uint32_t decrementRebootCount(); |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 195 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 196 | // Allow cereal class access to allow these next two function to be |
| 197 | // private |
| 198 | friend class cereal::access; |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 199 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 200 | /** @brief Function required by Cereal to perform serialization. |
| 201 | * |
| 202 | * @tparam Archive - Cereal archive type (binary in our case). |
| 203 | * @param[in] archive - reference to Cereal archive. |
| 204 | * @param[in] version - Class version that enables handling |
| 205 | * a serialized data across code levels |
| 206 | */ |
| 207 | template <class Archive> |
| 208 | void save(Archive& archive, const std::uint32_t version) const |
| 209 | { |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 210 | // version is not used currently |
| 211 | (void)(version); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 212 | archive(convertForMessage(sdbusplus::xyz::openbmc_project::State:: |
| 213 | server::Host::requestedHostTransition()), |
| 214 | convertForMessage(sdbusplus::xyz::openbmc_project::State::Boot:: |
| 215 | server::Progress::bootProgress()), |
| 216 | convertForMessage( |
| 217 | sdbusplus::xyz::openbmc_project::State::OperatingSystem:: |
| 218 | server::Status::operatingSystemState())); |
| 219 | } |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 220 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 221 | /** @brief Function required by Cereal to perform deserialization. |
| 222 | * |
| 223 | * @tparam Archive - Cereal archive type (binary in our case). |
| 224 | * @param[in] archive - reference to Cereal archive. |
| 225 | * @param[in] version - Class version that enables handling |
| 226 | * a serialized data across code levels |
| 227 | */ |
| 228 | template <class Archive> |
| 229 | void load(Archive& archive, const std::uint32_t version) |
| 230 | { |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 231 | // version is not used currently |
| 232 | (void)(version); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 233 | std::string reqTranState; |
| 234 | std::string bootProgress; |
| 235 | std::string osState; |
| 236 | archive(reqTranState, bootProgress, osState); |
| 237 | auto reqTran = Host::convertTransitionFromString(reqTranState); |
| 238 | // When restoring, set the requested state with persistent value |
| 239 | // but don't call the override which would execute it |
| 240 | sdbusplus::xyz::openbmc_project::State::server::Host:: |
| 241 | requestedHostTransition(reqTran); |
| 242 | sdbusplus::xyz::openbmc_project::State::Boot::server::Progress:: |
| 243 | bootProgress(Host::convertProgressStagesFromString(bootProgress)); |
| 244 | sdbusplus::xyz::openbmc_project::State::OperatingSystem::server:: |
| 245 | Status::operatingSystemState( |
| 246 | Host::convertOSStatusFromString(osState)); |
| 247 | } |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 248 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 249 | /** @brief Serialize and persist requested host state |
| 250 | * |
| 251 | * @param[in] dir - pathname of file where the serialized host state will |
| 252 | * be placed. |
| 253 | * |
| 254 | * @return fs::path - pathname of persisted requested host state. |
| 255 | */ |
| 256 | fs::path serialize(const fs::path& dir = fs::path(HOST_STATE_PERSIST_PATH)); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 257 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 258 | /** @brief Deserialze a persisted requested host state. |
| 259 | * |
| 260 | * @param[in] path - pathname of persisted host state file |
| 261 | * |
| 262 | * @return bool - true if the deserialization was successful, false |
| 263 | * otherwise. |
| 264 | */ |
| 265 | bool deserialize(const fs::path& path); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 266 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 267 | /** @brief Persistent sdbusplus DBus bus connection. */ |
| 268 | sdbusplus::bus::bus& bus; |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 269 | |
Andrew Geissler | e4039a8 | 2020-02-11 15:51:59 -0600 | [diff] [blame] | 270 | /** @brief Used to subscribe to dbus systemd JobRemoved signal **/ |
| 271 | sdbusplus::bus::match_t systemdSignalJobRemoved; |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 272 | |
Andrew Geissler | 47b9612 | 2020-02-11 16:13:13 -0600 | [diff] [blame] | 273 | /** @brief Used to subscribe to dbus systemd JobNew signal **/ |
| 274 | sdbusplus::bus::match_t systemdSignalJobNew; |
| 275 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 276 | // Settings objects of interest |
| 277 | settings::Objects settings; |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | } // namespace manager |
| 281 | } // namespace state |
| 282 | } // namespace phosphor |