Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 1 | #include <iostream> |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 2 | #include <map> |
| 3 | #include <string> |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 4 | #include <systemd/sd-bus.h> |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 5 | #include <cereal/cereal.hpp> |
| 6 | #include <cereal/types/string.hpp> |
| 7 | #include <cereal/types/vector.hpp> |
| 8 | #include <cereal/types/tuple.hpp> |
| 9 | #include <cereal/archives/json.hpp> |
| 10 | #include <fstream> |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 11 | #include <sdbusplus/server.hpp> |
Saqib Khan | a8006a2 | 2017-02-14 11:37:08 -0600 | [diff] [blame] | 12 | #include <phosphor-logging/log.hpp> |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 13 | #include <phosphor-logging/elog-errors.hpp> |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp> |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Common/error.hpp> |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 16 | #include "host_state_manager.hpp" |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 17 | #include "config.h" |
| 18 | |
Vishwanatha Subbanna | 0a83873 | 2017-10-05 12:43:19 +0530 | [diff] [blame] | 19 | // Register class version with Cereal |
| 20 | CEREAL_CLASS_VERSION(phosphor::state::manager::Host, CLASS_VERSION); |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace state |
| 25 | { |
| 26 | namespace manager |
| 27 | { |
| 28 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 29 | // 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] | 30 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 31 | namespace reboot = sdbusplus::xyz::openbmc_project::Control::Boot::server; |
Dhruvaraj Subhashchandran | a3b8d7e | 2017-08-10 05:40:04 -0500 | [diff] [blame] | 32 | namespace bootprogress = sdbusplus::xyz::openbmc_project::State::Boot::server; |
| 33 | namespace osstatus = |
| 34 | sdbusplus::xyz::openbmc_project::State::OperatingSystem::server; |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 35 | using namespace phosphor::logging; |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 36 | namespace fs = std::experimental::filesystem; |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 37 | |
Andrew Geissler | 4f309e8 | 2017-05-24 15:18:01 -0500 | [diff] [blame] | 38 | // host-shutdown notifies host of shutdown and that leads to host-stop being |
| 39 | // called so initiate a host shutdown with the -shutdown target and consider the |
| 40 | // host shut down when the -stop target is complete |
| 41 | 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] | 42 | constexpr auto HOST_STATE_POWEROFF_TGT = "obmc-host-stop@0.target"; |
| 43 | constexpr auto HOST_STATE_POWERON_TGT = "obmc-host-start@0.target"; |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame^] | 44 | constexpr auto HOST_STATE_POWERON_MIN_TGT = "obmc-host-startmin@0.target"; |
Andrew Geissler | a27a6e8 | 2017-07-27 16:44:43 -0500 | [diff] [blame] | 45 | constexpr auto HOST_STATE_REBOOT_TGT = "obmc-host-reboot@0.target"; |
Josh D. King | cc3fb5d | 2017-04-19 15:45:10 -0500 | [diff] [blame] | 46 | constexpr auto HOST_STATE_QUIESCE_TGT = "obmc-host-quiesce@0.target"; |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 47 | |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 48 | constexpr auto ACTIVE_STATE = "active"; |
| 49 | constexpr auto ACTIVATING_STATE = "activating"; |
| 50 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 51 | /* Map a transition to it's systemd target */ |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 52 | const std::map<server::Host::Transition, std::string> SYSTEMD_TARGET_TABLE = { |
| 53 | {server::Host::Transition::Off, HOST_STATE_SOFT_POWEROFF_TGT}, |
| 54 | {server::Host::Transition::On, HOST_STATE_POWERON_TGT}, |
| 55 | {server::Host::Transition::Reboot, HOST_STATE_REBOOT_TGT}}; |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 56 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 57 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 58 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 59 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 60 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 61 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
Leonel Gonzalez | f318d87 | 2017-03-16 13:47:49 -0500 | [diff] [blame] | 62 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 63 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 64 | |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 65 | constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 66 | constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit"; |
| 67 | |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 68 | /* Map a system state to the HostState */ |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 69 | const std::map<std::string, server::Host::HostState> SYS_HOST_STATE_TABLE = { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 70 | {"HOST_BOOTING", server::Host::HostState::Running}, |
| 71 | {"HOST_POWERED_OFF", server::Host::HostState::Off}, |
| 72 | {"HOST_QUIESCED", server::Host::HostState::Quiesced}}; |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 73 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 74 | void Host::subscribeToSystemdSignals() |
| 75 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 76 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 77 | SYSTEMD_INTERFACE, "Subscribe"); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 78 | this->bus.call_noreply(method); |
| 79 | |
| 80 | return; |
| 81 | } |
| 82 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 83 | void Host::determineInitialState() |
| 84 | { |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 85 | |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame^] | 86 | if (stateActive(HOST_STATE_POWERON_MIN_TGT)) |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 87 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 88 | log<level::INFO>("Initial Host State will be Running", |
| 89 | entry("CURRENT_HOST_STATE=%s", |
| 90 | convertForMessage(HostState::Running).c_str())); |
Andrew Geissler | 9792414 | 2017-01-24 16:10:18 -0600 | [diff] [blame] | 91 | server::Host::currentHostState(HostState::Running); |
| 92 | server::Host::requestedHostTransition(Transition::On); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 93 | } |
| 94 | else |
| 95 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 96 | log<level::INFO>("Initial Host State will be Off", |
| 97 | entry("CURRENT_HOST_STATE=%s", |
| 98 | convertForMessage(HostState::Off).c_str())); |
Andrew Geissler | 9792414 | 2017-01-24 16:10:18 -0600 | [diff] [blame] | 99 | server::Host::currentHostState(HostState::Off); |
| 100 | server::Host::requestedHostTransition(Transition::Off); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 101 | } |
| 102 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 103 | if (!deserialize(HOST_STATE_PERSIST_PATH)) |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 104 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 105 | // set to default value. |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 106 | server::Host::requestedHostTransition(Transition::Off); |
| 107 | } |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 108 | |
| 109 | return; |
| 110 | } |
| 111 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 112 | void Host::executeTransition(Transition tranReq) |
| 113 | { |
| 114 | auto sysdUnit = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 115 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 116 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 117 | SYSTEMD_INTERFACE, "StartUnit"); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 118 | |
| 119 | method.append(sysdUnit); |
| 120 | method.append("replace"); |
| 121 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 122 | this->bus.call_noreply(method); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 123 | |
| 124 | return; |
| 125 | } |
| 126 | |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 127 | bool Host::stateActive(const std::string& target) |
| 128 | { |
| 129 | sdbusplus::message::variant<std::string> currentState; |
| 130 | sdbusplus::message::object_path unitTargetPath; |
| 131 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 132 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 133 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 134 | |
| 135 | method.append(target); |
| 136 | auto result = this->bus.call(method); |
| 137 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 138 | // Check that the bus call didn't result in an error |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 139 | if (result.is_method_error()) |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 140 | { |
| 141 | log<level::ERR>("Error in bus call - could not resolve GetUnit for:", |
| 142 | entry(" %s", SYSTEMD_INTERFACE)); |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | result.read(unitTargetPath); |
| 147 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 148 | method = this->bus.new_method_call( |
| 149 | SYSTEMD_SERVICE, |
| 150 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 151 | SYSTEMD_PROPERTY_IFACE, "Get"); |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 152 | |
| 153 | method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState"); |
| 154 | result = this->bus.call(method); |
| 155 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 156 | // Check that the bus call didn't result in an error |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 157 | if (result.is_method_error()) |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 158 | { |
| 159 | log<level::ERR>("Error in bus call - could not resolve Get for:", |
| 160 | entry(" %s", SYSTEMD_PROPERTY_IFACE)); |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | result.read(currentState); |
| 165 | |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 166 | if (currentState != ACTIVE_STATE && currentState != ACTIVATING_STATE) |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 167 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 168 | // False - not active |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 169 | return false; |
| 170 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 171 | // True - active |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 175 | bool Host::isAutoReboot() |
| 176 | { |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 177 | using namespace settings; |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 178 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 179 | auto method = bus.new_method_call( |
| 180 | settings.service(settings.autoReboot, autoRebootIntf).c_str(), |
| 181 | settings.autoReboot.c_str(), "org.freedesktop.DBus.Properties", "Get"); |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 182 | method.append(autoRebootIntf, "AutoReboot"); |
| 183 | auto reply = bus.call(method); |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 184 | if (reply.is_method_error()) |
| 185 | { |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 186 | log<level::ERR>("Error in AutoReboot Get"); |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 190 | sdbusplus::message::variant<bool> result; |
| 191 | reply.read(result); |
| 192 | auto autoReboot = result.get<bool>(); |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 193 | auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft(); |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 194 | |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 195 | if (autoReboot) |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 196 | { |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 197 | if (rebootCounterParam > 0) |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 198 | { |
| 199 | // Reduce BOOTCOUNT by 1 |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 200 | log<level::INFO>("Auto reboot enabled, rebooting"); |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 201 | return true; |
| 202 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 203 | else if (rebootCounterParam == 0) |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 204 | { |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 205 | // Reset reboot counter and go to quiesce state |
Saqib Khan | d5ac635 | 2017-04-04 09:53:59 -0500 | [diff] [blame] | 206 | log<level::INFO>("Auto reboot enabled. " |
| 207 | "HOST BOOTCOUNT already set to 0."); |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 208 | attemptsLeft(BOOT_COUNT_MAX_ALLOWED); |
Saqib Khan | d5ac635 | 2017-04-04 09:53:59 -0500 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | log<level::INFO>("Auto reboot enabled. " |
| 214 | "HOST BOOTCOUNT has an invalid value."); |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 215 | return false; |
| 216 | } |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 217 | } |
Saqib Khan | d5ac635 | 2017-04-04 09:53:59 -0500 | [diff] [blame] | 218 | else |
| 219 | { |
| 220 | log<level::INFO>("Auto reboot disabled."); |
| 221 | return false; |
| 222 | } |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 223 | } |
| 224 | |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 225 | void Host::sysStateChange(sdbusplus::message::message& msg) |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 226 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 227 | uint32_t newStateID{}; |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 228 | sdbusplus::message::object_path newStateObjPath; |
| 229 | std::string newStateUnit{}; |
| 230 | std::string newStateResult{}; |
| 231 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 232 | // Read the msg and populate each variable |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 233 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 234 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 235 | if ((newStateUnit == HOST_STATE_POWEROFF_TGT) && |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame^] | 236 | (newStateResult == "done") && |
| 237 | (!stateActive(HOST_STATE_POWERON_MIN_TGT))) |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 238 | { |
Andrew Jeffery | 55b983e | 2017-04-19 11:11:26 +0930 | [diff] [blame] | 239 | log<level::INFO>("Received signal that host is off"); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 240 | this->currentHostState(server::Host::HostState::Off); |
Dhruvaraj Subhashchandran | a3b8d7e | 2017-08-10 05:40:04 -0500 | [diff] [blame] | 241 | this->bootProgress(bootprogress::Progress::ProgressStages::Unspecified); |
| 242 | this->operatingSystemState(osstatus::Status::OSStatus::Inactive); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 243 | } |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame^] | 244 | else if ((newStateUnit == HOST_STATE_POWERON_MIN_TGT) && |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 245 | (newStateResult == "done") && |
Andrew Geissler | 969b261 | 2018-03-29 10:16:51 -0700 | [diff] [blame^] | 246 | (stateActive(HOST_STATE_POWERON_MIN_TGT))) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 247 | { |
| 248 | log<level::INFO>("Received signal that host is running"); |
| 249 | this->currentHostState(server::Host::HostState::Running); |
| 250 | } |
| 251 | else if ((newStateUnit == HOST_STATE_QUIESCE_TGT) && |
Josh D. King | 29d025d | 2017-04-27 12:40:22 -0500 | [diff] [blame] | 252 | (newStateResult == "done") && |
| 253 | (stateActive(HOST_STATE_QUIESCE_TGT))) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 254 | { |
| 255 | if (Host::isAutoReboot()) |
| 256 | { |
| 257 | log<level::INFO>("Beginning reboot..."); |
| 258 | Host::requestedHostTransition(server::Host::Transition::Reboot); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | log<level::INFO>("Maintaining quiesce"); |
| 263 | this->currentHostState(server::Host::HostState::Quiesced); |
| 264 | } |
| 265 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 266 | } |
| 267 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 268 | uint32_t Host::decrementRebootCount() |
| 269 | { |
| 270 | auto rebootCount = reboot::RebootAttempts::attemptsLeft(); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 271 | if (rebootCount > 0) |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 272 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 273 | return (reboot::RebootAttempts::attemptsLeft(rebootCount - 1)); |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 274 | } |
| 275 | return rebootCount; |
| 276 | } |
| 277 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 278 | fs::path Host::serialize(const fs::path& dir) |
| 279 | { |
| 280 | std::ofstream os(dir.c_str(), std::ios::binary); |
| 281 | cereal::JSONOutputArchive oarchive(os); |
| 282 | oarchive(*this); |
| 283 | return dir; |
| 284 | } |
| 285 | |
| 286 | bool Host::deserialize(const fs::path& path) |
| 287 | { |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 288 | try |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 289 | { |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 290 | if (fs::exists(path)) |
| 291 | { |
| 292 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 293 | cereal::JSONInputArchive iarchive(is); |
| 294 | iarchive(*this); |
| 295 | return true; |
| 296 | } |
| 297 | return false; |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 298 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 299 | catch (cereal::Exception& e) |
Jayanth Othayoth | e39f379 | 2017-09-19 23:53:31 -0500 | [diff] [blame] | 300 | { |
| 301 | log<level::ERR>(e.what()); |
| 302 | fs::remove(path); |
| 303 | return false; |
| 304 | } |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 307 | Host::Transition Host::requestedHostTransition(Transition value) |
| 308 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 309 | log<level::INFO>("Host State transaction request", |
| 310 | entry("REQUESTED_HOST_TRANSITION=%s", |
| 311 | convertForMessage(value).c_str())); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 312 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 313 | // If this is not a power off request then we need to |
| 314 | // decrement the reboot counter. This code should |
| 315 | // never prevent a power on, it should just decrement |
| 316 | // the count to 0. The quiesce handling is where the |
| 317 | // check of this count will occur |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 318 | if (value != server::Host::Transition::Off) |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 319 | { |
| 320 | decrementRebootCount(); |
| 321 | } |
| 322 | |
Andrew Geissler | a27a6e8 | 2017-07-27 16:44:43 -0500 | [diff] [blame] | 323 | executeTransition(value); |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 324 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 325 | auto retVal = server::Host::requestedHostTransition(value); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 326 | serialize(); |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 327 | return retVal; |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 328 | } |
| 329 | |
Dhruvaraj Subhashchandran | 4e6534f | 2017-09-19 06:13:20 -0500 | [diff] [blame] | 330 | Host::ProgressStages Host::bootProgress(ProgressStages value) |
| 331 | { |
| 332 | auto retVal = bootprogress::Progress::bootProgress(value); |
| 333 | serialize(); |
| 334 | return retVal; |
| 335 | } |
| 336 | |
| 337 | Host::OSStatus Host::operatingSystemState(OSStatus value) |
| 338 | { |
| 339 | auto retVal = osstatus::Status::operatingSystemState(value); |
| 340 | serialize(); |
| 341 | return retVal; |
| 342 | } |
| 343 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 344 | Host::HostState Host::currentHostState(HostState value) |
| 345 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 346 | log<level::INFO>( |
| 347 | "Change to Host State", |
| 348 | entry("CURRENT_HOST_STATE=%s", convertForMessage(value).c_str())); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 349 | return server::Host::currentHostState(value); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 350 | } |
| 351 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 352 | } // namespace manager |
| 353 | } // namespace state |
| 354 | } // namepsace phosphor |