blob: 45474722ba5cac368d8700e647f56c5f309a3354 [file] [log] [blame]
Alexander Hansenaa9c24a2025-10-30 13:59:26 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 IBM Corporation
3
Matt Spinler7c33bff2017-06-02 12:29:41 -05004#include "monitor.hpp"
5
Matt Spinlercc6ee9c2018-09-19 13:23:13 -05006#include <phosphor-logging/log.hpp>
7
Matt Spinler7c33bff2017-06-02 12:29:41 -05008namespace phosphor
9{
10namespace unit
11{
12namespace failure
13{
14
15using namespace phosphor::logging;
16
Brad Bishopa098a372022-05-05 15:19:04 -040017constexpr auto failedState = "failed";
18constexpr auto startMethod = "StartUnit";
19constexpr auto stopMethod = "StopUnit";
Matt Spinler7c33bff2017-06-02 12:29:41 -050020
Brad Bishopa098a372022-05-05 15:19:04 -040021constexpr auto systemdService = "org.freedesktop.systemd1";
22constexpr auto systemdObjPath = "/org/freedesktop/systemd1";
23constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
24constexpr auto systemdPropertyInterface = "org.freedesktop.DBus.Properties";
25constexpr auto systemdUnitInterface = "org.freedesktop.systemd1.Unit";
Matt Spinler7c33bff2017-06-02 12:29:41 -050026
Matt Spinler7c33bff2017-06-02 12:29:41 -050027void Monitor::analyze()
28{
Brad Bishop1f623802022-05-31 18:22:10 -040029 if (inFailedState(getSourceUnitPath()))
Matt Spinler7c33bff2017-06-02 12:29:41 -050030 {
31 runTargetAction();
32 }
33}
34
Brad Bishop1f623802022-05-31 18:22:10 -040035bool Monitor::inFailedState(const std::string& path)
Matt Spinler7c33bff2017-06-02 12:29:41 -050036{
Patrick Williams2bb2d6b2020-05-13 17:59:02 -050037 std::variant<std::string> property;
Matt Spinler7c33bff2017-06-02 12:29:41 -050038
Brad Bishopa098a372022-05-05 15:19:04 -040039 auto method = bus.new_method_call(systemdService, path.c_str(),
40 systemdPropertyInterface, "Get");
Matt Spinler7c33bff2017-06-02 12:29:41 -050041
Brad Bishopa098a372022-05-05 15:19:04 -040042 method.append(systemdUnitInterface, "ActiveState");
Matt Spinler7c33bff2017-06-02 12:29:41 -050043
Patrick Williams85e956a2025-11-04 23:00:31 -050044 try
45 {
46 auto reply = bus.call(method);
47
48 reply.read(property);
49 }
50 catch (const sdbusplus::exception_t&)
Matt Spinler7c33bff2017-06-02 12:29:41 -050051 {
52 log<level::ERR>("Failed reading ActiveState DBus property",
Ed Tanous167e2372018-05-07 11:59:10 -070053 entry("UNIT=%s", source.c_str()));
Patrick Williams85e956a2025-11-04 23:00:31 -050054 throw;
Matt Spinler7c33bff2017-06-02 12:29:41 -050055 }
56
Patrick Williamsb05bc122020-05-13 12:21:00 -050057 auto value = std::get<std::string>(property);
Brad Bishopa098a372022-05-05 15:19:04 -040058 return (value == failedState);
Matt Spinler7c33bff2017-06-02 12:29:41 -050059}
60
Matt Spinler7c33bff2017-06-02 12:29:41 -050061std::string Monitor::getSourceUnitPath()
62{
63 sdbusplus::message::object_path path;
64
Brad Bishopa098a372022-05-05 15:19:04 -040065 auto method = bus.new_method_call(systemdService, systemdObjPath,
66 systemdInterface, "GetUnit");
Matt Spinler7c33bff2017-06-02 12:29:41 -050067 method.append(source);
Matt Spinler7c33bff2017-06-02 12:29:41 -050068
Patrick Williams85e956a2025-11-04 23:00:31 -050069 try
70 {
71 auto reply = bus.call(method);
72 reply.read(path);
73 }
74 catch (const sdbusplus::exception_t&)
Matt Spinler7c33bff2017-06-02 12:29:41 -050075 {
76 log<level::ERR>("Failed GetUnit DBus method call",
Ed Tanous167e2372018-05-07 11:59:10 -070077 entry("UNIT=%s", source.c_str()));
Patrick Williams85e956a2025-11-04 23:00:31 -050078 throw;
Matt Spinler7c33bff2017-06-02 12:29:41 -050079 }
80
Matt Spinler7c33bff2017-06-02 12:29:41 -050081 return static_cast<std::string>(path);
82}
83
Matt Spinler7c33bff2017-06-02 12:29:41 -050084void Monitor::runTargetAction()
85{
Ed Tanous167e2372018-05-07 11:59:10 -070086 // Start or stop the target unit
Patrick Williams9052ebd2024-08-16 15:22:16 -040087 const auto* methodCall =
88 (action == Action::start) ? startMethod : stopMethod;
Matt Spinler7c33bff2017-06-02 12:29:41 -050089
90 log<level::INFO>("The source unit is in failed state, "
91 "running target action",
92 entry("SOURCE=%s", source.c_str()),
93 entry("TARGET=%s", target.c_str()),
94 entry("ACTION=%s", methodCall));
95
Brad Bishopa098a372022-05-05 15:19:04 -040096 auto method = this->bus.new_method_call(systemdService, systemdObjPath,
97 systemdInterface, methodCall);
Matt Spinler7c33bff2017-06-02 12:29:41 -050098 method.append(target);
99 method.append("replace");
100
Patrick Williams85e956a2025-11-04 23:00:31 -0500101 try
102 {
103 auto reply = bus.call(method);
104 }
105 catch (const sdbusplus::exception_t&)
Matt Spinler7c33bff2017-06-02 12:29:41 -0500106 {
107 log<level::ERR>("Failed to run action on the target unit",
Ed Tanous167e2372018-05-07 11:59:10 -0700108 entry("UNIT=%s", target.c_str()));
Patrick Williams85e956a2025-11-04 23:00:31 -0500109 throw;
Matt Spinler7c33bff2017-06-02 12:29:41 -0500110 }
111}
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500112} // namespace failure
113} // namespace unit
114} // namespace phosphor