blob: cc3cf4cfc89d23170683a2cdab112979f4d5c675 [file] [log] [blame]
Andrew Geisslere426b582020-05-28 12:40:55 -05001#include "systemd_target_signal.hpp"
2
Andrew Geissler234a3172019-08-09 14:30:02 -05003#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -05004#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -05005#include <sdbusplus/exception.hpp>
Andrew Geissler234a3172019-08-09 14:30:02 -05006#include <sdbusplus/server/manager.hpp>
7#include <sdeventplus/event.hpp>
Andrew Geissler234a3172019-08-09 14:30:02 -05008#include <xyz/openbmc_project/Common/error.hpp>
9
10namespace phosphor
11{
12namespace state
13{
14namespace manager
15{
16
17using phosphor::logging::elog;
Andrew Geissler8ffdb262021-09-20 15:25:19 -050018PHOSPHOR_LOG2_USING;
19
Andrew Geissler234a3172019-08-09 14:30:02 -050020using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
21
Andrew Geissler21d305e2022-02-11 16:49:51 -060022void SystemdTargetLogging::createBmcDump()
23{
24 auto method = this->bus.new_method_call(
25 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump/bmc",
26 "xyz.openbmc_project.Dump.Create", "CreateDump");
27 method.append(
28 std::vector<
29 std::pair<std::string, std::variant<std::string, uint64_t>>>());
30 try
31 {
32 this->bus.call_noreply(method);
33 }
34 catch (const sdbusplus::exception::exception& e)
35 {
36 error("Failed to create BMC dump, exception:{ERROR}", "ERROR", e);
37 // just continue, this is error path anyway so we're just collecting
38 // what we can
39 }
40}
41
Andrew Geissler8ffdb262021-09-20 15:25:19 -050042void SystemdTargetLogging::logError(const std::string& errorLog,
Andrew Geisslere6841032022-02-11 10:31:50 -060043 const std::string& result,
44 const std::string& unit)
Andrew Geissler234a3172019-08-09 14:30:02 -050045{
46 auto method = this->bus.new_method_call(
47 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
48 "xyz.openbmc_project.Logging.Create", "Create");
49 // Signature is ssa{ss}
Andrew Geissler8ffdb262021-09-20 15:25:19 -050050 method.append(errorLog);
Andrew Geissler234a3172019-08-09 14:30:02 -050051 method.append("xyz.openbmc_project.Logging.Entry.Level.Critical");
Andrew Geisslere6841032022-02-11 10:31:50 -060052 method.append(std::array<std::pair<std::string, std::string>, 2>(
53 {std::pair<std::string, std::string>({"SYSTEMD_RESULT", result}),
54 std::pair<std::string, std::string>({"SYSTEMD_UNIT", unit})}));
Andrew Geissler234a3172019-08-09 14:30:02 -050055 try
56 {
57 this->bus.call_noreply(method);
58 }
Patrick Williams0a675212021-09-02 09:49:43 -050059 catch (const sdbusplus::exception::exception& e)
Andrew Geissler234a3172019-08-09 14:30:02 -050060 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050061 error("Failed to create systemd target error, error:{ERROR_MSG}, "
62 "result:{RESULT}, exception:{ERROR}",
63 "ERROR_MSG", errorLog, "RESULT", result, "ERROR", e);
Andrew Geissler234a3172019-08-09 14:30:02 -050064 }
65}
66
Andrew Geisslerf3870c62022-02-10 16:15:28 -060067const std::string SystemdTargetLogging::processError(const std::string& unit,
68 const std::string& result)
Andrew Geissler234a3172019-08-09 14:30:02 -050069{
70 auto targetEntry = this->targetData.find(unit);
71 if (targetEntry != this->targetData.end())
72 {
73 // Check if its result matches any of our monitored errors
74 if (std::find(targetEntry->second.errorsToMonitor.begin(),
75 targetEntry->second.errorsToMonitor.end(),
76 result) != targetEntry->second.errorsToMonitor.end())
77 {
Andrew Geisslerad65b2d2021-09-21 12:53:29 -050078 info(
79 "Monitored systemd unit has hit an error, unit:{UNIT}, result:{RESULT}",
80 "UNIT", unit, "RESULT", result);
Andrew Geisslerf3870c62022-02-10 16:15:28 -060081 return (targetEntry->second.errorToLog);
Andrew Geissler234a3172019-08-09 14:30:02 -050082 }
83 }
Andrew Geisslerf3870c62022-02-10 16:15:28 -060084
85 // Check if it's in our list of services to monitor
86 if (std::find(this->serviceData.begin(), this->serviceData.end(), unit) !=
87 this->serviceData.end())
88 {
89 if (result == "failed")
90 {
91 info(
92 "Monitored systemd service has hit an error, unit:{UNIT}, result:{RESULT}",
93 "UNIT", unit, "RESULT", result);
Andrew Geissler21d305e2022-02-11 16:49:51 -060094
95 // Generate a BMC dump when a critical service fails
96 createBmcDump();
Andrew Geisslerf3870c62022-02-10 16:15:28 -060097 return (std::string{
98 "xyz.openbmc_project.State.Error.CriticalServiceFailure"});
99 }
100 }
101
102 return (std::string{});
Andrew Geissler234a3172019-08-09 14:30:02 -0500103}
104
105void SystemdTargetLogging::systemdUnitChange(sdbusplus::message::message& msg)
106{
107 uint32_t id;
108 sdbusplus::message::object_path objPath;
109 std::string unit{};
110 std::string result{};
111
112 msg.read(id, objPath, unit, result);
113
114 // In most cases it will just be success, in which case just return
115 if (result != "done")
116 {
Andrew Geisslerf3870c62022-02-10 16:15:28 -0600117 const std::string error = processError(unit, result);
Andrew Geissler234a3172019-08-09 14:30:02 -0500118
119 // If this is a monitored error then log it
Andrew Geisslerf3870c62022-02-10 16:15:28 -0600120 if (!error.empty())
Andrew Geissler234a3172019-08-09 14:30:02 -0500121 {
Andrew Geisslere6841032022-02-11 10:31:50 -0600122 logError(error, result, unit);
Andrew Geissler234a3172019-08-09 14:30:02 -0500123 }
124 }
125 return;
126}
127
Andrew Geissler38605ee2019-11-11 16:01:56 -0600128void SystemdTargetLogging::processNameChangeSignal(
129 sdbusplus::message::message& msg)
130{
131 std::string name; // well-known
132 std::string old_owner; // unique-name
133 std::string new_owner; // unique-name
134
135 msg.read(name, old_owner, new_owner);
136
137 // Looking for systemd to be on dbus so we can call it
138 if (name == "org.freedesktop.systemd1")
139 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500140 info("org.freedesktop.systemd1 is now on dbus");
Andrew Geissler38605ee2019-11-11 16:01:56 -0600141 subscribeToSystemdSignals();
142 }
143 return;
144}
145
Andrew Geissler234a3172019-08-09 14:30:02 -0500146void SystemdTargetLogging::subscribeToSystemdSignals()
147{
148 auto method = this->bus.new_method_call(
149 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
150 "org.freedesktop.systemd1.Manager", "Subscribe");
151
152 try
153 {
154 this->bus.call(method);
155 }
Patrick Williams0a675212021-09-02 09:49:43 -0500156 catch (const sdbusplus::exception::exception& e)
Andrew Geissler234a3172019-08-09 14:30:02 -0500157 {
Andrew Geissler38605ee2019-11-11 16:01:56 -0600158 // If error indicates systemd is not on dbus yet then do nothing.
159 // The systemdNameChangeSignals callback will detect when it is on
160 // dbus and then call this function again
161 const std::string noDbus("org.freedesktop.DBus.Error.ServiceUnknown");
162 if (noDbus == e.name())
163 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500164 info("org.freedesktop.systemd1 not on dbus yet");
Andrew Geissler38605ee2019-11-11 16:01:56 -0600165 }
166 else
167 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500168 error("Failed to subscribe to systemd signals: {ERROR}", "ERROR",
169 e);
Andrew Geissler38605ee2019-11-11 16:01:56 -0600170 elog<InternalFailure>();
171 }
172 return;
Andrew Geissler234a3172019-08-09 14:30:02 -0500173 }
174
Andrew Geissler38605ee2019-11-11 16:01:56 -0600175 // Call destructor on match callback since application is now subscribed to
176 // systemd signals
177 this->systemdNameOwnedChangedSignal.~match();
178
Andrew Geissler234a3172019-08-09 14:30:02 -0500179 return;
180}
181
182} // namespace manager
183} // namespace state
184} // namespace phosphor