pldmtool: Exit non-zero on request failures exec()'s three failure paths (instance ID allocation, createRequestMsg(), pldmSendRecv()) now report failure through the CLI framework's exit-code mechanism instead of an uncaught exception or a plain return. Motivation: pldmtool surfaces the instance ID allocation failure as an exception rather than silently discarding it. Nothing above exec() catches that exception, so a real, expected failure (instance IDs running out under normal contention, not misuse) crashes the whole process instead of exiting cleanly. The other two paths avoid that crash, but log an error and still return successfully as far as the caller can tell, so a script checking the exit status cannot distinguish a failed request from a successful one. Tested: Forced each of the three failure paths above on Yosemite4. Instance ID pool forced empty, before: ``` $ pldmtool base GetTID -m 30 <3> Instance ID allocation failed for EID 30: Failed to allocate instance ID for EID 30: No free instance ids terminate called after throwing an instance of 'pldm::InstanceIdError' what(): Failed to allocate instance ID for EID 30: No free instance ids Aborted (core dumped) $ echo $? 134 ``` after: ``` $ pldmtool base GetTID -m 30 <3> Instance ID allocation failed for EID 30: Failed to allocate instance ID for EID 30: No free instance ids $ echo $? 1 ``` createRequestMsg() forced to return an encode-validation error, before: ``` $ pldmtool base GetTID -m 30 Failed to encode request message for base:GetTID rc = 2 $ echo $? 0 ``` after: ``` $ pldmtool base GetTID -m 30 Failed to encode request message for base:GetTID rc = 2 $ echo $? 1 ``` pldmSendRecv() forced to return a transport send error, before: ``` $ pldmtool base GetTID -m 30 pldmSendRecv: Failed to receive RC = -7 $ echo $? 0 ``` after: ``` $ pldmtool base GetTID -m 30 pldmSendRecv: Failed to receive RC = -7 $ echo $? 1 ``` Change-Id: Ifbc0235b431e50df104322d3d5d156df383a8aa2 Signed-off-by: Eric Yang <eric.yang.wiwynn@gmail.com>
diff --git a/pldmtool/pldm_cmd_helper.cpp b/pldmtool/pldm_cmd_helper.cpp index 36b1ee2..5c11b90 100644 --- a/pldmtool/pldm_cmd_helper.cpp +++ b/pldmtool/pldm_cmd_helper.cpp
@@ -90,7 +90,9 @@ auto instanceIdResult = instanceIdDb.next(mctp_eid); if (!instanceIdResult) { - throw pldm::InstanceIdError(instanceIdResult.error()); + // InstanceIdDb::next() already lg2::error()s this failure; avoid + // printing it a second time via std::cerr. + throw CLI::RuntimeError(1); } instanceId = instanceIdResult.value(); auto [rc, requestMsg] = createRequestMsg(); @@ -99,7 +101,7 @@ instanceIdDb.free(mctp_eid, instanceId); std::cerr << "Failed to encode request message for " << pldmType << ":" << commandName << " rc = " << rc << "\n"; - return; + throw CLI::RuntimeError(1); } std::vector<uint8_t> responseMsg; @@ -109,7 +111,7 @@ { instanceIdDb.free(mctp_eid, instanceId); std::cerr << "pldmSendRecv: Failed to receive RC = " << rc << "\n"; - return; + throw CLI::RuntimeError(1); } auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg.data());