| Alexander Hansen | aa9c24a | 2025-10-30 13:59:26 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 IBM Corporation |
| 3 | |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 4 | #include "monitor.hpp" |
| 5 | |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/log.hpp> |
| 7 | |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace unit |
| 11 | { |
| 12 | namespace failure |
| 13 | { |
| 14 | |
| 15 | using namespace phosphor::logging; |
| 16 | |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 17 | constexpr auto failedState = "failed"; |
| 18 | constexpr auto startMethod = "StartUnit"; |
| 19 | constexpr auto stopMethod = "StopUnit"; |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 20 | |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 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"; |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 26 | |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 27 | void Monitor::analyze() |
| 28 | { |
| Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 29 | if (inFailedState(getSourceUnitPath())) |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 30 | { |
| 31 | runTargetAction(); |
| 32 | } |
| 33 | } |
| 34 | |
| Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 35 | bool Monitor::inFailedState(const std::string& path) |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 36 | { |
| Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 37 | std::variant<std::string> property; |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 38 | |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 39 | auto method = bus.new_method_call(systemdService, path.c_str(), |
| 40 | systemdPropertyInterface, "Get"); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 41 | |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 42 | method.append(systemdUnitInterface, "ActiveState"); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 43 | |
| 44 | auto reply = bus.call(method); |
| 45 | if (reply.is_method_error()) |
| 46 | { |
| 47 | log<level::ERR>("Failed reading ActiveState DBus property", |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 48 | entry("UNIT=%s", source.c_str())); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 49 | // TODO openbmc/openbmc#851 - Once available, throw returned error |
| 50 | throw std::runtime_error("Failed reading ActiveState DBus property"); |
| 51 | } |
| 52 | |
| 53 | reply.read(property); |
| 54 | |
| Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 55 | auto value = std::get<std::string>(property); |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 56 | return (value == failedState); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 59 | std::string Monitor::getSourceUnitPath() |
| 60 | { |
| 61 | sdbusplus::message::object_path path; |
| 62 | |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 63 | auto method = bus.new_method_call(systemdService, systemdObjPath, |
| 64 | systemdInterface, "GetUnit"); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 65 | method.append(source); |
| 66 | auto reply = bus.call(method); |
| 67 | |
| 68 | if (reply.is_method_error()) |
| 69 | { |
| 70 | log<level::ERR>("Failed GetUnit DBus method call", |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 71 | entry("UNIT=%s", source.c_str())); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 72 | // TODO openbmc/openbmc#851 - Once available, throw returned error |
| 73 | throw std::runtime_error("Failed GetUnit DBus method call"); |
| 74 | } |
| 75 | |
| 76 | reply.read(path); |
| 77 | |
| 78 | return static_cast<std::string>(path); |
| 79 | } |
| 80 | |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 81 | void Monitor::runTargetAction() |
| 82 | { |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 83 | // Start or stop the target unit |
| Patrick Williams | 9052ebd | 2024-08-16 15:22:16 -0400 | [diff] [blame] | 84 | const auto* methodCall = |
| 85 | (action == Action::start) ? startMethod : stopMethod; |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 86 | |
| 87 | log<level::INFO>("The source unit is in failed state, " |
| 88 | "running target action", |
| 89 | entry("SOURCE=%s", source.c_str()), |
| 90 | entry("TARGET=%s", target.c_str()), |
| 91 | entry("ACTION=%s", methodCall)); |
| 92 | |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 93 | auto method = this->bus.new_method_call(systemdService, systemdObjPath, |
| 94 | systemdInterface, methodCall); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 95 | method.append(target); |
| 96 | method.append("replace"); |
| 97 | |
| 98 | auto reply = bus.call(method); |
| 99 | |
| 100 | if (reply.is_method_error()) |
| 101 | { |
| 102 | log<level::ERR>("Failed to run action on the target unit", |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 103 | entry("UNIT=%s", target.c_str())); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 104 | // TODO openbmc/openbmc#851 - Once available, throw returned error |
| 105 | throw std::runtime_error("Failed to run action on the target unit"); |
| 106 | } |
| 107 | } |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 108 | } // namespace failure |
| 109 | } // namespace unit |
| 110 | } // namespace phosphor |