Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 1 | #include "systemd_target_signal.hpp" |
| 2 | |
Andrew Geissler | 55e96ac | 2022-04-19 11:44:53 -0400 | [diff] [blame] | 3 | #include "utils.hpp" |
| 4 | |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/elog-errors.hpp> |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 7 | #include <sdbusplus/exception.hpp> |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 8 | #include <sdbusplus/server/manager.hpp> |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Common/error.hpp> |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Logging/Create/client.hpp> |
| 11 | #include <xyz/openbmc_project/Logging/Entry/client.hpp> |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace state |
| 16 | { |
| 17 | namespace manager |
| 18 | { |
| 19 | |
| 20 | using phosphor::logging::elog; |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 21 | PHOSPHOR_LOG2_USING; |
| 22 | |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 23 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 24 | |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 25 | using LoggingCreate = |
| 26 | sdbusplus::client::xyz::openbmc_project::logging::Create<>; |
| 27 | using LoggingEntry = sdbusplus::client::xyz::openbmc_project::logging::Entry<>; |
| 28 | |
Andrew Geissler | 73d2ac9 | 2022-02-17 16:55:50 -0600 | [diff] [blame] | 29 | void SystemdTargetLogging::startBmcQuiesceTarget() |
| 30 | { |
| 31 | auto method = this->bus.new_method_call( |
| 32 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 33 | "org.freedesktop.systemd1.Manager", "StartUnit"); |
| 34 | |
| 35 | // TODO: Enhance when needed to support multiple-bmc instance systems |
| 36 | method.append("obmc-bmc-service-quiesce@0.target"); |
| 37 | method.append("replace"); |
| 38 | try |
| 39 | { |
| 40 | this->bus.call_noreply(method); |
| 41 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 42 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 73d2ac9 | 2022-02-17 16:55:50 -0600 | [diff] [blame] | 43 | { |
| 44 | error("Failed to start BMC quiesce target, exception:{ERROR}", "ERROR", |
| 45 | e); |
| 46 | // just continue, this is error path anyway so we're just doing what |
| 47 | // we can |
| 48 | } |
| 49 | |
| 50 | return; |
| 51 | } |
| 52 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 53 | void SystemdTargetLogging::logError(const std::string& errorLog, |
Andrew Geissler | e684103 | 2022-02-11 10:31:50 -0600 | [diff] [blame] | 54 | const std::string& result, |
| 55 | const std::string& unit) |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 56 | { |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame] | 57 | auto method = this->bus.new_method_call( |
| 58 | LoggingCreate::default_service, LoggingCreate::instance_path, |
| 59 | LoggingCreate::interface, "Create"); |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 60 | // Signature is ssa{ss} |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 61 | method.append( |
| 62 | errorLog, LoggingEntry::Level::Critical, |
| 63 | std::array<std::pair<std::string, std::string>, 2>( |
| 64 | {std::pair<std::string, std::string>({"SYSTEMD_RESULT", result}), |
| 65 | std::pair<std::string, std::string>({"SYSTEMD_UNIT", unit})})); |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 66 | try |
| 67 | { |
| 68 | this->bus.call_noreply(method); |
| 69 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 70 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 71 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 72 | error("Failed to create systemd target error, error:{ERROR_MSG}, " |
| 73 | "result:{RESULT}, exception:{ERROR}", |
| 74 | "ERROR_MSG", errorLog, "RESULT", result, "ERROR", e); |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Pavithra Barithaya | d7a15cb | 2024-06-21 11:24:46 -0500 | [diff] [blame] | 78 | std::string SystemdTargetLogging::processError(const std::string& unit, |
| 79 | const std::string& result) |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 80 | { |
| 81 | auto targetEntry = this->targetData.find(unit); |
| 82 | if (targetEntry != this->targetData.end()) |
| 83 | { |
| 84 | // Check if its result matches any of our monitored errors |
| 85 | if (std::find(targetEntry->second.errorsToMonitor.begin(), |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame] | 86 | targetEntry->second.errorsToMonitor.end(), result) != |
| 87 | targetEntry->second.errorsToMonitor.end()) |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 88 | { |
Andrew Geissler | ad65b2d | 2021-09-21 12:53:29 -0500 | [diff] [blame] | 89 | info( |
| 90 | "Monitored systemd unit has hit an error, unit:{UNIT}, result:{RESULT}", |
| 91 | "UNIT", unit, "RESULT", result); |
Andrew Geissler | d59006a | 2022-03-21 14:25:08 -0500 | [diff] [blame] | 92 | |
| 93 | // Generate a BMC dump when a monitored target fails |
Andrew Geissler | 55e96ac | 2022-04-19 11:44:53 -0400 | [diff] [blame] | 94 | utils::createBmcDump(this->bus); |
Andrew Geissler | f3870c6 | 2022-02-10 16:15:28 -0600 | [diff] [blame] | 95 | return (targetEntry->second.errorToLog); |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 96 | } |
| 97 | } |
Andrew Geissler | f3870c6 | 2022-02-10 16:15:28 -0600 | [diff] [blame] | 98 | |
| 99 | // Check if it's in our list of services to monitor |
| 100 | if (std::find(this->serviceData.begin(), this->serviceData.end(), unit) != |
| 101 | this->serviceData.end()) |
| 102 | { |
| 103 | if (result == "failed") |
| 104 | { |
| 105 | info( |
| 106 | "Monitored systemd service has hit an error, unit:{UNIT}, result:{RESULT}", |
| 107 | "UNIT", unit, "RESULT", result); |
Andrew Geissler | 21d305e | 2022-02-11 16:49:51 -0600 | [diff] [blame] | 108 | |
| 109 | // Generate a BMC dump when a critical service fails |
Andrew Geissler | 55e96ac | 2022-04-19 11:44:53 -0400 | [diff] [blame] | 110 | utils::createBmcDump(this->bus); |
Andrew Geissler | 73d2ac9 | 2022-02-17 16:55:50 -0600 | [diff] [blame] | 111 | // Enter BMC Quiesce when a critical service fails |
| 112 | startBmcQuiesceTarget(); |
Andrew Geissler | f3870c6 | 2022-02-10 16:15:28 -0600 | [diff] [blame] | 113 | return (std::string{ |
| 114 | "xyz.openbmc_project.State.Error.CriticalServiceFailure"}); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return (std::string{}); |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 121 | void SystemdTargetLogging::systemdUnitChange(sdbusplus::message_t& msg) |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 122 | { |
| 123 | uint32_t id; |
| 124 | sdbusplus::message::object_path objPath; |
| 125 | std::string unit{}; |
| 126 | std::string result{}; |
| 127 | |
| 128 | msg.read(id, objPath, unit, result); |
| 129 | |
| 130 | // In most cases it will just be success, in which case just return |
| 131 | if (result != "done") |
| 132 | { |
Andrew Geissler | f3870c6 | 2022-02-10 16:15:28 -0600 | [diff] [blame] | 133 | const std::string error = processError(unit, result); |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 134 | |
| 135 | // If this is a monitored error then log it |
Andrew Geissler | f3870c6 | 2022-02-10 16:15:28 -0600 | [diff] [blame] | 136 | if (!error.empty()) |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 137 | { |
Andrew Geissler | e684103 | 2022-02-11 10:31:50 -0600 | [diff] [blame] | 138 | logError(error, result, unit); |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | return; |
| 142 | } |
| 143 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 144 | void SystemdTargetLogging::processNameChangeSignal(sdbusplus::message_t& msg) |
Andrew Geissler | 38605ee | 2019-11-11 16:01:56 -0600 | [diff] [blame] | 145 | { |
| 146 | std::string name; // well-known |
| 147 | std::string old_owner; // unique-name |
| 148 | std::string new_owner; // unique-name |
| 149 | |
| 150 | msg.read(name, old_owner, new_owner); |
| 151 | |
| 152 | // Looking for systemd to be on dbus so we can call it |
| 153 | if (name == "org.freedesktop.systemd1") |
| 154 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 155 | info("org.freedesktop.systemd1 is now on dbus"); |
Andrew Geissler | 38605ee | 2019-11-11 16:01:56 -0600 | [diff] [blame] | 156 | subscribeToSystemdSignals(); |
| 157 | } |
| 158 | return; |
| 159 | } |
| 160 | |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 161 | void SystemdTargetLogging::subscribeToSystemdSignals() |
| 162 | { |
| 163 | auto method = this->bus.new_method_call( |
| 164 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 165 | "org.freedesktop.systemd1.Manager", "Subscribe"); |
| 166 | |
| 167 | try |
| 168 | { |
| 169 | this->bus.call(method); |
| 170 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 171 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 172 | { |
Andrew Geissler | 38605ee | 2019-11-11 16:01:56 -0600 | [diff] [blame] | 173 | // If error indicates systemd is not on dbus yet then do nothing. |
| 174 | // The systemdNameChangeSignals callback will detect when it is on |
| 175 | // dbus and then call this function again |
| 176 | const std::string noDbus("org.freedesktop.DBus.Error.ServiceUnknown"); |
| 177 | if (noDbus == e.name()) |
| 178 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 179 | info("org.freedesktop.systemd1 not on dbus yet"); |
Andrew Geissler | 38605ee | 2019-11-11 16:01:56 -0600 | [diff] [blame] | 180 | } |
| 181 | else |
| 182 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 183 | error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", |
| 184 | e); |
Andrew Geissler | 38605ee | 2019-11-11 16:01:56 -0600 | [diff] [blame] | 185 | elog<InternalFailure>(); |
| 186 | } |
| 187 | return; |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Andrew Geissler | 38605ee | 2019-11-11 16:01:56 -0600 | [diff] [blame] | 190 | // Call destructor on match callback since application is now subscribed to |
| 191 | // systemd signals |
| 192 | this->systemdNameOwnedChangedSignal.~match(); |
| 193 | |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | |
| 197 | } // namespace manager |
| 198 | } // namespace state |
| 199 | } // namespace phosphor |