diff options
author | Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com> | 2017-05-31 18:54:22 +0530 |
---|---|---|
committer | Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com> | 2017-06-08 12:50:48 +0530 |
commit | 4d5ef3f92ecec91ca92deec1da2984917a4672bb (patch) | |
tree | a7aab486c95ada6cf580348f55c124c58147a33e | |
parent | 00bd377e612a646b43e0186f84e1343f160daa0f (diff) | |
download | phosphor-watchdog-4d5ef3f92ecec91ca92deec1da2984917a4672bb.tar.gz phosphor-watchdog-4d5ef3f92ecec91ca92deec1da2984917a4672bb.zip |
Commit error and exit the application on error scenarios
Typically sd_event* calls can not go wrong and if they do, better
would be to terminate the application so we get a coredump.
Change-Id: I899177c369332cabf6d9bc22daaa142b50d758d0
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | mainapp.cpp | 43 | ||||
-rw-r--r-- | test/Makefile.am | 7 | ||||
-rw-r--r-- | timer.cpp | 36 |
4 files changed, 56 insertions, 31 deletions
diff --git a/Makefile.am b/Makefile.am index 2e9a4f1..806a0aa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,5 +18,4 @@ phosphor_watchdog_CXXFLAGS = $(SYSTEMD_CFLAGS)\ ${PHOSPHOR_LOGGING_CFLAGS} \ ${SDBUSPLUS_CFLAGS} \ ${PHOSPHOR_DBUS_INTERFACES_CFLAGS} - SUBDIRS = test diff --git a/mainapp.cpp b/mainapp.cpp index 7751aa9..43328c3 100644 --- a/mainapp.cpp +++ b/mainapp.cpp @@ -16,6 +16,9 @@ #include <iostream> #include <phosphor-logging/log.hpp> +#include <phosphor-logging/elog.hpp> +#include <phosphor-logging/elog-errors.hpp> +#include <xyz/openbmc_project/Common/error.hpp> #include "argument.hpp" #include "watchdog.hpp" @@ -29,7 +32,8 @@ static void exitWithError(const char* err, char** argv) int main(int argc, char** argv) { using namespace phosphor::logging; - + using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: + Error::InternalFailure; // Read arguments. auto options = phosphor::watchdog::ArgumentParser(argc, argv); @@ -71,23 +75,32 @@ int main(int argc, char** argv) // Attach the bus to sd_event to service user requests bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); - // Create a watchdog object - phosphor::watchdog::Watchdog watchdog(bus, path.c_str(), - eventP, std::move(target)); - - // Claim the bus - bus.request_name(service.c_str()); - - // Wait until the timer has expired - while(!watchdog.timerExpired()) + try { - // -1 denotes wait for ever - r = sd_event_run(eventP.get(), (uint64_t)-1); - if (r < 0) + // Create a watchdog object + phosphor::watchdog::Watchdog watchdog(bus, path.c_str(), + eventP, std::move(target)); + // Claim the bus + bus.request_name(service.c_str()); + + // Wait until the timer has expired + while(!watchdog.timerExpired()) { - log<level::ERR>("Error waiting for events"); - return -1; + // -1 denotes wait for ever + r = sd_event_run(eventP.get(), (uint64_t)-1); + if (r < 0) + { + log<level::ERR>("Error waiting for events"); + elog<InternalFailure>(); + } } } + catch(InternalFailure& e) + { + phosphor::logging::commit<InternalFailure>(); + + // Need a coredump in the error cases. + std::terminate(); + } return 0; } diff --git a/test/Makefile.am b/test/Makefile.am index 654f0f1..259e357 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -34,6 +34,7 @@ watchdog_test_LDFLAGS = ${utestLDFLAGS} timer_test_SOURCES = timer_test.cpp watchdog_test_SOURCES = watchdog_test.cpp -timer_test_LDADD = $(top_builddir)/timer.o -watchdog_test_LDADD = $(top_builddir)/timer.o \ - $(top_builddir)/watchdog.o +timer_test_LDADD = $(top_builddir)/timer.o + +watchdog_test_LDADD = $(top_builddir)/watchdog.o \ + $(top_builddir)/timer.o @@ -1,20 +1,28 @@ #include <chrono> #include <systemd/sd-event.h> #include <phosphor-logging/log.hpp> +#include <phosphor-logging/elog.hpp> +#include <phosphor-logging/elog-errors.hpp> +#include <xyz/openbmc_project/Common/error.hpp> #include "timer.hpp" namespace phosphor { namespace watchdog { +// For throwing exception +using namespace phosphor::logging; +using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: + Error::InternalFailure; + // Initializes the timer object void Timer::initialize() { // This can not be called more than once. if (eventSource.get()) { - // TODO: Need to throw elog exception stating its already added. - throw std::runtime_error("Timer already initialized"); + log<level::ERR>("Timer already initialized"); + elog<InternalFailure>(); } // Add infinite expiration time @@ -30,8 +38,8 @@ void Timer::initialize() if (r < 0) { - // TODO: throw elog exception - throw std::runtime_error("Timer initialization failed"); + log<level::ERR>("Timer initialization failed"); + elog<InternalFailure>(); } // Disable the timer for now @@ -68,6 +76,8 @@ std::chrono::microseconds Timer::getCurrentTime() // Sets the expiration time and arms the timer void Timer::start(std::chrono::microseconds usec) { + using namespace std::chrono; + // Get the current MONOTONIC time and add the delta auto expireTime = getCurrentTime() + usec; @@ -76,8 +86,9 @@ void Timer::start(std::chrono::microseconds usec) expireTime.count()); if (r < 0) { - // TODO throw elog exception - throw std::runtime_error("Error setting the expiration time"); + log<level::ERR>("Error setting the expiration time", + entry("MSEC=%llu",duration_cast<milliseconds>(usec).count())); + elog<InternalFailure>(); } } @@ -88,8 +99,8 @@ int Timer::getEnabled() const auto r = sd_event_source_get_enabled(eventSource.get(), &enabled); if (r < 0) { - // TODO: Need to throw elog exception - throw std::runtime_error("Error geting current time enablement state"); + log<level::ERR>("Error geting current timer type enablement state"); + elog<InternalFailure>(); } return enabled; } @@ -100,8 +111,9 @@ void Timer::setEnabled(int type) auto r = sd_event_source_set_enabled(eventSource.get(), type); if (r < 0) { - // TODO: Need to throw elog exception - throw std::runtime_error("Error altering enabled property"); + log<level::ERR>("Error setting the timer type", + entry("TYPE=%d",type)); + elog<InternalFailure>(); } } @@ -112,8 +124,8 @@ std::chrono::microseconds Timer::getRemaining() const auto r = sd_event_source_get_time(eventSource.get(), &next); if (r < 0) { - // TODO: Need to throw elog exception - throw std::runtime_error("Error altering enabled property"); + log<level::ERR>("Error fetching remaining time to expire"); + elog<InternalFailure>(); } return std::chrono::microseconds(next); } |