blob: 246936336fe053ff2514f059bed8596331a4056a [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
Patrick Venture09eebe32017-08-11 15:23:17 -070040 // Parse out continue argument.
41 auto continueParam = (options)["continue"];
42 // Default it to exit on watchdog timeout
43 auto continueAfterTimeout = false;
44 if (continueParam == phosphor::watchdog::ArgumentParser::trueString)
45 {
46 continueAfterTimeout = true;
47 }
48
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053049 // Parse out path argument.
50 auto path = (options)["path"];
51 if (path == phosphor::watchdog::ArgumentParser::emptyString)
52 {
53 exitWithError("Path not specified.", argv);
54 }
55
56 // Parse out service name argument
57 auto service = (options)["service"];
58 if (service == phosphor::watchdog::ArgumentParser::emptyString)
59 {
60 exitWithError("Service not specified.", argv);
61 }
62
63 // Parse out target argument. It is fine if the caller does not
64 // pass this if they are not interested in calling into any target
65 // on meeting a condition.
66 auto target = (options)["target"];
67
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053068 sd_event* event = nullptr;
69 auto r = sd_event_default(&event);
70 if (r < 0)
71 {
72 log<level::ERR>("Error creating a default sd_event handler");
73 return r;
74 }
75 phosphor::watchdog::EventPtr eventP{event};
76 event = nullptr;
77
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053078 // Get a handle to system dbus.
79 auto bus = sdbusplus::bus::new_default();
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053080
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053081 // Add systemd object manager.
82 sdbusplus::server::manager::manager(bus, path.c_str());
83
84 // Attach the bus to sd_event to service user requests
85 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
86
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053087 try
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053088 {
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053089 // Create a watchdog object
90 phosphor::watchdog::Watchdog watchdog(bus, path.c_str(),
91 eventP, std::move(target));
92 // Claim the bus
93 bus.request_name(service.c_str());
94
Patrick Venture09eebe32017-08-11 15:23:17 -070095 // Loop forever processing events
96 while (true)
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053097 {
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053098 // -1 denotes wait for ever
99 r = sd_event_run(eventP.get(), (uint64_t)-1);
100 if (r < 0)
101 {
102 log<level::ERR>("Error waiting for events");
103 elog<InternalFailure>();
104 }
Patrick Venture09eebe32017-08-11 15:23:17 -0700105
106 // The timer expiring is an event that breaks from the above.
107 if (watchdog.timerExpired())
108 {
109 // Either disable the timer or exit.
110 if (continueAfterTimeout)
111 {
112 // The watchdog will be disabled but left running to be
113 // re-enabled.
114 watchdog.enabled(false);
115 }
116 else
117 {
118 // The watchdog daemon will now exit.
119 break;
120 }
121 }
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530122 }
123 }
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530124 catch(InternalFailure& e)
125 {
126 phosphor::logging::commit<InternalFailure>();
127
128 // Need a coredump in the error cases.
129 std::terminate();
130 }
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +0530131 return 0;
132}