Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 1 | #include "watchdog.hpp" |
| 2 | |
William A. Kennington III | 7036c56 | 2018-10-03 23:36:16 -0700 | [diff] [blame] | 3 | #include <phosphor-logging/elog.hpp> |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 7036c56 | 2018-10-03 23:36:16 -0700 | [diff] [blame] | 5 | #include <sdbusplus/exception.hpp> |
| 6 | #include <xyz/openbmc_project/Common/error.hpp> |
| 7 | |
Patrick Williams | 6ac6a34 | 2023-05-10 07:51:12 -0500 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <chrono> |
| 10 | #include <string_view> |
| 11 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace watchdog |
| 15 | { |
| 16 | using namespace std::chrono; |
| 17 | using namespace std::chrono_literals; |
| 18 | using namespace phosphor::logging; |
| 19 | |
William A. Kennington III | 7036c56 | 2018-10-03 23:36:16 -0700 | [diff] [blame] | 20 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 21 | |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 22 | // systemd service to kick start a target. |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 23 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 24 | constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; |
| 25 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 26 | |
William A. Kennington III | 1726df6 | 2018-04-23 10:28:28 -0700 | [diff] [blame] | 27 | void Watchdog::resetTimeRemaining(bool enableWatchdog) |
| 28 | { |
| 29 | timeRemaining(interval()); |
| 30 | if (enableWatchdog) |
| 31 | { |
| 32 | enabled(true); |
| 33 | } |
| 34 | } |
| 35 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 36 | // Enable or disable watchdog |
| 37 | bool Watchdog::enabled(bool value) |
| 38 | { |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 39 | if (!value) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 40 | { |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 41 | // Make sure we accurately reflect our enabled state to the |
| 42 | // tryFallbackOrDisable() call |
| 43 | WatchdogInherits::enabled(value); |
| 44 | |
| 45 | // Attempt to fallback or disable our timer if needed |
| 46 | tryFallbackOrDisable(); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 47 | |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 48 | return false; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 49 | } |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 50 | else if (!this->enabled()) |
| 51 | { |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 52 | auto interval_ms = this->interval(); |
| 53 | timer.restart(milliseconds(interval_ms)); |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 54 | log<level::INFO>("watchdog: enabled and started", |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 55 | entry("INTERVAL=%llu", interval_ms)); |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 58 | return WatchdogInherits::enabled(value); |
| 59 | } |
| 60 | |
| 61 | // Get the remaining time before timer expires. |
| 62 | // If the timer is disabled, returns 0 |
| 63 | uint64_t Watchdog::timeRemaining() const |
| 64 | { |
William A. Kennington III | 1c9515f | 2018-02-27 18:54:36 -0800 | [diff] [blame] | 65 | // timer may have already expired and disabled |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 66 | if (!timerEnabled()) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 67 | { |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 68 | return 0; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 69 | } |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 70 | |
| 71 | return duration_cast<milliseconds>(timer.getRemaining()).count(); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Reset the timer to a new expiration value |
| 75 | uint64_t Watchdog::timeRemaining(uint64_t value) |
| 76 | { |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 77 | if (!timerEnabled()) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 78 | { |
William A. Kennington III | 5e3f877 | 2018-02-27 18:59:58 -0800 | [diff] [blame] | 79 | // We don't need to update the timer because it is off |
| 80 | return 0; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 81 | } |
William A. Kennington III | 5e3f877 | 2018-02-27 18:59:58 -0800 | [diff] [blame] | 82 | |
Kun Yi | ef82470 | 2019-12-04 18:58:20 -0800 | [diff] [blame] | 83 | if (this->enabled()) |
| 84 | { |
| 85 | // Update interval to minInterval if applicable |
| 86 | value = std::max(value, minInterval); |
| 87 | } |
| 88 | else |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 89 | { |
| 90 | // Having a timer but not displaying an enabled value means we |
| 91 | // are inside of the fallback |
| 92 | value = fallback->interval; |
| 93 | } |
| 94 | |
William A. Kennington III | 5e3f877 | 2018-02-27 18:59:58 -0800 | [diff] [blame] | 95 | // Update new expiration |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 96 | timer.setRemaining(milliseconds(value)); |
William A. Kennington III | 5e3f877 | 2018-02-27 18:59:58 -0800 | [diff] [blame] | 97 | |
| 98 | // Update Base class data. |
| 99 | return WatchdogInherits::timeRemaining(value); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 100 | } |
| 101 | |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 102 | // Set value of Interval |
| 103 | uint64_t Watchdog::interval(uint64_t value) |
| 104 | { |
| 105 | return WatchdogInherits::interval(std::max(value, minInterval)); |
| 106 | } |
| 107 | |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 108 | // Optional callback function on timer expiration |
| 109 | void Watchdog::timeOutHandler() |
| 110 | { |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 111 | Action action = expireAction(); |
| 112 | if (!this->enabled()) |
| 113 | { |
| 114 | action = fallback->action; |
| 115 | } |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 116 | |
William A. Kennington III | 47a9ec4 | 2019-04-18 15:42:16 -0700 | [diff] [blame] | 117 | expiredTimerUse(currentTimerUse()); |
Yong Li | 50ae4b1 | 2019-03-20 17:08:24 +0800 | [diff] [blame] | 118 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 119 | auto target = actionTargetMap.find(action); |
| 120 | if (target == actionTargetMap.end()) |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 121 | { |
| 122 | log<level::INFO>("watchdog: Timed out with no target", |
William A. Kennington III | 47a9ec4 | 2019-04-18 15:42:16 -0700 | [diff] [blame] | 123 | entry("ACTION=%s", convertForMessage(action).c_str()), |
| 124 | entry("TIMER_USE=%s", |
| 125 | convertForMessage(expiredTimerUse()).c_str())); |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 126 | } |
| 127 | else |
| 128 | { |
William A. Kennington III | 47a9ec4 | 2019-04-18 15:42:16 -0700 | [diff] [blame] | 129 | log<level::INFO>( |
| 130 | "watchdog: Timed out", |
| 131 | entry("ACTION=%s", convertForMessage(action).c_str()), |
| 132 | entry("TIMER_USE=%s", convertForMessage(expiredTimerUse()).c_str()), |
| 133 | entry("TARGET=%s", target->second.c_str())); |
William A. Kennington III | 7036c56 | 2018-10-03 23:36:16 -0700 | [diff] [blame] | 134 | |
| 135 | try |
| 136 | { |
| 137 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 138 | SYSTEMD_INTERFACE, "StartUnit"); |
| 139 | method.append(target->second); |
| 140 | method.append("replace"); |
| 141 | |
| 142 | bus.call_noreply(method); |
| 143 | } |
Patrick Williams | 73bd527 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 144 | catch (const sdbusplus::exception_t& e) |
William A. Kennington III | 7036c56 | 2018-10-03 23:36:16 -0700 | [diff] [blame] | 145 | { |
| 146 | log<level::ERR>("watchdog: Failed to start unit", |
| 147 | entry("TARGET=%s", target->second.c_str()), |
| 148 | entry("ERROR=%s", e.what())); |
| 149 | commit<InternalFailure>(); |
| 150 | } |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 151 | } |
selvaganapathi | fcc0039 | 2022-11-14 23:09:44 +0530 | [diff] [blame] | 152 | try |
| 153 | { |
| 154 | auto signal = bus.new_signal(objPath.data(), |
| 155 | "xyz.openbmc_project.Watchdog", "Timeout"); |
| 156 | signal.append(convertForMessage(action).c_str()); |
| 157 | signal.signal_send(); |
| 158 | } |
| 159 | catch (const sdbusplus::exception_t& e) |
| 160 | { |
| 161 | log<level::ERR>("watchdog: failed to send timeout signal", |
| 162 | entry("ERROR=%s", e.what())); |
| 163 | } |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 164 | |
William A. Kennington III | d1b1e79 | 2022-11-21 18:29:11 -0800 | [diff] [blame] | 165 | if (exitAfterTimeout) |
| 166 | { |
| 167 | timer.get_event().exit(0); |
| 168 | } |
| 169 | |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 170 | tryFallbackOrDisable(); |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 171 | } |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 172 | |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 173 | void Watchdog::tryFallbackOrDisable() |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 174 | { |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 175 | // We only re-arm the watchdog if we were already enabled and have |
| 176 | // a possible fallback |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 177 | if (fallback && (fallback->always || this->enabled())) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 178 | { |
| 179 | auto interval_ms = fallback->interval; |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 180 | timer.restart(milliseconds(interval_ms)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 181 | log<level::INFO>("watchdog: falling back", |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 182 | entry("INTERVAL=%llu", interval_ms)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 183 | } |
| 184 | else if (timerEnabled()) |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 185 | { |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 186 | timer.setEnabled(false); |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 187 | |
| 188 | log<level::INFO>("watchdog: disabled"); |
| 189 | } |
| 190 | |
| 191 | // Make sure we accurately reflect our enabled state to the |
| 192 | // dbus interface. |
| 193 | WatchdogInherits::enabled(false); |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 194 | } |
| 195 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 196 | } // namespace watchdog |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 197 | } // namespace phosphor |