blob: b9e61219982283e80194da1e12adeb3600ed6ba9 [file] [log] [blame]
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +05301/**
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 Subbanna7e146552017-05-29 17:03:33 +053018#include <phosphor-logging/log.hpp>
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053019#include "argument.hpp"
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053020#include "watchdog.hpp"
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053021
22static 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
29int main(int argc, char** argv)
30{
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053031 using namespace phosphor::logging;
32
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053033 // 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 Subbanna7e146552017-05-29 17:03:33 +053055 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 Subbannad7a3f132017-05-29 19:39:08 +053065 // Get a handle to system dbus.
66 auto bus = sdbusplus::bus::new_default();
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053067
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053068 // 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 Subbanna7e146552017-05-29 17:03:33 +053082 {
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 Subbanna15b1dc12017-05-23 15:16:13 +053091 return 0;
92}