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