blob: 43328c31e13c6c2d080ad0cce9c740c6aa729275 [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 Subbanna4d5ef3f2017-05-31 18:54:22 +053019#include <phosphor-logging/elog.hpp>
20#include <phosphor-logging/elog-errors.hpp>
21#include <xyz/openbmc_project/Common/error.hpp>
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053022#include "argument.hpp"
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053023#include "watchdog.hpp"
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053024
25static 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
32int main(int argc, char** argv)
33{
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053034 using namespace phosphor::logging;
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053035 using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
36 Error::InternalFailure;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053037 // Read arguments.
38 auto options = phosphor::watchdog::ArgumentParser(argc, argv);
39
40 // Parse out path argument.
41 auto path = (options)["path"];
42 if (path == phosphor::watchdog::ArgumentParser::emptyString)
43 {
44 exitWithError("Path not specified.", argv);
45 }
46
47 // Parse out service name argument
48 auto service = (options)["service"];
49 if (service == phosphor::watchdog::ArgumentParser::emptyString)
50 {
51 exitWithError("Service not specified.", argv);
52 }
53
54 // Parse out target argument. It is fine if the caller does not
55 // pass this if they are not interested in calling into any target
56 // on meeting a condition.
57 auto target = (options)["target"];
58
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053059 sd_event* event = nullptr;
60 auto r = sd_event_default(&event);
61 if (r < 0)
62 {
63 log<level::ERR>("Error creating a default sd_event handler");
64 return r;
65 }
66 phosphor::watchdog::EventPtr eventP{event};
67 event = nullptr;
68
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053069 // Get a handle to system dbus.
70 auto bus = sdbusplus::bus::new_default();
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053071
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053072 // Add systemd object manager.
73 sdbusplus::server::manager::manager(bus, path.c_str());
74
75 // Attach the bus to sd_event to service user requests
76 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
77
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053078 try
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053079 {
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053080 // Create a watchdog object
81 phosphor::watchdog::Watchdog watchdog(bus, path.c_str(),
82 eventP, std::move(target));
83 // Claim the bus
84 bus.request_name(service.c_str());
85
86 // Wait until the timer has expired
87 while(!watchdog.timerExpired())
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053088 {
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053089 // -1 denotes wait for ever
90 r = sd_event_run(eventP.get(), (uint64_t)-1);
91 if (r < 0)
92 {
93 log<level::ERR>("Error waiting for events");
94 elog<InternalFailure>();
95 }
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053096 }
97 }
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053098 catch(InternalFailure& e)
99 {
100 phosphor::logging::commit<InternalFailure>();
101
102 // Need a coredump in the error cases.
103 std::terminate();
104 }
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +0530105 return 0;
106}