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 | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 19 | #include "argument.hpp" |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame^] | 20 | #include "watchdog.hpp" |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 21 | |
| 22 | static void exitWithError(const char* err, char** argv) |
| 23 | { |
| 24 | phosphor::watchdog::ArgumentParser::usage(argv); |
| 25 | std::cerr << "ERROR: " << err << "\n"; |
| 26 | exit(EXIT_FAILURE); |
| 27 | } |
| 28 | |
| 29 | int main(int argc, char** argv) |
| 30 | { |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 31 | using namespace phosphor::logging; |
| 32 | |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 33 | // Read arguments. |
| 34 | auto options = phosphor::watchdog::ArgumentParser(argc, argv); |
| 35 | |
| 36 | // Parse out path argument. |
| 37 | auto path = (options)["path"]; |
| 38 | if (path == phosphor::watchdog::ArgumentParser::emptyString) |
| 39 | { |
| 40 | exitWithError("Path not specified.", argv); |
| 41 | } |
| 42 | |
| 43 | // Parse out service name argument |
| 44 | auto service = (options)["service"]; |
| 45 | if (service == phosphor::watchdog::ArgumentParser::emptyString) |
| 46 | { |
| 47 | exitWithError("Service not specified.", argv); |
| 48 | } |
| 49 | |
| 50 | // Parse out target argument. It is fine if the caller does not |
| 51 | // pass this if they are not interested in calling into any target |
| 52 | // on meeting a condition. |
| 53 | auto target = (options)["target"]; |
| 54 | |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 55 | sd_event* event = nullptr; |
| 56 | auto r = sd_event_default(&event); |
| 57 | if (r < 0) |
| 58 | { |
| 59 | log<level::ERR>("Error creating a default sd_event handler"); |
| 60 | return r; |
| 61 | } |
| 62 | phosphor::watchdog::EventPtr eventP{event}; |
| 63 | event = nullptr; |
| 64 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame^] | 65 | // Get a handle to system dbus. |
| 66 | auto bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 67 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame^] | 68 | // Add systemd object manager. |
| 69 | sdbusplus::server::manager::manager(bus, path.c_str()); |
| 70 | |
| 71 | // Attach the bus to sd_event to service user requests |
| 72 | bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); |
| 73 | |
| 74 | // Create a watchdog object |
| 75 | phosphor::watchdog::Watchdog watchdog(bus, path.c_str(), eventP); |
| 76 | |
| 77 | // Claim the bus |
| 78 | bus.request_name(service.c_str()); |
| 79 | |
| 80 | // Wait until the timer has expired |
| 81 | while(!watchdog.timerExpired()) |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 82 | { |
| 83 | // -1 denotes wait for ever |
| 84 | r = sd_event_run(eventP.get(), (uint64_t)-1); |
| 85 | if (r < 0) |
| 86 | { |
| 87 | log<level::ERR>("Error waiting for events"); |
| 88 | return -1; |
| 89 | } |
| 90 | } |
Vishwanatha Subbanna | 15b1dc1 | 2017-05-23 15:16:13 +0530 | [diff] [blame] | 91 | return 0; |
| 92 | } |