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