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