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