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 | 9397526 | 2018-02-02 16:00:50 -0800 | [diff] [blame^] | 106 | printActionTargets(actionTargets); |
| 107 | |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 108 | sd_event* event = nullptr; |
| 109 | auto r = sd_event_default(&event); |
| 110 | if (r < 0) |
| 111 | { |
| 112 | log<level::ERR>("Error creating a default sd_event handler"); |
| 113 | return r; |
| 114 | } |
| 115 | phosphor::watchdog::EventPtr eventP{event}; |
| 116 | event = nullptr; |
| 117 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 118 | // Get a handle to system dbus. |
| 119 | auto bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 120 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 121 | // Add systemd object manager. |
| 122 | sdbusplus::server::manager::manager(bus, path.c_str()); |
| 123 | |
| 124 | // Attach the bus to sd_event to service user requests |
| 125 | bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); |
| 126 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 127 | try |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 128 | { |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 129 | // Create a watchdog object |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 130 | Watchdog watchdog(bus, path.c_str(), eventP, std::move(actionTargets)); |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 131 | // Claim the bus |
| 132 | bus.request_name(service.c_str()); |
| 133 | |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 134 | // Loop forever processing events |
| 135 | while (true) |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 136 | { |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 137 | // -1 denotes wait for ever |
| 138 | r = sd_event_run(eventP.get(), (uint64_t)-1); |
| 139 | if (r < 0) |
| 140 | { |
| 141 | log<level::ERR>("Error waiting for events"); |
| 142 | elog<InternalFailure>(); |
| 143 | } |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 144 | |
| 145 | // The timer expiring is an event that breaks from the above. |
| 146 | if (watchdog.timerExpired()) |
| 147 | { |
| 148 | // Either disable the timer or exit. |
| 149 | if (continueAfterTimeout) |
| 150 | { |
| 151 | // The watchdog will be disabled but left running to be |
| 152 | // re-enabled. |
| 153 | watchdog.enabled(false); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | // The watchdog daemon will now exit. |
| 158 | break; |
| 159 | } |
| 160 | } |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 161 | } |
| 162 | } |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 163 | catch(InternalFailure& e) |
| 164 | { |
| 165 | phosphor::logging::commit<InternalFailure>(); |
| 166 | |
| 167 | // Need a coredump in the error cases. |
| 168 | std::terminate(); |
| 169 | } |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 170 | return 0; |
| 171 | } |