netipmid: move event loop to boost::asio::io_context

Replacing the event loop with asio provides for more flexibility and
less code than the sd_event model. Intially, this will require the loop
to handle both sd_events with a wrapper, but after all the sd_event
sources are replaced with asio event sources the wrapper can be removed.

Change-Id: Icf020c6c26a214bb1239641733c89603501c0c49
Signed-off-by: Vernon Mauery <vernon.mauery@linux.intel.com>
diff --git a/sd_event_loop.cpp b/sd_event_loop.cpp
index aa5224a..c6ad063 100644
--- a/sd_event_loop.cpp
+++ b/sd_event_loop.cpp
@@ -8,7 +8,9 @@
 #include <sys/socket.h>
 #include <systemd/sd-daemon.h>
 
+#include <boost/asio/io_context.hpp>
 #include <phosphor-logging/log.hpp>
+#include <sdbusplus/asio/sd_event.hpp>
 
 namespace eventloop
 {
@@ -170,35 +172,29 @@
     return 0;
 }
 
-int EventLoop::startEventLoop(sd_event* events)
+int EventLoop::startEventLoop()
 {
     int fd = -1;
     int r = 0;
     int listen_fd;
     sigset_t ss;
     sd_event_source* source = nullptr;
-    auto bus = ipmid_get_sd_bus_connection();
 
-    event = events;
-    // Attach the bus to sd_event to service user requests
-    r = sd_bus_attach_event(bus, event, SD_EVENT_PRIORITY_NORMAL);
-    if (r < 0)
-    {
-        goto finish;
-    }
+    sdbusplus::asio::sd_event_wrapper sdEvents(*io);
+    event = sdEvents.get();
 
     if (sigemptyset(&ss) < 0 || sigaddset(&ss, SIGTERM) < 0 ||
         sigaddset(&ss, SIGINT) < 0)
     {
         r = -errno;
-        goto finish;
+        return EXIT_FAILURE;
     }
 
     /* Block SIGTERM first, so that the event loop can handle it */
     if (sigprocmask(SIG_BLOCK, &ss, nullptr) < 0)
     {
         r = -errno;
-        goto finish;
+        return EXIT_FAILURE;
     }
 
     /* Let's make use of the default handler and "floating" reference features
@@ -206,13 +202,13 @@
     r = sd_event_add_signal(event, nullptr, SIGTERM, nullptr, nullptr);
     if (r < 0)
     {
-        goto finish;
+        return EXIT_FAILURE;
     }
 
     r = sd_event_add_signal(event, nullptr, SIGINT, nullptr, nullptr);
     if (r < 0)
     {
-        goto finish;
+        return EXIT_FAILURE;
     }
 
     // Create our own socket if SysD did not supply one.
@@ -224,7 +220,7 @@
     else if (listen_fd > 1)
     {
         log<level::ERR>("Too many file descriptors received");
-        goto finish;
+        return 1;
     }
     else
     {
@@ -233,7 +229,7 @@
         {
             r = -errno;
             log<level::ERR>("Unable to manually open socket");
-            goto finish;
+            return EXIT_FAILURE;
         }
 
         address.sin_family = AF_INET;
@@ -244,35 +240,24 @@
         {
             r = -errno;
             log<level::ERR>("Unable to bind socket");
-            goto finish;
+            close(fd);
+            return EXIT_FAILURE;
         }
     }
 
     r = sd_event_add_io(event, &source, fd, EPOLLIN, udp623Handler, nullptr);
     if (r < 0)
     {
-        goto finish;
+        close(fd);
+        return EXIT_FAILURE;
     }
 
     udpIPMI.reset(source);
     source = nullptr;
 
-    r = sd_event_loop(event);
+    io->run();
 
-finish:
-
-    if (fd >= 0)
-    {
-        (void)close(fd);
-    }
-
-    if (r < 0)
-    {
-        log<level::ERR>("Event Loop Failure:",
-                        entry("FAILURE=%s", strerror(-r)));
-    }
-
-    return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+    return EXIT_SUCCESS;
 }
 
 void EventLoop::startHostConsole(const sol::CustomFD& fd)