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 | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
| 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; |
| 18 | using phosphor::logging::entry; |
| 19 | using phosphor::logging::level; |
| 20 | using phosphor::logging::log; |
| 21 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 22 | |
| 23 | void SystemdTargetLogging::logError(const std::string& error, |
| 24 | const std::string& result) |
| 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} |
| 30 | method.append(error); |
| 31 | method.append("xyz.openbmc_project.Logging.Entry.Level.Critical"); |
| 32 | method.append(std::array<std::pair<std::string, std::string>, 1>( |
| 33 | {std::pair<std::string, std::string>({"SYSTEMD_RESULT", result})})); |
| 34 | try |
| 35 | { |
| 36 | this->bus.call_noreply(method); |
| 37 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 38 | catch (const sdbusplus::exception::exception& e) |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 39 | { |
| 40 | log<level::ERR>("Failed to create systemd target error", |
| 41 | entry("ERROR=%s", error.c_str()), |
| 42 | entry("RESULT=%s", result.c_str()), |
| 43 | entry("SDBUSERR=%s", e.what())); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | const std::string* SystemdTargetLogging::processError(const std::string& unit, |
| 48 | const std::string& result) |
| 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 | { |
| 58 | log<level::INFO>("Monitored systemd unit has hit an error", |
| 59 | entry("UNIT=%s", unit.c_str()), |
| 60 | entry("RESULT=%s", result.c_str())); |
| 61 | return (&targetEntry->second.errorToLog); |
| 62 | } |
| 63 | } |
Andrew Geissler | b154fa9 | 2019-12-10 15:18:10 -0600 | [diff] [blame] | 64 | return nullptr; |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void SystemdTargetLogging::systemdUnitChange(sdbusplus::message::message& msg) |
| 68 | { |
| 69 | uint32_t id; |
| 70 | sdbusplus::message::object_path objPath; |
| 71 | std::string unit{}; |
| 72 | std::string result{}; |
| 73 | |
| 74 | msg.read(id, objPath, unit, result); |
| 75 | |
| 76 | // In most cases it will just be success, in which case just return |
| 77 | if (result != "done") |
| 78 | { |
| 79 | const std::string* error = processError(unit, result); |
| 80 | |
| 81 | // If this is a monitored error then log it |
| 82 | if (error) |
| 83 | { |
| 84 | logError(*error, result); |
| 85 | } |
| 86 | } |
| 87 | return; |
| 88 | } |
| 89 | |
Andrew Geissler | 38605ee | 2019-11-11 16:01:56 -0600 | [diff] [blame] | 90 | void SystemdTargetLogging::processNameChangeSignal( |
| 91 | sdbusplus::message::message& msg) |
| 92 | { |
| 93 | std::string name; // well-known |
| 94 | std::string old_owner; // unique-name |
| 95 | std::string new_owner; // unique-name |
| 96 | |
| 97 | msg.read(name, old_owner, new_owner); |
| 98 | |
| 99 | // Looking for systemd to be on dbus so we can call it |
| 100 | if (name == "org.freedesktop.systemd1") |
| 101 | { |
| 102 | log<level::INFO>("org.freedesktop.systemd1 is now on dbus"); |
| 103 | subscribeToSystemdSignals(); |
| 104 | } |
| 105 | return; |
| 106 | } |
| 107 | |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 108 | void SystemdTargetLogging::subscribeToSystemdSignals() |
| 109 | { |
| 110 | auto method = this->bus.new_method_call( |
| 111 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 112 | "org.freedesktop.systemd1.Manager", "Subscribe"); |
| 113 | |
| 114 | try |
| 115 | { |
| 116 | this->bus.call(method); |
| 117 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 118 | catch (const sdbusplus::exception::exception& e) |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 119 | { |
Andrew Geissler | 38605ee | 2019-11-11 16:01:56 -0600 | [diff] [blame] | 120 | // If error indicates systemd is not on dbus yet then do nothing. |
| 121 | // The systemdNameChangeSignals callback will detect when it is on |
| 122 | // dbus and then call this function again |
| 123 | const std::string noDbus("org.freedesktop.DBus.Error.ServiceUnknown"); |
| 124 | if (noDbus == e.name()) |
| 125 | { |
| 126 | log<level::INFO>("org.freedesktop.systemd1 not on dbus yet"); |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | log<level::ERR>("Failed to subscribe to systemd signals", |
| 131 | entry("SDBUSERR=%s", e.what())); |
| 132 | elog<InternalFailure>(); |
| 133 | } |
| 134 | return; |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Andrew Geissler | 38605ee | 2019-11-11 16:01:56 -0600 | [diff] [blame] | 137 | // Call destructor on match callback since application is now subscribed to |
| 138 | // systemd signals |
| 139 | this->systemdNameOwnedChangedSignal.~match(); |
| 140 | |
Andrew Geissler | 234a317 | 2019-08-09 14:30:02 -0500 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | } // namespace manager |
| 145 | } // namespace state |
| 146 | } // namespace phosphor |