blob: 16f2bbff64babe809640cb8289646ec489cb7dae [file] [log] [blame]
Andrew Geisslere426b582020-05-28 12:40:55 -05001#include "config.h"
2
3#include "host_state_manager.hpp"
4
Andrew Geissler0d1c3f12021-07-27 16:21:01 -04005#include "host_check.hpp"
6
Andrew Geissler131d04a2021-03-12 11:02:41 -06007#include <stdio.h>
Andrew Geisslere426b582020-05-28 12:40:55 -05008#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 Geissler8ffdb262021-09-20 15:25:19 -050016#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050017#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 Geissler131d04a2021-03-12 11:02:41 -060022#include <filesystem>
Andrew Geisslere426b582020-05-28 12:40:55 -050023#include <fstream>
Andrew Geissler36529022016-11-29 15:23:54 -060024#include <iostream>
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060025#include <map>
26#include <string>
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050027
Vishwanatha Subbanna0a838732017-10-05 12:43:19 +053028// Register class version with Cereal
Andrew Geissler769a62f2019-12-06 13:36:08 -060029CEREAL_CLASS_VERSION(phosphor::state::manager::Host, CLASS_VERSION)
Andrew Geissler36529022016-11-29 15:23:54 -060030
31namespace phosphor
32{
33namespace state
34{
35namespace manager
36{
37
Andrew Geissler8ffdb262021-09-20 15:25:19 -050038PHOSPHOR_LOG2_USING;
39
Andrew Geissler7b90a622017-08-08 11:41:08 -050040// When you see server:: or reboot:: you know we're referencing our base class
Andrew Geissler3e3b84b2016-12-02 15:46:17 -060041namespace server = sdbusplus::xyz::openbmc_project::State::server;
Andrew Geissler7b90a622017-08-08 11:41:08 -050042namespace reboot = sdbusplus::xyz::openbmc_project::Control::Boot::server;
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -050043namespace bootprogress = sdbusplus::xyz::openbmc_project::State::Boot::server;
44namespace osstatus =
45 sdbusplus::xyz::openbmc_project::State::OperatingSystem::server;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060046using namespace phosphor::logging;
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050047namespace fs = std::experimental::filesystem;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050048using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060049
Andrew Geissler4f309e82017-05-24 15:18:01 -050050// 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
53constexpr auto HOST_STATE_SOFT_POWEROFF_TGT = "obmc-host-shutdown@0.target";
Josh D. Kingca357922017-04-11 13:44:09 -050054constexpr auto HOST_STATE_POWEROFF_TGT = "obmc-host-stop@0.target";
55constexpr auto HOST_STATE_POWERON_TGT = "obmc-host-start@0.target";
Andrew Geissler969b2612018-03-29 10:16:51 -070056constexpr auto HOST_STATE_POWERON_MIN_TGT = "obmc-host-startmin@0.target";
Andrew Geisslera27a6e82017-07-27 16:44:43 -050057constexpr auto HOST_STATE_REBOOT_TGT = "obmc-host-reboot@0.target";
Andrew Geissler40dd6e72020-02-07 14:31:12 -060058constexpr auto HOST_STATE_WARM_REBOOT = "obmc-host-warm-reboot@0.target";
59constexpr auto HOST_STATE_FORCE_WARM_REBOOT =
60 "obmc-host-force-warm-reboot@0.target";
Andrew Geissler47b96122020-02-11 16:13:13 -060061constexpr auto HOST_STATE_DIAGNOSTIC_MODE =
62 "obmc-host-diagnostic-mode@0.target";
Andrew Geissler40dd6e72020-02-07 14:31:12 -060063
Josh D. Kingcc3fb5d2017-04-19 15:45:10 -050064constexpr auto HOST_STATE_QUIESCE_TGT = "obmc-host-quiesce@0.target";
Andrew Geissler4da7e002017-01-24 15:21:40 -060065
Josh D. King929ef702017-03-02 10:58:11 -060066constexpr auto ACTIVE_STATE = "active";
67constexpr auto ACTIVATING_STATE = "activating";
68
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060069/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080070const 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 Geissler40dd6e72020-02-07 14:31:12 -060073 {server::Host::Transition::Reboot, HOST_STATE_REBOOT_TGT},
Andrew Geissler7fdad602020-06-22 13:46:16 -050074// 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 Geissler40dd6e72020-02-07 14:31:12 -060077 {server::Host::Transition::GracefulWarmReboot, HOST_STATE_WARM_REBOOT},
78 {server::Host::Transition::ForceWarmReboot, HOST_STATE_FORCE_WARM_REBOOT}};
Andrew Geissler7fdad602020-06-22 13:46:16 -050079#else
80 {server::Host::Transition::GracefulWarmReboot, HOST_STATE_REBOOT_TGT},
81 {server::Host::Transition::ForceWarmReboot, HOST_STATE_REBOOT_TGT}};
82#endif
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060083
Andrew Geissler58a18012018-01-19 19:36:05 -080084constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
85constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060086constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
87
Josh D. King929ef702017-03-02 10:58:11 -060088constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
89constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
90
Andrew Geissler4da7e002017-01-24 15:21:40 -060091void Host::subscribeToSystemdSignals()
92{
Andrew Geissler58a18012018-01-19 19:36:05 -080093 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
94 SYSTEMD_INTERFACE, "Subscribe");
Andrew Geissler2f60aae2019-09-12 13:25:21 -050095 try
96 {
97 this->bus.call_noreply(method);
98 }
Patrick Williams0a675212021-09-02 09:49:43 -050099 catch (const sdbusplus::exception::exception& e)
Andrew Geissler2f60aae2019-09-12 13:25:21 -0500100 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500101 error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
Andrew Geissler2f60aae2019-09-12 13:25:21 -0500102 elog<InternalFailure>();
103 }
Andrew Geissler4da7e002017-01-24 15:21:40 -0600104 return;
105}
106
Andrew Geissleref3c1842016-12-01 12:33:09 -0600107void Host::determineInitialState()
108{
Andrew Geissleref3c1842016-12-01 12:33:09 -0600109
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400110 if (stateActive(HOST_STATE_POWERON_MIN_TGT) || isHostRunning())
Andrew Geissleref3c1842016-12-01 12:33:09 -0600111 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500112 info("Initial Host State will be Running");
Andrew Geissler97924142017-01-24 16:10:18 -0600113 server::Host::currentHostState(HostState::Running);
114 server::Host::requestedHostTransition(Transition::On);
Andrew Geissleref3c1842016-12-01 12:33:09 -0600115 }
116 else
117 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500118 info("Initial Host State will be Off");
Andrew Geissler97924142017-01-24 16:10:18 -0600119 server::Host::currentHostState(HostState::Off);
120 server::Host::requestedHostTransition(Transition::Off);
Andrew Geissleref3c1842016-12-01 12:33:09 -0600121 }
122
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500123 if (!deserialize(HOST_STATE_PERSIST_PATH))
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500124 {
Andrew Geissler58a18012018-01-19 19:36:05 -0800125 // set to default value.
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500126 server::Host::requestedHostTransition(Transition::Off);
127 }
Andrew Geissleref3c1842016-12-01 12:33:09 -0600128
129 return;
130}
131
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600132void Host::executeTransition(Transition tranReq)
133{
134 auto sysdUnit = SYSTEMD_TARGET_TABLE.find(tranReq)->second;
135
Andrew Geissler58a18012018-01-19 19:36:05 -0800136 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
137 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600138
139 method.append(sysdUnit);
140 method.append("replace");
141
Andrew Geissler4da7e002017-01-24 15:21:40 -0600142 this->bus.call_noreply(method);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600143
144 return;
145}
146
Josh D. King929ef702017-03-02 10:58:11 -0600147bool Host::stateActive(const std::string& target)
148{
Patrick Williams2975e262020-05-13 18:01:09 -0500149 std::variant<std::string> currentState;
Josh D. King929ef702017-03-02 10:58:11 -0600150 sdbusplus::message::object_path unitTargetPath;
151
Andrew Geissler58a18012018-01-19 19:36:05 -0800152 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
153 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King929ef702017-03-02 10:58:11 -0600154
155 method.append(target);
Josh D. King929ef702017-03-02 10:58:11 -0600156
Anthony Wilson32c532e2018-10-25 21:56:07 -0500157 try
Josh D. King929ef702017-03-02 10:58:11 -0600158 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500159 auto result = this->bus.call(method);
160 result.read(unitTargetPath);
161 }
Patrick Williams0a675212021-09-02 09:49:43 -0500162 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500163 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500164 error("Error in GetUnit call: {ERROR}", "ERROR", e);
Josh D. King929ef702017-03-02 10:58:11 -0600165 return false;
166 }
167
Andrew Geissler58a18012018-01-19 19:36:05 -0800168 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. King929ef702017-03-02 10:58:11 -0600172
173 method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
Josh D. King929ef702017-03-02 10:58:11 -0600174
Anthony Wilson32c532e2018-10-25 21:56:07 -0500175 try
Josh D. King929ef702017-03-02 10:58:11 -0600176 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500177 auto result = this->bus.call(method);
178 result.read(currentState);
179 }
Patrick Williams0a675212021-09-02 09:49:43 -0500180 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500181 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500182 error("Error in ActiveState Get: {ERROR}", "ERROR", e);
Josh D. King929ef702017-03-02 10:58:11 -0600183 return false;
184 }
185
Patrick Williams37413dc2020-05-13 11:29:54 -0500186 const auto& currentStateStr = std::get<std::string>(currentState);
William A. Kennington III7a0689a2018-11-12 17:19:33 -0800187 return currentStateStr == ACTIVE_STATE ||
188 currentStateStr == ACTIVATING_STATE;
Josh D. King929ef702017-03-02 10:58:11 -0600189}
190
Michael Tritz206a8332017-02-06 16:01:23 -0600191bool Host::isAutoReboot()
192{
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500193 using namespace settings;
Michael Tritz206a8332017-02-06 16:01:23 -0600194
Andrew Geissler7f620832020-10-23 08:25:52 -0500195 /* 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 Geissler58a18012018-01-19 19:36:05 -0800201 settings.service(settings.autoReboot, autoRebootIntf).c_str(),
Andrew Geissler7f620832020-10-23 08:25:52 -0500202 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 Tritz206a8332017-02-06 16:01:23 -0600209
Anthony Wilson32c532e2018-10-25 21:56:07 -0500210 try
Michael Tritz206a8332017-02-06 16:01:23 -0600211 {
Andrew Geissler7f620832020-10-23 08:25:52 -0500212 auto reply = bus.call(methodOneTime);
Patrick Williams2975e262020-05-13 18:01:09 -0500213 std::variant<bool> result;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500214 reply.read(result);
Patrick Williams37413dc2020-05-13 11:29:54 -0500215 auto autoReboot = std::get<bool>(result);
Andrew Geissler7f620832020-10-23 08:25:52 -0500216
217 if (!autoReboot)
218 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500219 info("Auto reboot (one-time) disabled");
Andrew Geissler7f620832020-10-23 08:25:52 -0500220 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 Wilson32c532e2018-10-25 21:56:07 -0500230 auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft();
231
232 if (autoReboot)
Saqib Khancbe08d12017-03-10 01:29:20 -0600233 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500234 if (rebootCounterParam > 0)
235 {
236 // Reduce BOOTCOUNT by 1
Andrew Geisslerad65b2d2021-09-21 12:53:29 -0500237 info(
238 "Auto reboot enabled and boot count at {BOOTCOUNT}, rebooting",
239 "BOOTCOUNT", rebootCounterParam);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500240 return true;
241 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500242 else
243 {
Andrew Geissler43284792021-09-23 10:32:15 -0500244 // We are at 0 so reset reboot counter and go to quiesce state
245 info("Auto reboot enabled but HOST BOOTCOUNT already set to 0");
246 attemptsLeft(BOOT_COUNT_MAX_ALLOWED);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500247 return false;
248 }
Saqib Khand5ac6352017-04-04 09:53:59 -0500249 }
250 else
251 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500252 info("Auto reboot disabled.");
Saqib Khancbe08d12017-03-10 01:29:20 -0600253 return false;
254 }
Michael Tritz206a8332017-02-06 16:01:23 -0600255 }
Patrick Williams0a675212021-09-02 09:49:43 -0500256 catch (const sdbusplus::exception::exception& e)
Saqib Khand5ac6352017-04-04 09:53:59 -0500257 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500258 error("Error in AutoReboot Get, {ERROR}", "ERROR", e);
Saqib Khand5ac6352017-04-04 09:53:59 -0500259 return false;
260 }
Michael Tritz206a8332017-02-06 16:01:23 -0600261}
262
Andrew Geisslere4039a82020-02-11 15:51:59 -0600263void Host::sysStateChangeJobRemoved(sdbusplus::message::message& msg)
Andrew Geissler4da7e002017-01-24 15:21:40 -0600264{
Andrew Geissler58a18012018-01-19 19:36:05 -0800265 uint32_t newStateID{};
Andrew Geissler4da7e002017-01-24 15:21:40 -0600266 sdbusplus::message::object_path newStateObjPath;
267 std::string newStateUnit{};
268 std::string newStateResult{};
269
Andrew Geissler58a18012018-01-19 19:36:05 -0800270 // Read the msg and populate each variable
Patrick Williamsd22706f2017-05-04 05:42:49 -0500271 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600272
Andrew Geissler58a18012018-01-19 19:36:05 -0800273 if ((newStateUnit == HOST_STATE_POWEROFF_TGT) &&
Andrew Geissler969b2612018-03-29 10:16:51 -0700274 (newStateResult == "done") &&
275 (!stateActive(HOST_STATE_POWERON_MIN_TGT)))
Andrew Geissleref621162016-12-08 12:56:21 -0600276 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500277 info("Received signal that host is off");
Andrew Geissler4da7e002017-01-24 15:21:40 -0600278 this->currentHostState(server::Host::HostState::Off);
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -0500279 this->bootProgress(bootprogress::Progress::ProgressStages::Unspecified);
280 this->operatingSystemState(osstatus::Status::OSStatus::Inactive);
Andrew Geissleref621162016-12-08 12:56:21 -0600281 }
Andrew Geissler969b2612018-03-29 10:16:51 -0700282 else if ((newStateUnit == HOST_STATE_POWERON_MIN_TGT) &&
Andrew Geissler58a18012018-01-19 19:36:05 -0800283 (newStateResult == "done") &&
Andrew Geissler969b2612018-03-29 10:16:51 -0700284 (stateActive(HOST_STATE_POWERON_MIN_TGT)))
Andrew Geissler58a18012018-01-19 19:36:05 -0800285 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500286 info("Received signal that host is running");
Andrew Geissler58a18012018-01-19 19:36:05 -0800287 this->currentHostState(server::Host::HostState::Running);
Andrew Geissler131d04a2021-03-12 11:02:41 -0600288
289 // Remove temporary file which is utilized for scenarios where the
290 // BMC is rebooted while the host is still up.
291 // This file is used to indicate to host related systemd services
292 // that the host is already running and they should skip running.
293 // Once the host state is back to running we can clear this file.
294 auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0);
295 size++; // null
296 std::unique_ptr<char[]> hostFile(new char[size]);
297 std::snprintf(hostFile.get(), size, HOST_RUNNING_FILE, 0);
298 if (std::filesystem::exists(hostFile.get()))
299 {
300 std::filesystem::remove(hostFile.get());
301 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800302 }
303 else if ((newStateUnit == HOST_STATE_QUIESCE_TGT) &&
Josh D. King29d025d2017-04-27 12:40:22 -0500304 (newStateResult == "done") &&
305 (stateActive(HOST_STATE_QUIESCE_TGT)))
Andrew Geissler58a18012018-01-19 19:36:05 -0800306 {
307 if (Host::isAutoReboot())
308 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500309 info("Beginning reboot...");
Andrew Geissler58a18012018-01-19 19:36:05 -0800310 Host::requestedHostTransition(server::Host::Transition::Reboot);
311 }
312 else
313 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500314 info("Maintaining quiesce");
Andrew Geissler58a18012018-01-19 19:36:05 -0800315 this->currentHostState(server::Host::HostState::Quiesced);
316 }
317 }
Andrew Geissleref621162016-12-08 12:56:21 -0600318}
319
Andrew Geissler47b96122020-02-11 16:13:13 -0600320void Host::sysStateChangeJobNew(sdbusplus::message::message& msg)
321{
322 uint32_t newStateID{};
323 sdbusplus::message::object_path newStateObjPath;
324 std::string newStateUnit{};
325
326 // Read the msg and populate each variable
327 msg.read(newStateID, newStateObjPath, newStateUnit);
328
329 if (newStateUnit == HOST_STATE_DIAGNOSTIC_MODE)
330 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500331 info("Received signal that host is in diagnostice mode");
Andrew Geissler47b96122020-02-11 16:13:13 -0600332 this->currentHostState(server::Host::HostState::DiagnosticMode);
333 }
334}
335
Andrew Geissler7b90a622017-08-08 11:41:08 -0500336uint32_t Host::decrementRebootCount()
337{
338 auto rebootCount = reboot::RebootAttempts::attemptsLeft();
Andrew Geissler58a18012018-01-19 19:36:05 -0800339 if (rebootCount > 0)
Andrew Geissler7b90a622017-08-08 11:41:08 -0500340 {
Andrew Geissler58a18012018-01-19 19:36:05 -0800341 return (reboot::RebootAttempts::attemptsLeft(rebootCount - 1));
Andrew Geissler7b90a622017-08-08 11:41:08 -0500342 }
343 return rebootCount;
344}
345
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500346fs::path Host::serialize(const fs::path& dir)
347{
348 std::ofstream os(dir.c_str(), std::ios::binary);
349 cereal::JSONOutputArchive oarchive(os);
350 oarchive(*this);
351 return dir;
352}
353
354bool Host::deserialize(const fs::path& path)
355{
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500356 try
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500357 {
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500358 if (fs::exists(path))
359 {
360 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
361 cereal::JSONInputArchive iarchive(is);
362 iarchive(*this);
363 return true;
364 }
365 return false;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500366 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800367 catch (cereal::Exception& e)
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500368 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500369 error("deserialize exception: {ERROR}", "ERROR", e);
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500370 fs::remove(path);
371 return false;
372 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500373}
374
Andrew Geissleref3c1842016-12-01 12:33:09 -0600375Host::Transition Host::requestedHostTransition(Transition value)
376{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500377 info("Host state transition request of {REQ}", "REQ", value);
Andrew Geissler7b90a622017-08-08 11:41:08 -0500378 // If this is not a power off request then we need to
379 // decrement the reboot counter. This code should
380 // never prevent a power on, it should just decrement
381 // the count to 0. The quiesce handling is where the
382 // check of this count will occur
Andrew Geissler58a18012018-01-19 19:36:05 -0800383 if (value != server::Host::Transition::Off)
Andrew Geissler7b90a622017-08-08 11:41:08 -0500384 {
385 decrementRebootCount();
386 }
387
Andrew Geisslera27a6e82017-07-27 16:44:43 -0500388 executeTransition(value);
Andrew Geissler7b90a622017-08-08 11:41:08 -0500389
Andrew Geissler58a18012018-01-19 19:36:05 -0800390 auto retVal = server::Host::requestedHostTransition(value);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500391 serialize();
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500392 return retVal;
Andrew Geissleref3c1842016-12-01 12:33:09 -0600393}
394
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -0500395Host::ProgressStages Host::bootProgress(ProgressStages value)
396{
397 auto retVal = bootprogress::Progress::bootProgress(value);
398 serialize();
399 return retVal;
400}
401
402Host::OSStatus Host::operatingSystemState(OSStatus value)
403{
404 auto retVal = osstatus::Status::operatingSystemState(value);
405 serialize();
406 return retVal;
407}
408
Andrew Geissleref3c1842016-12-01 12:33:09 -0600409Host::HostState Host::currentHostState(HostState value)
410{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500411 info("Change to Host State: {STATE}", "STATE", value);
Andrew Geissleref621162016-12-08 12:56:21 -0600412 return server::Host::currentHostState(value);
Andrew Geissleref3c1842016-12-01 12:33:09 -0600413}
414
Andrew Geissler36529022016-11-29 15:23:54 -0600415} // namespace manager
416} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700417} // namespace phosphor