blob: 6c46558804a085ce62e00e1c63528ac2f06caaf2 [file] [log] [blame]
Matt Spinler7c33bff2017-06-02 12:29:41 -05001/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinler7c33bff2017-06-02 12:29:41 -050016#include "monitor.hpp"
17
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050018#include <phosphor-logging/log.hpp>
19
Matt Spinler7c33bff2017-06-02 12:29:41 -050020namespace phosphor
21{
22namespace unit
23{
24namespace failure
25{
26
27using namespace phosphor::logging;
28
29constexpr auto FAILED_STATE = "failed";
30constexpr auto START_METHOD = "StartUnit";
31constexpr auto STOP_METHOD = "StopUnit";
32
Ed Tanous167e2372018-05-07 11:59:10 -070033constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
34constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Matt Spinler7c33bff2017-06-02 12:29:41 -050035constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
36constexpr auto SYSTEMD_PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
37constexpr auto SYSTEMD_UNIT_INTERFACE = "org.freedesktop.systemd1.Unit";
38
Matt Spinler7c33bff2017-06-02 12:29:41 -050039void Monitor::analyze()
40{
41 if (inFailedState(std::move(getSourceUnitPath())))
42 {
43 runTargetAction();
44 }
45}
46
Matt Spinler7c33bff2017-06-02 12:29:41 -050047bool Monitor::inFailedState(const std::string&& path)
48{
Patrick Williams2bb2d6b2020-05-13 17:59:02 -050049 std::variant<std::string> property;
Matt Spinler7c33bff2017-06-02 12:29:41 -050050
Ed Tanous167e2372018-05-07 11:59:10 -070051 auto method = bus.new_method_call(SYSTEMD_SERVICE, path.c_str(),
52 SYSTEMD_PROPERTY_INTERFACE, "Get");
Matt Spinler7c33bff2017-06-02 12:29:41 -050053
54 method.append(SYSTEMD_UNIT_INTERFACE, "ActiveState");
55
56 auto reply = bus.call(method);
57 if (reply.is_method_error())
58 {
59 log<level::ERR>("Failed reading ActiveState DBus property",
Ed Tanous167e2372018-05-07 11:59:10 -070060 entry("UNIT=%s", source.c_str()));
Matt Spinler7c33bff2017-06-02 12:29:41 -050061 // TODO openbmc/openbmc#851 - Once available, throw returned error
62 throw std::runtime_error("Failed reading ActiveState DBus property");
63 }
64
65 reply.read(property);
66
Patrick Williamsb05bc122020-05-13 12:21:00 -050067 auto value = std::get<std::string>(property);
Matt Spinler7c33bff2017-06-02 12:29:41 -050068 return (value == FAILED_STATE);
69}
70
Matt Spinler7c33bff2017-06-02 12:29:41 -050071std::string Monitor::getSourceUnitPath()
72{
73 sdbusplus::message::object_path path;
74
Ed Tanous167e2372018-05-07 11:59:10 -070075 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
76 SYSTEMD_INTERFACE, "GetUnit");
Matt Spinler7c33bff2017-06-02 12:29:41 -050077 method.append(source);
78 auto reply = bus.call(method);
79
80 if (reply.is_method_error())
81 {
82 log<level::ERR>("Failed GetUnit DBus method call",
Ed Tanous167e2372018-05-07 11:59:10 -070083 entry("UNIT=%s", source.c_str()));
Matt Spinler7c33bff2017-06-02 12:29:41 -050084 // TODO openbmc/openbmc#851 - Once available, throw returned error
85 throw std::runtime_error("Failed GetUnit DBus method call");
86 }
87
88 reply.read(path);
89
90 return static_cast<std::string>(path);
91}
92
Matt Spinler7c33bff2017-06-02 12:29:41 -050093void Monitor::runTargetAction()
94{
Ed Tanous167e2372018-05-07 11:59:10 -070095 // Start or stop the target unit
96 auto methodCall = (action == Action::start) ? START_METHOD : STOP_METHOD;
Matt Spinler7c33bff2017-06-02 12:29:41 -050097
98 log<level::INFO>("The source unit is in failed state, "
99 "running target action",
100 entry("SOURCE=%s", source.c_str()),
101 entry("TARGET=%s", target.c_str()),
102 entry("ACTION=%s", methodCall));
103
Ed Tanous167e2372018-05-07 11:59:10 -0700104 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
105 SYSTEMD_INTERFACE, methodCall);
Matt Spinler7c33bff2017-06-02 12:29:41 -0500106 method.append(target);
107 method.append("replace");
108
109 auto reply = bus.call(method);
110
111 if (reply.is_method_error())
112 {
113 log<level::ERR>("Failed to run action on the target unit",
Ed Tanous167e2372018-05-07 11:59:10 -0700114 entry("UNIT=%s", target.c_str()));
Matt Spinler7c33bff2017-06-02 12:29:41 -0500115 // TODO openbmc/openbmc#851 - Once available, throw returned error
116 throw std::runtime_error("Failed to run action on the target unit");
117 }
118}
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500119} // namespace failure
120} // namespace unit
121} // namespace phosphor