blob: a8d38424e60649d29d92f33414a152b5c56af242 [file] [log] [blame]
Andrew Geissler5259f972021-08-30 15:46:55 -05001#include "config.h"
2
Andrew Geissler556153a2021-08-11 15:29:58 -04003#include <unistd.h>
4
5#include <phosphor-logging/elog.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -05006#include <phosphor-logging/lg2.hpp>
Andrew Geisslerba0163a2021-08-11 15:09:50 -04007#include <sdbusplus/bus.hpp>
8#include <sdbusplus/exception.hpp>
Patrick Williams9a286db2024-01-17 06:29:47 -06009#include <xyz/openbmc_project/Logging/Create/client.hpp>
10#include <xyz/openbmc_project/Logging/Entry/client.hpp>
11#include <xyz/openbmc_project/State/Boot/Progress/client.hpp>
Andrew Geisslerba0163a2021-08-11 15:09:50 -040012
13#include <cstdlib>
Andrew Geissler5259f972021-08-30 15:46:55 -050014#include <fstream>
Andrew Geisslerba0163a2021-08-11 15:09:50 -040015#include <string>
16
Andrew Geissler8ffdb262021-09-20 15:25:19 -050017namespace phosphor
18{
19namespace state
20{
21namespace manager
22{
23
24PHOSPHOR_LOG2_USING;
25
Patrick Williams9a286db2024-01-17 06:29:47 -060026using BootProgress =
27 sdbusplus::client::xyz::openbmc_project::state::boot::Progress<>;
28using LoggingCreate =
29 sdbusplus::client::xyz::openbmc_project::logging::Create<>;
30using LoggingEntry = sdbusplus::client::xyz::openbmc_project::logging::Entry<>;
31
Andrew Geisslerba0163a2021-08-11 15:09:50 -040032constexpr auto HOST_STATE_SVC = "xyz.openbmc_project.State.Host";
33constexpr auto HOST_STATE_PATH = "/xyz/openbmc_project/state/host0";
34constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
Andrew Geisslerba0163a2021-08-11 15:09:50 -040035constexpr auto BOOT_PROGRESS_PROP = "BootProgress";
36
Andrew Geissler7b01b0b2021-08-11 16:14:49 -040037constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
38constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
39constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
40constexpr auto HOST_STATE_QUIESCE_TGT = "obmc-host-quiesce@0.target";
41
Patrick Williamsf053e6f2022-07-22 19:26:54 -050042bool wasHostBooting(sdbusplus::bus_t& bus)
Andrew Geisslerba0163a2021-08-11 15:09:50 -040043{
44 try
45 {
Patrick Williams9a286db2024-01-17 06:29:47 -060046 using ProgressStages = BootProgress::ProgressStages;
NodeMan9780db4752022-05-04 09:06:40 -050047
Andrew Geisslerba0163a2021-08-11 15:09:50 -040048 auto method = bus.new_method_call(HOST_STATE_SVC, HOST_STATE_PATH,
49 PROPERTY_INTERFACE, "Get");
Patrick Williams9a286db2024-01-17 06:29:47 -060050 method.append(BootProgress::interface, BOOT_PROGRESS_PROP);
Andrew Geisslerba0163a2021-08-11 15:09:50 -040051
52 auto response = bus.call(method);
53
NodeMan9780db4752022-05-04 09:06:40 -050054 std::variant<ProgressStages> bootProgressV;
55 response.read(bootProgressV);
56 auto bootProgress = std::get<ProgressStages>(bootProgressV);
Andrew Geisslerba0163a2021-08-11 15:09:50 -040057
NodeMan9780db4752022-05-04 09:06:40 -050058 if (bootProgress == ProgressStages::Unspecified)
Andrew Geisslerba0163a2021-08-11 15:09:50 -040059 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050060 info("Host was not booting before BMC reboot");
Andrew Geisslerba0163a2021-08-11 15:09:50 -040061 return false;
62 }
63
Andrew Geissler8ffdb262021-09-20 15:25:19 -050064 info("Host was booting before BMC reboot: {BOOTPROGRESS}",
NodeMan9780db4752022-05-04 09:06:40 -050065 "BOOTPROGRESS", bootProgress);
Andrew Geisslerba0163a2021-08-11 15:09:50 -040066 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -050067 catch (const sdbusplus::exception_t& e)
Andrew Geisslerba0163a2021-08-11 15:09:50 -040068 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050069 error("Error reading BootProgress, error {ERROR}, service {SERVICE}, "
70 "path {PATH}",
71 "ERROR", e, "SERVICE", HOST_STATE_SVC, "PATH", HOST_STATE_PATH);
Andrew Geisslerba0163a2021-08-11 15:09:50 -040072
73 throw;
74 }
75
76 return true;
77}
78
Patrick Williamsf053e6f2022-07-22 19:26:54 -050079void createErrorLog(sdbusplus::bus_t& bus)
Andrew Geissler556153a2021-08-11 15:29:58 -040080{
81 try
82 {
83 // Create interface requires something for additionalData
84 std::map<std::string, std::string> additionalData;
85 additionalData.emplace("_PID", std::to_string(getpid()));
86
87 static constexpr auto errorMessage =
88 "xyz.openbmc_project.State.Error.HostNotRunning";
Patrick Williams9a286db2024-01-17 06:29:47 -060089 auto method = bus.new_method_call(LoggingCreate::default_service,
90 LoggingCreate::instance_path,
91 LoggingCreate::interface, "Create");
92 method.append(errorMessage, LoggingEntry::Level::Error, additionalData);
Andrew Geissler556153a2021-08-11 15:29:58 -040093 auto resp = bus.call(method);
94 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -050095 catch (const sdbusplus::exception_t& e)
Andrew Geissler556153a2021-08-11 15:29:58 -040096 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050097 error(
98 "sdbusplus D-Bus call exception, error {ERROR}, objpath {OBJPATH}, "
99 "interface {INTERFACE}",
Patrick Williams9a286db2024-01-17 06:29:47 -0600100 "ERROR", e, "OBJPATH", LoggingCreate::instance_path, "INTERFACE",
101 LoggingCreate::interface);
Andrew Geissler556153a2021-08-11 15:29:58 -0400102
103 throw std::runtime_error(
104 "Error in invoking D-Bus logging create interface");
105 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500106 catch (const std::exception& e)
Andrew Geissler556153a2021-08-11 15:29:58 -0400107 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500108 error("D-bus call exception: {ERROR}", "ERROR", e);
Andrew Geissler556153a2021-08-11 15:29:58 -0400109 throw e;
110 }
111}
112
Andrew Geissler5259f972021-08-30 15:46:55 -0500113// Once CHASSIS_ON_FILE is removed, the obmc-chassis-poweron@.target has
114// completed and the phosphor-chassis-state-manager code has processed it.
115bool isChassisTargetComplete()
116{
117 auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0);
118 size++; // null
119 std::unique_ptr<char[]> buf(new char[size]);
120 std::snprintf(buf.get(), size, CHASSIS_ON_FILE, 0);
121
122 std::ifstream f(buf.get());
123 return !f.good();
124}
125
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500126void moveToHostQuiesce(sdbusplus::bus_t& bus)
Andrew Geissler7b01b0b2021-08-11 16:14:49 -0400127{
128 try
129 {
130 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
131 SYSTEMD_INTERFACE, "StartUnit");
132
133 method.append(HOST_STATE_QUIESCE_TGT);
134 method.append("replace");
135
136 bus.call_noreply(method);
137 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500138 catch (const sdbusplus::exception_t& e)
Andrew Geissler7b01b0b2021-08-11 16:14:49 -0400139 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500140 error("sdbusplus call exception starting quiesce target: {ERROR}",
141 "ERROR", e);
Andrew Geissler7b01b0b2021-08-11 16:14:49 -0400142
143 throw std::runtime_error(
144 "Error in invoking D-Bus systemd StartUnit method");
145 }
146}
147
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500148} // namespace manager
149} // namespace state
150} // namespace phosphor
151
Andrew Geisslerba0163a2021-08-11 15:09:50 -0400152int main()
153{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500154 using namespace phosphor::state::manager;
155 PHOSPHOR_LOG2_USING;
Andrew Geisslerba0163a2021-08-11 15:09:50 -0400156
157 auto bus = sdbusplus::bus::new_default();
158
Andrew Geissler5259f972021-08-30 15:46:55 -0500159 // Chassis power is on if this service starts but need to wait for the
160 // obmc-chassis-poweron@.target to complete before potentially initiating
161 // another systemd target transition (i.e. Quiesce->Reboot)
162 while (!isChassisTargetComplete())
163 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500164 debug("Waiting for chassis on target to complete");
Andrew Geissler5259f972021-08-30 15:46:55 -0500165 std::this_thread::sleep_for(std::chrono::seconds(1));
166
167 // There is no timeout here, wait until it happens or until system
168 // is powered off and this service is stopped
169 }
170
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500171 info("Chassis power on has completed, checking if host is "
172 "still running after the BMC reboot");
Andrew Geisslerba0163a2021-08-11 15:09:50 -0400173
174 // Check the last BootProgeress to see if the host was booting before
175 // the BMC reboot occurred
176 if (!wasHostBooting(bus))
177 {
178 return 0;
179 }
180
181 // Host was booting before the BMC reboot so log an error and go to host
182 // quiesce target
Andrew Geissler556153a2021-08-11 15:29:58 -0400183 createErrorLog(bus);
Andrew Geissler7b01b0b2021-08-11 16:14:49 -0400184 moveToHostQuiesce(bus);
Andrew Geisslerba0163a2021-08-11 15:09:50 -0400185
186 return 0;
187}