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> |
| 21 | #include <xyz/openbmc_project/Common/error.hpp> |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 22 | #include "argument.hpp" |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 23 | #include "watchdog.hpp" |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 24 | |
| 25 | static void exitWithError(const char* err, char** argv) |
| 26 | { |
| 27 | phosphor::watchdog::ArgumentParser::usage(argv); |
| 28 | std::cerr << "ERROR: " << err << "\n"; |
| 29 | exit(EXIT_FAILURE); |
| 30 | } |
| 31 | |
| 32 | int main(int argc, char** argv) |
| 33 | { |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 34 | using namespace phosphor::logging; |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 35 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 36 | Error::InternalFailure; |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 37 | // Read arguments. |
| 38 | auto options = phosphor::watchdog::ArgumentParser(argc, argv); |
| 39 | |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 40 | // Parse out continue argument. |
| 41 | auto continueParam = (options)["continue"]; |
| 42 | // Default it to exit on watchdog timeout |
| 43 | auto continueAfterTimeout = false; |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame^] | 44 | if (!continueParam.empty()) |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 45 | { |
| 46 | continueAfterTimeout = true; |
| 47 | } |
| 48 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 49 | // Parse out path argument. |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame^] | 50 | auto pathParam = (options)["path"]; |
| 51 | if (pathParam.empty()) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 52 | { |
| 53 | exitWithError("Path not specified.", argv); |
| 54 | } |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame^] | 55 | if (pathParam.size() > 1) |
| 56 | { |
| 57 | exitWithError("Multiple paths specified.", argv); |
| 58 | } |
| 59 | auto path = pathParam.back(); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 60 | |
| 61 | // Parse out service name argument |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame^] | 62 | auto serviceParam = (options)["service"]; |
| 63 | if (serviceParam.empty()) |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 64 | { |
| 65 | exitWithError("Service not specified.", argv); |
| 66 | } |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame^] | 67 | if (serviceParam.size() > 1) |
| 68 | { |
| 69 | exitWithError("Multiple services specified.", argv); |
| 70 | } |
| 71 | auto service = serviceParam.back(); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 72 | |
| 73 | // Parse out target argument. It is fine if the caller does not |
| 74 | // pass this if they are not interested in calling into any target |
| 75 | // on meeting a condition. |
William A. Kennington III | 5d30718 | 2018-01-23 22:00:55 -0800 | [diff] [blame^] | 76 | auto targetParam = (options)["target"]; |
| 77 | if (targetParam.size() > 1) |
| 78 | { |
| 79 | exitWithError("Multiple targets specified.", argv); |
| 80 | } |
| 81 | auto target = targetParam.back(); |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 82 | |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 83 | sd_event* event = nullptr; |
| 84 | auto r = sd_event_default(&event); |
| 85 | if (r < 0) |
| 86 | { |
| 87 | log<level::ERR>("Error creating a default sd_event handler"); |
| 88 | return r; |
| 89 | } |
| 90 | phosphor::watchdog::EventPtr eventP{event}; |
| 91 | event = nullptr; |
| 92 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 93 | // Get a handle to system dbus. |
| 94 | auto bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 95 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 96 | // Add systemd object manager. |
| 97 | sdbusplus::server::manager::manager(bus, path.c_str()); |
| 98 | |
| 99 | // Attach the bus to sd_event to service user requests |
| 100 | bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); |
| 101 | |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 102 | try |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 103 | { |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 104 | // Create a watchdog object |
| 105 | phosphor::watchdog::Watchdog watchdog(bus, path.c_str(), |
| 106 | eventP, std::move(target)); |
| 107 | // Claim the bus |
| 108 | bus.request_name(service.c_str()); |
| 109 | |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 110 | // Loop forever processing events |
| 111 | while (true) |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 112 | { |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 113 | // -1 denotes wait for ever |
| 114 | r = sd_event_run(eventP.get(), (uint64_t)-1); |
| 115 | if (r < 0) |
| 116 | { |
| 117 | log<level::ERR>("Error waiting for events"); |
| 118 | elog<InternalFailure>(); |
| 119 | } |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 120 | |
| 121 | // The timer expiring is an event that breaks from the above. |
| 122 | if (watchdog.timerExpired()) |
| 123 | { |
| 124 | // Either disable the timer or exit. |
| 125 | if (continueAfterTimeout) |
| 126 | { |
| 127 | // The watchdog will be disabled but left running to be |
| 128 | // re-enabled. |
| 129 | watchdog.enabled(false); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | // The watchdog daemon will now exit. |
| 134 | break; |
| 135 | } |
| 136 | } |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 137 | } |
| 138 | } |
Vishwanatha Subbanna | 4d5ef3f | 2017-05-31 18:54:22 +0530 | [diff] [blame] | 139 | catch(InternalFailure& e) |
| 140 | { |
| 141 | phosphor::logging::commit<InternalFailure>(); |
| 142 | |
| 143 | // Need a coredump in the error cases. |
| 144 | std::terminate(); |
| 145 | } |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 146 | return 0; |
| 147 | } |