blob: 6f93841d221c07b6ecaa14894edaaad21e1eec03 [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
44 auto reply = bus.call(method);
45 if (reply.is_method_error())
46 {
47 log<level::ERR>("Failed reading ActiveState DBus property",
Ed Tanous167e2372018-05-07 11:59:10 -070048 entry("UNIT=%s", source.c_str()));
Matt Spinler7c33bff2017-06-02 12:29:41 -050049 // TODO openbmc/openbmc#851 - Once available, throw returned error
50 throw std::runtime_error("Failed reading ActiveState DBus property");
51 }
52
53 reply.read(property);
54
Patrick Williamsb05bc122020-05-13 12:21:00 -050055 auto value = std::get<std::string>(property);
Brad Bishopa098a372022-05-05 15:19:04 -040056 return (value == failedState);
Matt Spinler7c33bff2017-06-02 12:29:41 -050057}
58
Matt Spinler7c33bff2017-06-02 12:29:41 -050059std::string Monitor::getSourceUnitPath()
60{
61 sdbusplus::message::object_path path;
62
Brad Bishopa098a372022-05-05 15:19:04 -040063 auto method = bus.new_method_call(systemdService, systemdObjPath,
64 systemdInterface, "GetUnit");
Matt Spinler7c33bff2017-06-02 12:29:41 -050065 method.append(source);
66 auto reply = bus.call(method);
67
68 if (reply.is_method_error())
69 {
70 log<level::ERR>("Failed GetUnit DBus method call",
Ed Tanous167e2372018-05-07 11:59:10 -070071 entry("UNIT=%s", source.c_str()));
Matt Spinler7c33bff2017-06-02 12:29:41 -050072 // TODO openbmc/openbmc#851 - Once available, throw returned error
73 throw std::runtime_error("Failed GetUnit DBus method call");
74 }
75
76 reply.read(path);
77
78 return static_cast<std::string>(path);
79}
80
Matt Spinler7c33bff2017-06-02 12:29:41 -050081void Monitor::runTargetAction()
82{
Ed Tanous167e2372018-05-07 11:59:10 -070083 // Start or stop the target unit
Patrick Williams9052ebd2024-08-16 15:22:16 -040084 const auto* methodCall =
85 (action == Action::start) ? startMethod : stopMethod;
Matt Spinler7c33bff2017-06-02 12:29:41 -050086
87 log<level::INFO>("The source unit is in failed state, "
88 "running target action",
89 entry("SOURCE=%s", source.c_str()),
90 entry("TARGET=%s", target.c_str()),
91 entry("ACTION=%s", methodCall));
92
Brad Bishopa098a372022-05-05 15:19:04 -040093 auto method = this->bus.new_method_call(systemdService, systemdObjPath,
94 systemdInterface, methodCall);
Matt Spinler7c33bff2017-06-02 12:29:41 -050095 method.append(target);
96 method.append("replace");
97
98 auto reply = bus.call(method);
99
100 if (reply.is_method_error())
101 {
102 log<level::ERR>("Failed to run action on the target unit",
Ed Tanous167e2372018-05-07 11:59:10 -0700103 entry("UNIT=%s", target.c_str()));
Matt Spinler7c33bff2017-06-02 12:29:41 -0500104 // TODO openbmc/openbmc#851 - Once available, throw returned error
105 throw std::runtime_error("Failed to run action on the target unit");
106 }
107}
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500108} // namespace failure
109} // namespace unit
110} // namespace phosphor