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