1 #pragma once 2 3 #include "systemd_target_parser.hpp" 4 5 #include <sdbusplus/bus.hpp> 6 #include <sdbusplus/bus/match.hpp> 7 8 extern bool gVerbose; 9 10 namespace phosphor 11 { 12 namespace state 13 { 14 namespace manager 15 { 16 /** @class SystemdTargetLogging 17 * @brief Object to monitor input systemd targets and create corresponding 18 * input errors for on failures 19 */ 20 class 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), 34 systemdJobRemovedSignal( 35 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), 43 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)) 49 {} 50 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 90 /** @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 100 /** @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 106 /** @brief Used to subscribe to dbus systemd JobRemoved signals **/ 107 sdbusplus::bus::match_t systemdJobRemovedSignal; 108 109 /** @brief Used to know when systemd has registered on dbus **/ 110 sdbusplus::bus::match_t systemdNameOwnedChangedSignal; 111 }; 112 113 } // namespace manager 114 } // namespace state 115 } // namespace phosphor 116