blob: 845e291a982bc3fc7067778a25881d5127e64bd0 [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 III27df4b52018-02-02 16:02:05 -0800106 // Parse out the action_target arguments. We allow one target to map
107 // to an action. These targets can replace the target specified above.
108 for (const auto& actionTarget : (options)["action_target"])
109 {
110 size_t keyValueSplit = actionTarget.find("=");
111 if (keyValueSplit == std::string::npos)
112 {
113 exitWithError(
114 "Invalid action_target format, expect <action>=<target>.",
115 argv);
116 }
117
118 std::string key = actionTarget.substr(0, keyValueSplit);
119 std::string value = actionTarget.substr(keyValueSplit+1);
120
121 // Convert an action from a fully namespaced value
122 Watchdog::Action action;
123 try
124 {
125 action = Watchdog::convertActionFromString(key);
126 }
127 catch (const sdbusplus::exception::InvalidEnumString &)
128 {
129 exitWithError("Bad action specified.", argv);
130 }
131
132 actionTargets[action] = std::move(value);
133 }
William A. Kennington III93975262018-02-02 16:00:50 -0800134 printActionTargets(actionTargets);
135
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530136 sd_event* event = nullptr;
137 auto r = sd_event_default(&event);
138 if (r < 0)
139 {
140 log<level::ERR>("Error creating a default sd_event handler");
141 return r;
142 }
143 phosphor::watchdog::EventPtr eventP{event};
144 event = nullptr;
145
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530146 // Get a handle to system dbus.
147 auto bus = sdbusplus::bus::new_default();
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530148
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530149 // Add systemd object manager.
150 sdbusplus::server::manager::manager(bus, path.c_str());
151
152 // Attach the bus to sd_event to service user requests
153 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
154
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530155 try
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530156 {
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530157 // Create a watchdog object
William A. Kennington III1232a152018-02-02 15:57:34 -0800158 Watchdog watchdog(bus, path.c_str(), eventP, std::move(actionTargets));
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530159 // Claim the bus
160 bus.request_name(service.c_str());
161
Patrick Venture09eebe32017-08-11 15:23:17 -0700162 // Loop forever processing events
163 while (true)
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530164 {
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530165 // -1 denotes wait for ever
166 r = sd_event_run(eventP.get(), (uint64_t)-1);
167 if (r < 0)
168 {
169 log<level::ERR>("Error waiting for events");
170 elog<InternalFailure>();
171 }
Patrick Venture09eebe32017-08-11 15:23:17 -0700172
173 // The timer expiring is an event that breaks from the above.
174 if (watchdog.timerExpired())
175 {
176 // Either disable the timer or exit.
177 if (continueAfterTimeout)
178 {
179 // The watchdog will be disabled but left running to be
180 // re-enabled.
181 watchdog.enabled(false);
182 }
183 else
184 {
185 // The watchdog daemon will now exit.
186 break;
187 }
188 }
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530189 }
190 }
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530191 catch(InternalFailure& e)
192 {
193 phosphor::logging::commit<InternalFailure>();
194
195 // Need a coredump in the error cases.
196 std::terminate();
197 }
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +0530198 return 0;
199}