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