chassis-state-manager: Clean up sdbusplus exception handling
In order to support error handling for message operations, we need to
make sure we are catching exceptions thrown by sdbusplus.
Tested:
Built / ran on a fully tray pulled zaius. Correctly detected the
host was off and did the automatic transition to boot the machine
at BMC start. Chassis power commands still work as expected and
show up as state transitions.
Change-Id: I3468209b215df6535bb5357a62335da151d81897
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/chassis_state_manager.cpp b/chassis_state_manager.cpp
index d78a6f7..f333254 100644
--- a/chassis_state_manager.cpp
+++ b/chassis_state_manager.cpp
@@ -1,4 +1,5 @@
#include <sdbusplus/bus.hpp>
+#include <sdbusplus/exception.hpp>
#include <phosphor-logging/log.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include "xyz/openbmc_project/Common/error.hpp"
@@ -22,6 +23,7 @@
namespace server = sdbusplus::xyz::openbmc_project::State::server;
using namespace phosphor::logging;
+using sdbusplus::exception::SdBusError;
constexpr auto CHASSIS_STATE_POWEROFF_TGT = "obmc-chassis-poweroff@0.target";
constexpr auto CHASSIS_STATE_HARD_POWEROFF_TGT =
@@ -66,7 +68,23 @@
method.append("org.openbmc.control.Power", "pgood");
auto reply = this->bus.call(method);
- reply.read(pgood);
+ if (reply.is_method_error())
+ {
+ log<level::ERR>("Error in bus call - could not get initial pgood");
+ goto fail;
+ }
+
+ try
+ {
+ reply.read(pgood);
+ }
+ catch (const SdBusError& e)
+ {
+ log<level::ERR>("Error in bus response - bad encoding of pgood",
+ entry("ERROR=%s", e.what()),
+ entry("REPLY_SIG=%s", reply.get_signature()));
+ goto fail;
+ }
if (pgood == 1)
{
@@ -75,15 +93,15 @@
convertForMessage(PowerState::On).c_str()));
server::Chassis::currentPowerState(PowerState::On);
server::Chassis::requestedPowerTransition(Transition::On);
+ return;
}
- else
- {
- log<level::INFO>("Initial Chassis State will be Off",
- entry("CHASSIS_CURRENT_POWER_STATE=%s",
- convertForMessage(PowerState::Off).c_str()));
- server::Chassis::currentPowerState(PowerState::Off);
- server::Chassis::requestedPowerTransition(Transition::Off);
- }
+
+fail:
+ log<level::INFO>("Initial Chassis State will be Off",
+ entry("CHASSIS_CURRENT_POWER_STATE=%s",
+ convertForMessage(PowerState::Off).c_str()));
+ server::Chassis::currentPowerState(PowerState::Off);
+ server::Chassis::requestedPowerTransition(Transition::Off);
return;
}
@@ -122,7 +140,17 @@
return false;
}
- result.read(unitTargetPath);
+ try
+ {
+ result.read(unitTargetPath);
+ }
+ catch (const SdBusError& e)
+ {
+ log<level::ERR>("Error in bus response - bad encoding for GetUnit",
+ entry("ERROR=%s", e.what()),
+ entry("REPLY_SIG=%s", result.get_signature()));
+ return false;
+ }
method = this->bus.new_method_call(
SYSTEMD_SERVICE,
@@ -159,7 +187,17 @@
std::string newStateResult{};
// Read the msg and populate each variable
- msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
+ try
+ {
+ msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
+ }
+ catch (const SdBusError& e)
+ {
+ log<level::ERR>("Error in state change - bad encoding",
+ entry("ERROR=%s", e.what()),
+ entry("REPLY_SIG=%s", msg.get_signature()));
+ return 0;
+ }
if ((newStateUnit == CHASSIS_STATE_POWEROFF_TGT) &&
(newStateResult == "done") && (!stateActive(CHASSIS_STATE_POWERON_TGT)))