blob: d9a58c790bfeebf10aa825d2bd19e63c15a0f163 [file] [log] [blame]
Andrew Geissler234a3172019-08-09 14:30:02 -05001#pragma once
2
Andrew Geisslere426b582020-05-28 12:40:55 -05003#include "systemd_target_parser.hpp"
4
Andrew Geissler234a3172019-08-09 14:30:02 -05005#include <sdbusplus/bus.hpp>
6#include <sdbusplus/bus/match.hpp>
Andrew Geissler234a3172019-08-09 14:30:02 -05007
8extern bool gVerbose;
9
10namespace phosphor
11{
12namespace state
13{
14namespace manager
15{
16/** @class SystemdTargetLogging
17 * @brief Object to monitor input systemd targets and create corresponding
18 * input errors for on failures
19 */
20class SystemdTargetLogging
21{
22 public:
23 SystemdTargetLogging() = delete;
24 SystemdTargetLogging(const SystemdTargetLogging&) = delete;
25 SystemdTargetLogging& operator=(const SystemdTargetLogging&) = delete;
26 SystemdTargetLogging(SystemdTargetLogging&&) = delete;
27 SystemdTargetLogging& operator=(SystemdTargetLogging&&) = delete;
28 virtual ~SystemdTargetLogging() = default;
29
30 SystemdTargetLogging(const TargetErrorData& targetData,
31 sdbusplus::bus::bus& bus) :
32 targetData(targetData),
33 bus(bus),
Andrew Geisslerb558ae72019-11-07 10:40:06 -060034 systemdJobRemovedSignal(
Andrew Geissler234a3172019-08-09 14:30:02 -050035 bus,
36 sdbusplus::bus::match::rules::type::signal() +
37 sdbusplus::bus::match::rules::member("JobRemoved") +
38 sdbusplus::bus::match::rules::path(
39 "/org/freedesktop/systemd1") +
40 sdbusplus::bus::match::rules::interface(
41 "org.freedesktop.systemd1.Manager"),
42 std::bind(std::mem_fn(&SystemdTargetLogging::systemdUnitChange),
Andrew Geissler38605ee2019-11-11 16:01:56 -060043 this, std::placeholders::_1)),
44 systemdNameOwnedChangedSignal(
45 bus, sdbusplus::bus::match::rules::nameOwnerChanged(),
46 std::bind(
47 std::mem_fn(&SystemdTargetLogging::processNameChangeSignal),
48 this, std::placeholders::_1))
Andrew Geisslere426b582020-05-28 12:40:55 -050049 {}
Andrew Geissler234a3172019-08-09 14:30:02 -050050
51 /**
52 * @brief subscribe to the systemd signals
53 *
54 * This object needs to monitor systemd target changes so it can create
55 * the required error logs on failures
56 *
57 **/
58 void subscribeToSystemdSignals();
59
60 /** @brief Process the target fail and return error to log
61 *
62 * @note This is public for unit testing purposes
63 *
64 * @param[in] unit - The systemd unit that failed
65 * @param[in] result - The failure code from the system unit
66 *
67 * @return valid pointer to error to log, otherwise nullptr
68 */
69 const std::string* processError(const std::string& unit,
70 const std::string& result);
71
72 private:
73 /** @brief Call phosphor-logging to create error
74 *
75 * @param[in] error - The error to log
76 * @param[in] result - The failure code from the systemd unit
77 */
78 void logError(const std::string& error, const std::string& result);
79
80 /** @brief Check if systemd state change is one to monitor
81 *
82 * Instance specific interface to handle the detected systemd state
83 * change
84 *
85 * @param[in] msg - Data associated with subscribed signal
86 *
87 */
88 void systemdUnitChange(sdbusplus::message::message& msg);
89
Andrew Geissler38605ee2019-11-11 16:01:56 -060090 /** @brief Wait for systemd to show up on dbus
91 *
92 * Once systemd is on dbus, this application can subscribe to systemd
93 * signal changes
94 *
95 * @param[in] msg - Data associated with subscribed signal
96 *
97 */
98 void processNameChangeSignal(sdbusplus::message::message& msg);
99
Andrew Geissler234a3172019-08-09 14:30:02 -0500100 /** @brief Systemd targets to monitor and error logs to create */
101 const TargetErrorData& targetData;
102
103 /** @brief Persistent sdbusplus DBus bus connection. */
104 sdbusplus::bus::bus& bus;
105
Andrew Geisslerb558ae72019-11-07 10:40:06 -0600106 /** @brief Used to subscribe to dbus systemd JobRemoved signals **/
107 sdbusplus::bus::match_t systemdJobRemovedSignal;
Andrew Geissler38605ee2019-11-11 16:01:56 -0600108
109 /** @brief Used to know when systemd has registered on dbus **/
110 sdbusplus::bus::match_t systemdNameOwnedChangedSignal;
Andrew Geissler234a3172019-08-09 14:30:02 -0500111};
112
113} // namespace manager
114} // namespace state
115} // namespace phosphor