Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "host_state_manager.hpp" |
| 4 | |
Matt Spinler | bbbc016 | 2020-11-11 14:43:50 -0600 | [diff] [blame] | 5 | #include <fmt/format.h> |
Andrew Geissler | 131d04a | 2021-03-12 11:02:41 -0600 | [diff] [blame] | 6 | #include <stdio.h> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 7 | #include <systemd/sd-bus.h> |
| 8 | |
| 9 | #include <cereal/archives/json.hpp> |
| 10 | #include <cereal/cereal.hpp> |
| 11 | #include <cereal/types/string.hpp> |
| 12 | #include <cereal/types/tuple.hpp> |
| 13 | #include <cereal/types/vector.hpp> |
| 14 | #include <phosphor-logging/elog-errors.hpp> |
| 15 | #include <phosphor-logging/log.hpp> |
| 16 | #include <sdbusplus/exception.hpp> |
| 17 | #include <sdbusplus/server.hpp> |
| 18 | #include <xyz/openbmc_project/Common/error.hpp> |
| 19 | #include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp> |
| 20 | |
Andrew Geissler | 131d04a | 2021-03-12 11:02:41 -0600 | [diff] [blame] | 21 | #include <filesystem> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 22 | #include <fstream> |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 23 | #include <iostream> |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 24 | #include <map> |
| 25 | #include <string> |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 26 | |
Vishwanatha Subbanna | 0a83873 | 2017-10-05 12:43:19 +0530 | [diff] [blame] | 27 | // Register class version with Cereal |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 28 | CEREAL_CLASS_VERSION(phosphor::state::manager::Host, CLASS_VERSION) |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 29 | |
| 30 | namespace phosphor |
| 31 | { |
| 32 | namespace state |
| 33 | { |
| 34 | namespace manager |
| 35 | { |
| 36 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 37 | // When you see server:: or reboot:: you know we're referencing our base class |
Andrew Geissler | 3e3b84b | 2016-12-02 15:46:17 -0600 | [diff] [blame] | 38 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 39 | namespace reboot = sdbusplus::xyz::openbmc_project::Control::Boot::server; |
Dhruvaraj Subhashchandran | a3b8d7e | 2017-08-10 05:40:04 -0500 | [diff] [blame] | 40 | namespace bootprogress = sdbusplus::xyz::openbmc_project::State::Boot::server; |
| 41 | namespace osstatus = |
| 42 | sdbusplus::xyz::openbmc_project::State::OperatingSystem::server; |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 43 | using namespace phosphor::logging; |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 44 | namespace fs = std::experimental::filesystem; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 45 | using sdbusplus::exception::SdBusError; |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 46 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 47 | |
Andrew Geissler | 4f309e8 | 2017-05-24 15:18:01 -0500 | [diff] [blame] | 48 | // host-shutdown notifies host of shutdown and that leads to host-stop being |
| 49 | // called so initiate a host shutdown with the -shutdown target and consider the |
| 50 | // host shut down when the -stop target is complete |
| 51 | constexpr auto HOST_STATE_SOFT_POWEROFF_TGT = "obmc-host-shutdown@0.target"; |
Josh D. King | ca35792 | 2017-04-11 13:44:09 -0500 | [diff] [blame] | 52 | constexpr auto HOST_STATE_POWEROFF_TGT = "obmc-host-stop@0.target"; |
| 53 | constexpr auto HOST_STATE_POWERON_TGT = "obmc-host-start@0.target"; |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame] | 54 | constexpr auto HOST_STATE_POWERON_MIN_TGT = "obmc-host-startmin@0.target"; |
Andrew Geissler | a27a6e8 | 2017-07-27 16:44:43 -0500 | [diff] [blame] | 55 | constexpr auto HOST_STATE_REBOOT_TGT = "obmc-host-reboot@0.target"; |
Andrew Geissler | 40dd6e7 | 2020-02-07 14:31:12 -0600 | [diff] [blame] | 56 | constexpr auto HOST_STATE_WARM_REBOOT = "obmc-host-warm-reboot@0.target"; |
| 57 | constexpr auto HOST_STATE_FORCE_WARM_REBOOT = |
| 58 | "obmc-host-force-warm-reboot@0.target"; |
Andrew Geissler | 47b9612 | 2020-02-11 16:13:13 -0600 | [diff] [blame] | 59 | constexpr auto HOST_STATE_DIAGNOSTIC_MODE = |
| 60 | "obmc-host-diagnostic-mode@0.target"; |
Andrew Geissler | 40dd6e7 | 2020-02-07 14:31:12 -0600 | [diff] [blame] | 61 | |
Josh D. King | cc3fb5d | 2017-04-19 15:45:10 -0500 | [diff] [blame] | 62 | constexpr auto HOST_STATE_QUIESCE_TGT = "obmc-host-quiesce@0.target"; |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 63 | |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 64 | constexpr auto ACTIVE_STATE = "active"; |
| 65 | constexpr auto ACTIVATING_STATE = "activating"; |
| 66 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 67 | /* Map a transition to it's systemd target */ |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 68 | const std::map<server::Host::Transition, std::string> SYSTEMD_TARGET_TABLE = { |
| 69 | {server::Host::Transition::Off, HOST_STATE_SOFT_POWEROFF_TGT}, |
| 70 | {server::Host::Transition::On, HOST_STATE_POWERON_TGT}, |
Andrew Geissler | 40dd6e7 | 2020-02-07 14:31:12 -0600 | [diff] [blame] | 71 | {server::Host::Transition::Reboot, HOST_STATE_REBOOT_TGT}, |
Andrew Geissler | 7fdad60 | 2020-06-22 13:46:16 -0500 | [diff] [blame] | 72 | // Some systems do not support a warm reboot so just map the reboot |
| 73 | // requests to our normal cold reboot in that case |
| 74 | #if ENABLE_WARM_REBOOT |
Andrew Geissler | 40dd6e7 | 2020-02-07 14:31:12 -0600 | [diff] [blame] | 75 | {server::Host::Transition::GracefulWarmReboot, HOST_STATE_WARM_REBOOT}, |
| 76 | {server::Host::Transition::ForceWarmReboot, HOST_STATE_FORCE_WARM_REBOOT}}; |
Andrew Geissler | 7fdad60 | 2020-06-22 13:46:16 -0500 | [diff] [blame] | 77 | #else |
| 78 | {server::Host::Transition::GracefulWarmReboot, HOST_STATE_REBOOT_TGT}, |
| 79 | {server::Host::Transition::ForceWarmReboot, HOST_STATE_REBOOT_TGT}}; |
| 80 | #endif |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 81 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 82 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 83 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 84 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 85 | |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 86 | constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 87 | constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit"; |
| 88 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 89 | void Host::subscribeToSystemdSignals() |
| 90 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 91 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 92 | SYSTEMD_INTERFACE, "Subscribe"); |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 93 | try |
| 94 | { |
| 95 | this->bus.call_noreply(method); |
| 96 | } |
| 97 | catch (const SdBusError& e) |
| 98 | { |
| 99 | log<level::ERR>("Failed to subscribe to systemd signals", |
| 100 | entry("ERR=%s", e.what())); |
| 101 | elog<InternalFailure>(); |
| 102 | } |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 106 | void Host::determineInitialState() |
| 107 | { |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 108 | |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame] | 109 | if (stateActive(HOST_STATE_POWERON_MIN_TGT)) |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 110 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 111 | log<level::INFO>("Initial Host State will be Running", |
| 112 | entry("CURRENT_HOST_STATE=%s", |
| 113 | convertForMessage(HostState::Running).c_str())); |
Andrew Geissler | 9792414 | 2017-01-24 16:10:18 -0600 | [diff] [blame] | 114 | server::Host::currentHostState(HostState::Running); |
| 115 | server::Host::requestedHostTransition(Transition::On); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 116 | } |
| 117 | else |
| 118 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 119 | log<level::INFO>("Initial Host State will be Off", |
| 120 | entry("CURRENT_HOST_STATE=%s", |
| 121 | convertForMessage(HostState::Off).c_str())); |
Andrew Geissler | 9792414 | 2017-01-24 16:10:18 -0600 | [diff] [blame] | 122 | server::Host::currentHostState(HostState::Off); |
| 123 | server::Host::requestedHostTransition(Transition::Off); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 124 | } |
| 125 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 126 | if (!deserialize(HOST_STATE_PERSIST_PATH)) |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 127 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 128 | // set to default value. |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 129 | server::Host::requestedHostTransition(Transition::Off); |
| 130 | } |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 131 | |
| 132 | return; |
| 133 | } |
| 134 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 135 | void Host::executeTransition(Transition tranReq) |
| 136 | { |
| 137 | auto sysdUnit = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 138 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 139 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 140 | SYSTEMD_INTERFACE, "StartUnit"); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 141 | |
| 142 | method.append(sysdUnit); |
| 143 | method.append("replace"); |
| 144 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 145 | this->bus.call_noreply(method); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 146 | |
| 147 | return; |
| 148 | } |
| 149 | |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 150 | bool Host::stateActive(const std::string& target) |
| 151 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 152 | std::variant<std::string> currentState; |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 153 | sdbusplus::message::object_path unitTargetPath; |
| 154 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 155 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 156 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 157 | |
| 158 | method.append(target); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 159 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 160 | try |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 161 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 162 | auto result = this->bus.call(method); |
| 163 | result.read(unitTargetPath); |
| 164 | } |
| 165 | catch (const SdBusError& e) |
| 166 | { |
| 167 | log<level::ERR>("Error in GetUnit call", entry("ERROR=%s", e.what())); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 171 | method = this->bus.new_method_call( |
| 172 | SYSTEMD_SERVICE, |
| 173 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 174 | SYSTEMD_PROPERTY_IFACE, "Get"); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 175 | |
| 176 | method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState"); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 177 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 178 | try |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 179 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 180 | auto result = this->bus.call(method); |
| 181 | result.read(currentState); |
| 182 | } |
| 183 | catch (const SdBusError& e) |
| 184 | { |
| 185 | log<level::ERR>("Error in ActiveState Get", |
| 186 | entry("ERROR=%s", e.what())); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 190 | const auto& currentStateStr = std::get<std::string>(currentState); |
William A. Kennington III | 7a0689a | 2018-11-12 17:19:33 -0800 | [diff] [blame] | 191 | return currentStateStr == ACTIVE_STATE || |
| 192 | currentStateStr == ACTIVATING_STATE; |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 193 | } |
| 194 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 195 | bool Host::isAutoReboot() |
| 196 | { |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 197 | using namespace settings; |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 198 | |
Andrew Geissler | 7f62083 | 2020-10-23 08:25:52 -0500 | [diff] [blame] | 199 | /* The logic here is to first check the one-time AutoReboot setting. |
| 200 | * If this property is true (the default) then look at the persistent |
| 201 | * user setting in the non one-time object, otherwise honor the one-time |
| 202 | * setting and do not auto reboot. |
| 203 | */ |
| 204 | auto methodOneTime = bus.new_method_call( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 205 | settings.service(settings.autoReboot, autoRebootIntf).c_str(), |
Andrew Geissler | 7f62083 | 2020-10-23 08:25:52 -0500 | [diff] [blame] | 206 | settings.autoRebootOneTime.c_str(), SYSTEMD_PROPERTY_IFACE, "Get"); |
| 207 | methodOneTime.append(autoRebootIntf, "AutoReboot"); |
| 208 | |
| 209 | auto methodUserSetting = bus.new_method_call( |
| 210 | settings.service(settings.autoReboot, autoRebootIntf).c_str(), |
| 211 | settings.autoReboot.c_str(), SYSTEMD_PROPERTY_IFACE, "Get"); |
| 212 | methodUserSetting.append(autoRebootIntf, "AutoReboot"); |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 213 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 214 | try |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 215 | { |
Andrew Geissler | 7f62083 | 2020-10-23 08:25:52 -0500 | [diff] [blame] | 216 | auto reply = bus.call(methodOneTime); |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 217 | std::variant<bool> result; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 218 | reply.read(result); |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 219 | auto autoReboot = std::get<bool>(result); |
Andrew Geissler | 7f62083 | 2020-10-23 08:25:52 -0500 | [diff] [blame] | 220 | |
| 221 | if (!autoReboot) |
| 222 | { |
| 223 | log<level::INFO>("Auto reboot (one-time) disabled"); |
| 224 | return false; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | // one-time is true so read the user setting |
| 229 | reply = bus.call(methodUserSetting); |
| 230 | reply.read(result); |
| 231 | autoReboot = std::get<bool>(result); |
| 232 | } |
| 233 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 234 | auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft(); |
| 235 | |
| 236 | if (autoReboot) |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 237 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 238 | if (rebootCounterParam > 0) |
| 239 | { |
| 240 | // Reduce BOOTCOUNT by 1 |
| 241 | log<level::INFO>("Auto reboot enabled, rebooting"); |
| 242 | return true; |
| 243 | } |
| 244 | else if (rebootCounterParam == 0) |
| 245 | { |
| 246 | // Reset reboot counter and go to quiesce state |
| 247 | log<level::INFO>("Auto reboot enabled. " |
| 248 | "HOST BOOTCOUNT already set to 0."); |
| 249 | attemptsLeft(BOOT_COUNT_MAX_ALLOWED); |
| 250 | return false; |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | log<level::INFO>("Auto reboot enabled. " |
| 255 | "HOST BOOTCOUNT has an invalid value."); |
| 256 | return false; |
| 257 | } |
Saqib Khan | d5ac635 | 2017-04-04 09:53:59 -0500 | [diff] [blame] | 258 | } |
| 259 | else |
| 260 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 261 | log<level::INFO>("Auto reboot disabled."); |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 262 | return false; |
| 263 | } |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 264 | } |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 265 | catch (const SdBusError& e) |
Saqib Khan | d5ac635 | 2017-04-04 09:53:59 -0500 | [diff] [blame] | 266 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 267 | log<level::ERR>("Error in AutoReboot Get", entry("ERROR=%s", e.what())); |
Saqib Khan | d5ac635 | 2017-04-04 09:53:59 -0500 | [diff] [blame] | 268 | return false; |
| 269 | } |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 270 | } |
| 271 | |
Andrew Geissler | e4039a8 | 2020-02-11 15:51:59 -0600 | [diff] [blame] | 272 | void Host::sysStateChangeJobRemoved(sdbusplus::message::message& msg) |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 273 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 274 | uint32_t newStateID{}; |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 275 | sdbusplus::message::object_path newStateObjPath; |
| 276 | std::string newStateUnit{}; |
| 277 | std::string newStateResult{}; |
| 278 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 279 | // Read the msg and populate each variable |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 280 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 281 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 282 | if ((newStateUnit == HOST_STATE_POWEROFF_TGT) && |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame] | 283 | (newStateResult == "done") && |
| 284 | (!stateActive(HOST_STATE_POWERON_MIN_TGT))) |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 285 | { |
Andrew Jeffery | 55b983e | 2017-04-19 11:11:26 +0930 | [diff] [blame] | 286 | log<level::INFO>("Received signal that host is off"); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 287 | this->currentHostState(server::Host::HostState::Off); |
Dhruvaraj Subhashchandran | a3b8d7e | 2017-08-10 05:40:04 -0500 | [diff] [blame] | 288 | this->bootProgress(bootprogress::Progress::ProgressStages::Unspecified); |
| 289 | this->operatingSystemState(osstatus::Status::OSStatus::Inactive); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 290 | } |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame] | 291 | else if ((newStateUnit == HOST_STATE_POWERON_MIN_TGT) && |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 292 | (newStateResult == "done") && |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame] | 293 | (stateActive(HOST_STATE_POWERON_MIN_TGT))) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 294 | { |
| 295 | log<level::INFO>("Received signal that host is running"); |
| 296 | this->currentHostState(server::Host::HostState::Running); |
Andrew Geissler | 131d04a | 2021-03-12 11:02:41 -0600 | [diff] [blame] | 297 | |
| 298 | // Remove temporary file which is utilized for scenarios where the |
| 299 | // BMC is rebooted while the host is still up. |
| 300 | // This file is used to indicate to host related systemd services |
| 301 | // that the host is already running and they should skip running. |
| 302 | // Once the host state is back to running we can clear this file. |
| 303 | auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0); |
| 304 | size++; // null |
| 305 | std::unique_ptr<char[]> hostFile(new char[size]); |
| 306 | std::snprintf(hostFile.get(), size, HOST_RUNNING_FILE, 0); |
| 307 | if (std::filesystem::exists(hostFile.get())) |
| 308 | { |
| 309 | std::filesystem::remove(hostFile.get()); |
| 310 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 311 | } |
| 312 | else if ((newStateUnit == HOST_STATE_QUIESCE_TGT) && |
Josh D. King | 29d025d | 2017-04-27 12:40:22 -0500 | [diff] [blame] | 313 | (newStateResult == "done") && |
| 314 | (stateActive(HOST_STATE_QUIESCE_TGT))) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 315 | { |
| 316 | if (Host::isAutoReboot()) |
| 317 | { |
| 318 | log<level::INFO>("Beginning reboot..."); |
| 319 | Host::requestedHostTransition(server::Host::Transition::Reboot); |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | log<level::INFO>("Maintaining quiesce"); |
| 324 | this->currentHostState(server::Host::HostState::Quiesced); |
| 325 | } |
| 326 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 327 | } |
| 328 | |
Andrew Geissler | 47b9612 | 2020-02-11 16:13:13 -0600 | [diff] [blame] | 329 | void Host::sysStateChangeJobNew(sdbusplus::message::message& msg) |
| 330 | { |
| 331 | uint32_t newStateID{}; |
| 332 | sdbusplus::message::object_path newStateObjPath; |
| 333 | std::string newStateUnit{}; |
| 334 | |
| 335 | // Read the msg and populate each variable |
| 336 | msg.read(newStateID, newStateObjPath, newStateUnit); |
| 337 | |
| 338 | if (newStateUnit == HOST_STATE_DIAGNOSTIC_MODE) |
| 339 | { |
| 340 | log<level::INFO>("Received signal that host is in diagnostice mode"); |
| 341 | this->currentHostState(server::Host::HostState::DiagnosticMode); |
| 342 | } |
| 343 | } |
| 344 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 345 | uint32_t Host::decrementRebootCount() |
| 346 | { |
| 347 | auto rebootCount = reboot::RebootAttempts::attemptsLeft(); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 348 | if (rebootCount > 0) |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 349 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 350 | return (reboot::RebootAttempts::attemptsLeft(rebootCount - 1)); |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 351 | } |
| 352 | return rebootCount; |
| 353 | } |
| 354 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 355 | fs::path Host::serialize(const fs::path& dir) |
| 356 | { |
| 357 | std::ofstream os(dir.c_str(), std::ios::binary); |
| 358 | cereal::JSONOutputArchive oarchive(os); |
| 359 | oarchive(*this); |
| 360 | return dir; |
| 361 | } |
| 362 | |
| 363 | bool Host::deserialize(const fs::path& path) |
| 364 | { |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 365 | try |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 366 | { |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 367 | if (fs::exists(path)) |
| 368 | { |
| 369 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 370 | cereal::JSONInputArchive iarchive(is); |
| 371 | iarchive(*this); |
| 372 | return true; |
| 373 | } |
| 374 | return false; |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 375 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 376 | catch (cereal::Exception& e) |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 377 | { |
| 378 | log<level::ERR>(e.what()); |
| 379 | fs::remove(path); |
| 380 | return false; |
| 381 | } |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 384 | Host::Transition Host::requestedHostTransition(Transition value) |
| 385 | { |
Matt Spinler | bbbc016 | 2020-11-11 14:43:50 -0600 | [diff] [blame] | 386 | log<level::INFO>(fmt::format("Host state transition request of {}", |
| 387 | convertForMessage(value)) |
| 388 | .c_str()); |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 389 | // If this is not a power off request then we need to |
| 390 | // decrement the reboot counter. This code should |
| 391 | // never prevent a power on, it should just decrement |
| 392 | // the count to 0. The quiesce handling is where the |
| 393 | // check of this count will occur |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 394 | if (value != server::Host::Transition::Off) |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 395 | { |
| 396 | decrementRebootCount(); |
| 397 | } |
| 398 | |
Andrew Geissler | a27a6e8 | 2017-07-27 16:44:43 -0500 | [diff] [blame] | 399 | executeTransition(value); |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 400 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 401 | auto retVal = server::Host::requestedHostTransition(value); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 402 | serialize(); |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 403 | return retVal; |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 404 | } |
| 405 | |
Dhruvaraj Subhashchandran | 4e6534f | 2017-09-19 06:13:20 -0500 | [diff] [blame] | 406 | Host::ProgressStages Host::bootProgress(ProgressStages value) |
| 407 | { |
| 408 | auto retVal = bootprogress::Progress::bootProgress(value); |
| 409 | serialize(); |
| 410 | return retVal; |
| 411 | } |
| 412 | |
| 413 | Host::OSStatus Host::operatingSystemState(OSStatus value) |
| 414 | { |
| 415 | auto retVal = osstatus::Status::operatingSystemState(value); |
| 416 | serialize(); |
| 417 | return retVal; |
| 418 | } |
| 419 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 420 | Host::HostState Host::currentHostState(HostState value) |
| 421 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 422 | log<level::INFO>( |
Matt Spinler | bbbc016 | 2020-11-11 14:43:50 -0600 | [diff] [blame] | 423 | fmt::format("Change to Host State: {}", convertForMessage(value)) |
| 424 | .c_str()); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 425 | return server::Host::currentHostState(value); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 426 | } |
| 427 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 428 | } // namespace manager |
| 429 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 430 | } // namespace phosphor |