mainapp: Terminate cleanly for TERM and INT

This makes it possible to instrument the application for memory leaks
and other required sanitization at termination.

Change-Id: If83bf7b5151231e8db35bf2e2f2d5ddc74b9627d
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/mainapp.cpp b/src/mainapp.cpp
index 63ae7e2..26a4f39 100644
--- a/src/mainapp.cpp
+++ b/src/mainapp.cpp
@@ -27,6 +27,8 @@
 #include <sdbusplus/exception.hpp>
 #include <sdbusplus/server/manager.hpp>
 #include <sdeventplus/event.hpp>
+#include <sdeventplus/source/signal.hpp>
+#include <stdplus/signal.hpp>
 #include <string>
 #include <xyz/openbmc_project/Common/error.hpp>
 
@@ -246,8 +248,16 @@
         // Claim the bus
         bus.request_name(service.c_str());
 
+        bool done = false;
+        auto intCb = [&](sdeventplus::source::Signal&,
+                         const struct signalfd_siginfo*) { done = true; };
+        stdplus::signal::block(SIGINT);
+        sdeventplus::source::Signal sigint(event, SIGINT, intCb);
+        stdplus::signal::block(SIGTERM);
+        sdeventplus::source::Signal sigterm(event, SIGTERM, std::move(intCb));
+
         // Loop until our timer expires and we don't want to continue
-        while (continueAfterTimeout || !watchdog.timerExpired())
+        while (!done && (continueAfterTimeout || !watchdog.timerExpired()))
         {
             // Run and never timeout
             event.run(std::nullopt);