treewide: Move all sd-event to sdeventplus

Change-Id: I157d8b5cf4db96b75602efae55870950994dcc17
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/network_manager_main.cpp b/src/network_manager_main.cpp
index 5dc5b8d..c28b02e 100644
--- a/src/network_manager_main.cpp
+++ b/src/network_manager_main.cpp
@@ -23,6 +23,8 @@
 #include <sdbusplus/bus/match.hpp>
 #include <sdbusplus/server/manager.hpp>
 #include <sdeventplus/event.hpp>
+#include <sdeventplus/source/signal.hpp>
+#include <stdplus/signal.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 
 using phosphor::logging::elog;
@@ -255,34 +257,30 @@
     }
 }
 
-void initializeTimers()
+void initializeTimers(sdeventplus::Event& event)
 {
-    auto event = sdeventplus::Event::get_default();
     refreshObjectTimer =
         std::make_unique<Timer>(event, std::bind(refreshObjects));
     reloadTimer = std::make_unique<Timer>(event, std::bind(reloadNetworkd));
 }
 
+void termCb(sdeventplus::source::Signal& signal, const struct signalfd_siginfo*)
+{
+    log<level::NOTICE>("Got TERM, exiting");
+    signal.get_event().exit(0);
+}
+
 int main()
 {
-    initializeTimers();
+    auto event = sdeventplus::Event::get_default();
+    stdplus::signal::block(SIGTERM);
+    sdeventplus::source::Signal(event, SIGTERM, termCb).set_floating(true);
+
+    initializeTimers(event);
 
     auto bus = sdbusplus::bus::new_default();
-
-    // Need sd_event to watch for OCC device errors
-    sd_event* event = nullptr;
-    auto r = sd_event_default(&event);
-    if (r < 0)
-    {
-        log<level::ERR>("Error creating a default sd_event handler");
-        return r;
-    }
-
-    EventPtr eventPtr{event};
-    event = nullptr;
-
     // Attach the bus to sd_event to service user requests
-    bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
+    bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
 
     // Add sdbusplus Object Manager for the 'root' path of the network manager.
     sdbusplus::server::manager_t objManager(bus, DEFAULT_OBJPATH);
@@ -298,7 +296,7 @@
     }
 
     // RTNETLINK event handler
-    rtnetlink::Server svr(eventPtr);
+    rtnetlink::Server svr(event);
 
 #ifdef SYNC_MAC_FROM_INVENTORY
     std::ifstream in(configFile);
@@ -312,7 +310,7 @@
     // fully configured.
     refreshObjectTimer->restartOnce(refreshTimeout);
 
-    return sd_event_loop(eventPtr.get());
+    return event.loop();
 }
 
 } // namespace network
diff --git a/src/rtnetlink_server.cpp b/src/rtnetlink_server.cpp
index 3722a678..d14b28e 100644
--- a/src/rtnetlink_server.cpp
+++ b/src/rtnetlink_server.cpp
@@ -1,17 +1,15 @@
 #include "rtnetlink_server.hpp"
 
+#include "types.hpp"
+
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
 #include <netinet/in.h>
 
 #include <memory>
-#include <phosphor-logging/elog-errors.hpp>
-#include <phosphor-logging/log.hpp>
 #include <stdplus/fd/create.hpp>
 #include <stdplus/fd/ops.hpp>
-#include <stdplus/signal.hpp>
 #include <string_view>
-#include <xyz/openbmc_project/Common/error.hpp>
 
 namespace phosphor
 {
@@ -52,16 +50,14 @@
 }
 
 /* Call Back for the sd event loop */
