blob: 1936e2783e9024364a9fa31c1150d94c4bd87662 [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
32constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
33constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
34constexpr 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
38
39void Monitor::analyze()
40{
41 if (inFailedState(std::move(getSourceUnitPath())))
42 {
43 runTargetAction();
44 }
45}
46
47
48bool Monitor::inFailedState(const std::string&& path)
49{
50 sdbusplus::message::variant<std::string> property;
51
52 auto method = bus.new_method_call(SYSTEMD_SERVICE,
53 path.c_str(),
54 SYSTEMD_PROPERTY_INTERFACE,
55 "Get");
56
57 method.append(SYSTEMD_UNIT_INTERFACE, "ActiveState");
58
59 auto reply = bus.call(method);
60 if (reply.is_method_error())
61 {
62 log<level::ERR>("Failed reading ActiveState DBus property",
63 entry("UNIT=%s", source.c_str()));
64 // TODO openbmc/openbmc#851 - Once available, throw returned error
65 throw std::runtime_error("Failed reading ActiveState DBus property");
66 }
67
68 reply.read(property);
69
70 auto value = sdbusplus::message::variant_ns::get<std::string>(property);
71 return (value == FAILED_STATE);
72}
73
74
75std::string Monitor::getSourceUnitPath()
76{
77 sdbusplus::message::object_path path;
78
79 auto method = bus.new_method_call(SYSTEMD_SERVICE,
80 SYSTEMD_OBJ_PATH,
81 SYSTEMD_INTERFACE,
82 "GetUnit");
83 method.append(source);
84 auto reply = bus.call(method);
85
86 if (reply.is_method_error())
87 {
88 log<level::ERR>("Failed GetUnit DBus method call",
89 entry("UNIT=%s", source.c_str()));
90 // TODO openbmc/openbmc#851 - Once available, throw returned error
91 throw std::runtime_error("Failed GetUnit DBus method call");
92 }
93
94 reply.read(path);
95
96 return static_cast<std::string>(path);
97}
98
99
100void Monitor::runTargetAction()
101{
102 //Start or stop the target unit
103 auto methodCall = (action == Action::start) ?
104 START_METHOD : STOP_METHOD;
105
106 log<level::INFO>("The source unit is in failed state, "
107 "running target action",
108 entry("SOURCE=%s", source.c_str()),
109 entry("TARGET=%s", target.c_str()),
110 entry("ACTION=%s", methodCall));
111
112 auto method = this->bus.new_method_call(SYSTEMD_SERVICE,
113 SYSTEMD_OBJ_PATH,
114 SYSTEMD_INTERFACE,
115 methodCall);
116 method.append(target);
117 method.append("replace");
118
119 auto reply = bus.call(method);
120
121 if (reply.is_method_error())
122 {
123 log<level::ERR>("Failed to run action on the target unit",
124 entry("UNIT=%s", target.c_str()));
125 // TODO openbmc/openbmc#851 - Once available, throw returned error
126 throw std::runtime_error("Failed to run action on the target unit");
127 }
128}
129
130}
131}
132}