Andrew Geissler | 5259f97 | 2021-08-30 15:46:55 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Andrew Geissler | 556153a | 2021-08-11 15:29:58 -0400 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | |
| 5 | #include <phosphor-logging/elog.hpp> |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <sdbusplus/exception.hpp> |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 9 | #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 Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 12 | |
| 13 | #include <cstdlib> |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 14 | #include <format> |
Andrew Geissler | 5259f97 | 2021-08-30 15:46:55 -0500 | [diff] [blame] | 15 | #include <fstream> |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 18 | namespace phosphor |
| 19 | { |
| 20 | namespace state |
| 21 | { |
| 22 | namespace manager |
| 23 | { |
| 24 | |
| 25 | PHOSPHOR_LOG2_USING; |
| 26 | |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 27 | using BootProgress = |
| 28 | sdbusplus::client::xyz::openbmc_project::state::boot::Progress<>; |
| 29 | using LoggingCreate = |
| 30 | sdbusplus::client::xyz::openbmc_project::logging::Create<>; |
| 31 | using LoggingEntry = sdbusplus::client::xyz::openbmc_project::logging::Entry<>; |
| 32 | |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 33 | constexpr auto HOST_STATE_SVC = "xyz.openbmc_project.State.Host"; |
| 34 | constexpr auto HOST_STATE_PATH = "/xyz/openbmc_project/state/host0"; |
| 35 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 36 | constexpr auto BOOT_PROGRESS_PROP = "BootProgress"; |
| 37 | |
Andrew Geissler | 7b01b0b | 2021-08-11 16:14:49 -0400 | [diff] [blame] | 38 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 39 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 40 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 41 | constexpr auto HOST_STATE_QUIESCE_TGT = "obmc-host-quiesce@0.target"; |
| 42 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 43 | bool wasHostBooting(sdbusplus::bus_t& bus) |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 44 | { |
| 45 | try |
| 46 | { |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 47 | using ProgressStages = BootProgress::ProgressStages; |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 48 | |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 49 | auto method = bus.new_method_call(HOST_STATE_SVC, HOST_STATE_PATH, |
| 50 | PROPERTY_INTERFACE, "Get"); |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 51 | method.append(BootProgress::interface, BOOT_PROGRESS_PROP); |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 52 | |
| 53 | auto response = bus.call(method); |
| 54 | |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 55 | std::variant<ProgressStages> bootProgressV; |
| 56 | response.read(bootProgressV); |
| 57 | auto bootProgress = std::get<ProgressStages>(bootProgressV); |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 58 | |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 59 | if (bootProgress == ProgressStages::Unspecified) |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 60 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 61 | info("Host was not booting before BMC reboot"); |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 65 | info("Host was booting before BMC reboot: {BOOTPROGRESS}", |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 66 | "BOOTPROGRESS", bootProgress); |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 67 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 68 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 69 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 70 | error("Error reading BootProgress, error {ERROR}, service {SERVICE}, " |
| 71 | "path {PATH}", |
| 72 | "ERROR", e, "SERVICE", HOST_STATE_SVC, "PATH", HOST_STATE_PATH); |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 73 | |
| 74 | throw; |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 80 | void createErrorLog(sdbusplus::bus_t& bus) |
Andrew Geissler | 556153a | 2021-08-11 15:29:58 -0400 | [diff] [blame] | 81 | { |
| 82 | try |
| 83 | { |
| 84 | // Create interface requires something for additionalData |
| 85 | std::map<std::string, std::string> additionalData; |
| 86 | additionalData.emplace("_PID", std::to_string(getpid())); |
| 87 | |
| 88 | static constexpr auto errorMessage = |
| 89 | "xyz.openbmc_project.State.Error.HostNotRunning"; |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 90 | auto method = bus.new_method_call(LoggingCreate::default_service, |
| 91 | LoggingCreate::instance_path, |
| 92 | LoggingCreate::interface, "Create"); |
| 93 | method.append(errorMessage, LoggingEntry::Level::Error, additionalData); |
Andrew Geissler | 556153a | 2021-08-11 15:29:58 -0400 | [diff] [blame] | 94 | auto resp = bus.call(method); |
| 95 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 96 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 556153a | 2021-08-11 15:29:58 -0400 | [diff] [blame] | 97 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 98 | error( |
| 99 | "sdbusplus D-Bus call exception, error {ERROR}, objpath {OBJPATH}, " |
| 100 | "interface {INTERFACE}", |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 101 | "ERROR", e, "OBJPATH", LoggingCreate::instance_path, "INTERFACE", |
| 102 | LoggingCreate::interface); |
Andrew Geissler | 556153a | 2021-08-11 15:29:58 -0400 | [diff] [blame] | 103 | |
| 104 | throw std::runtime_error( |
| 105 | "Error in invoking D-Bus logging create interface"); |
| 106 | } |
Patrick Williams | 8583b3b | 2021-10-06 12:19:20 -0500 | [diff] [blame] | 107 | catch (const std::exception& e) |
Andrew Geissler | 556153a | 2021-08-11 15:29:58 -0400 | [diff] [blame] | 108 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 109 | error("D-bus call exception: {ERROR}", "ERROR", e); |
Andrew Geissler | 556153a | 2021-08-11 15:29:58 -0400 | [diff] [blame] | 110 | throw e; |
| 111 | } |
| 112 | } |
| 113 | |
Andrew Geissler | 5259f97 | 2021-08-30 15:46:55 -0500 | [diff] [blame] | 114 | // Once CHASSIS_ON_FILE is removed, the obmc-chassis-poweron@.target has |
| 115 | // completed and the phosphor-chassis-state-manager code has processed it. |
| 116 | bool isChassisTargetComplete() |
| 117 | { |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 118 | auto chassisFile = std::format(CHASSIS_ON_FILE, 0); |
Andrew Geissler | 5259f97 | 2021-08-30 15:46:55 -0500 | [diff] [blame] | 119 | |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 120 | std::ifstream f(chassisFile); |
Andrew Geissler | 5259f97 | 2021-08-30 15:46:55 -0500 | [diff] [blame] | 121 | return !f.good(); |
| 122 | } |
| 123 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 124 | void moveToHostQuiesce(sdbusplus::bus_t& bus) |
Andrew Geissler | 7b01b0b | 2021-08-11 16:14:49 -0400 | [diff] [blame] | 125 | { |
| 126 | try |
| 127 | { |
| 128 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 129 | SYSTEMD_INTERFACE, "StartUnit"); |
| 130 | |
| 131 | method.append(HOST_STATE_QUIESCE_TGT); |
| 132 | method.append("replace"); |
| 133 | |
| 134 | bus.call_noreply(method); |
| 135 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 136 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 7b01b0b | 2021-08-11 16:14:49 -0400 | [diff] [blame] | 137 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 138 | error("sdbusplus call exception starting quiesce target: {ERROR}", |
| 139 | "ERROR", e); |
Andrew Geissler | 7b01b0b | 2021-08-11 16:14:49 -0400 | [diff] [blame] | 140 | |
| 141 | throw std::runtime_error( |
| 142 | "Error in invoking D-Bus systemd StartUnit method"); |
| 143 | } |
| 144 | } |
| 145 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 146 | } // namespace manager |
| 147 | } // namespace state |
| 148 | } // namespace phosphor |
| 149 | |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 150 | int main() |
| 151 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 152 | using namespace phosphor::state::manager; |
| 153 | PHOSPHOR_LOG2_USING; |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 154 | |
| 155 | auto bus = sdbusplus::bus::new_default(); |
| 156 | |
Andrew Geissler | 5259f97 | 2021-08-30 15:46:55 -0500 | [diff] [blame] | 157 | // Chassis power is on if this service starts but need to wait for the |
| 158 | // obmc-chassis-poweron@.target to complete before potentially initiating |
| 159 | // another systemd target transition (i.e. Quiesce->Reboot) |
| 160 | while (!isChassisTargetComplete()) |
| 161 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 162 | debug("Waiting for chassis on target to complete"); |
Andrew Geissler | 5259f97 | 2021-08-30 15:46:55 -0500 | [diff] [blame] | 163 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 164 | |
| 165 | // There is no timeout here, wait until it happens or until system |
| 166 | // is powered off and this service is stopped |
| 167 | } |
| 168 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 169 | info("Chassis power on has completed, checking if host is " |
| 170 | "still running after the BMC reboot"); |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 171 | |
| 172 | // Check the last BootProgeress to see if the host was booting before |
| 173 | // the BMC reboot occurred |
| 174 | if (!wasHostBooting(bus)) |
| 175 | { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | // Host was booting before the BMC reboot so log an error and go to host |
| 180 | // quiesce target |
Andrew Geissler | 556153a | 2021-08-11 15:29:58 -0400 | [diff] [blame] | 181 | createErrorLog(bus); |
Andrew Geissler | 7b01b0b | 2021-08-11 16:14:49 -0400 | [diff] [blame] | 182 | moveToHostQuiesce(bus); |
Andrew Geissler | ba0163a | 2021-08-11 15:09:50 -0400 | [diff] [blame] | 183 | |
| 184 | return 0; |
| 185 | } |