1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2017 IBM Corporation
3
4 #include "monitor.hpp"
5
6 #include <phosphor-logging/log.hpp>
7
8 namespace phosphor
9 {
10 namespace unit
11 {
12 namespace failure
13 {
14
15 using namespace phosphor::logging;
16
17 constexpr auto failedState = "failed";
18 constexpr auto startMethod = "StartUnit";
19 constexpr auto stopMethod = "StopUnit";
20
21 constexpr auto systemdService = "org.freedesktop.systemd1";
22 constexpr auto systemdObjPath = "/org/freedesktop/systemd1";
23 constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
24 constexpr auto systemdPropertyInterface = "org.freedesktop.DBus.Properties";
25 constexpr auto systemdUnitInterface = "org.freedesktop.systemd1.Unit";
26
analyze()27 void Monitor::analyze()
28 {
29 if (inFailedState(getSourceUnitPath()))
30 {
31 runTargetAction();
32 }
33 }
34
inFailedState(const std::string & path)35 bool Monitor::inFailedState(const std::string& path)
36 {
37 std::variant<std::string> property;
38
39 auto method = bus.new_method_call(systemdService, path.c_str(),
40 systemdPropertyInterface, "Get");
41
42 method.append(systemdUnitInterface, "ActiveState");
43
44 try
45 {
46 auto reply = bus.call(method);
47
48 reply.read(property);
49 }
50 catch (const sdbusplus::exception_t&)
51 {
52 log<level::ERR>("Failed reading ActiveState DBus property",
53 entry("UNIT=%s", source.c_str()));
54 throw;
55 }
56
57 auto value = std::get<std::string>(property);
58 return (value == failedState);
59 }
60
getSourceUnitPath()61 std::string Monitor::getSourceUnitPath()
62 {
63 sdbusplus::message::object_path path;
64
65 auto method = bus.new_method_call(systemdService, systemdObjPath,
66 systemdInterface, "GetUnit");
67 method.append(source);
68
69 try
70 {
71 auto reply = bus.call(method);
72 reply.read(path);
73 }
74 catch (const sdbusplus::exception_t&)
75 {
76 log<level::ERR>("Failed GetUnit DBus method call",
77 entry("UNIT=%s", source.c_str()));
78 throw;
79 }
80
81 return static_cast<std::string>(path);
82 }
83
runTargetAction()84 void Monitor::runTargetAction()
85 {
86 // Start or stop the target unit
87 const auto* methodCall =
88 (action == Action::start) ? startMethod : stopMethod;
89
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
96 auto method = this->bus.new_method_call(systemdService, systemdObjPath,
97 systemdInterface, methodCall);
98 method.append(target);
99 method.append("replace");
100
101 try
102 {
103 auto reply = bus.call(method);
104 }
105 catch (const sdbusplus::exception_t&)
106 {
107 log<level::ERR>("Failed to run action on the target unit",
108 entry("UNIT=%s", target.c_str()));
109 throw;
110 }
111 }
112 } // namespace failure
113 } // namespace unit
114 } // namespace phosphor
115