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