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" |
Andrew Geissler | 1ab2b6c | 2022-03-10 16:16:15 -0600 | [diff] [blame] | 6 | #include "utils.hpp" |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 7 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 8 | #include <systemd/sd-bus.h> |
| 9 | |
| 10 | #include <cereal/archives/json.hpp> |
| 11 | #include <cereal/cereal.hpp> |
| 12 | #include <cereal/types/string.hpp> |
| 13 | #include <cereal/types/tuple.hpp> |
| 14 | #include <cereal/types/vector.hpp> |
| 15 | #include <phosphor-logging/elog-errors.hpp> |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 16 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 17 | #include <sdbusplus/exception.hpp> |
| 18 | #include <sdbusplus/server.hpp> |
| 19 | #include <xyz/openbmc_project/Common/error.hpp> |
| 20 | #include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp> |
Andrew Geissler | 765446a | 2023-05-25 15:38:20 -0400 | [diff] [blame] | 21 | #include <xyz/openbmc_project/State/Host/error.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 22 | |
Andrew Geissler | 131d04a | 2021-03-12 11:02:41 -0600 | [diff] [blame] | 23 | #include <filesystem> |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 24 | #include <format> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 25 | #include <fstream> |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 26 | #include <iostream> |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 27 | #include <map> |
Andrew Geissler | 54ce639 | 2024-02-23 10:09:09 -0600 | [diff] [blame] | 28 | #include <set> |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 29 | #include <string> |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 30 | |
Vishwanatha Subbanna | 0a83873 | 2017-10-05 12:43:19 +0530 | [diff] [blame] | 31 | // Register class version with Cereal |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 32 | CEREAL_CLASS_VERSION(phosphor::state::manager::Host, CLASS_VERSION) |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 33 | |
| 34 | namespace phosphor |
| 35 | { |
| 36 | namespace state |
| 37 | { |
| 38 | namespace manager |
| 39 | { |
| 40 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 41 | PHOSPHOR_LOG2_USING; |
| 42 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 43 | // When you see server:: or reboot:: you know we're referencing our base class |
Patrick Williams | 7e969cb | 2023-08-23 16:24:23 -0500 | [diff] [blame] | 44 | namespace server = sdbusplus::server::xyz::openbmc_project::state; |
| 45 | namespace reboot = sdbusplus::server::xyz::openbmc_project::control::boot; |
| 46 | namespace bootprogress = sdbusplus::server::xyz::openbmc_project::state::boot; |
Dhruvaraj Subhashchandran | a3b8d7e | 2017-08-10 05:40:04 -0500 | [diff] [blame] | 47 | namespace osstatus = |
Patrick Williams | 7e969cb | 2023-08-23 16:24:23 -0500 | [diff] [blame] | 48 | sdbusplus::server::xyz::openbmc_project::state::operating_system; |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 49 | using namespace phosphor::logging; |
Patrick Williams | 6ed41ea | 2022-04-05 15:50:21 -0500 | [diff] [blame] | 50 | namespace fs = std::filesystem; |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 51 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 52 | |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 53 | constexpr auto ACTIVE_STATE = "active"; |
| 54 | constexpr auto ACTIVATING_STATE = "activating"; |
| 55 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 56 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 57 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 58 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 59 | |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 60 | constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 61 | constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit"; |
| 62 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 63 | void Host::determineInitialState() |
| 64 | { |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 65 | if (stateActive(getTarget(server::Host::HostState::Running)) || |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 66 | isHostRunning(id)) |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 67 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 68 | info("Initial Host State will be Running"); |
Peter Yin | 73d7ad0 | 2024-03-14 13:41:11 +0800 | [diff] [blame] | 69 | server::Host::currentHostState(HostState::Running, true); |
| 70 | server::Host::requestedHostTransition(Transition::On, true); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 71 | } |
| 72 | else |
| 73 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 74 | info("Initial Host State will be Off"); |
Peter Yin | 73d7ad0 | 2024-03-14 13:41:11 +0800 | [diff] [blame] | 75 | server::Host::currentHostState(HostState::Off, true); |
| 76 | server::Host::requestedHostTransition(Transition::Off, true); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 77 | } |
| 78 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 79 | if (!deserialize()) |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 80 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 81 | // set to default value. |
Peter Yin | 73d7ad0 | 2024-03-14 13:41:11 +0800 | [diff] [blame] | 82 | server::Host::requestedHostTransition(Transition::Off, true); |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 83 | } |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
Andrew Geissler | 54ce639 | 2024-02-23 10:09:09 -0600 | [diff] [blame] | 87 | void Host::setupSupportedTransitions() |
| 88 | { |
| 89 | std::set<Transition> supportedTransitions = { |
Andrew Geissler | 31cddb7 | 2024-02-27 14:20:26 -0600 | [diff] [blame] | 90 | Transition::On, |
| 91 | Transition::Off, |
| 92 | Transition::Reboot, |
| 93 | Transition::GracefulWarmReboot, |
| 94 | #if ENABLE_FORCE_WARM_REBOOT |
| 95 | Transition::ForceWarmReboot, |
| 96 | #endif |
| 97 | }; |
Andrew Geissler | 54ce639 | 2024-02-23 10:09:09 -0600 | [diff] [blame] | 98 | server::Host::allowedHostTransitions(supportedTransitions); |
| 99 | } |
| 100 | |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 101 | void Host::createSystemdTargetMaps() |
| 102 | { |
| 103 | stateTargetTable = { |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 104 | {HostState::Off, std::format("obmc-host-stop@{}.target", id)}, |
| 105 | {HostState::Running, std::format("obmc-host-startmin@{}.target", id)}, |
| 106 | {HostState::Quiesced, std::format("obmc-host-quiesce@{}.target", id)}, |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 107 | {HostState::DiagnosticMode, |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 108 | std::format("obmc-host-diagnostic-mode@{}.target", id)}}; |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 109 | |
| 110 | transitionTargetTable = { |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 111 | {Transition::Off, std::format("obmc-host-shutdown@{}.target", id)}, |
| 112 | {Transition::On, std::format("obmc-host-start@{}.target", id)}, |
| 113 | {Transition::Reboot, std::format("obmc-host-reboot@{}.target", id)}, |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 114 | // Some systems do not support a warm reboot so just map the reboot |
| 115 | // requests to our normal cold reboot in that case |
| 116 | #if ENABLE_WARM_REBOOT |
| 117 | {Transition::GracefulWarmReboot, |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 118 | std::format("obmc-host-warm-reboot@{}.target", id)}, |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 119 | {Transition::ForceWarmReboot, |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame] | 120 | std::format("obmc-host-force-warm-reboot@{}.target", id)}}; |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 121 | #else |
| 122 | {Transition::GracefulWarmReboot, |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 123 | std::format("obmc-host-reboot@{}.target", id)}, |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 124 | {Transition::ForceWarmReboot, |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame] | 125 | std::format("obmc-host-reboot@{}.target", id)}}; |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 126 | #endif |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 127 | hostCrashTarget = std::format("obmc-host-crash@{}.target", id); |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | const std::string& Host::getTarget(HostState state) |
| 131 | { |
| 132 | return stateTargetTable[state]; |
| 133 | }; |
| 134 | |
| 135 | const std::string& Host::getTarget(Transition tranReq) |
| 136 | { |
| 137 | return transitionTargetTable[tranReq]; |
| 138 | }; |
| 139 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 140 | void Host::executeTransition(Transition tranReq) |
| 141 | { |
Pavithra Barithaya | 319eda4 | 2024-06-21 11:54:43 -0500 | [diff] [blame] | 142 | const auto& sysdUnit = getTarget(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 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 145 | SYSTEMD_INTERFACE, "StartUnit"); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 146 | |
| 147 | method.append(sysdUnit); |
| 148 | method.append("replace"); |
| 149 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 150 | this->bus.call_noreply(method); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 151 | |
| 152 | return; |
| 153 | } |
| 154 | |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 155 | bool Host::stateActive(const std::string& target) |
| 156 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 157 | std::variant<std::string> currentState; |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 158 | sdbusplus::message::object_path unitTargetPath; |
| 159 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 160 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 161 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 162 | |
| 163 | method.append(target); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 164 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 165 | try |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 166 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 167 | auto result = this->bus.call(method); |
| 168 | result.read(unitTargetPath); |
| 169 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 170 | catch (const sdbusplus::exception_t& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 171 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 172 | error("Error in GetUnit call: {ERROR}", "ERROR", e); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 176 | method = this->bus.new_method_call( |
| 177 | SYSTEMD_SERVICE, |
| 178 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 179 | SYSTEMD_PROPERTY_IFACE, "Get"); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 180 | |
| 181 | method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState"); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 182 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 183 | try |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 184 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 185 | auto result = this->bus.call(method); |
| 186 | result.read(currentState); |
| 187 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 188 | catch (const sdbusplus::exception_t& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 189 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 190 | error("Error in ActiveState Get: {ERROR}", "ERROR", e); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 194 | const auto& currentStateStr = std::get<std::string>(currentState); |
William A. Kennington III | 7a0689a | 2018-11-12 17:19:33 -0800 | [diff] [blame] | 195 | return currentStateStr == ACTIVE_STATE || |
| 196 | currentStateStr == ACTIVATING_STATE; |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 197 | } |
| 198 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 199 | bool Host::isAutoReboot() |
| 200 | { |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 201 | using namespace settings; |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 202 | |
Andrew Geissler | 7f62083 | 2020-10-23 08:25:52 -0500 | [diff] [blame] | 203 | /* The logic here is to first check the one-time AutoReboot setting. |
| 204 | * If this property is true (the default) then look at the persistent |
| 205 | * user setting in the non one-time object, otherwise honor the one-time |
| 206 | * setting and do not auto reboot. |
| 207 | */ |
| 208 | auto methodOneTime = bus.new_method_call( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 209 | settings.service(settings.autoReboot, autoRebootIntf).c_str(), |
Andrew Geissler | 7f62083 | 2020-10-23 08:25:52 -0500 | [diff] [blame] | 210 | settings.autoRebootOneTime.c_str(), SYSTEMD_PROPERTY_IFACE, "Get"); |
| 211 | methodOneTime.append(autoRebootIntf, "AutoReboot"); |
| 212 | |
| 213 | auto methodUserSetting = bus.new_method_call( |
| 214 | settings.service(settings.autoReboot, autoRebootIntf).c_str(), |
| 215 | settings.autoReboot.c_str(), SYSTEMD_PROPERTY_IFACE, "Get"); |
| 216 | methodUserSetting.append(autoRebootIntf, "AutoReboot"); |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 217 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 218 | try |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 219 | { |
Andrew Geissler | 7f62083 | 2020-10-23 08:25:52 -0500 | [diff] [blame] | 220 | auto reply = bus.call(methodOneTime); |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 221 | std::variant<bool> result; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 222 | reply.read(result); |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 223 | auto autoReboot = std::get<bool>(result); |
Andrew Geissler | 7f62083 | 2020-10-23 08:25:52 -0500 | [diff] [blame] | 224 | |
| 225 | if (!autoReboot) |
| 226 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 227 | info("Auto reboot (one-time) disabled"); |
Andrew Geissler | 7f62083 | 2020-10-23 08:25:52 -0500 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | // one-time is true so read the user setting |
| 233 | reply = bus.call(methodUserSetting); |
| 234 | reply.read(result); |
| 235 | autoReboot = std::get<bool>(result); |
| 236 | } |
| 237 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 238 | auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft(); |
| 239 | |
| 240 | if (autoReboot) |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 241 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 242 | if (rebootCounterParam > 0) |
| 243 | { |
| 244 | // Reduce BOOTCOUNT by 1 |
Andrew Geissler | ad65b2d | 2021-09-21 12:53:29 -0500 | [diff] [blame] | 245 | info( |
| 246 | "Auto reboot enabled and boot count at {BOOTCOUNT}, rebooting", |
| 247 | "BOOTCOUNT", rebootCounterParam); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 248 | return true; |
| 249 | } |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 250 | else |
| 251 | { |
Andrew Geissler | 4328479 | 2021-09-23 10:32:15 -0500 | [diff] [blame] | 252 | // We are at 0 so reset reboot counter and go to quiesce state |
| 253 | info("Auto reboot enabled but HOST BOOTCOUNT already set to 0"); |
NodeMan97 | b4cbfac | 2022-05-09 23:56:39 -0500 | [diff] [blame] | 254 | attemptsLeft(reboot::RebootAttempts::retryAttempts()); |
Andrew Geissler | 1ab2b6c | 2022-03-10 16:16:15 -0600 | [diff] [blame] | 255 | |
| 256 | // Generate log since we will now be sitting in Quiesce |
| 257 | const std::string errorMsg = |
| 258 | "xyz.openbmc_project.State.Error.HostQuiesce"; |
| 259 | utils::createError(this->bus, errorMsg, |
| 260 | sdbusplus::xyz::openbmc_project::Logging:: |
| 261 | server::Entry::Level::Critical); |
Andrew Geissler | 5bcaee1 | 2022-04-19 15:40:40 -0400 | [diff] [blame] | 262 | |
| 263 | // Generate BMC dump to assist with debug |
| 264 | utils::createBmcDump(this->bus); |
| 265 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 266 | return false; |
| 267 | } |
Saqib Khan | d5ac635 | 2017-04-04 09:53:59 -0500 | [diff] [blame] | 268 | } |
| 269 | else |
| 270 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 271 | info("Auto reboot disabled."); |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 272 | return false; |
| 273 | } |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 274 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 275 | catch (const sdbusplus::exception_t& e) |
Saqib Khan | d5ac635 | 2017-04-04 09:53:59 -0500 | [diff] [blame] | 276 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 277 | error("Error in AutoReboot Get, {ERROR}", "ERROR", e); |
Saqib Khan | d5ac635 | 2017-04-04 09:53:59 -0500 | [diff] [blame] | 278 | return false; |
| 279 | } |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 280 | } |
| 281 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 282 | void Host::sysStateChangeJobRemoved(sdbusplus::message_t& msg) |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 283 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 284 | uint32_t newStateID{}; |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 285 | sdbusplus::message::object_path newStateObjPath; |
| 286 | std::string newStateUnit{}; |
| 287 | std::string newStateResult{}; |
| 288 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 289 | // Read the msg and populate each variable |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 290 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 291 | |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 292 | if ((newStateUnit == getTarget(server::Host::HostState::Off)) && |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame] | 293 | (newStateResult == "done") && |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 294 | (!stateActive(getTarget(server::Host::HostState::Running)))) |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 295 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 296 | info("Received signal that host is off"); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 297 | this->currentHostState(server::Host::HostState::Off); |
Dhruvaraj Subhashchandran | a3b8d7e | 2017-08-10 05:40:04 -0500 | [diff] [blame] | 298 | this->bootProgress(bootprogress::Progress::ProgressStages::Unspecified); |
| 299 | this->operatingSystemState(osstatus::Status::OSStatus::Inactive); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 300 | } |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 301 | else if ((newStateUnit == getTarget(server::Host::HostState::Running)) && |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 302 | (newStateResult == "done") && |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 303 | (stateActive(getTarget(server::Host::HostState::Running)))) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 304 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 305 | info("Received signal that host is running"); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 306 | this->currentHostState(server::Host::HostState::Running); |
Andrew Geissler | 131d04a | 2021-03-12 11:02:41 -0600 | [diff] [blame] | 307 | |
| 308 | // Remove temporary file which is utilized for scenarios where the |
| 309 | // BMC is rebooted while the host is still up. |
| 310 | // This file is used to indicate to host related systemd services |
| 311 | // that the host is already running and they should skip running. |
| 312 | // Once the host state is back to running we can clear this file. |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 313 | std::string hostFile = std::format(HOST_RUNNING_FILE, 0); |
| 314 | if (std::filesystem::exists(hostFile)) |
Andrew Geissler | 131d04a | 2021-03-12 11:02:41 -0600 | [diff] [blame] | 315 | { |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 316 | std::filesystem::remove(hostFile); |
Andrew Geissler | 131d04a | 2021-03-12 11:02:41 -0600 | [diff] [blame] | 317 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 318 | } |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 319 | else if ((newStateUnit == getTarget(server::Host::HostState::Quiesced)) && |
Josh D. King | 29d025d | 2017-04-27 12:40:22 -0500 | [diff] [blame] | 320 | (newStateResult == "done") && |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 321 | (stateActive(getTarget(server::Host::HostState::Quiesced)))) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 322 | { |
| 323 | if (Host::isAutoReboot()) |
| 324 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 325 | info("Beginning reboot..."); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 326 | Host::requestedHostTransition(server::Host::Transition::Reboot); |
| 327 | } |
| 328 | else |
| 329 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 330 | info("Maintaining quiesce"); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 331 | this->currentHostState(server::Host::HostState::Quiesced); |
| 332 | } |
| 333 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 334 | } |
| 335 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 336 | void Host::sysStateChangeJobNew(sdbusplus::message_t& msg) |
Andrew Geissler | 47b9612 | 2020-02-11 16:13:13 -0600 | [diff] [blame] | 337 | { |
| 338 | uint32_t newStateID{}; |
| 339 | sdbusplus::message::object_path newStateObjPath; |
| 340 | std::string newStateUnit{}; |
| 341 | |
| 342 | // Read the msg and populate each variable |
| 343 | msg.read(newStateID, newStateObjPath, newStateUnit); |
| 344 | |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 345 | if (newStateUnit == getTarget(server::Host::HostState::DiagnosticMode)) |
Andrew Geissler | 47b9612 | 2020-02-11 16:13:13 -0600 | [diff] [blame] | 346 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 347 | info("Received signal that host is in diagnostice mode"); |
Andrew Geissler | 47b9612 | 2020-02-11 16:13:13 -0600 | [diff] [blame] | 348 | this->currentHostState(server::Host::HostState::DiagnosticMode); |
| 349 | } |
Andrew Geissler | 128ea8e | 2022-06-22 12:28:15 -0400 | [diff] [blame] | 350 | else if ((newStateUnit == hostCrashTarget) && |
| 351 | (server::Host::currentHostState() == |
| 352 | server::Host::HostState::Running)) |
| 353 | { |
| 354 | // Only decrease the boot count if host was running when the host crash |
| 355 | // target was started. Systemd will sometimes trigger multiple |
| 356 | // JobNew events for the same target. This seems to be related to |
| 357 | // how OpenBMC utilizes the targets in the reboot scenario |
| 358 | info("Received signal that host has crashed, decrement reboot count"); |
| 359 | |
| 360 | // A host crash can cause a reboot of the host so decrement the reboot |
| 361 | // count |
| 362 | decrementRebootCount(); |
| 363 | } |
Andrew Geissler | 47b9612 | 2020-02-11 16:13:13 -0600 | [diff] [blame] | 364 | } |
| 365 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 366 | uint32_t Host::decrementRebootCount() |
| 367 | { |
| 368 | auto rebootCount = reboot::RebootAttempts::attemptsLeft(); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 369 | if (rebootCount > 0) |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 370 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 371 | return (reboot::RebootAttempts::attemptsLeft(rebootCount - 1)); |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 372 | } |
| 373 | return rebootCount; |
| 374 | } |
| 375 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 376 | fs::path Host::serialize() |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 377 | { |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 378 | fs::path path{std::format(HOST_STATE_PERSIST_PATH, id)}; |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 379 | std::ofstream os(path.c_str(), std::ios::binary); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 380 | cereal::JSONOutputArchive oarchive(os); |
| 381 | oarchive(*this); |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 382 | return path; |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 383 | } |
| 384 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 385 | bool Host::deserialize() |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 386 | { |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 387 | fs::path path{std::format(HOST_STATE_PERSIST_PATH, id)}; |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 388 | try |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 389 | { |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 390 | if (fs::exists(path)) |
| 391 | { |
| 392 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 393 | cereal::JSONInputArchive iarchive(is); |
| 394 | iarchive(*this); |
| 395 | return true; |
| 396 | } |
| 397 | return false; |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 398 | } |
Patrick Williams | 8583b3b | 2021-10-06 12:19:20 -0500 | [diff] [blame] | 399 | catch (const cereal::Exception& e) |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 400 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 401 | error("deserialize exception: {ERROR}", "ERROR", e); |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 402 | fs::remove(path); |
| 403 | return false; |
| 404 | } |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 405 | } |
| 406 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 407 | Host::Transition Host::requestedHostTransition(Transition value) |
| 408 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 409 | info("Host state transition request of {REQ}", "REQ", value); |
Andrew Geissler | 765446a | 2023-05-25 15:38:20 -0400 | [diff] [blame] | 410 | |
| 411 | #if ONLY_ALLOW_BOOT_WHEN_BMC_READY |
| 412 | if ((value != Transition::Off) && (!utils::isBmcReady(this->bus))) |
| 413 | { |
| 414 | info("BMC State is not Ready so no host on operations allowed"); |
| 415 | throw sdbusplus::xyz::openbmc_project::State::Host::Error:: |
| 416 | BMCNotReady(); |
| 417 | } |
| 418 | #endif |
| 419 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 420 | // If this is not a power off request then we need to |
| 421 | // decrement the reboot counter. This code should |
| 422 | // never prevent a power on, it should just decrement |
| 423 | // the count to 0. The quiesce handling is where the |
| 424 | // check of this count will occur |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 425 | if (value != server::Host::Transition::Off) |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 426 | { |
Thang Tran | 9f38152 | 2024-10-18 10:11:01 +0700 | [diff] [blame^] | 427 | #ifdef CHECK_FWUPDATE_BEFORE_DO_TRANSITION |
| 428 | /* |
| 429 | * Do not do transition when the any firmware being updated |
| 430 | */ |
| 431 | if (phosphor::state::manager::utils::isFirmwareUpdating(this->bus)) |
| 432 | { |
| 433 | info("Firmware being updated, reject the transition request"); |
| 434 | throw sdbusplus::xyz::openbmc_project::Common::Error::Unavailable(); |
| 435 | } |
| 436 | #endif // CHECK_FWUPDATE_BEFORE_DO_TRANSITION |
| 437 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 438 | decrementRebootCount(); |
| 439 | } |
| 440 | |
Andrew Geissler | a27a6e8 | 2017-07-27 16:44:43 -0500 | [diff] [blame] | 441 | executeTransition(value); |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 442 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 443 | auto retVal = server::Host::requestedHostTransition(value); |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 444 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 445 | serialize(); |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 446 | return retVal; |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 447 | } |
| 448 | |
Dhruvaraj Subhashchandran | 4e6534f | 2017-09-19 06:13:20 -0500 | [diff] [blame] | 449 | Host::ProgressStages Host::bootProgress(ProgressStages value) |
| 450 | { |
| 451 | auto retVal = bootprogress::Progress::bootProgress(value); |
| 452 | serialize(); |
| 453 | return retVal; |
| 454 | } |
| 455 | |
| 456 | Host::OSStatus Host::operatingSystemState(OSStatus value) |
| 457 | { |
| 458 | auto retVal = osstatus::Status::operatingSystemState(value); |
| 459 | serialize(); |
| 460 | return retVal; |
| 461 | } |
| 462 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 463 | Host::HostState Host::currentHostState(HostState value) |
| 464 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 465 | info("Change to Host State: {STATE}", "STATE", value); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 466 | return server::Host::currentHostState(value); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 467 | } |
| 468 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 469 | } // namespace manager |
| 470 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 471 | } // namespace phosphor |