blob: a273a795a8f76c72db91d9bf5616cdc97d0d665d [file] [log] [blame]
Andrew Geissler234a3172019-08-09 14:30:02 -05001#pragma once
2
Andrew Geisslerf3870c62022-02-10 16:15:28 -06003#include "systemd_service_parser.hpp"
Andrew Geisslere426b582020-05-28 12:40:55 -05004#include "systemd_target_parser.hpp"
5
Andrew Geissler234a3172019-08-09 14:30:02 -05006#include <sdbusplus/bus.hpp>
7#include <sdbusplus/bus/match.hpp>
Andrew Geissler234a3172019-08-09 14:30:02 -05008
9extern bool gVerbose;
10
11namespace phosphor
12{
13namespace state
14{
15namespace manager
16{
17/** @class SystemdTargetLogging
18 * @brief Object to monitor input systemd targets and create corresponding
19 * input errors for on failures
20 */
21class SystemdTargetLogging
22{
23 public:
24 SystemdTargetLogging() = delete;
25 SystemdTargetLogging(const SystemdTargetLogging&) = delete;
26 SystemdTargetLogging& operator=(const SystemdTargetLogging&) = delete;
27 SystemdTargetLogging(SystemdTargetLogging&&) = delete;
28 SystemdTargetLogging& operator=(SystemdTargetLogging&&) = delete;
29 virtual ~SystemdTargetLogging() = default;
30
31 SystemdTargetLogging(const TargetErrorData& targetData,
Andrew Geisslerf3870c62022-02-10 16:15:28 -060032 const ServiceMonitorData& serviceData,
Patrick Williamsf053e6f2022-07-22 19:26:54 -050033 sdbusplus::bus_t& bus) :
Patrick Williams1b2c3c02024-08-16 15:20:29 -040034 targetData(targetData), serviceData(serviceData), bus(bus),
Andrew Geisslerb558ae72019-11-07 10:40:06 -060035 systemdJobRemovedSignal(
Andrew Geissler234a3172019-08-09 14:30:02 -050036 bus,
37 sdbusplus::bus::match::rules::type::signal() +
38 sdbusplus::bus::match::rules::member("JobRemoved") +
39 sdbusplus::bus::match::rules::path(
40 "/org/freedesktop/systemd1") +
41 sdbusplus::bus::match::rules::interface(
42 "org.freedesktop.systemd1.Manager"),
William A. Kennington IIIbd28f022022-11-22 17:11:49 -080043 [this](sdbusplus::message_t& m) { systemdUnitChange(m); }),
Andrew Geissler38605ee2019-11-11 16:01:56 -060044 systemdNameOwnedChangedSignal(
45 bus, sdbusplus::bus::match::rules::nameOwnerChanged(),
William A. Kennington IIIbd28f022022-11-22 17:11:49 -080046 [this](sdbusplus::message_t& m) { processNameChangeSignal(m); })
Andrew Geisslere426b582020-05-28 12:40:55 -050047 {}
Andrew Geissler234a3172019-08-09 14:30:02 -050048
49 /**
50 * @brief subscribe to the systemd signals
51 *
52 * This object needs to monitor systemd target changes so it can create
53 * the required error logs on failures
54 *
55 **/
56 void subscribeToSystemdSignals();
57
58 /** @brief Process the target fail and return error to log
59 *
60 * @note This is public for unit testing purposes
61 *
62 * @param[in] unit - The systemd unit that failed
63 * @param[in] result - The failure code from the system unit
64 *
65 * @return valid pointer to error to log, otherwise nullptr
66 */
Pavithra Barithayad7a15cb2024-06-21 11:24:46 -050067 std::string processError(const std::string& unit,
68 const std::string& result);
Andrew Geissler234a3172019-08-09 14:30:02 -050069
70 private:
Andrew Geissler73d2ac92022-02-17 16:55:50 -060071 /** @brief Start BMC Quiesce Target to indicate critical service failure */
72 void startBmcQuiesceTarget();
73
Andrew Geissler234a3172019-08-09 14:30:02 -050074 /** @brief Call phosphor-logging to create error
75 *
76 * @param[in] error - The error to log
77 * @param[in] result - The failure code from the systemd unit
Andrew Geisslere6841032022-02-11 10:31:50 -060078 * @param[in] unit - The name of the failed unit
Andrew Geissler234a3172019-08-09 14:30:02 -050079 */
Andrew Geisslere6841032022-02-11 10:31:50 -060080 void logError(const std::string& error, const std::string& result,
81 const std::string& unit);
Andrew Geissler234a3172019-08-09 14:30:02 -050082
83 /** @brief Check if systemd state change is one to monitor
84 *
85 * Instance specific interface to handle the detected systemd state
86 * change
87 *
88 * @param[in] msg - Data associated with subscribed signal
89 *
90 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -050091 void systemdUnitChange(sdbusplus::message_t& msg);
Andrew Geissler234a3172019-08-09 14:30:02 -050092
Andrew Geissler38605ee2019-11-11 16:01:56 -060093 /** @brief Wait for systemd to show up on dbus
94 *
95 * Once systemd is on dbus, this application can subscribe to systemd
96 * signal changes
97 *
98 * @param[in] msg - Data associated with subscribed signal
99 *
100 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500101 void processNameChangeSignal(sdbusplus::message_t& msg);
Andrew Geissler38605ee2019-11-11 16:01:56 -0600102
Andrew Geissler234a3172019-08-09 14:30:02 -0500103 /** @brief Systemd targets to monitor and error logs to create */
104 const TargetErrorData& targetData;
105
Andrew Geisslerf3870c62022-02-10 16:15:28 -0600106 /** @brief Systemd targets to monitor and error logs to create */
107 const ServiceMonitorData& serviceData;
108
Andrew Geissler234a3172019-08-09 14:30:02 -0500109 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500110 sdbusplus::bus_t& bus;
Andrew Geissler234a3172019-08-09 14:30:02 -0500111
Andrew Geisslerb558ae72019-11-07 10:40:06 -0600112 /** @brief Used to subscribe to dbus systemd JobRemoved signals **/
113 sdbusplus::bus::match_t systemdJobRemovedSignal;
Andrew Geissler38605ee2019-11-11 16:01:56 -0600114
115 /** @brief Used to know when systemd has registered on dbus **/
116 sdbusplus::bus::match_t systemdNameOwnedChangedSignal;
Andrew Geissler234a3172019-08-09 14:30:02 -0500117};
118
119} // namespace manager
120} // namespace state
121} // namespace phosphor