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 | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 17 | #include "argument.hpp" |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 18 | #include "watchdog.hpp" |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 19 | |
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> |
| 25 | #include <string> |
| 26 | #include <xyz/openbmc_project/Common/error.hpp> |
| 27 | |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 28 | using phosphor::watchdog::ArgumentParser; |
| 29 | using phosphor::watchdog::Watchdog; |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 30 | using sdbusplus::xyz::openbmc_project::State::server::convertForMessage; |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 31 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 32 | static void exitWithError(const char* err, char** argv) |
| 33 | { |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 34 | ArgumentParser::usage(argv); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 35 | std::cerr << "ERROR: " << err << "\n"; |
| 36 | exit(EXIT_FAILURE); |
| 37 | } |
| 38 | |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 39 | void printActionTargets( |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 40 | const std::map<Watchdog::Action, std::string>& actionTargets) |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 41 | { |
| 42 | std::cerr << "Action Targets:\n"; |
| 43 | for (const auto& actionTarget : actionTargets) |
| 44 | { |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 45 | std::cerr << " " << convertForMessage(actionTarget.first) << " -> " |
| 46 | << actionTarget.second << "\n"; |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 47 | } |
| 48 | std::cerr << std::flush; |
| 49 | } |
| 50 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 51 | int main(int argc, char** argv) |
| 52 | { |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 53 | using namespace phosphor::logging; |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 54 | using InternalFailure = |
| 55 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 56 | // Read arguments. |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 57 | auto options = ArgumentParser(argc, argv); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 58 | |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 59 | // Parse out continue argument. |
| 60 | auto continueParam = (options)["continue"]; |
| 61 | // Default it to exit on watchdog timeout |
| 62 | auto continueAfterTimeout = false; |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 63 | if (!continueParam.empty()) |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 64 | { |
| 65 | continueAfterTimeout = true; |
| 66 | } |
| 67 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 68 | // Parse out path argument. |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 69 | auto pathParam = (options)["path"]; |
| 70 | if (pathParam.empty()) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 71 | { |
| 72 | exitWithError("Path not specified.", argv); |
| 73 | } |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 74 | if (pathParam.size() > 1) |
| 75 | { |
| 76 | exitWithError("Multiple paths specified.", argv); |
| 77 | } |
| 78 | auto path = pathParam.back(); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 79 | |
| 80 | // Parse out service name argument |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 81 | auto serviceParam = (options)["service"]; |
| 82 | if (serviceParam.empty()) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 83 | { |
| 84 | exitWithError("Service not specified.", argv); |
| 85 | } |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 86 | if (serviceParam.size() > 1) |
| 87 | { |
| 88 | exitWithError("Multiple services specified.", argv); |
| 89 | } |
| 90 | auto service = serviceParam.back(); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 91 | |
| 92 | // Parse out target argument. It is fine if the caller does not |
| 93 | // pass this if they are not interested in calling into any target |
| 94 | // on meeting a condition. |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 95 | auto targetParam = (options)["target"]; |
| 96 | if (targetParam.size() > 1) |
| 97 | { |
| 98 | exitWithError("Multiple targets specified.", argv); |
| 99 | } |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 100 | std::map<Watchdog::Action, Watchdog::TargetName> actionTargets; |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 101 | if (!targetParam.empty()) |
| 102 | { |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 103 | auto target = targetParam.back(); |
| 104 | actionTargets[Watchdog::Action::HardReset] = target; |
| 105 | actionTargets[Watchdog::Action::PowerOff] = target; |
| 106 | actionTargets[Watchdog::Action::PowerCycle] = target; |
| 107 | } |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 108 | |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 109 | // Parse out the action_target arguments. We allow one target to map |
| 110 | // to an action. These targets can replace the target specified above. |
| 111 | for (const auto& actionTarget : (options)["action_target"]) |
| 112 | { |
| 113 | size_t keyValueSplit = actionTarget.find("="); |
| 114 | if (keyValueSplit == std::string::npos) |
| 115 | { |
| 116 | exitWithError( |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 117 | "Invalid action_target format, expect <action>=<target>.", |
| 118 | argv); |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | std::string key = actionTarget.substr(0, keyValueSplit); |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 122 | std::string value = actionTarget.substr(keyValueSplit + 1); |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 123 | |
| 124 | // Convert an action from a fully namespaced value |
| 125 | Watchdog::Action action; |
| 126 | try |
| 127 | { |
| 128 | action = Watchdog::convertActionFromString(key); |
| 129 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 130 | catch (const sdbusplus::exception::InvalidEnumString&) |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 131 | { |
| 132 | exitWithError("Bad action specified.", argv); |
| 133 | } |
| 134 | |
| 135 | actionTargets[action] = std::move(value); |
| 136 | } |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 137 | printActionTargets(actionTargets); |
| 138 | |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 139 | // Parse out the fallback settings for the watchdog. Note that we require |
| 140 | // both of the fallback arguments to do anything here, but having a fallback |
| 141 | // is entirely optional. |
| 142 | auto fallbackActionParam = (options)["fallback_action"]; |
| 143 | auto fallbackIntervalParam = (options)["fallback_interval"]; |
| 144 | if (fallbackActionParam.empty() ^ fallbackIntervalParam.empty()) |
| 145 | { |
| 146 | exitWithError("Only one of the fallback options was specified.", argv); |
| 147 | } |
| 148 | if (fallbackActionParam.size() > 1 || fallbackIntervalParam.size() > 1) |
| 149 | { |
| 150 | exitWithError("Multiple fallbacks specified.", argv); |
| 151 | } |
William A. Kennington III | 73c2cfb | 2018-09-12 18:01:37 -0700 | [diff] [blame^] | 152 | std::optional<Watchdog::Fallback> fallback; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 153 | if (!fallbackActionParam.empty()) |
| 154 | { |
| 155 | Watchdog::Action action; |
| 156 | try |
| 157 | { |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 158 | action = |
| 159 | Watchdog::convertActionFromString(fallbackActionParam.back()); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 160 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 161 | catch (const sdbusplus::exception::InvalidEnumString&) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 162 | { |
| 163 | exitWithError("Bad action specified.", argv); |
| 164 | } |
| 165 | uint64_t interval; |
| 166 | try |
| 167 | { |
| 168 | interval = std::stoull(fallbackIntervalParam.back()); |
| 169 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 170 | catch (const std::logic_error&) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 171 | { |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 172 | exitWithError("Failed to convert fallback interval to integer.", |
| 173 | argv); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 174 | } |
| 175 | fallback = Watchdog::Fallback{ |
| 176 | .action = action, |
| 177 | .interval = interval, |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 178 | .always = false, |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 179 | }; |
| 180 | } |
| 181 | |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 182 | auto fallbackAlwaysParam = (options)["fallback_always"]; |
| 183 | if (!fallbackAlwaysParam.empty()) |
| 184 | { |
| 185 | if (!fallback) |
| 186 | { |
| 187 | exitWithError("Specified the fallback should always be enabled but " |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 188 | "no fallback provided.", |
| 189 | argv); |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 190 | } |
| 191 | fallback->always = true; |
| 192 | } |
| 193 | |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 194 | sd_event* event = nullptr; |
| 195 | auto r = sd_event_default(&event); |
| 196 | if (r < 0) |
| 197 | { |
| 198 | log<level::ERR>("Error creating a default sd_event handler"); |
| 199 | return r; |
| 200 | } |
| 201 | phosphor::watchdog::EventPtr eventP{event}; |
| 202 | event = nullptr; |
| 203 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 204 | // Get a handle to system dbus. |
| 205 | auto bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 206 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 207 | // Add systemd object manager. |
| 208 | sdbusplus::server::manager::manager(bus, path.c_str()); |
| 209 | |
| 210 | // Attach the bus to sd_event to service user requests |
| 211 | bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); |
| 212 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 213 | try |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 214 | { |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 215 | // Create a watchdog object |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 216 | Watchdog watchdog(bus, path.c_str(), eventP, std::move(actionTargets), |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 217 | std::move(fallback)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 218 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 219 | // Claim the bus |
| 220 | bus.request_name(service.c_str()); |
| 221 | |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 222 | // Loop until our timer expires and we don't want to continue |
| 223 | while (continueAfterTimeout || !watchdog.timerExpired()) |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 224 | { |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 225 | // -1 denotes wait for ever |
| 226 | r = sd_event_run(eventP.get(), (uint64_t)-1); |
| 227 | if (r < 0) |
| 228 | { |
| 229 | log<level::ERR>("Error waiting for events"); |
| 230 | elog<InternalFailure>(); |
| 231 | } |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 232 | } |
| 233 | } |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 234 | catch (InternalFailure& e) |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 235 | { |
| 236 | phosphor::logging::commit<InternalFailure>(); |
| 237 | |
| 238 | // Need a coredump in the error cases. |
| 239 | std::terminate(); |
| 240 | } |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 241 | return 0; |
| 242 | } |