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> |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <xyz/openbmc_project/Common/error.hpp> |
| 32 | |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 33 | using phosphor::watchdog::Watchdog; |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 34 | using sdbusplus::xyz::openbmc_project::State::server::convertForMessage; |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 35 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 36 | void printActionTargetMap(const Watchdog::ActionTargetMap& actionTargetMap) |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 37 | { |
| 38 | std::cerr << "Action Targets:\n"; |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 39 | for (const auto& [action, target] : actionTargetMap) |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 40 | { |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 41 | std::cerr << " " << convertForMessage(action) << " -> " << target |
| 42 | << "\n"; |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 43 | } |
| 44 | std::cerr << std::flush; |
| 45 | } |
| 46 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 47 | void printFallback(const Watchdog::Fallback& fallback) |
| 48 | { |
| 49 | std::cerr << "Fallback Options:\n"; |
| 50 | std::cerr << " Action: " << convertForMessage(fallback.action) << "\n"; |
| 51 | std::cerr << " Interval(ms): " << std::dec << fallback.interval << "\n"; |
| 52 | std::cerr << " Always re-execute: " << std::boolalpha << fallback.always |
| 53 | << "\n"; |
| 54 | std::cerr << std::flush; |
| 55 | } |
| 56 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 57 | int main(int argc, char* argv[]) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 58 | { |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 59 | using namespace phosphor::logging; |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 60 | using InternalFailure = |
| 61 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 62 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 63 | CLI::App app{"Canonical openbmc host watchdog daemon"}; |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 64 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 65 | // Service related options |
| 66 | const std::string serviceGroup = "Service Options"; |
| 67 | std::string path; |
| 68 | app.add_option("-p,--path", path, |
| 69 | "DBus Object Path. " |
| 70 | "Ex: /xyz/openbmc_project/state/watchdog/host0") |
| 71 | ->required() |
| 72 | ->group(serviceGroup); |
| 73 | std::string service; |
| 74 | app.add_option("-s,--service", service, |
| 75 | "DBus Service Name. " |
| 76 | "Ex: xyz.openbmc_project.State.Watchdog.Host") |
| 77 | ->required() |
| 78 | ->group(serviceGroup); |
| 79 | bool continueAfterTimeout; |
| 80 | app.add_flag("-c,--continue", continueAfterTimeout, |
| 81 | "Continue daemon after watchdog timeout") |
| 82 | ->group(serviceGroup); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 83 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 84 | // Target related options |
| 85 | const std::string targetGroup = "Target Options"; |
| 86 | std::optional<std::string> target; |
| 87 | app.add_option("-t,--target", target, |
| 88 | "Systemd unit to be called on " |
| 89 | "timeout for all actions but NONE. " |
| 90 | "Deprecated, use --action_target instead.") |
| 91 | ->group(targetGroup); |
| 92 | std::vector<std::string> actionTargets; |
| 93 | app.add_option("-a,--action_target", actionTargets, |
| 94 | "Map of action to " |
| 95 | "systemd unit to be called on timeout if that action is " |
| 96 | "set for ExpireAction when the timer expires.") |
| 97 | ->group(targetGroup); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 98 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 99 | // Fallback related options |
| 100 | const std::string fallbackGroup = "Fallback Options"; |
| 101 | std::optional<std::string> fallbackAction; |
| 102 | auto fallbackActionOpt = |
| 103 | app.add_option("-f,--fallback_action", fallbackAction, |
| 104 | "Enables the " |
| 105 | "watchdog even when disabled via the dbus interface. " |
| 106 | "Perform this action when the fallback expires.") |
| 107 | ->group(fallbackGroup); |
| 108 | std::optional<unsigned> fallbackIntervalMs; |
| 109 | auto fallbackIntervalOpt = |
| 110 | app.add_option("-i,--fallback_interval", fallbackIntervalMs, |
| 111 | "Enables the " |
| 112 | "watchdog even when disabled via the dbus interface. " |
| 113 | "Waits for this interval before performing the fallback " |
| 114 | "action.") |
| 115 | ->group(fallbackGroup); |
| 116 | fallbackIntervalOpt->needs(fallbackActionOpt); |
| 117 | fallbackActionOpt->needs(fallbackIntervalOpt); |
| 118 | bool fallbackAlways; |
| 119 | app.add_flag("-e,--fallback_always", fallbackAlways, |
| 120 | "Enables the " |
| 121 | "watchdog even when disabled by the dbus interface. " |
| 122 | "This option is only valid with a fallback specified") |
| 123 | ->group(fallbackGroup) |
| 124 | ->needs(fallbackActionOpt) |
| 125 | ->needs(fallbackIntervalOpt); |
| 126 | |
William A. Kennington III | 26eef26 | 2019-04-04 15:30:30 -0700 | [diff] [blame^] | 127 | // Should we watch for postcodes |
| 128 | bool watchPostcodes; |
| 129 | app.add_flag("-w,--watch_postcodes", watchPostcodes, |
| 130 | "Should we reset the time remaining any time a postcode " |
| 131 | "is signaled."); |
| 132 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 133 | CLI11_PARSE(app, argc, argv); |
| 134 | |
| 135 | // Put together a list of actions and associated systemd targets |
| 136 | // The new --action_target options take precedence over the legacy |
| 137 | // --target |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 138 | Watchdog::ActionTargetMap actionTargetMap; |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 139 | if (target) |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 140 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 141 | actionTargetMap[Watchdog::Action::HardReset] = *target; |
| 142 | actionTargetMap[Watchdog::Action::PowerOff] = *target; |
| 143 | actionTargetMap[Watchdog::Action::PowerCycle] = *target; |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 144 | } |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 145 | for (const auto& actionTarget : actionTargets) |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 146 | { |
| 147 | size_t keyValueSplit = actionTarget.find("="); |
| 148 | if (keyValueSplit == std::string::npos) |
| 149 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 150 | std::cerr << "Invalid action_target format, " |
| 151 | "expect <action>=<target>." |
| 152 | << std::endl; |
| 153 | return 1; |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | std::string key = actionTarget.substr(0, keyValueSplit); |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 157 | std::string value = actionTarget.substr(keyValueSplit + 1); |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 158 | |
| 159 | // Convert an action from a fully namespaced value |
| 160 | Watchdog::Action action; |
| 161 | try |
| 162 | { |
| 163 | action = Watchdog::convertActionFromString(key); |
| 164 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 165 | catch (const sdbusplus::exception::InvalidEnumString&) |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 166 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 167 | std::cerr << "Bad action specified: " << key << std::endl; |
| 168 | return 1; |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 169 | } |
| 170 | |
William A. Kennington III | c2c26ce | 2018-09-13 18:35:56 -0700 | [diff] [blame] | 171 | // Detect duplicate action target arguments |
| 172 | if (actionTargetMap.find(action) != actionTargetMap.end()) |
| 173 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 174 | std::cerr << "Got duplicate action: " << key << std::endl; |
| 175 | return 1; |
William A. Kennington III | c2c26ce | 2018-09-13 18:35:56 -0700 | [diff] [blame] | 176 | } |
| 177 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 178 | actionTargetMap[action] = std::move(value); |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 179 | } |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 180 | printActionTargetMap(actionTargetMap); |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 181 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 182 | // Build the fallback option used for the Watchdog |
| 183 | std::optional<Watchdog::Fallback> maybeFallback; |
| 184 | if (fallbackAction) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 185 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 186 | Watchdog::Fallback fallback; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 187 | try |
| 188 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 189 | fallback.action = |
| 190 | Watchdog::convertActionFromString(*fallbackAction); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 191 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 192 | catch (const sdbusplus::exception::InvalidEnumString&) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 193 | { |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 194 | std::cerr << "Bad fallback action specified: " << *fallbackAction |
| 195 | << std::endl; |
| 196 | return 1; |
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 | fallback.interval = *fallbackIntervalMs; |
| 199 | fallback.always = fallbackAlways; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 200 | |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 201 | printFallback(fallback); |
| 202 | maybeFallback = std::move(fallback); |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 205 | try |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 206 | { |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 207 | // Get a default event loop |
| 208 | auto event = sdeventplus::Event::get_default(); |
| 209 | |
| 210 | // Get a handle to system dbus. |
| 211 | auto bus = sdbusplus::bus::new_default(); |
| 212 | |
| 213 | // Add systemd object manager. |
| 214 | sdbusplus::server::manager::manager(bus, path.c_str()); |
| 215 | |
| 216 | // Attach the bus to sd_event to service user requests |
| 217 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 218 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 219 | // Create a watchdog object |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 220 | Watchdog watchdog(bus, path.c_str(), event, std::move(actionTargetMap), |
William A. Kennington III | 1eb97d9 | 2018-09-13 00:36:12 -0700 | [diff] [blame] | 221 | std::move(maybeFallback)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 222 | |
William A. Kennington III | 26eef26 | 2019-04-04 15:30:30 -0700 | [diff] [blame^] | 223 | std::optional<sdbusplus::bus::match::match> watchPostcodeMatch; |
| 224 | if (watchPostcodes) |
| 225 | { |
| 226 | watchPostcodeMatch.emplace( |
| 227 | bus, |
| 228 | sdbusplus::bus::match::rules::propertiesChanged( |
| 229 | "/xyz/openbmc_project/state/boot/raw", |
| 230 | "xyz.openbmc_project.State.Boot.Raw"), |
| 231 | std::bind(&Watchdog::resetTimeRemaining, std::ref(watchdog), |
| 232 | false)); |
| 233 | } |
| 234 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 235 | // Claim the bus |
| 236 | bus.request_name(service.c_str()); |
| 237 | |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 238 | // Loop until our timer expires and we don't want to continue |
| 239 | while (continueAfterTimeout || !watchdog.timerExpired()) |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 240 | { |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 241 | // Run and never timeout |
| 242 | event.run(std::nullopt); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 243 | } |
| 244 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 245 | catch (InternalFailure& e) |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 246 | { |
| 247 | phosphor::logging::commit<InternalFailure>(); |
| 248 | |
| 249 | // Need a coredump in the error cases. |
| 250 | std::terminate(); |
| 251 | } |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 252 | return 0; |
| 253 | } |