Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 1 | /** |
| 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 Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 16 | #include "monitor.hpp" |
| 17 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 18 | #include <phosphor-logging/log.hpp> |
| 19 | |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 20 | namespace phosphor |
| 21 | { |
| 22 | namespace unit |
| 23 | { |
| 24 | namespace failure |
| 25 | { |
| 26 | |
| 27 | using namespace phosphor::logging; |
| 28 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 29 | constexpr auto failedState = "failed"; |
| 30 | constexpr auto startMethod = "StartUnit"; |
| 31 | constexpr auto stopMethod = "StopUnit"; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 32 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 33 | constexpr auto systemdService = "org.freedesktop.systemd1"; |
| 34 | constexpr auto systemdObjPath = "/org/freedesktop/systemd1"; |
| 35 | constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager"; |
| 36 | constexpr auto systemdPropertyInterface = "org.freedesktop.DBus.Properties"; |
| 37 | constexpr auto systemdUnitInterface = "org.freedesktop.systemd1.Unit"; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 38 | |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 39 | void Monitor::analyze() |
| 40 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame^] | 41 | if (inFailedState(getSourceUnitPath())) |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 42 | { |
| 43 | runTargetAction(); |
| 44 | } |
| 45 | } |
| 46 | |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame^] | 47 | bool Monitor::inFailedState(const std::string& path) |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 48 | { |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 49 | std::variant<std::string> property; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 50 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 51 | auto method = bus.new_method_call(systemdService, path.c_str(), |
| 52 | systemdPropertyInterface, "Get"); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 53 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 54 | method.append(systemdUnitInterface, "ActiveState"); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 55 | |
| 56 | auto reply = bus.call(method); |
| 57 | if (reply.is_method_error()) |
| 58 | { |
| 59 | log<level::ERR>("Failed reading ActiveState DBus property", |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 60 | entry("UNIT=%s", source.c_str())); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 61 | // 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 Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 67 | auto value = std::get<std::string>(property); |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 68 | return (value == failedState); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 71 | std::string Monitor::getSourceUnitPath() |
| 72 | { |
| 73 | sdbusplus::message::object_path path; |
| 74 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 75 | auto method = bus.new_method_call(systemdService, systemdObjPath, |
| 76 | systemdInterface, "GetUnit"); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 77 | 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 Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 83 | entry("UNIT=%s", source.c_str())); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 84 | // 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 Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 93 | void Monitor::runTargetAction() |
| 94 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 95 | // Start or stop the target unit |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame^] | 96 | const auto* methodCall = |
| 97 | (action == Action::start) ? startMethod : stopMethod; |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 98 | |
| 99 | log<level::INFO>("The source unit is in failed state, " |
| 100 | "running target action", |
| 101 | entry("SOURCE=%s", source.c_str()), |
| 102 | entry("TARGET=%s", target.c_str()), |
| 103 | entry("ACTION=%s", methodCall)); |
| 104 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 105 | auto method = this->bus.new_method_call(systemdService, systemdObjPath, |
| 106 | systemdInterface, methodCall); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 107 | method.append(target); |
| 108 | method.append("replace"); |
| 109 | |
| 110 | auto reply = bus.call(method); |
| 111 | |
| 112 | if (reply.is_method_error()) |
| 113 | { |
| 114 | log<level::ERR>("Failed to run action on the target unit", |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 115 | entry("UNIT=%s", target.c_str())); |
Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 116 | // TODO openbmc/openbmc#851 - Once available, throw returned error |
| 117 | throw std::runtime_error("Failed to run action on the target unit"); |
| 118 | } |
| 119 | } |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 120 | } // namespace failure |
| 121 | } // namespace unit |
| 122 | } // namespace phosphor |