blob: 05a95e929483a5af83d3c9225f0e8ec604386378 [file] [log] [blame]
Patrick Venture8f6c5152018-09-11 17:45:33 -07001#include "watchdog.hpp"
2
Ofer Yehiellic35135d2019-06-14 11:30:25 -07003#include <algorithm>
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +05304#include <chrono>
William A. Kennington III7036c562018-10-03 23:36:16 -07005#include <phosphor-logging/elog.hpp>
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +05306#include <phosphor-logging/log.hpp>
William A. Kennington III7036c562018-10-03 23:36:16 -07007#include <sdbusplus/exception.hpp>
Willy Tu9ef0d0f2021-07-06 22:36:23 -07008#include <string_view>
William A. Kennington III7036c562018-10-03 23:36:16 -07009#include <xyz/openbmc_project/Common/error.hpp>
10
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053011namespace phosphor
12{
13namespace watchdog
14{
15using namespace std::chrono;
16using namespace std::chrono_literals;
17using namespace phosphor::logging;
18
William A. Kennington III7036c562018-10-03 23:36:16 -070019using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
20
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053021// systemd service to kick start a target.
Patrick Venture8f6c5152018-09-11 17:45:33 -070022constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
23constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
24constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053025
William A. Kennington III1726df62018-04-23 10:28:28 -070026void Watchdog::resetTimeRemaining(bool enableWatchdog)
27{
28 timeRemaining(interval());
29 if (enableWatchdog)
30 {
31 enabled(true);
32 }
33}
34
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053035// Enable or disable watchdog
36bool Watchdog::enabled(bool value)
37{
William A. Kennington III825f4982018-02-27 19:10:56 -080038 if (!value)
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053039 {
William A. Kennington IIId1331082018-02-27 18:47:05 -080040 // 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 Subbannad7a3f132017-05-29 19:39:08 +053046
William A. Kennington III825f4982018-02-27 19:10:56 -080047 return false;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053048 }
William A. Kennington III825f4982018-02-27 19:10:56 -080049 else if (!this->enabled())
50 {
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070051 auto interval_ms = this->interval();
52 timer.restart(milliseconds(interval_ms));
William A. Kennington III825f4982018-02-27 19:10:56 -080053 log<level::INFO>("watchdog: enabled and started",
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070054 entry("INTERVAL=%llu", interval_ms));
William A. Kennington III825f4982018-02-27 19:10:56 -080055 }
56
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053057 return WatchdogInherits::enabled(value);
58}
59
60// Get the remaining time before timer expires.
61// If the timer is disabled, returns 0
62uint64_t Watchdog::timeRemaining() const
63{
William A. Kennington III1c9515f2018-02-27 18:54:36 -080064 // timer may have already expired and disabled
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070065 if (!timerEnabled())
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053066 {
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070067 return 0;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053068 }
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070069
70 return duration_cast<milliseconds>(timer.getRemaining()).count();
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053071}
72
73// Reset the timer to a new expiration value
74uint64_t Watchdog::timeRemaining(uint64_t value)
75{
William A. Kennington III0650a3f2018-03-01 10:53:25 -080076 if (!timerEnabled())
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053077 {
William A. Kennington III5e3f8772018-02-27 18:59:58 -080078 // We don't need to update the timer because it is off
79 return 0;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053080 }
William A. Kennington III5e3f8772018-02-27 18:59:58 -080081
Kun Yief824702019-12-04 18:58:20 -080082 if (this->enabled())
83 {
84 // Update interval to minInterval if applicable
85 value = std::max(value, minInterval);
86 }
87 else
William A. Kennington IIId1331082018-02-27 18:47:05 -080088 {
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 III5e3f8772018-02-27 18:59:58 -080094 // Update new expiration
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070095 timer.setRemaining(milliseconds(value));
William A. Kennington III5e3f8772018-02-27 18:59:58 -080096
97 // Update Base class data.
98 return WatchdogInherits::timeRemaining(value);
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053099}
100
Ofer Yehiellic35135d2019-06-14 11:30:25 -0700101// Set value of Interval
102uint64_t Watchdog::interval(uint64_t value)
103{
104 return WatchdogInherits::interval(std::max(value, minInterval));
105}
106
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +0530107// Optional callback function on timer expiration
108void Watchdog::timeOutHandler()
109{
William A. Kennington IIId1331082018-02-27 18:47:05 -0800110 Action action = expireAction();
111 if (!this->enabled())
112 {
113 action = fallback->action;
114 }
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +0530115
William A. Kennington III47a9ec42019-04-18 15:42:16 -0700116 expiredTimerUse(currentTimerUse());
Yong Li50ae4b12019-03-20 17:08:24 +0800117
William A. Kennington III3bb2f402018-09-13 00:35:47 -0700118 auto target = actionTargetMap.find(action);
119 if (target == actionTargetMap.end())
William A. Kennington III1232a152018-02-02 15:57:34 -0800120 {
121 log<level::INFO>("watchdog: Timed out with no target",
William A. Kennington III47a9ec42019-04-18 15:42:16 -0700122 entry("ACTION=%s", convertForMessage(action).c_str()),
123 entry("TIMER_USE=%s",
124 convertForMessage(expiredTimerUse()).c_str()));
William A. Kennington III825f4982018-02-27 19:10:56 -0800125 }
126 else
127 {
William A. Kennington III47a9ec42019-04-18 15:42:16 -0700128 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 III7036c562018-10-03 23:36:16 -0700133
134 try
135 {
Willy Tu9ef0d0f2021-07-06 22:36:23 -0700136 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 Williams15c5bd12021-09-02 09:51:29 -0500141 catch (const sdbusplus::exception::exception& e)
Willy Tu9ef0d0f2021-07-06 22:36:23 -0700142 {
143 log<level::ERR>("watchdog: failed to send timeout signal",
144 entry("ERROR=%s", e.what()));
145 }
146
147 try
148 {
William A. Kennington III7036c562018-10-03 23:36:16 -0700149 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 Williams15c5bd12021-09-02 09:51:29 -0500156 catch (const sdbusplus::exception::exception& e)
William A. Kennington III7036c562018-10-03 23:36:16 -0700157 {
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 Subbanna3473d702017-05-30 16:38:50 +0530163 }
William A. Kennington III1232a152018-02-02 15:57:34 -0800164
William A. Kennington IIId1331082018-02-27 18:47:05 -0800165 tryFallbackOrDisable();
William A. Kennington III825f4982018-02-27 19:10:56 -0800166}
William A. Kennington III1232a152018-02-02 15:57:34 -0800167
William A. Kennington IIId1331082018-02-27 18:47:05 -0800168void Watchdog::tryFallbackOrDisable()
William A. Kennington III825f4982018-02-27 19:10:56 -0800169{
William A. Kennington IIId1331082018-02-27 18:47:05 -0800170 // We only re-arm the watchdog if we were already enabled and have
171 // a possible fallback
William A. Kennington III22352192018-02-27 18:51:44 -0800172 if (fallback && (fallback->always || this->enabled()))
William A. Kennington IIId1331082018-02-27 18:47:05 -0800173 {
174 auto interval_ms = fallback->interval;
William A. Kennington IIIf505fc02018-09-12 18:30:09 -0700175 timer.restart(milliseconds(interval_ms));
William A. Kennington IIId1331082018-02-27 18:47:05 -0800176 log<level::INFO>("watchdog: falling back",
Patrick Venture8f6c5152018-09-11 17:45:33 -0700177 entry("INTERVAL=%llu", interval_ms));
William A. Kennington IIId1331082018-02-27 18:47:05 -0800178 }
179 else if (timerEnabled())
William A. Kennington III825f4982018-02-27 19:10:56 -0800180 {
William A. Kennington IIIf505fc02018-09-12 18:30:09 -0700181 timer.setEnabled(false);
William A. Kennington III825f4982018-02-27 19:10:56 -0800182
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 Subbanna8c5a2292017-05-30 15:34:23 +0530189}
190
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530191} // namespace watchdog
Patrick Venture8f6c5152018-09-11 17:45:33 -0700192} // namespace phosphor