1 #pragma once
2 
3 #include <sdbusplus/bus.hpp>
4 #include <sdbusplus/bus/match.hpp>
5 #include <systemd_target_parser.hpp>
6 
7 extern bool gVerbose;
8 
9 namespace phosphor
10 {
11 namespace state
12 {
13 namespace manager
14 {
15 /** @class SystemdTargetLogging
16  *  @brief Object to monitor input systemd targets and create corresponding
17  *         input errors for on failures
18  */
19 class SystemdTargetLogging
20 {
21   public:
22     SystemdTargetLogging() = delete;
23     SystemdTargetLogging(const SystemdTargetLogging&) = delete;
24     SystemdTargetLogging& operator=(const SystemdTargetLogging&) = delete;
25     SystemdTargetLogging(SystemdTargetLogging&&) = delete;
26     SystemdTargetLogging& operator=(SystemdTargetLogging&&) = delete;
27     virtual ~SystemdTargetLogging() = default;
28 
29     SystemdTargetLogging(const TargetErrorData& targetData,
30                          sdbusplus::bus::bus& bus) :
31         targetData(targetData),
32         bus(bus),
33         systemdJobRemovedSignal(
34             bus,
35             sdbusplus::bus::match::rules::type::signal() +
36                 sdbusplus::bus::match::rules::member("JobRemoved") +
37                 sdbusplus::bus::match::rules::path(
38                     "/org/freedesktop/systemd1") +
39                 sdbusplus::bus::match::rules::interface(
40                     "org.freedesktop.systemd1.Manager"),
41             std::bind(std::mem_fn(&SystemdTargetLogging::systemdUnitChange),
42                       this, std::placeholders::_1))
43     {
44     }
45 
46     /**
47      * @brief subscribe to the systemd signals
48      *
49      * This object needs to monitor systemd target changes so it can create
50      * the required error logs on failures
51      *
52      **/
53     void subscribeToSystemdSignals();
54 
55     /** @brief Process the target fail and return error to log
56      *
57      * @note This is public for unit testing purposes
58      *
59      * @param[in]  unit       - The systemd unit that failed
60      * @param[in]  result     - The failure code from the system unit
61      *
62      * @return valid pointer to error to log, otherwise nullptr
63      */
64     const std::string* processError(const std::string& unit,
65                                     const std::string& result);
66 
67   private:
68     /** @brief Call phosphor-logging to create error
69      *
70      * @param[in]  error      - The error to log
71      * @param[in]  result     - The failure code from the systemd unit
72      */
73     void logError(const std::string& error, const std::string& result);
74 
75     /** @brief Check if systemd state change is one to monitor
76      *
77      * Instance specific interface to handle the detected systemd state
78      * change
79      *
80      * @param[in]  msg       - Data associated with subscribed signal
81      *
82      */
83     void systemdUnitChange(sdbusplus::message::message& msg);
84 
85     /** @brief Systemd targets to monitor and error logs to create */
86     const TargetErrorData& targetData;
87 
88     /** @brief Persistent sdbusplus DBus bus connection. */
89     sdbusplus::bus::bus& bus;
90 
91     /** @brief Used to subscribe to dbus systemd JobRemoved signals **/
92     sdbusplus::bus::match_t systemdJobRemovedSignal;
93 };
94 
95 } // namespace manager
96 } // namespace state
97 } // namespace phosphor
98