blob: bf60c883b029397b744b44ee9e648f2f8f96faa9 [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>
William A. Kennington III93975262018-02-02 16:00:50 -080021#include <string>
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053022#include <xyz/openbmc_project/Common/error.hpp>
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053023#include "argument.hpp"
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053024#include "watchdog.hpp"
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053025
William A. Kennington III1232a152018-02-02 15:57:34 -080026using phosphor::watchdog::ArgumentParser;
27using phosphor::watchdog::Watchdog;
William A. Kennington III93975262018-02-02 16:00:50 -080028using sdbusplus::xyz::openbmc_project::State::server::convertForMessage;
William A. Kennington III1232a152018-02-02 15:57:34 -080029
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053030static void exitWithError(const char* err, char** argv)
31{
William A. Kennington III1232a152018-02-02 15:57:34 -080032 ArgumentParser::usage(argv);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053033 std::cerr << "ERROR: " << err << "\n";
34 exit(EXIT_FAILURE);
35}
36
William A. Kennington III93975262018-02-02 16:00:50 -080037void printActionTargets(
38 const std::map<Watchdog::Action, std::string>& actionTargets)
39{
40 std::cerr << "Action Targets:\n";
41 for (const auto& actionTarget : actionTargets)
42 {
43 std::cerr << " " << convertForMessage(actionTarget.first) << " -> " <<
44 actionTarget.second << "\n";
45 }
46 std::cerr << std::flush;
47}
48
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053049int main(int argc, char** argv)
50{
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053051 using namespace phosphor::logging;
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +053052 using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
53 Error::InternalFailure;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053054 // Read arguments.
William A. Kennington III1232a152018-02-02 15:57:34 -080055 auto options = ArgumentParser(argc, argv);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053056
Patrick Venture09eebe32017-08-11 15:23:17 -070057 // Parse out continue argument.
58 auto continueParam = (options)["continue"];
59 // Default it to exit on watchdog timeout
60 auto continueAfterTimeout = false;
William A. Kennington III5d307182018-01-23 22:00:55 -080061 if (!continueParam.empty())
Patrick Venture09eebe32017-08-11 15:23:17 -070062 {
63 continueAfterTimeout = true;
64 }
65
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053066 // Parse out path argument.
William A. Kennington III5d307182018-01-23 22:00:55 -080067 auto pathParam = (options)["path"];
68 if (pathParam.empty())
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053069 {
70 exitWithError("Path not specified.", argv);
71 }
William A. Kennington III5d307182018-01-23 22:00:55 -080072 if (pathParam.size() > 1)
73 {
74 exitWithError("Multiple paths specified.", argv);
75 }
76 auto path = pathParam.back();
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053077
78 // Parse out service name argument
William A. Kennington III5d307182018-01-23 22:00:55 -080079 auto serviceParam = (options)["service"];
80 if (serviceParam.empty())
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053081 {
82 exitWithError("Service not specified.", argv);
83 }
William A. Kennington III5d307182018-01-23 22:00:55 -080084 if (serviceParam.size() > 1)
85 {
86 exitWithError("Multiple services specified.", argv);
87 }
88 auto service = serviceParam.back();
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053089
90 // Parse out target argument. It is fine if the caller does not
91 // pass this if they are not interested in calling into any target
92 // on meeting a condition.
William A. Kennington III5d307182018-01-23 22:00:55 -080093 auto targetParam = (options)["target"];
94 if (targetParam.size() > 1)
95 {
96 exitWithError("Multiple targets specified.", argv);
97 }
William A. Kennington III1232a152018-02-02 15:57:34 -080098 std::map<Watchdog::Action, Watchdog::TargetName> actionTargets;
99 if (!targetParam.empty()) {
100 auto target = targetParam.back();
101 actionTargets[Watchdog::Action::HardReset] = target;
102 actionTargets[Watchdog::Action::PowerOff] = target;
103 actionTargets[Watchdog::Action::PowerCycle] = target;
104 }
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +0530105
William A. Kennington III93975262018-02-02 16:00:50 -0800106 printActionTargets(actionTargets);
107
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530108 sd_event* event = nullptr;
109 auto r = sd_event_default(&event);
110 if (r < 0)
111 {
112 log<level::ERR>("Error creating a default sd_event handler");
113 return r;
114 }
115 phosphor::watchdog::EventPtr eventP{event};
116 event = nullptr;
117
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530118 // Get a handle to system dbus.
119 auto bus = sdbusplus::bus::new_default();
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530120
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530121 // Add systemd object manager.
122 sdbusplus::server::manager::manager(bus, path.c_str());
123
124 // Attach the bus to sd_event to service user requests
125 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
126
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530127 try
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530128 {
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530129 // Create a watchdog object
William A. Kennington III1232a152018-02-02 15:57:34 -0800130 Watchdog watchdog(bus, path.c_str(), eventP, std::move(actionTargets));
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530131 // Claim the bus
132 bus.request_name(service.c_str());
133
Patrick Venture09eebe32017-08-11 15:23:17 -0700134 // Loop forever processing events
135 while (true)
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530136 {
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530137 // -1 denotes wait for ever
138 r = sd_event_run(eventP.get(), (uint64_t)-1);
139 if (r < 0)
140 {
141 log<level::ERR>("Error waiting for events");
142 elog<InternalFailure>();
143 }
Patrick Venture09eebe32017-08-11 15:23:17 -0700144
145 // The timer expiring is an event that breaks from the above.
146 if (watchdog.timerExpired())
147 {
148 // Either disable the timer or exit.
149 if (continueAfterTimeout)
150 {
151 // The watchdog will be disabled but left running to be
152 // re-enabled.
153 watchdog.enabled(false);
154 }
155 else
156 {
157 // The watchdog daemon will now exit.
158 break;
159 }
160 }
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530161 }
162 }
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530163 catch(InternalFailure& e)
164 {
165 phosphor::logging::commit<InternalFailure>();
166
167 // Need a coredump in the error cases.
168 std::terminate();
169 }
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +0530170 return 0;
171}