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