Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 17 | #include "watchdog.hpp" |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 18 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 19 | #include <CLI/CLI.hpp> |
William A. Kennington III | 26eef26 | 2019-04-04 15:30:30 -0700 | [diff] [blame] | 20 | #include <functional> |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 21 | #include <iostream> |
William A. Kennington III | 73c2cfb | 2018-09-12 18:01:37 -0700 | [diff] [blame] | 22 | #include <optional> |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 23 | #include <phosphor-logging/elog-errors.hpp> |
| 24 | #include <phosphor-logging/elog.hpp> |
| 25 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 8d8bc46 | 2019-01-16 14:59:07 -0800 | [diff] [blame] | 26 | #include <sdbusplus/bus.hpp> |
| 27 | #include <sdbusplus/exception.hpp> |
| 28 | #include <sdbusplus/server/manager.hpp> |
| 29 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 658527b | 2021-12-19 20:43:22 -0800 | [diff] [blame] | 30 | #include <sdeventplus/source/signal.hpp> |
| 31 | #include <stdplus/signal.hpp> |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 32 | #include <string> |
| 33 | #include <xyz/openbmc_project/Common/error.hpp> |
| 34 | |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 35 | using phosphor::watchdog::Watchdog; |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 36 | using sdbusplus::xyz::openbmc_project::State::server::convertForMessage; |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 37 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 38 | void printActionTargetMap(const Watchdog::ActionTargetMap& actionTargetMap) |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 39 | { |
| 40 | std::cerr << "Action Targets:\n"; |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 41 | for (const auto& [action, target] : actionTargetMap) |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 42 | { |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 43 | std::cerr << " " << convertForMessage(action) << " -> " << target |
| 44 | << "\n"; |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 45 | } |
| 46 | std::cerr << std::flush; |
| 47 | } |
| 48 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 49 | void printFallback(const Watchdog::Fallback& fallback) |
| 50 | { |
| 51 | std::cerr << "Fallback Options:\n"; |
| 52 | std::cerr << " Action: " << convertForMessage(fallback.action) << "\n"; |
| 53 | std::cerr << " Interval(ms): " << std::dec << fallback.interval << "\n"; |
| 54 | std::cerr << " Always re-execute: " << std::boolalpha << fallback.always |
| 55 | << "\n"; |
| 56 | std::cerr << std::flush; |
| 57 | } |
| 58 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 59 | int main(int argc, char* argv[]) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 60 | { |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 61 | using namespace phosphor::logging; |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 62 | using InternalFailure = |
| 63 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 64 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 65 | CLI::App app{"Canonical openbmc host watchdog daemon"}; |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 66 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 67 | // Service related options |
| 68 | const std::string serviceGroup = "Service Options"; |
| 69 | std::string path; |
| 70 | app.add_option("-p,--path", path, |
| 71 | "DBus Object Path. " |
| 72 | "Ex: /xyz/openbmc_project/state/watchdog/host0") |
| 73 | ->required() |
| 74 | ->group(serviceGroup); |
| 75 | std::string service; |
| 76 | app.add_option("-s,--service", service, |
| 77 | "DBus Service Name. " |
| 78 | "Ex: xyz.openbmc_project.State.Watchdog.Host") |
| 79 | ->required() |
| 80 | ->group(serviceGroup); |
Jae Hyun Yoo | 61bc6cd | 2021-07-01 14:48:41 -0700 | [diff] [blame] | 81 | bool continueAfterTimeout{false}; |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 82 | app.add_flag("-c,--continue", continueAfterTimeout, |
| 83 | "Continue daemon after watchdog timeout") |
| 84 | ->group(serviceGroup); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 85 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 86 | // Target related options |
| 87 | const std::string targetGroup = "Target Options"; |
| 88 | std::optional<std::string> target; |
| 89 | app.add_option("-t,--target", target, |
| 90 | "Systemd unit to be called on " |
| 91 | "timeout for all actions but NONE. " |
| 92 | "Deprecated, use --action_target instead.") |
| 93 | ->group(targetGroup); |
| 94 | std::vector<std::string> actionTargets; |
| 95 | app.add_option("-a,--action_target", actionTargets, |
| 96 | "Map of action to " |
| 97 | "systemd unit to be called on timeout if that action is " |
| 98 | "set for ExpireAction when the timer expires.") |
| 99 | ->group(targetGroup); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 100 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 101 | // Fallback related options |
| 102 | const std::string fallbackGroup = "Fallback Options"; |
| 103 | std::optional<std::string> fallbackAction; |
| 104 | auto fallbackActionOpt = |
| 105 | app.add_option("-f,--fallback_action", fallbackAction, |
| 106 | "Enables the " |
| 107 | "watchdog even when disabled via the dbus interface. " |
| 108 | "Perform this action when the fallback expires.") |
| 109 | ->group(fallbackGroup); |
| 110 | std::optional<unsigned> fallbackIntervalMs; |
| 111 | auto fallbackIntervalOpt = |
| 112 | app.add_option("-i,--fallback_interval", fallbackIntervalMs, |
| 113 | "Enables the " |
| 114 | "watchdog even when disabled via the dbus interface. " |
| 115 | "Waits for this interval before performing the fallback " |
| 116 | "action.") |
| 117 | ->group(fallbackGroup); |
| 118 | fallbackIntervalOpt->needs(fallbackActionOpt); |
| 119 | fallbackActionOpt->needs(fallbackIntervalOpt); |
Jae Hyun Yoo | 61bc6cd | 2021-07-01 14:48:41 -0700 | [diff] [blame] | 120 | bool fallbackAlways{false}; |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 121 | app.add_flag("-e,--fallback_always", fallbackAlways, |
| 122 | "Enables the " |
| 123 | "watchdog even when disabled by the dbus interface. " |
| 124 | "This option is only valid with a fallback specified") |
| 125 | ->group(fallbackGroup) |
| 126 | ->needs(fallbackActionOpt) |
| 127 | ->needs(fallbackIntervalOpt); |
| 128 | |
William A. Kennington III | 26eef26 | 2019-04-04 15:30:30 -0700 | [diff] [blame] | 129 | // Should we watch for postcodes |
Jae Hyun Yoo | 61bc6cd | 2021-07-01 14:48:41 -0700 | [diff] [blame] | 130 | bool watchPostcodes{false}; |
William A. Kennington III | 26eef26 | 2019-04-04 15:30:30 -0700 | [diff] [blame] | 131 | app.add_flag("-w,--watch_postcodes", watchPostcodes, |
| 132 | "Should we reset the time remaining any time a postcode " |
| 133 | "is signaled."); |
| 134 | |
Andrew Geissler | afc369a | 2021-06-03 14:17:16 -0500 | [diff] [blame] | 135 | // Interval related options |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 136 | uint64_t minInterval = phosphor::watchdog::DEFAULT_MIN_INTERVAL_MS; |
Kun Yi | 0868375 | 2019-12-05 10:34:45 -0800 | [diff] [blame] | 137 | app.add_option("-m,--min_interval", minInterval, |
| 138 | "Set minimum interval for watchdog in milliseconds"); |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 139 | |
Andrew Geissler | afc369a | 2021-06-03 14:17:16 -0500 | [diff] [blame] | 140 | // 0 to indicate to use default from PDI if not passed in |
| 141 | uint64_t defaultInterval = 0; |
| 142 | app.add_option("-d,--default_interval", defaultInterval, |
| 143 | "Set default interval for watchdog in milliseconds"); |
| 144 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 145 | CLI11_PARSE(app, argc, argv); |
| 146 | |
| 147 | // Put together a list of actions and associated systemd targets |
| 148 | // The new --action_target options take precedence over the legacy |
| 149 | // --target |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 150 | Watchdog::ActionTargetMap actionTargetMap; |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 151 | if (target) |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 152 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 153 | actionTargetMap[Watchdog::Action::HardReset] = *target; |
| 154 | actionTargetMap[Watchdog::Action::PowerOff] = *target; |
| 155 | actionTargetMap[Watchdog::Action::PowerCycle] = *target; |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 156 | } |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 157 | for (const auto& actionTarget : actionTargets) |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 158 | { |
| 159 | size_t keyValueSplit = actionTarget.find("="); |
| 160 | if (keyValueSplit == std::string::npos) |
| 161 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 162 | std::cerr << "Invalid action_target format, " |
| 163 | "expect <action>=<target>." |
| 164 | << std::endl; |
| 165 | return 1; |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | std::string key = actionTarget.substr(0, keyValueSplit); |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 169 | std::string value = actionTarget.substr(keyValueSplit + 1); |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 170 | |
| 171 | // Convert an action from a fully namespaced value |
| 172 | Watchdog::Action action; |
| 173 | try |
| 174 | { |
| 175 | action = Watchdog::convertActionFromString(key); |
| 176 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 177 | catch (const sdbusplus::exception::InvalidEnumString&) |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 178 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 179 | std::cerr << "Bad action specified: " << key << std::endl; |
| 180 | return 1; |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 181 | } |
| 182 | |
William A. Kennington III | c2c26ce | 2018-09-13 18:35:56 -0700 | [diff] [blame] | 183 | // Detect duplicate action target arguments |
| 184 | if (actionTargetMap.find(action) != actionTargetMap.end()) |
| 185 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 186 | std::cerr << "Got duplicate action: " << key << std::endl; |
| 187 | return 1; |
William A. Kennington III | c2c26ce | 2018-09-13 18:35:56 -0700 | [diff] [blame] | 188 | } |
| 189 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 190 | actionTargetMap[action] = std::move(value); |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 191 | } |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 192 | printActionTargetMap(actionTargetMap); |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 193 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 194 | // Build the fallback option used for the Watchdog |
| 195 | std::optional<Watchdog::Fallback> maybeFallback; |
| 196 | if (fallbackAction) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 197 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 198 | Watchdog::Fallback fallback; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 199 | try |
| 200 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 201 | fallback.action = |
| 202 | Watchdog::convertActionFromString(*fallbackAction); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 203 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 204 | catch (const sdbusplus::exception::InvalidEnumString&) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 205 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 206 | std::cerr << "Bad fallback action specified: " << *fallbackAction |
| 207 | << std::endl; |
| 208 | return 1; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 209 | } |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 210 | fallback.interval = *fallbackIntervalMs; |
| 211 | fallback.always = fallbackAlways; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 212 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 213 | printFallback(fallback); |
Willy Tu | cf4ce3c | 2021-12-09 20:39:37 -0800 | [diff] [blame] | 214 | maybeFallback = fallback; |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 217 | try |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 218 | { |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 219 | // Get a default event loop |
| 220 | auto event = sdeventplus::Event::get_default(); |
| 221 | |
| 222 | // Get a handle to system dbus. |
| 223 | auto bus = sdbusplus::bus::new_default(); |
| 224 | |
| 225 | // Add systemd object manager. |
Patrick Williams | 73bd527 | 2022-07-22 19:26:57 -0500 | [diff] [blame^] | 226 | sdbusplus::server::manager_t watchdogManager(bus, path.c_str()); |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 227 | |
| 228 | // Attach the bus to sd_event to service user requests |
| 229 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 230 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 231 | // Create a watchdog object |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 232 | Watchdog watchdog(bus, path.c_str(), event, std::move(actionTargetMap), |
Andrew Geissler | afc369a | 2021-06-03 14:17:16 -0500 | [diff] [blame] | 233 | std::move(maybeFallback), minInterval, |
| 234 | defaultInterval); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 235 | |
Patrick Williams | 73bd527 | 2022-07-22 19:26:57 -0500 | [diff] [blame^] | 236 | std::optional<sdbusplus::bus::match_t> watchPostcodeMatch; |
William A. Kennington III | 26eef26 | 2019-04-04 15:30:30 -0700 | [diff] [blame] | 237 | if (watchPostcodes) |
| 238 | { |
| 239 | watchPostcodeMatch.emplace( |
| 240 | bus, |
| 241 | sdbusplus::bus::match::rules::propertiesChanged( |
William A. Kennington III | 459b6c7 | 2021-05-12 12:02:48 -0700 | [diff] [blame] | 242 | "/xyz/openbmc_project/state/boot/raw0", |
William A. Kennington III | 26eef26 | 2019-04-04 15:30:30 -0700 | [diff] [blame] | 243 | "xyz.openbmc_project.State.Boot.Raw"), |
| 244 | std::bind(&Watchdog::resetTimeRemaining, std::ref(watchdog), |
| 245 | false)); |
| 246 | } |
| 247 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 248 | // Claim the bus |
| 249 | bus.request_name(service.c_str()); |
| 250 | |
William A. Kennington III | 658527b | 2021-12-19 20:43:22 -0800 | [diff] [blame] | 251 | bool done = false; |
| 252 | auto intCb = [&](sdeventplus::source::Signal&, |
| 253 | const struct signalfd_siginfo*) { done = true; }; |
| 254 | stdplus::signal::block(SIGINT); |
| 255 | sdeventplus::source::Signal sigint(event, SIGINT, intCb); |
| 256 | stdplus::signal::block(SIGTERM); |
| 257 | sdeventplus::source::Signal sigterm(event, SIGTERM, std::move(intCb)); |
| 258 | |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 259 | // Loop until our timer expires and we don't want to continue |
William A. Kennington III | 658527b | 2021-12-19 20:43:22 -0800 | [diff] [blame] | 260 | while (!done && (continueAfterTimeout || !watchdog.timerExpired())) |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 261 | { |
William A. Kennington III | 6f80f80 | 2022-01-07 01:14:55 -0800 | [diff] [blame] | 262 | // Process all outstanding bus events before running the loop. |
| 263 | // This prevents the sd-bus handling logic from leaking memory. |
| 264 | // TODO: Remove when upstream fixes this bug |
| 265 | while (bus.process_discard() > 0) |
| 266 | ; |
| 267 | |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 268 | // Run and never timeout |
| 269 | event.run(std::nullopt); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 270 | } |
| 271 | } |
Patrick Williams | 120bc4c | 2021-10-06 14:41:25 -0500 | [diff] [blame] | 272 | catch (const InternalFailure& e) |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 273 | { |
| 274 | phosphor::logging::commit<InternalFailure>(); |
| 275 | |
| 276 | // Need a coredump in the error cases. |
| 277 | std::terminate(); |
| 278 | } |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 279 | return 0; |
| 280 | } |