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