blob: 8890b563c88d11ff5ffdc0d88e4c77b128da2c70 [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"
Andrew Geissler1ab2b6c2022-03-10 16:16:15 -06006#include "utils.hpp"
Andrew Geissler0d1c3f12021-07-27 16:21:01 -04007
Allen.Wang79b45002022-02-10 17:59:20 +08008#include <fmt/format.h>
Andrew Geissler131d04a2021-03-12 11:02:41 -06009#include <stdio.h>
Andrew Geisslere426b582020-05-28 12:40:55 -050010#include <systemd/sd-bus.h>
11
12#include <cereal/archives/json.hpp>
13#include <cereal/cereal.hpp>
14#include <cereal/types/string.hpp>
15#include <cereal/types/tuple.hpp>
16#include <cereal/types/vector.hpp>
17#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050018#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050019#include <sdbusplus/exception.hpp>
20#include <sdbusplus/server.hpp>
21#include <xyz/openbmc_project/Common/error.hpp>
22#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
Andrew Geissler765446a2023-05-25 15:38:20 -040023#include <xyz/openbmc_project/State/Host/error.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050024
Andrew Geissler131d04a2021-03-12 11:02:41 -060025#include <filesystem>
Andrew Geisslere426b582020-05-28 12:40:55 -050026#include <fstream>
Andrew Geissler36529022016-11-29 15:23:54 -060027#include <iostream>
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060028#include <map>
29#include <string>
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050030
Vishwanatha Subbanna0a838732017-10-05 12:43:19 +053031// Register class version with Cereal
Andrew Geissler769a62f2019-12-06 13:36:08 -060032CEREAL_CLASS_VERSION(phosphor::state::manager::Host, CLASS_VERSION)
Andrew Geissler36529022016-11-29 15:23:54 -060033
34namespace phosphor
35{
36namespace state
37{
38namespace manager
39{
40
Andrew Geissler8ffdb262021-09-20 15:25:19 -050041PHOSPHOR_LOG2_USING;
42
Andrew Geissler7b90a622017-08-08 11:41:08 -050043// When you see server:: or reboot:: you know we're referencing our base class
Andrew Geissler3e3b84b2016-12-02 15:46:17 -060044namespace server = sdbusplus::xyz::openbmc_project::State::server;
Andrew Geissler7b90a622017-08-08 11:41:08 -050045namespace reboot = sdbusplus::xyz::openbmc_project::Control::Boot::server;
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -050046namespace bootprogress = sdbusplus::xyz::openbmc_project::State::Boot::server;
47namespace osstatus =
48 sdbusplus::xyz::openbmc_project::State::OperatingSystem::server;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060049using namespace phosphor::logging;
Patrick Williams6ed41ea2022-04-05 15:50:21 -050050namespace fs = std::filesystem;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050051using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060052
Josh D. King929ef702017-03-02 10:58:11 -060053constexpr auto ACTIVE_STATE = "active";
54constexpr auto ACTIVATING_STATE = "activating";
55
Andrew Geissler58a18012018-01-19 19:36:05 -080056constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
57constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060058constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
59
Josh D. King929ef702017-03-02 10:58:11 -060060constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
61constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
62
Andrew Geissleref3c1842016-12-01 12:33:09 -060063void Host::determineInitialState()
64{
Allen.Wang79b45002022-02-10 17:59:20 +080065 if (stateActive(getTarget(server::Host::HostState::Running)) ||
Potin Lai70f36d82022-03-15 10:25:39 +080066 isHostRunning(id))
Andrew Geissleref3c1842016-12-01 12:33:09 -060067 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050068 info("Initial Host State will be Running");
Andrew Geissler97924142017-01-24 16:10:18 -060069 server::Host::currentHostState(HostState::Running);
70 server::Host::requestedHostTransition(Transition::On);
Andrew Geissleref3c1842016-12-01 12:33:09 -060071 }
72 else
73 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050074 info("Initial Host State will be Off");
Andrew Geissler97924142017-01-24 16:10:18 -060075 server::Host::currentHostState(HostState::Off);
76 server::Host::requestedHostTransition(Transition::Off);
Andrew Geissleref3c1842016-12-01 12:33:09 -060077 }
78
Allen.Wangba182f02022-03-23 19:01:53 +080079 if (!deserialize())
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050080 {
Andrew Geissler58a18012018-01-19 19:36:05 -080081 // set to default value.
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050082 server::Host::requestedHostTransition(Transition::Off);
83 }
Andrew Geissleref3c1842016-12-01 12:33:09 -060084 return;
85}
86
Allen.Wang79b45002022-02-10 17:59:20 +080087void Host::createSystemdTargetMaps()
88{
89 stateTargetTable = {
90 {HostState::Off, fmt::format("obmc-host-stop@{}.target", id)},
91 {HostState::Running, fmt::format("obmc-host-startmin@{}.target", id)},
92 {HostState::Quiesced, fmt::format("obmc-host-quiesce@{}.target", id)},
93 {HostState::DiagnosticMode,
94 fmt::format("obmc-host-diagnostic-mode@{}.target", id)}};
95
96 transitionTargetTable = {
97 {Transition::Off, fmt::format("obmc-host-shutdown@{}.target", id)},
98 {Transition::On, fmt::format("obmc-host-start@{}.target", id)},
99 {Transition::Reboot, fmt::format("obmc-host-reboot@{}.target", id)},
100// Some systems do not support a warm reboot so just map the reboot
101// requests to our normal cold reboot in that case
102#if ENABLE_WARM_REBOOT
103 {Transition::GracefulWarmReboot,
104 fmt::format("obmc-host-warm-reboot@{}.target", id)},
105 {Transition::ForceWarmReboot,
106 fmt::format("obmc-host-force-warm-reboot@{}.target", id)}
107 };
108#else
109 {Transition::GracefulWarmReboot,
110 fmt::format("obmc-host-reboot@{}.target", id)},
111 {Transition::ForceWarmReboot,
112 fmt::format("obmc-host-reboot@{}.target", id)}
113 };
114#endif
Andrew Geissler128ea8e2022-06-22 12:28:15 -0400115 hostCrashTarget = fmt::format("obmc-host-crash@{}.target", id);
Allen.Wang79b45002022-02-10 17:59:20 +0800116}
117
118const std::string& Host::getTarget(HostState state)
119{
120 return stateTargetTable[state];
121};
122
123const std::string& Host::getTarget(Transition tranReq)
124{
125 return transitionTargetTable[tranReq];
126};
127
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600128void Host::executeTransition(Transition tranReq)
129{
Allen.Wang79b45002022-02-10 17:59:20 +0800130 auto& sysdUnit = getTarget(tranReq);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600131
Andrew Geissler58a18012018-01-19 19:36:05 -0800132 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
133 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600134
135 method.append(sysdUnit);
136 method.append("replace");
137
Andrew Geissler4da7e002017-01-24 15:21:40 -0600138 this->bus.call_noreply(method);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600139
140 return;
141}
142
Josh D. King929ef702017-03-02 10:58:11 -0600143bool Host::stateActive(const std::string& target)
144{
Patrick Williams2975e262020-05-13 18:01:09 -0500145 std::variant<std::string> currentState;
Josh D. King929ef702017-03-02 10:58:11 -0600146 sdbusplus::message::object_path unitTargetPath;
147
Andrew Geissler58a18012018-01-19 19:36:05 -0800148 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
149 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King929ef702017-03-02 10:58:11 -0600150
151 method.append(target);
Josh D. King929ef702017-03-02 10:58:11 -0600152
Anthony Wilson32c532e2018-10-25 21:56:07 -0500153 try
Josh D. King929ef702017-03-02 10:58:11 -0600154 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500155 auto result = this->bus.call(method);
156 result.read(unitTargetPath);
157 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500158 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500159 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500160 error("Error in GetUnit call: {ERROR}", "ERROR", e);
Josh D. King929ef702017-03-02 10:58:11 -0600161 return false;
162 }
163
Andrew Geissler58a18012018-01-19 19:36:05 -0800164 method = this->bus.new_method_call(
165 SYSTEMD_SERVICE,
166 static_cast<const std::string&>(unitTargetPath).c_str(),
167 SYSTEMD_PROPERTY_IFACE, "Get");
Josh D. King929ef702017-03-02 10:58:11 -0600168
169 method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
Josh D. King929ef702017-03-02 10:58:11 -0600170
Anthony Wilson32c532e2018-10-25 21:56:07 -0500171 try
Josh D. King929ef702017-03-02 10:58:11 -0600172 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500173 auto result = this->bus.call(method);
174 result.read(currentState);
175 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500176 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500177 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500178 error("Error in ActiveState Get: {ERROR}", "ERROR", e);
Josh D. King929ef702017-03-02 10:58:11 -0600179 return false;
180 }
181
Patrick Williams37413dc2020-05-13 11:29:54 -0500182 const auto& currentStateStr = std::get<std::string>(currentState);
William A. Kennington III7a0689a2018-11-12 17:19:33 -0800183 return currentStateStr == ACTIVE_STATE ||
184 currentStateStr == ACTIVATING_STATE;
Josh D. King929ef702017-03-02 10:58:11 -0600185}
186
Michael Tritz206a8332017-02-06 16:01:23 -0600187bool Host::isAutoReboot()
188{
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500189 using namespace settings;
Michael Tritz206a8332017-02-06 16:01:23 -0600190
Andrew Geissler7f620832020-10-23 08:25:52 -0500191 /* The logic here is to first check the one-time AutoReboot setting.
192 * If this property is true (the default) then look at the persistent
193 * user setting in the non one-time object, otherwise honor the one-time
194 * setting and do not auto reboot.
195 */
196 auto methodOneTime = bus.new_method_call(
Andrew Geissler58a18012018-01-19 19:36:05 -0800197 settings.service(settings.autoReboot, autoRebootIntf).c_str(),
Andrew Geissler7f620832020-10-23 08:25:52 -0500198 settings.autoRebootOneTime.c_str(), SYSTEMD_PROPERTY_IFACE, "Get");
199 methodOneTime.append(autoRebootIntf, "AutoReboot");
200
201 auto methodUserSetting = bus.new_method_call(
202 settings.service(settings.autoReboot, autoRebootIntf).c_str(),
203 settings.autoReboot.c_str(), SYSTEMD_PROPERTY_IFACE, "Get");
204 methodUserSetting.append(autoRebootIntf, "AutoReboot");
Michael Tritz206a8332017-02-06 16:01:23 -0600205
Anthony Wilson32c532e2018-10-25 21:56:07 -0500206 try
Michael Tritz206a8332017-02-06 16:01:23 -0600207 {
Andrew Geissler7f620832020-10-23 08:25:52 -0500208 auto reply = bus.call(methodOneTime);
Patrick Williams2975e262020-05-13 18:01:09 -0500209 std::variant<bool> result;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500210 reply.read(result);
Patrick Williams37413dc2020-05-13 11:29:54 -0500211 auto autoReboot = std::get<bool>(result);
Andrew Geissler7f620832020-10-23 08:25:52 -0500212
213 if (!autoReboot)
214 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500215 info("Auto reboot (one-time) disabled");
Andrew Geissler7f620832020-10-23 08:25:52 -0500216 return false;
217 }
218 else
219 {
220 // one-time is true so read the user setting
221 reply = bus.call(methodUserSetting);
222 reply.read(result);
223 autoReboot = std::get<bool>(result);
224 }
225
Anthony Wilson32c532e2018-10-25 21:56:07 -0500226 auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft();
227
228 if (autoReboot)
Saqib Khancbe08d12017-03-10 01:29:20 -0600229 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500230 if (rebootCounterParam > 0)
231 {
232 // Reduce BOOTCOUNT by 1
Andrew Geisslerad65b2d2021-09-21 12:53:29 -0500233 info(
234 "Auto reboot enabled and boot count at {BOOTCOUNT}, rebooting",
235 "BOOTCOUNT", rebootCounterParam);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500236 return true;
237 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500238 else
239 {
Andrew Geissler43284792021-09-23 10:32:15 -0500240 // We are at 0 so reset reboot counter and go to quiesce state
241 info("Auto reboot enabled but HOST BOOTCOUNT already set to 0");
NodeMan97b4cbfac2022-05-09 23:56:39 -0500242 attemptsLeft(reboot::RebootAttempts::retryAttempts());
Andrew Geissler1ab2b6c2022-03-10 16:16:15 -0600243
244 // Generate log since we will now be sitting in Quiesce
245 const std::string errorMsg =
246 "xyz.openbmc_project.State.Error.HostQuiesce";
247 utils::createError(this->bus, errorMsg,
248 sdbusplus::xyz::openbmc_project::Logging::
249 server::Entry::Level::Critical);
Andrew Geissler5bcaee12022-04-19 15:40:40 -0400250
251 // Generate BMC dump to assist with debug
252 utils::createBmcDump(this->bus);
253
Anthony Wilson32c532e2018-10-25 21:56:07 -0500254 return false;
255 }
Saqib Khand5ac6352017-04-04 09:53:59 -0500256 }
257 else
258 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500259 info("Auto reboot disabled.");
Saqib Khancbe08d12017-03-10 01:29:20 -0600260 return false;
261 }
Michael Tritz206a8332017-02-06 16:01:23 -0600262 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500263 catch (const sdbusplus::exception_t& e)
Saqib Khand5ac6352017-04-04 09:53:59 -0500264 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500265 error("Error in AutoReboot Get, {ERROR}", "ERROR", e);
Saqib Khand5ac6352017-04-04 09:53:59 -0500266 return false;
267 }
Michael Tritz206a8332017-02-06 16:01:23 -0600268}
269
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500270void Host::sysStateChangeJobRemoved(sdbusplus::message_t& msg)
Andrew Geissler4da7e002017-01-24 15:21:40 -0600271{
Andrew Geissler58a18012018-01-19 19:36:05 -0800272 uint32_t newStateID{};
Andrew Geissler4da7e002017-01-24 15:21:40 -0600273 sdbusplus::message::object_path newStateObjPath;
274 std::string newStateUnit{};
275 std::string newStateResult{};
276
Andrew Geissler58a18012018-01-19 19:36:05 -0800277 // Read the msg and populate each variable
Patrick Williamsd22706f2017-05-04 05:42:49 -0500278 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600279
Allen.Wang79b45002022-02-10 17:59:20 +0800280 if ((newStateUnit == getTarget(server::Host::HostState::Off)) &&
Andrew Geissler969b2612018-03-29 10:16:51 -0700281 (newStateResult == "done") &&
Allen.Wang79b45002022-02-10 17:59:20 +0800282 (!stateActive(getTarget(server::Host::HostState::Running))))
Andrew Geissleref621162016-12-08 12:56:21 -0600283 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500284 info("Received signal that host is off");
Andrew Geissler4da7e002017-01-24 15:21:40 -0600285 this->currentHostState(server::Host::HostState::Off);
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -0500286 this->bootProgress(bootprogress::Progress::ProgressStages::Unspecified);
287 this->operatingSystemState(osstatus::Status::OSStatus::Inactive);
Andrew Geissleref621162016-12-08 12:56:21 -0600288 }
Allen.Wang79b45002022-02-10 17:59:20 +0800289 else if ((newStateUnit == getTarget(server::Host::HostState::Running)) &&
Andrew Geissler58a18012018-01-19 19:36:05 -0800290 (newStateResult == "done") &&
Allen.Wang79b45002022-02-10 17:59:20 +0800291 (stateActive(getTarget(server::Host::HostState::Running))))
Andrew Geissler58a18012018-01-19 19:36:05 -0800292 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500293 info("Received signal that host is running");
Andrew Geissler58a18012018-01-19 19:36:05 -0800294 this->currentHostState(server::Host::HostState::Running);
Andrew Geissler131d04a2021-03-12 11:02:41 -0600295
296 // Remove temporary file which is utilized for scenarios where the
297 // BMC is rebooted while the host is still up.
298 // This file is used to indicate to host related systemd services
299 // that the host is already running and they should skip running.
300 // Once the host state is back to running we can clear this file.
301 auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0);
302 size++; // null
303 std::unique_ptr<char[]> hostFile(new char[size]);
304 std::snprintf(hostFile.get(), size, HOST_RUNNING_FILE, 0);
305 if (std::filesystem::exists(hostFile.get()))
306 {
307 std::filesystem::remove(hostFile.get());
308 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800309 }
Allen.Wang79b45002022-02-10 17:59:20 +0800310 else if ((newStateUnit == getTarget(server::Host::HostState::Quiesced)) &&
Josh D. King29d025d2017-04-27 12:40:22 -0500311 (newStateResult == "done") &&
Allen.Wang79b45002022-02-10 17:59:20 +0800312 (stateActive(getTarget(server::Host::HostState::Quiesced))))
Andrew Geissler58a18012018-01-19 19:36:05 -0800313 {
314 if (Host::isAutoReboot())
315 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500316 info("Beginning reboot...");
Andrew Geissler58a18012018-01-19 19:36:05 -0800317 Host::requestedHostTransition(server::Host::Transition::Reboot);
318 }
319 else
320 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500321 info("Maintaining quiesce");
Andrew Geissler58a18012018-01-19 19:36:05 -0800322 this->currentHostState(server::Host::HostState::Quiesced);
323 }
324 }
Andrew Geissleref621162016-12-08 12:56:21 -0600325}
326
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500327void Host::sysStateChangeJobNew(sdbusplus::message_t& msg)
Andrew Geissler47b96122020-02-11 16:13:13 -0600328{
329 uint32_t newStateID{};
330 sdbusplus::message::object_path newStateObjPath;
331 std::string newStateUnit{};
332
333 // Read the msg and populate each variable
334 msg.read(newStateID, newStateObjPath, newStateUnit);
335
Allen.Wang79b45002022-02-10 17:59:20 +0800336 if (newStateUnit == getTarget(server::Host::HostState::DiagnosticMode))
Andrew Geissler47b96122020-02-11 16:13:13 -0600337 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500338 info("Received signal that host is in diagnostice mode");
Andrew Geissler47b96122020-02-11 16:13:13 -0600339 this->currentHostState(server::Host::HostState::DiagnosticMode);
340 }
Andrew Geissler128ea8e2022-06-22 12:28:15 -0400341 else if ((newStateUnit == hostCrashTarget) &&
342 (server::Host::currentHostState() ==
343 server::Host::HostState::Running))
344 {
345 // Only decrease the boot count if host was running when the host crash
346 // target was started. Systemd will sometimes trigger multiple
347 // JobNew events for the same target. This seems to be related to
348 // how OpenBMC utilizes the targets in the reboot scenario
349 info("Received signal that host has crashed, decrement reboot count");
350
351 // A host crash can cause a reboot of the host so decrement the reboot
352 // count
353 decrementRebootCount();
354 }
Andrew Geissler47b96122020-02-11 16:13:13 -0600355}
356
Andrew Geissler7b90a622017-08-08 11:41:08 -0500357uint32_t Host::decrementRebootCount()
358{
359 auto rebootCount = reboot::RebootAttempts::attemptsLeft();
Andrew Geissler58a18012018-01-19 19:36:05 -0800360 if (rebootCount > 0)
Andrew Geissler7b90a622017-08-08 11:41:08 -0500361 {
Andrew Geissler58a18012018-01-19 19:36:05 -0800362 return (reboot::RebootAttempts::attemptsLeft(rebootCount - 1));
Andrew Geissler7b90a622017-08-08 11:41:08 -0500363 }
364 return rebootCount;
365}
366
Allen.Wangba182f02022-03-23 19:01:53 +0800367fs::path Host::serialize()
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500368{
Allen.Wangba182f02022-03-23 19:01:53 +0800369 fs::path path{fmt::format(HOST_STATE_PERSIST_PATH, id)};
370 std::ofstream os(path.c_str(), std::ios::binary);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500371 cereal::JSONOutputArchive oarchive(os);
372 oarchive(*this);
Allen.Wangba182f02022-03-23 19:01:53 +0800373 return path;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500374}
375
Allen.Wangba182f02022-03-23 19:01:53 +0800376bool Host::deserialize()
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500377{
Allen.Wangba182f02022-03-23 19:01:53 +0800378 fs::path path{fmt::format(HOST_STATE_PERSIST_PATH, id)};
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500379 try
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500380 {
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500381 if (fs::exists(path))
382 {
383 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
384 cereal::JSONInputArchive iarchive(is);
385 iarchive(*this);
386 return true;
387 }
388 return false;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500389 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500390 catch (const cereal::Exception& e)
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500391 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500392 error("deserialize exception: {ERROR}", "ERROR", e);
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500393 fs::remove(path);
394 return false;
395 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500396}
397
Andrew Geissleref3c1842016-12-01 12:33:09 -0600398Host::Transition Host::requestedHostTransition(Transition value)
399{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500400 info("Host state transition request of {REQ}", "REQ", value);
Andrew Geissler765446a2023-05-25 15:38:20 -0400401
402#if ONLY_ALLOW_BOOT_WHEN_BMC_READY
403 if ((value != Transition::Off) && (!utils::isBmcReady(this->bus)))
404 {
405 info("BMC State is not Ready so no host on operations allowed");
406 throw sdbusplus::xyz::openbmc_project::State::Host::Error::
407 BMCNotReady();
408 }
409#endif
410
Andrew Geissler7b90a622017-08-08 11:41:08 -0500411 // If this is not a power off request then we need to
412 // decrement the reboot counter. This code should
413 // never prevent a power on, it should just decrement
414 // the count to 0. The quiesce handling is where the
415 // check of this count will occur
Andrew Geissler58a18012018-01-19 19:36:05 -0800416 if (value != server::Host::Transition::Off)
Andrew Geissler7b90a622017-08-08 11:41:08 -0500417 {
418 decrementRebootCount();
419 }
420
Andrew Geisslera27a6e82017-07-27 16:44:43 -0500421 executeTransition(value);
Andrew Geissler7b90a622017-08-08 11:41:08 -0500422
Andrew Geissler58a18012018-01-19 19:36:05 -0800423 auto retVal = server::Host::requestedHostTransition(value);
Allen.Wangba182f02022-03-23 19:01:53 +0800424
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500425 serialize();
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500426 return retVal;
Andrew Geissleref3c1842016-12-01 12:33:09 -0600427}
428
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -0500429Host::ProgressStages Host::bootProgress(ProgressStages value)
430{
431 auto retVal = bootprogress::Progress::bootProgress(value);
432 serialize();
433 return retVal;
434}
435
436Host::OSStatus Host::operatingSystemState(OSStatus value)
437{
438 auto retVal = osstatus::Status::operatingSystemState(value);
439 serialize();
440 return retVal;
441}
442
Andrew Geissleref3c1842016-12-01 12:33:09 -0600443Host::HostState Host::currentHostState(HostState value)
444{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500445 info("Change to Host State: {STATE}", "STATE", value);
Andrew Geissleref621162016-12-08 12:56:21 -0600446 return server::Host::currentHostState(value);
Andrew Geissleref3c1842016-12-01 12:33:09 -0600447}
448
Andrew Geissler36529022016-11-29 15:23:54 -0600449} // namespace manager
450} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700451} // namespace phosphor