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> |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 20 | #include <phosphor-logging/elog-errors.hpp> |
| 21 | #include <phosphor-logging/elog.hpp> |
| 22 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 8d8bc46 | 2019-01-16 14:59:07 -0800 | [diff] [blame] | 23 | #include <sdbusplus/bus.hpp> |
| 24 | #include <sdbusplus/exception.hpp> |
| 25 | #include <sdbusplus/server/manager.hpp> |
| 26 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 658527b | 2021-12-19 20:43:22 -0800 | [diff] [blame] | 27 | #include <sdeventplus/source/signal.hpp> |
William A. Kennington III | d1b1e79 | 2022-11-21 18:29:11 -0800 | [diff] [blame] | 28 | #include <sdeventplus/utility/sdbus.hpp> |
William A. Kennington III | 658527b | 2021-12-19 20:43:22 -0800 | [diff] [blame] | 29 | #include <stdplus/signal.hpp> |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 30 | #include <xyz/openbmc_project/Common/error.hpp> |
| 31 | |
Patrick Williams | 6ac6a34 | 2023-05-10 07:51:12 -0500 | [diff] [blame] | 32 | #include <functional> |
| 33 | #include <iostream> |
| 34 | #include <optional> |
| 35 | #include <string> |
| 36 | |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 37 | using phosphor::watchdog::Watchdog; |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 38 | using sdbusplus::xyz::openbmc_project::State::server::convertForMessage; |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 39 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 40 | void printActionTargetMap(const Watchdog::ActionTargetMap& actionTargetMap) |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 41 | { |
| 42 | std::cerr << "Action Targets:\n"; |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 43 | for (const auto& [action, target] : actionTargetMap) |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 44 | { |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 45 | std::cerr << " " << convertForMessage(action) << " -> " << target |
| 46 | << "\n"; |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 47 | } |
| 48 | std::cerr << std::flush; |
| 49 | } |
| 50 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 51 | void printFallback(const Watchdog::Fallback& fallback) |
| 52 | { |
| 53 | std::cerr << "Fallback Options:\n"; |
| 54 | std::cerr << " Action: " << convertForMessage(fallback.action) << "\n"; |
| 55 | std::cerr << " Interval(ms): " << std::dec << fallback.interval << "\n"; |
| 56 | std::cerr << " Always re-execute: " << std::boolalpha << fallback.always |
| 57 | << "\n"; |
| 58 | std::cerr << std::flush; |
| 59 | } |
| 60 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 61 | int main(int argc, char* argv[]) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 62 | { |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 63 | using namespace phosphor::logging; |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 64 | using InternalFailure = |
| 65 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 66 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 67 | CLI::App app{"Canonical openbmc host watchdog daemon"}; |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 68 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 69 | // Service related options |
| 70 | const std::string serviceGroup = "Service Options"; |
| 71 | std::string path; |
| 72 | app.add_option("-p,--path", path, |
| 73 | "DBus Object Path. " |
| 74 | "Ex: /xyz/openbmc_project/state/watchdog/host0") |
| 75 | ->required() |
| 76 | ->group(serviceGroup); |
| 77 | std::string service; |
| 78 | app.add_option("-s,--service", service, |
| 79 | "DBus Service Name. " |
| 80 | "Ex: xyz.openbmc_project.State.Watchdog.Host") |
| 81 | ->required() |
| 82 | ->group(serviceGroup); |
Jae Hyun Yoo | 61bc6cd | 2021-07-01 14:48:41 -0700 | [diff] [blame] | 83 | bool continueAfterTimeout{false}; |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 84 | app.add_flag("-c,--continue", continueAfterTimeout, |
| 85 | "Continue daemon after watchdog timeout") |
| 86 | ->group(serviceGroup); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 87 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 88 | // Target related options |
| 89 | const std::string targetGroup = "Target Options"; |
| 90 | std::optional<std::string> target; |
| 91 | app.add_option("-t,--target", target, |
| 92 | "Systemd unit to be called on " |
| 93 | "timeout for all actions but NONE. " |
| 94 | "Deprecated, use --action_target instead.") |
| 95 | ->group(targetGroup); |
| 96 | std::vector<std::string> actionTargets; |
| 97 | app.add_option("-a,--action_target", actionTargets, |
| 98 | "Map of action to " |
| 99 | "systemd unit to be called on timeout if that action is " |
| 100 | "set for ExpireAction when the timer expires.") |
| 101 | ->group(targetGroup); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 102 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 103 | // Fallback related options |
| 104 | const std::string fallbackGroup = "Fallback Options"; |
| 105 | std::optional<std::string> fallbackAction; |
| 106 | auto fallbackActionOpt = |
| 107 | app.add_option("-f,--fallback_action", fallbackAction, |
| 108 | "Enables the " |
| 109 | "watchdog even when disabled via the dbus interface. " |
| 110 | "Perform this action when the fallback expires.") |
| 111 | ->group(fallbackGroup); |
| 112 | std::optional<unsigned> fallbackIntervalMs; |
| 113 | auto fallbackIntervalOpt = |
| 114 | app.add_option("-i,--fallback_interval", fallbackIntervalMs, |
| 115 | "Enables the " |
| 116 | "watchdog even when disabled via the dbus interface. " |
| 117 | "Waits for this interval before performing the fallback " |
| 118 | "action.") |
| 119 | ->group(fallbackGroup); |
| 120 | fallbackIntervalOpt->needs(fallbackActionOpt); |
| 121 | fallbackActionOpt->needs(fallbackIntervalOpt); |
Jae Hyun Yoo | 61bc6cd | 2021-07-01 14:48:41 -0700 | [diff] [blame] | 122 | bool fallbackAlways{false}; |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 123 | app.add_flag("-e,--fallback_always", fallbackAlways, |
| 124 | "Enables the " |
| 125 | "watchdog even when disabled by the dbus interface. " |
| 126 | "This option is only valid with a fallback specified") |
| 127 | ->group(fallbackGroup) |
| 128 | ->needs(fallbackActionOpt) |
| 129 | ->needs(fallbackIntervalOpt); |
| 130 | |
William A. Kennington III | 26eef26 | 2019-04-04 15:30:30 -0700 | [diff] [blame] | 131 | // Should we watch for postcodes |
Jae Hyun Yoo | 61bc6cd | 2021-07-01 14:48:41 -0700 | [diff] [blame] | 132 | bool watchPostcodes{false}; |
William A. Kennington III | 26eef26 | 2019-04-04 15:30:30 -0700 | [diff] [blame] | 133 | app.add_flag("-w,--watch_postcodes", watchPostcodes, |
| 134 | "Should we reset the time remaining any time a postcode " |
| 135 | "is signaled."); |
| 136 | |
Andrew Geissler | afc369a | 2021-06-03 14:17:16 -0500 | [diff] [blame] | 137 | // Interval related options |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 138 | uint64_t minInterval = phosphor::watchdog::DEFAULT_MIN_INTERVAL_MS; |
Kun Yi | 0868375 | 2019-12-05 10:34:45 -0800 | [diff] [blame] | 139 | app.add_option("-m,--min_interval", minInterval, |
| 140 | "Set minimum interval for watchdog in milliseconds"); |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 141 | |
Andrew Geissler | afc369a | 2021-06-03 14:17:16 -0500 | [diff] [blame] | 142 | // 0 to indicate to use default from PDI if not passed in |
| 143 | uint64_t defaultInterval = 0; |
| 144 | app.add_option("-d,--default_interval", defaultInterval, |
| 145 | "Set default interval for watchdog in milliseconds"); |
| 146 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 147 | CLI11_PARSE(app, argc, argv); |
| 148 | |
| 149 | // Put together a list of actions and associated systemd targets |
| 150 | // The new --action_target options take precedence over the legacy |
| 151 | // --target |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 152 | Watchdog::ActionTargetMap actionTargetMap; |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 153 | if (target) |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 154 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 155 | actionTargetMap[Watchdog::Action::HardReset] = *target; |
| 156 | actionTargetMap[Watchdog::Action::PowerOff] = *target; |
| 157 | actionTargetMap[Watchdog::Action::PowerCycle] = *target; |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 158 | } |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 159 | for (const auto& actionTarget : actionTargets) |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 160 | { |
| 161 | size_t keyValueSplit = actionTarget.find("="); |
| 162 | if (keyValueSplit == std::string::npos) |
| 163 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 164 | std::cerr << "Invalid action_target format, " |
| 165 | "expect <action>=<target>." |
| 166 | << std::endl; |
| 167 | return 1; |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | std::string key = actionTarget.substr(0, keyValueSplit); |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 171 | std::string value = actionTarget.substr(keyValueSplit + 1); |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 172 | |
| 173 | // Convert an action from a fully namespaced value |
| 174 | Watchdog::Action action; |
| 175 | try |
| 176 | { |
| 177 | action = Watchdog::convertActionFromString(key); |
| 178 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 179 | catch (const sdbusplus::exception::InvalidEnumString&) |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 180 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 181 | std::cerr << "Bad action specified: " << key << std::endl; |
| 182 | return 1; |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 183 | } |
| 184 | |
William A. Kennington III | c2c26ce | 2018-09-13 18:35:56 -0700 | [diff] [blame] | 185 | // Detect duplicate action target arguments |
| 186 | if (actionTargetMap.find(action) != actionTargetMap.end()) |
| 187 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 188 | std::cerr << "Got duplicate action: " << key << std::endl; |
| 189 | return 1; |
William A. Kennington III | c2c26ce | 2018-09-13 18:35:56 -0700 | [diff] [blame] | 190 | } |
| 191 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 192 | actionTargetMap[action] = std::move(value); |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 193 | } |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 194 | printActionTargetMap(actionTargetMap); |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 195 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 196 | // Build the fallback option used for the Watchdog |
| 197 | std::optional<Watchdog::Fallback> maybeFallback; |
| 198 | if (fallbackAction) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 199 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 200 | Watchdog::Fallback fallback; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 201 | try |
| 202 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 203 | fallback.action = |
| 204 | Watchdog::convertActionFromString(*fallbackAction); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 205 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 206 | catch (const sdbusplus::exception::InvalidEnumString&) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 207 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 208 | std::cerr << "Bad fallback action specified: " << *fallbackAction |
| 209 | << std::endl; |
| 210 | return 1; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 211 | } |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 212 | fallback.interval = *fallbackIntervalMs; |
| 213 | fallback.always = fallbackAlways; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 214 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 215 | printFallback(fallback); |
Willy Tu | cf4ce3c | 2021-12-09 20:39:37 -0800 | [diff] [blame] | 216 | maybeFallback = fallback; |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 219 | try |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 220 | { |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 221 | // Get a default event loop |
| 222 | auto event = sdeventplus::Event::get_default(); |
| 223 | |
| 224 | // Get a handle to system dbus. |
| 225 | auto bus = sdbusplus::bus::new_default(); |
| 226 | |
| 227 | // Add systemd object manager. |
Patrick Williams | 73bd527 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 228 | sdbusplus::server::manager_t watchdogManager(bus, path.c_str()); |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 229 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 230 | // Create a watchdog object |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 231 | Watchdog watchdog(bus, path.c_str(), event, std::move(actionTargetMap), |
Andrew Geissler | afc369a | 2021-06-03 14:17:16 -0500 | [diff] [blame] | 232 | std::move(maybeFallback), minInterval, |
William A. Kennington III | d1b1e79 | 2022-11-21 18:29:11 -0800 | [diff] [blame] | 233 | defaultInterval, |
| 234 | /*exitAfterTimeout=*/!continueAfterTimeout); |
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 | |
Patrick Williams | 5c42370 | 2023-10-20 11:19:39 -0500 | [diff] [blame] | 251 | auto intCb = [](sdeventplus::source::Signal& s, |
| 252 | const struct signalfd_siginfo*) { |
William A. Kennington III | d1b1e79 | 2022-11-21 18:29:11 -0800 | [diff] [blame] | 253 | s.get_event().exit(0); |
| 254 | }; |
William A. Kennington III | 658527b | 2021-12-19 20:43:22 -0800 | [diff] [blame] | 255 | stdplus::signal::block(SIGINT); |
| 256 | sdeventplus::source::Signal sigint(event, SIGINT, intCb); |
| 257 | stdplus::signal::block(SIGTERM); |
| 258 | sdeventplus::source::Signal sigterm(event, SIGTERM, std::move(intCb)); |
William A. Kennington III | d1b1e79 | 2022-11-21 18:29:11 -0800 | [diff] [blame] | 259 | return sdeventplus::utility::loopWithBus(event, bus); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 260 | } |
Patrick Williams | 120bc4c | 2021-10-06 14:41:25 -0500 | [diff] [blame] | 261 | catch (const InternalFailure& e) |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 262 | { |
| 263 | phosphor::logging::commit<InternalFailure>(); |
| 264 | |
| 265 | // Need a coredump in the error cases. |
| 266 | std::terminate(); |
| 267 | } |
William A. Kennington III | d1b1e79 | 2022-11-21 18:29:11 -0800 | [diff] [blame] | 268 | return 1; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 269 | } |