| 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 | |
| Patrick Williams | 85e956a | 2025-11-04 23:00:31 -0500 | [diff] [blame^] | 44 | try |
| 45 | { |
| 46 | auto reply = bus.call(method); |
| 47 | |
| 48 | reply.read(property); |
| 49 | } |
| 50 | catch (const sdbusplus::exception_t&) |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 51 | { |
| 52 | log<level::ERR>("Failed reading ActiveState DBus property", |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 53 | entry("UNIT=%s", source.c_str())); |
| Patrick Williams | 85e956a | 2025-11-04 23:00:31 -0500 | [diff] [blame^] | 54 | throw; |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 57 | auto value = std::get<std::string>(property); |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 58 | return (value == failedState); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 61 | std::string Monitor::getSourceUnitPath() |
| 62 | { |
| 63 | sdbusplus::message::object_path path; |
| 64 | |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 65 | auto method = bus.new_method_call(systemdService, systemdObjPath, |
| 66 | systemdInterface, "GetUnit"); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 67 | method.append(source); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 68 | |
| Patrick Williams | 85e956a | 2025-11-04 23:00:31 -0500 | [diff] [blame^] | 69 | try |
| 70 | { |
| 71 | auto reply = bus.call(method); |
| 72 | reply.read(path); |
| 73 | } |
| 74 | catch (const sdbusplus::exception_t&) |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 75 | { |
| 76 | log<level::ERR>("Failed GetUnit DBus method call", |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 77 | entry("UNIT=%s", source.c_str())); |
| Patrick Williams | 85e956a | 2025-11-04 23:00:31 -0500 | [diff] [blame^] | 78 | throw; |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 81 | return static_cast<std::string>(path); |
| 82 | } |
| 83 | |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 84 | void Monitor::runTargetAction() |
| 85 | { |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 86 | // Start or stop the target unit |
| Patrick Williams | 9052ebd | 2024-08-16 15:22:16 -0400 | [diff] [blame] | 87 | const auto* methodCall = |
| 88 | (action == Action::start) ? startMethod : stopMethod; |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 89 | |
| 90 | log<level::INFO>("The source unit is in failed state, " |
| 91 | "running target action", |
| 92 | entry("SOURCE=%s", source.c_str()), |
| 93 | entry("TARGET=%s", target.c_str()), |
| 94 | entry("ACTION=%s", methodCall)); |
| 95 | |
| Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 96 | auto method = this->bus.new_method_call(systemdService, systemdObjPath, |
| 97 | systemdInterface, methodCall); |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 98 | method.append(target); |
| 99 | method.append("replace"); |
| 100 | |
| Patrick Williams | 85e956a | 2025-11-04 23:00:31 -0500 | [diff] [blame^] | 101 | try |
| 102 | { |
| 103 | auto reply = bus.call(method); |
| 104 | } |
| 105 | catch (const sdbusplus::exception_t&) |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 106 | { |
| 107 | log<level::ERR>("Failed to run action on the target unit", |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 108 | entry("UNIT=%s", target.c_str())); |
| Patrick Williams | 85e956a | 2025-11-04 23:00:31 -0500 | [diff] [blame^] | 109 | throw; |
| Matt Spinler | 7c33bff | 2017-06-02 12:29:41 -0500 | [diff] [blame] | 110 | } |
| 111 | } |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 112 | } // namespace failure |
| 113 | } // namespace unit |
| 114 | } // namespace phosphor |