blob: 789862235dcf8359834fc415f1ffd02c1f567f00 [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>
Andrew Geissler556153a2021-08-11 15:29:58 -04009#include <xyz/openbmc_project/Logging/Create/server.hpp>
10#include <xyz/openbmc_project/Logging/Entry/server.hpp>
NodeMan9780db4752022-05-04 09:06:40 -050011#include <xyz/openbmc_project/State/Boot/Progress/server.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
Andrew Geisslerba0163a2021-08-11 15:09:50 -040026constexpr auto HOST_STATE_SVC = "xyz.openbmc_project.State.Host";
27constexpr auto HOST_STATE_PATH = "/xyz/openbmc_project/state/host0";
28constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
29constexpr auto BOOT_STATE_INTF = "xyz.openbmc_project.State.Boot.Progress";
30constexpr auto BOOT_PROGRESS_PROP = "BootProgress";
31
Andrew Geissler556153a2021-08-11 15:29:58 -040032constexpr auto LOGGING_SVC = "xyz.openbmc_project.Logging";
33constexpr auto LOGGING_PATH = "/xyz/openbmc_project/logging";
34constexpr auto LOGGING_CREATE_INTF = "xyz.openbmc_project.Logging.Create";
35
Andrew Geissler7b01b0b2021-08-11 16:14:49 -040036constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
37constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
38constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
39constexpr auto HOST_STATE_QUIESCE_TGT = "obmc-host-quiesce@0.target";
40
Andrew Geisslerba0163a2021-08-11 15:09:50 -040041bool wasHostBooting(sdbusplus::bus::bus& bus)
42{
43 try
44 {
NodeMan9780db4752022-05-04 09:06:40 -050045 using ProgressStages = sdbusplus::xyz::openbmc_project::State::Boot::
46 server::Progress::ProgressStages;
47
Andrew Geisslerba0163a2021-08-11 15:09:50 -040048 auto method = bus.new_method_call(HOST_STATE_SVC, HOST_STATE_PATH,
49 PROPERTY_INTERFACE, "Get");
50 method.append(BOOT_STATE_INTF, BOOT_PROGRESS_PROP);
51
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 }
67 catch (const sdbusplus::exception::exception& e)
68 {
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
Andrew Geissler556153a2021-08-11 15:29:58 -040079void createErrorLog(sdbusplus::bus::bus& bus)
80{
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";
89 auto method = bus.new_method_call(LOGGING_SVC, LOGGING_PATH,
90 LOGGING_CREATE_INTF, "Create");
91 auto level =
92 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
93 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
94 Error);
95 method.append(errorMessage, level, additionalData);
96 auto resp = bus.call(method);
97 }
98 catch (const sdbusplus::exception::exception& e)
99 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500100 error(
101 "sdbusplus D-Bus call exception, error {ERROR}, objpath {OBJPATH}, "
102 "interface {INTERFACE}",
103 "ERROR", e, "OBJPATH", LOGGING_PATH, "INTERFACE",
104 LOGGING_CREATE_INTF);
Andrew Geissler556153a2021-08-11 15:29:58 -0400105
106 throw std::runtime_error(
107 "Error in invoking D-Bus logging create interface");
108 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500109 catch (const std::exception& e)
Andrew Geissler556153a2021-08-11 15:29:58 -0400110 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500111 error("D-bus call exception: {ERROR}", "ERROR", e);
Andrew Geissler556153a2021-08-11 15:29:58 -0400112 throw e;
113 }
114}
115
Andrew Geissler5259f972021-08-30 15:46:55 -0500116// Once CHASSIS_ON_FILE is removed, the obmc-chassis-poweron@.target has
117// completed and the phosphor-chassis-state-manager code has processed it.
118bool isChassisTargetComplete()
119{
120 auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0);
121 size++; // null
122 std::unique_ptr<char[]> buf(new char[size]);
123 std::snprintf(buf.get(), size, CHASSIS_ON_FILE, 0);
124
125 std::ifstream f(buf.get());
126 return !f.good();
127}
128
Andrew Geissler7b01b0b2021-08-11 16:14:49 -0400129void moveToHostQuiesce(sdbusplus::bus::bus& bus)
130{
131 try
132 {
133 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
134 SYSTEMD_INTERFACE, "StartUnit");
135
136 method.append(HOST_STATE_QUIESCE_TGT);
137 method.append("replace");
138
139 bus.call_noreply(method);
140 }
141 catch (const sdbusplus::exception::exception& e)
142 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500143 error("sdbusplus call exception starting quiesce target: {ERROR}",
144 "ERROR", e);
Andrew Geissler7b01b0b2021-08-11 16:14:49 -0400145
146 throw std::runtime_error(
147 "Error in invoking D-Bus systemd StartUnit method");
148 }
149}
150
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500151} // namespace manager
152} // namespace state
153} // namespace phosphor
154
Andrew Geisslerba0163a2021-08-11 15:09:50 -0400155int main()
156{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500157 using namespace phosphor::state::manager;
158 PHOSPHOR_LOG2_USING;
Andrew Geisslerba0163a2021-08-11 15:09:50 -0400159
160 auto bus = sdbusplus::bus::new_default();
161
Andrew Geissler5259f972021-08-30 15:46:55 -0500162 // Chassis power is on if this service starts but need to wait for the
163 // obmc-chassis-poweron@.target to complete before potentially initiating
164 // another systemd target transition (i.e. Quiesce->Reboot)
165 while (!isChassisTargetComplete())
166 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500167 debug("Waiting for chassis on target to complete");
Andrew Geissler5259f972021-08-30 15:46:55 -0500168 std::this_thread::sleep_for(std::chrono::seconds(1));
169
170 // There is no timeout here, wait until it happens or until system
171 // is powered off and this service is stopped
172 }
173
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500174 info("Chassis power on has completed, checking if host is "
175 "still running after the BMC reboot");
Andrew Geisslerba0163a2021-08-11 15:09:50 -0400176
177 // Check the last BootProgeress to see if the host was booting before
178 // the BMC reboot occurred
179 if (!wasHostBooting(bus))
180 {
181 return 0;
182 }
183
184 // Host was booting before the BMC reboot so log an error and go to host
185 // quiesce target
Andrew Geissler556153a2021-08-11 15:29:58 -0400186 createErrorLog(bus);
Andrew Geissler7b01b0b2021-08-11 16:14:49 -0400187 moveToHostQuiesce(bus);
Andrew Geisslerba0163a2021-08-11 15:09:50 -0400188
189 return 0;
190}