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