-static int eventHandler(sd_event_source* /*es*/, int fd, uint32_t /*revents*/,
-                        void* /*userdata*/)
+static void eventHandler(sdeventplus::source::IO&, int fd, uint32_t)
 {
-    std::array<char, phosphor::network::rtnetlink::BUFSIZE> buffer = {};
+    std::array<char, BUFSIZE> buffer = {};
     int len{};
 
     auto netLinkHeader = reinterpret_cast<struct nlmsghdr*>(buffer.data());
 
-    while ((len = recv(fd, netLinkHeader, phosphor::network::rtnetlink::BUFSIZE,
-                       0)) > 0)
+    while ((len = recv(fd, netLinkHeader, buffer.size(), 0)) > 0)
     {
         for (; (NLMSG_OK(netLinkHeader, len)) &&
                (netLinkHeader->nlmsg_type != NLMSG_DONE);
@@ -88,8 +84,6 @@
 
         netLinkHeader = reinterpret_cast<struct nlmsghdr*>(buffer.data());
     } // end while
-
-    return 0;
 }
 
 static stdplus::ManagedFd makeSock()
@@ -110,45 +104,9 @@
     return sock;
 }
 
-Server::Server(EventPtr& eventPtr) : sock(makeSock())
+Server::Server(sdeventplus::Event& event) :
+    sock(makeSock()), io(event, sock.get(), EPOLLIN | EPOLLET, eventHandler)
 {
-    using namespace phosphor::logging;
-    using InternalFailure =
-        sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
-    int r{};
-
-    /* Let's make use of the default handler and "floating"
-       reference features of sd_event_add_signal() */
-
-    stdplus::signal::block(SIGTERM);
-    r = sd_event_add_signal(eventPtr.get(), NULL, SIGTERM, NULL, NULL);
-    if (r < 0)
-    {
-        goto finish;
-    }
-
-    stdplus::signal::block(SIGINT);
-    r = sd_event_add_signal(eventPtr.get(), NULL, SIGINT, NULL, NULL);
-    if (r < 0)
-    {
-        goto finish;
-    }
-
-    r = sd_event_add_io(eventPtr.get(), nullptr, sock.get(), EPOLLIN,
-                        eventHandler, nullptr);
-    if (r < 0)
-    {
-        goto finish;
-    }
-
-finish:
-
-    if (r < 0)
-    {
-        log<level::ERR>("Failure Occurred in starting of server:",
-                        entry("ERRNO=%d", errno));
-        elog<InternalFailure>();
-    }
 }
 
 } // namespace rtnetlink
diff --git a/src/rtnetlink_server.hpp b/src/rtnetlink_server.hpp
index aca3744..1e849ee 100644
--- a/src/rtnetlink_server.hpp
+++ b/src/rtnetlink_server.hpp
@@ -1,9 +1,13 @@
 #pragma once
-
-#include "types.hpp"
-
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/source/io.hpp>
 #include <stdplus/fd/managed.hpp>
 
+namespace sdeventplus
+{
+class Event;
+}
+
 namespace phosphor
 {
 namespace network
@@ -30,7 +34,7 @@
      *  @param[in] eventPtr - Unique ptr reference to sd_event.
      *  @param[in] socket - netlink socket.
      */
-    Server(EventPtr& eventPtr);
+    Server(sdeventplus::Event& event);
 
     /** @brief Gets the socket associated with this netlink server */
     inline stdplus::Fd& getSock()
@@ -40,6 +44,7 @@
 
   private:
     stdplus::ManagedFd sock;
+    sdeventplus::source::IO io;
 };
 
 } // namespace rtnetlink
diff --git a/src/types.hpp b/src/types.hpp
index a6011ff..53619ea 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -2,12 +2,10 @@
 #include <fmt/core.h>
 #include <net/ethernet.h>
 #include <netinet/in.h>
-#include <systemd/sd-event.h>
 
 #include <algorithm>
 #include <array>
 #include <chrono>
-#include <memory>
 #include <sdeventplus/clock.hpp>
 #include <sdeventplus/utility/timer.hpp>
 #include <string>
@@ -31,16 +29,6 @@
 // configuration takes 3-4 sec to reconfigure at most.
 constexpr auto refreshTimeout = 4s;
 
-/* Need a custom deleter for freeing up sd_event */
-struct EventDeleter
-{
-    void operator()(sd_event* event) const
-    {
-        sd_event_unref(event);
-    }
-};
-using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
-
 // Byte representations for common address types in network byte order
 using InAddrAny = std::variant<in_addr, in6_addr>;