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 | |
| 17 | #include <iostream> |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 18 | #include <phosphor-logging/log.hpp> |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 19 | #include <phosphor-logging/elog.hpp> |
| 20 | #include <phosphor-logging/elog-errors.hpp> |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 21 | #include <string> |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 22 | #include <xyz/openbmc_project/Common/error.hpp> |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 23 | #include "argument.hpp" |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 24 | #include "watchdog.hpp" |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 25 | |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 26 | using phosphor::watchdog::ArgumentParser; |
| 27 | using phosphor::watchdog::Watchdog; |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 28 | using sdbusplus::xyz::openbmc_project::State::server::convertForMessage; |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 29 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 30 | static void exitWithError(const char* err, char** argv) |
| 31 | { |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 32 | ArgumentParser::usage(argv); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 33 | std::cerr << "ERROR: " << err << "\n"; |
| 34 | exit(EXIT_FAILURE); |
| 35 | } |
| 36 | |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 37 | void printActionTargets( |
| 38 | const std::map<Watchdog::Action, std::string>& actionTargets) |
| 39 | { |
| 40 | std::cerr << "Action Targets:\n"; |
| 41 | for (const auto& actionTarget : actionTargets) |
| 42 | { |
| 43 | std::cerr << " " << convertForMessage(actionTarget.first) << " -> " << |
| 44 | actionTarget.second << "\n"; |
| 45 | } |
| 46 | std::cerr << std::flush; |
| 47 | } |
| 48 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 49 | int main(int argc, char** argv) |
| 50 | { |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 51 | using namespace phosphor::logging; |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 52 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 53 | Error::InternalFailure; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 54 | // Read arguments. |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 55 | auto options = ArgumentParser(argc, argv); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 56 | |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 57 | // Parse out continue argument. |
| 58 | auto continueParam = (options)["continue"]; |
| 59 | // Default it to exit on watchdog timeout |
| 60 | auto continueAfterTimeout = false; |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 61 | if (!continueParam.empty()) |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 62 | { |
| 63 | continueAfterTimeout = true; |
| 64 | } |
| 65 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 66 | // Parse out path argument. |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 67 | auto pathParam = (options)["path"]; |
| 68 | if (pathParam.empty()) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 69 | { |
| 70 | exitWithError("Path not specified.", argv); |
| 71 | } |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 72 | if (pathParam.size() > 1) |
| 73 | { |
| 74 | exitWithError("Multiple paths specified.", argv); |
| 75 | } |
| 76 | auto path = pathParam.back(); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 77 | |
| 78 | // Parse out service name argument |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 79 | auto serviceParam = (options)["service"]; |
| 80 | if (serviceParam.empty()) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 81 | { |
| 82 | exitWithError("Service not specified.", argv); |
| 83 | } |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 84 | if (serviceParam.size() > 1) |
| 85 | { |
| 86 | exitWithError("Multiple services specified.", argv); |
| 87 | } |
| 88 | auto service = serviceParam.back(); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 89 | |
| 90 | // Parse out target argument. It is fine if the caller does not |
| 91 | // pass this if they are not interested in calling into any target |
| 92 | // on meeting a condition. |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame] | 93 | auto targetParam = (options)["target"]; |
| 94 | if (targetParam.size() > 1) |
| 95 | { |
| 96 | exitWithError("Multiple targets specified.", argv); |
| 97 | } |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 98 | std::map<Watchdog::Action, Watchdog::TargetName> actionTargets; |
| 99 | if (!targetParam.empty()) { |
| 100 | auto target = targetParam.back(); |
| 101 | actionTargets[Watchdog::Action::HardReset] = target; |
| 102 | actionTargets[Watchdog::Action::PowerOff] = target; |
| 103 | actionTargets[Watchdog::Action::PowerCycle] = target; |
| 104 | } |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 105 | |
William A. Kennington III | 27df4b5 | 2018-02-02 16:02:05 -0800 | [diff] [blame] | 106 | // Parse out the action_target arguments. We allow one target to map |
| 107 | // to an action. These targets can replace the target specified above. |
| 108 | for (const auto& actionTarget : (options)["action_target"]) |
| 109 | { |
| 110 | size_t keyValueSplit = actionTarget.find("="); |
| 111 | if (keyValueSplit == std::string::npos) |
| 112 | { |
| 113 | exitWithError( |
| 114 | "Invalid action_target format, expect <action>=<target>.", |
| 115 | argv); |
| 116 | } |
| 117 | |
| 118 | std::string key = actionTarget.substr(0, keyValueSplit); |
| 119 | std::string value = actionTarget.substr(keyValueSplit+1); |
| 120 | |
| 121 | // Convert an action from a fully namespaced value |
| 122 | Watchdog::Action action; |
| 123 | try |
| 124 | { |
| 125 | action = Watchdog::convertActionFromString(key); |
| 126 | } |
| 127 | catch (const sdbusplus::exception::InvalidEnumString &) |
| 128 | { |
| 129 | exitWithError("Bad action specified.", argv); |
| 130 | } |
| 131 | |
| 132 | actionTargets[action] = std::move(value); |
| 133 | } |
William A. Kennington III | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame] | 134 | printActionTargets(actionTargets); |
| 135 | |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 136 | sd_event* event = nullptr; |
| 137 | auto r = sd_event_default(&event); |
| 138 | if (r < 0) |
| 139 | { |
| 140 | log<level::ERR>("Error creating a default sd_event handler"); |
| 141 | return r; |
| 142 | } |
| 143 | phosphor::watchdog::EventPtr eventP{event}; |
| 144 | event = nullptr; |
| 145 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 146 | // Get a handle to system dbus. |
| 147 | auto bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 148 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 149 | // Add systemd object manager. |
| 150 | sdbusplus::server::manager::manager(bus, path.c_str()); |
| 151 | |
| 152 | // Attach the bus to sd_event to service user requests |
| 153 | bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); |
| 154 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 155 | try |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 156 | { |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 157 | // Create a watchdog object |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 158 | Watchdog watchdog(bus, path.c_str(), eventP, std::move(actionTargets)); |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 159 | // Claim the bus |
| 160 | bus.request_name(service.c_str()); |
| 161 | |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 162 | // Loop forever processing events |
| 163 | while (true) |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 164 | { |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 165 | // -1 denotes wait for ever |
| 166 | r = sd_event_run(eventP.get(), (uint64_t)-1); |
| 167 | if (r < 0) |
| 168 | { |
| 169 | log<level::ERR>("Error waiting for events"); |
| 170 | elog<InternalFailure>(); |
| 171 | } |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 172 | |
| 173 | // The timer expiring is an event that breaks from the above. |
| 174 | if (watchdog.timerExpired()) |
| 175 | { |
| 176 | // Either disable the timer or exit. |
| 177 | if (continueAfterTimeout) |
| 178 | { |
| 179 | // The watchdog will be disabled but left running to be |
| 180 | // re-enabled. |
| 181 | watchdog.enabled(false); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | // The watchdog daemon will now exit. |
| 186 | break; |
| 187 | } |
| 188 | } |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 189 | } |
| 190 | } |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 191 | catch(InternalFailure& e) |
| 192 | { |
| 193 | phosphor::logging::commit<InternalFailure>(); |
| 194 | |
| 195 | // Need a coredump in the error cases. |
| 196 | std::terminate(); |
| 197 | } |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 198 | return 0; |
| 199 | } |