blob: 9a0722a88cfbb9c33c25e15d93ce95b29bf27768 [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 */
16#include <phosphor-logging/log.hpp>
17#include "monitor.hpp"
18
19namespace phosphor
20{
21namespace unit
22{
23namespace failure
24{
25
26using namespace phosphor::logging;
27
28constexpr auto FAILED_STATE = "failed";
29constexpr auto START_METHOD = "StartUnit";
30constexpr auto STOP_METHOD = "StopUnit";
31
Ed Tanous167e2372018-05-07 11:59:10 -070032constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
33constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Matt Spinler7c33bff2017-06-02 12:29:41 -050034constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
35constexpr auto SYSTEMD_PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
36constexpr auto SYSTEMD_UNIT_INTERFACE = "org.freedesktop.systemd1.Unit";
37
Matt Spinler7c33bff2017-06-02 12:29:41 -050038void Monitor::analyze()
39{
40 if (inFailedState(std::move(getSourceUnitPath())))
41 {
42 runTargetAction();
43 }
44}
45
Matt Spinler7c33bff2017-06-02 12:29:41 -050046bool Monitor::inFailedState(const std::string&& path)
47{
48 sdbusplus::message::variant<std::string> property;
49
Ed Tanous167e2372018-05-07 11:59:10 -070050 auto method = bus.new_method_call(SYSTEMD_SERVICE, path.c_str(),
51 SYSTEMD_PROPERTY_INTERFACE, "Get");
Matt Spinler7c33bff2017-06-02 12:29:41 -050052
53 method.append(SYSTEMD_UNIT_INTERFACE, "ActiveState");
54
55 auto reply = bus.call(method);
56 if (reply.is_method_error())
57 {
58 log<level::ERR>("Failed reading ActiveState DBus property",
Ed Tanous167e2372018-05-07 11:59:10 -070059 entry("UNIT=%s", source.c_str()));
Matt Spinler7c33bff2017-06-02 12:29:41 -050060 // TODO openbmc/openbmc#851 - Once available, throw returned error
61 throw std::runtime_error("Failed reading ActiveState DBus property");
62 }
63
64 reply.read(property);
65
66 auto value = sdbusplus::message::variant_ns::get<std::string>(property);
67 return (value == FAILED_STATE);
68}
69
Matt Spinler7c33bff2017-06-02 12:29:41 -050070std::string Monitor::getSourceUnitPath()
71{
72 sdbusplus::message::object_path path;
73
Ed Tanous167e2372018-05-07 11:59:10 -070074 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
75 SYSTEMD_INTERFACE, "GetUnit");
Matt Spinler7c33bff2017-06-02 12:29:41 -050076 method.append(source);
77 auto reply = bus.call(method);
78
79 if (reply.is_method_error())
80 {
81 log<level::ERR>("Failed GetUnit DBus method call",
Ed Tanous167e2372018-05-07 11:59:10 -070082 entry("UNIT=%s", source.c_str()));
Matt Spinler7c33bff2017-06-02 12:29:41 -050083 // TODO openbmc/openbmc#851 - Once available, throw returned error
84 throw std::runtime_error("Failed GetUnit DBus method call");
85 }
86
87 reply.read(path);
88
89 return static_cast<std::string>(path);
90}
91
Matt Spinler7c33bff2017-06-02 12:29:41 -050092void Monitor::runTargetAction()
93{
Ed Tanous167e2372018-05-07 11:59:10 -070094 // Start or stop the target unit
95 auto methodCall = (action == Action::start) ? START_METHOD : STOP_METHOD;
Matt Spinler7c33bff2017-06-02 12:29:41 -050096
97 log<level::INFO>("The source unit is in failed state, "
98 "running target action",
99 entry("SOURCE=%s", source.c_str()),
100 entry("TARGET=%s", target.c_str()),
101 entry("ACTION=%s", methodCall));
102
Ed Tanous167e2372018-05-07 11:59:10 -0700103 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
104 SYSTEMD_INTERFACE, methodCall);
Matt Spinler7c33bff2017-06-02 12:29:41 -0500105 method.append(target);
106 method.append("replace");
107
108 auto reply = bus.call(method);
109
110 if (reply.is_method_error())
111 {
112 log<level::ERR>("Failed to run action on the target unit",
Ed Tanous167e2372018-05-07 11:59:10 -0700113 entry("UNIT=%s", target.c_str()));
Matt Spinler7c33bff2017-06-02 12:29:41 -0500114 // TODO openbmc/openbmc#851 - Once available, throw returned error
115 throw std::runtime_error("Failed to run action on the target unit");
116 }
117}
Matt Spinler7c33bff2017-06-02 12:29:41 -0500118}
119}
120}