blob: b8c84c94d7f2154dc07b000dc91cdc20ed774561 [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
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053017#include "watchdog.hpp"
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053018
William A. Kennington III1eb97d92018-09-13 00:36:12 -070019#include <CLI/CLI.hpp>
Patrick Venture8f6c5152018-09-11 17:45:33 -070020#include <iostream>
William A. Kennington III73c2cfb2018-09-12 18:01:37 -070021#include <optional>
Patrick Venture8f6c5152018-09-11 17:45:33 -070022#include <phosphor-logging/elog-errors.hpp>
23#include <phosphor-logging/elog.hpp>
24#include <phosphor-logging/log.hpp>
William A. Kennington III8d8bc462019-01-16 14:59:07 -080025#include <sdbusplus/bus.hpp>
26#include <sdbusplus/exception.hpp>
27#include <sdbusplus/server/manager.hpp>
28#include <sdeventplus/event.hpp>
Patrick Venture8f6c5152018-09-11 17:45:33 -070029#include <string>
30#include <xyz/openbmc_project/Common/error.hpp>
31
William A. Kennington III1232a152018-02-02 15:57:34 -080032using phosphor::watchdog::Watchdog;
William A. Kennington III93975262018-02-02 16:00:50 -080033using sdbusplus::xyz::openbmc_project::State::server::convertForMessage;
William A. Kennington III1232a152018-02-02 15:57:34 -080034
William A. Kennington III3bb2f402018-09-13 00:35:47 -070035void printActionTargetMap(const Watchdog::ActionTargetMap& actionTargetMap)
William A. Kennington III93975262018-02-02 16:00:50 -080036{
37 std::cerr << "Action Targets:\n";
William A. Kennington III3bb2f402018-09-13 00:35:47 -070038 for (const auto& [action, target] : actionTargetMap)
William A. Kennington III93975262018-02-02 16:00:50 -080039 {
William A. Kennington III3bb2f402018-09-13 00:35:47 -070040 std::cerr << " " << convertForMessage(action) << " -> " << target
41 << "\n";
William A. Kennington III93975262018-02-02 16:00:50 -080042 }
43 std::cerr << std::flush;
44}
45
William A. Kennington III1eb97d92018-09-13 00:36:12 -070046void printFallback(const Watchdog::Fallback& fallback)
47{
48 std::cerr << "Fallback Options:\n";
49 std::cerr << " Action: " << convertForMessage(fallback.action) << "\n";
50 std::cerr << " Interval(ms): " << std::dec << fallback.interval << "\n";
51 std::cerr << " Always re-execute: " << std::boolalpha << fallback.always
52 << "\n";
53 std::cerr << std::flush;
54}
55
William A. Kennington III3bb2f402018-09-13 00:35:47 -070056int main(int argc, char* argv[])
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053057{
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053058 using namespace phosphor::logging;
Patrick Venture8f6c5152018-09-11 17:45:33 -070059 using InternalFailure =
60 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053061
William A. Kennington III1eb97d92018-09-13 00:36:12 -070062 CLI::App app{"Canonical openbmc host watchdog daemon"};
Patrick Venture09eebe32017-08-11 15:23:17 -070063
William A. Kennington III1eb97d92018-09-13 00:36:12 -070064 // Service related options
65 const std::string serviceGroup = "Service Options";
66 std::string path;
67 app.add_option("-p,--path", path,
68 "DBus Object Path. "
69 "Ex: /xyz/openbmc_project/state/watchdog/host0")
70 ->required()
71 ->group(serviceGroup);
72 std::string service;
73 app.add_option("-s,--service", service,
74 "DBus Service Name. "
75 "Ex: xyz.openbmc_project.State.Watchdog.Host")
76 ->required()
77 ->group(serviceGroup);
78 bool continueAfterTimeout;
79 app.add_flag("-c,--continue", continueAfterTimeout,
80 "Continue daemon after watchdog timeout")
81 ->group(serviceGroup);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053082
William A. Kennington III1eb97d92018-09-13 00:36:12 -070083 // Target related options
84 const std::string targetGroup = "Target Options";
85 std::optional<std::string> target;
86 app.add_option("-t,--target", target,
87 "Systemd unit to be called on "
88 "timeout for all actions but NONE. "
89 "Deprecated, use --action_target instead.")
90 ->group(targetGroup);
91 std::vector<std::string> actionTargets;
92 app.add_option("-a,--action_target", actionTargets,
93 "Map of action to "
94 "systemd unit to be called on timeout if that action is "
95 "set for ExpireAction when the timer expires.")
96 ->group(targetGroup);
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053097
William A. Kennington III1eb97d92018-09-13 00:36:12 -070098 // Fallback related options
99 const std::string fallbackGroup = "Fallback Options";
100 std::optional<std::string> fallbackAction;
101 auto fallbackActionOpt =
102 app.add_option("-f,--fallback_action", fallbackAction,
103 "Enables the "
104 "watchdog even when disabled via the dbus interface. "
105 "Perform this action when the fallback expires.")
106 ->group(fallbackGroup);
107 std::optional<unsigned> fallbackIntervalMs;
108 auto fallbackIntervalOpt =
109 app.add_option("-i,--fallback_interval", fallbackIntervalMs,
110 "Enables the "
111 "watchdog even when disabled via the dbus interface. "
112 "Waits for this interval before performing the fallback "
113 "action.")
114 ->group(fallbackGroup);
115 fallbackIntervalOpt->needs(fallbackActionOpt);
116 fallbackActionOpt->needs(fallbackIntervalOpt);
117 bool fallbackAlways;
118 app.add_flag("-e,--fallback_always", fallbackAlways,
119 "Enables the "
120 "watchdog even when disabled by the dbus interface. "
121 "This option is only valid with a fallback specified")
122 ->group(fallbackGroup)
123 ->needs(fallbackActionOpt)
124 ->needs(fallbackIntervalOpt);
125
126 CLI11_PARSE(app, argc, argv);
127
128 // Put together a list of actions and associated systemd targets
129 // The new --action_target options take precedence over the legacy
130 // --target
William A. Kennington III3bb2f402018-09-13 00:35:47 -0700131 Watchdog::ActionTargetMap actionTargetMap;
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700132 if (target)
Patrick Venture8f6c5152018-09-11 17:45:33 -0700133 {
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700134 actionTargetMap[Watchdog::Action::HardReset] = *target;
135 actionTargetMap[Watchdog::Action::PowerOff] = *target;
136 actionTargetMap[Watchdog::Action::PowerCycle] = *target;
William A. Kennington III1232a152018-02-02 15:57:34 -0800137 }
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700138 for (const auto& actionTarget : actionTargets)
William A. Kennington III27df4b52018-02-02 16:02:05 -0800139 {
140 size_t keyValueSplit = actionTarget.find("=");
141 if (keyValueSplit == std::string::npos)
142 {
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700143 std::cerr << "Invalid action_target format, "
144 "expect <action>=<target>."
145 << std::endl;
146 return 1;
William A. Kennington III27df4b52018-02-02 16:02:05 -0800147 }
148
149 std::string key = actionTarget.substr(0, keyValueSplit);
Patrick Venture8f6c5152018-09-11 17:45:33 -0700150 std::string value = actionTarget.substr(keyValueSplit + 1);
William A. Kennington III27df4b52018-02-02 16:02:05 -0800151
152 // Convert an action from a fully namespaced value
153 Watchdog::Action action;
154 try
155 {
156 action = Watchdog::convertActionFromString(key);
157 }
Patrick Venture8f6c5152018-09-11 17:45:33 -0700158 catch (const sdbusplus::exception::InvalidEnumString&)
William A. Kennington III27df4b52018-02-02 16:02:05 -0800159 {
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700160 std::cerr << "Bad action specified: " << key << std::endl;
161 return 1;
William A. Kennington III27df4b52018-02-02 16:02:05 -0800162 }
163
William A. Kennington IIIc2c26ce2018-09-13 18:35:56 -0700164 // Detect duplicate action target arguments
165 if (actionTargetMap.find(action) != actionTargetMap.end())
166 {
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700167 std::cerr << "Got duplicate action: " << key << std::endl;
168 return 1;
William A. Kennington IIIc2c26ce2018-09-13 18:35:56 -0700169 }
170
William A. Kennington III3bb2f402018-09-13 00:35:47 -0700171 actionTargetMap[action] = std::move(value);
William A. Kennington III27df4b52018-02-02 16:02:05 -0800172 }
William A. Kennington III3bb2f402018-09-13 00:35:47 -0700173 printActionTargetMap(actionTargetMap);
William A. Kennington III93975262018-02-02 16:00:50 -0800174
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700175 // Build the fallback option used for the Watchdog
176 std::optional<Watchdog::Fallback> maybeFallback;
177 if (fallbackAction)
William A. Kennington IIId1331082018-02-27 18:47:05 -0800178 {
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700179 Watchdog::Fallback fallback;
William A. Kennington IIId1331082018-02-27 18:47:05 -0800180 try
181 {
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700182 fallback.action =
183 Watchdog::convertActionFromString(*fallbackAction);
William A. Kennington IIId1331082018-02-27 18:47:05 -0800184 }
Patrick Venture8f6c5152018-09-11 17:45:33 -0700185 catch (const sdbusplus::exception::InvalidEnumString&)
William A. Kennington IIId1331082018-02-27 18:47:05 -0800186 {
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700187 std::cerr << "Bad fallback action specified: " << *fallbackAction
188 << std::endl;
189 return 1;
William A. Kennington IIId1331082018-02-27 18:47:05 -0800190 }
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700191 fallback.interval = *fallbackIntervalMs;
192 fallback.always = fallbackAlways;
William A. Kennington IIId1331082018-02-27 18:47:05 -0800193
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700194 printFallback(fallback);
195 maybeFallback = std::move(fallback);
William A. Kennington III22352192018-02-27 18:51:44 -0800196 }
197
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530198 try
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530199 {
William A. Kennington IIIf505fc02018-09-12 18:30:09 -0700200 // Get a default event loop
201 auto event = sdeventplus::Event::get_default();
202
203 // Get a handle to system dbus.
204 auto bus = sdbusplus::bus::new_default();
205
206 // Add systemd object manager.
207 sdbusplus::server::manager::manager(bus, path.c_str());
208
209 // Attach the bus to sd_event to service user requests
210 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
211
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530212 // Create a watchdog object
William A. Kennington IIIf505fc02018-09-12 18:30:09 -0700213 Watchdog watchdog(bus, path.c_str(), event, std::move(actionTargetMap),
William A. Kennington III1eb97d92018-09-13 00:36:12 -0700214 std::move(maybeFallback));
William A. Kennington IIId1331082018-02-27 18:47:05 -0800215
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530216 // Claim the bus
217 bus.request_name(service.c_str());
218
William A. Kennington III825f4982018-02-27 19:10:56 -0800219 // Loop until our timer expires and we don't want to continue
220 while (continueAfterTimeout || !watchdog.timerExpired())
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530221 {
William A. Kennington IIIf505fc02018-09-12 18:30:09 -0700222 // Run and never timeout
223 event.run(std::nullopt);
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +0530224 }
225 }
Patrick Venture8f6c5152018-09-11 17:45:33 -0700226 catch (InternalFailure& e)
Vishwanatha Subbanna4d5ef3f2017-05-31 18:54:22 +0530227 {
228 phosphor::logging::commit<InternalFailure>();
229
230 // Need a coredump in the error cases.
231 std::terminate();
232 }
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +0530233 return 0;
234}