Change linkage of ipmid_get_sdbus_plus_handler() to libipmid.so

ipmid_get_sdbus_plus_handler() was re-added to be a part of
systemintfcmds where it was used. This moves it to libipmid.so
because that is where symbols used by both ipmid and the providers
should be.

Because ipmid_get_sdbus_plus_handler() relies on the io service and the
main sdbus::asio::connection is also right there, this moves those
symbols to libipmid as well to keep coherent.

Change-Id: Ib125a0c217c8bcf47a8a4bd0c557eb69e928245b
Signed-off-by: Vernon Mauery <vernon.mauery@linux.intel.com>
diff --git a/include/ipmid/api.hpp b/include/ipmid/api.hpp
index 47dc04d..b691bed 100644
--- a/include/ipmid/api.hpp
+++ b/include/ipmid/api.hpp
@@ -221,8 +221,8 @@
 
 } // namespace ipmi
 
-// any client can interact with the main asio service
-std::shared_ptr<boost::asio::io_service> getIoService();
+// any client can interact with the main asio context
+std::shared_ptr<boost::asio::io_context> getIoContext();
 
 // any client can interact with the main sdbus
 std::shared_ptr<sdbusplus::asio::connection> getSdBus();
@@ -239,5 +239,5 @@
 template <typename WorkFn>
 static inline void post_work(WorkFn work)
 {
-    getIoService()->post(std::forward<WorkFn>(work));
+    getIoContext()->post(std::forward<WorkFn>(work));
 }
diff --git a/ipmid-new.cpp b/ipmid-new.cpp
index b7c6205..bf7795c 100644
--- a/ipmid-new.cpp
+++ b/ipmid-new.cpp
@@ -458,18 +458,6 @@
 
 } // namespace ipmi
 
-static std::shared_ptr<boost::asio::io_service> io;
-std::shared_ptr<boost::asio::io_service> getIoService()
-{
-    return io;
-}
-
-static std::shared_ptr<sdbusplus::asio::connection> sdbusp;
-std::shared_ptr<sdbusplus::asio::connection> getSdBus()
-{
-    return sdbusp;
-}
-
 #ifdef ALLOW_DEPRECATED_API
 /* legacy registration */
 void ipmi_register_callback(ipmi_netfn_t netFn, ipmi_cmd_t cmd,
@@ -559,9 +547,10 @@
 
     dest = m.get_sender();
     path = m.get_path();
-    sdbusp->async_method_call([](boost::system::error_code ec) {}, dest, path,
-                              DBUS_INTF, "sendMessage", seq, netFn, lun, cmd,
-                              response->cc, response->payload.raw);
+    getSdBus()->async_method_call([](boost::system::error_code ec) {}, dest,
+                                  path, DBUS_INTF, "sendMessage", seq, netFn,
+                                  lun, cmd, response->cc,
+                                  response->payload.raw);
 }
 
 #endif /* ALLOW_DEPRECATED_API */
@@ -579,10 +568,16 @@
     return cmdManager;
 }
 
+// These are symbols that are present in libipmid, but not expected
+// to be used except here (or maybe a unit test), so declare them here
+extern void setIoContext(std::shared_ptr<boost::asio::io_context>& newIo);
+extern void setSdBus(std::shared_ptr<sdbusplus::asio::connection>& newBus);
+
 int main(int argc, char* argv[])
 {
     // Connect to system bus
-    io = std::make_shared<boost::asio::io_service>();
+    auto io = std::make_shared<boost::asio::io_context>();
+    setIoContext(io);
     if (argc > 1 && std::string(argv[1]) == "-session")
     {
         sd_bus_default_user(&bus);
@@ -591,7 +586,8 @@
     {
         sd_bus_default_system(&bus);
     }
-    sdbusp = std::make_shared<sdbusplus::asio::connection>(*io, bus);
+    auto sdbusp = std::make_shared<sdbusplus::asio::connection>(*io, bus);
+    setSdBus(sdbusp);
     sdbusp->request_name("xyz.openbmc_project.Ipmi.Host");
 
     // TODO: Hack to keep the sdEvents running.... Not sure why the sd_event
diff --git a/libipmid/Makefile.am b/libipmid/Makefile.am
index e5724a8..ac35bbf 100644
--- a/libipmid/Makefile.am
+++ b/libipmid/Makefile.am
@@ -1,8 +1,21 @@
+COMMON_CXX = \
+	-flto \
+	-Wno-psabi \
+	$(SYSTEMD_CFLAGS) \
+	$(SDBUSPLUS_CFLAGS) \
+	-DBOOST_ERROR_CODE_HEADER_ONLY \
+	-DBOOST_SYSTEM_NO_DEPRECATED \
+	-DBOOST_COROUTINES_NO_DEPRECATION_WARNING \
+	-DBOOST_ASIO_DISABLE_THREADS \
+	-DBOOST_ALL_NO_LIB
+
 pkgconfig_DATA = libipmid.pc
 lib_LTLIBRARIES = libipmid.la
-libipmid_la_SOURCES =
+libipmid_la_SOURCES = \
+	sdbus-asio.cpp \
+	systemintf-sdbus.cpp
 libipmid_la_LDFLAGS = \
 	$(SYSTEMD_LIBS) \
 	-version-info 0:0:0 -shared
 libipmid_la_CXXFLAGS = \
-	$(SYSTEMD_CFLAGS)
+	$(COMMON_CXX)
diff --git a/libipmid/sdbus-asio.cpp b/libipmid/sdbus-asio.cpp
new file mode 100644
index 0000000..e505797
--- /dev/null
+++ b/libipmid/sdbus-asio.cpp
@@ -0,0 +1,31 @@
+#include <boost/asio.hpp>
+#include <memory>
+#include <sdbusplus/asio/connection.hpp>
+
+namespace
+{
+
+std::shared_ptr<boost::asio::io_context> ioCtx;
+std::shared_ptr<sdbusplus::asio::connection> sdbusp;
+
+} // namespace
+
+void setIoContext(std::shared_ptr<boost::asio::io_context>& newIo)
+{
+    ioCtx = newIo;
+}
+
+std::shared_ptr<boost::asio::io_context> getIoContext()
+{
+    return ioCtx;
+}
+
+void setSdBus(std::shared_ptr<sdbusplus::asio::connection>& newBus)
+{
+    sdbusp = newBus;
+}
+
+std::shared_ptr<sdbusplus::asio::connection> getSdBus()
+{
+    return sdbusp;
+}
diff --git a/libipmid/systemintf-sdbus.cpp b/libipmid/systemintf-sdbus.cpp
new file mode 100644
index 0000000..abd30ec
--- /dev/null
+++ b/libipmid/systemintf-sdbus.cpp
@@ -0,0 +1,32 @@
+#include <ipmid/api.hpp>
+#include <memory>
+#include <sdbusplus/asio/connection.hpp>
+
+namespace
+{
+
+std::unique_ptr<sdbusplus::asio::connection> sdbusp;
+
+} // namespace
+
+/**
+ * @brief ipmid_get_sdbus_plus_handler is used by some ipmi providers
+ *
+ * @return: a reference to a unique pointer of the systemd connection
+ *          managed by the systemintfcmds code
+ */
+std::unique_ptr<sdbusplus::asio::connection>& ipmid_get_sdbus_plus_handler()
+{
+    if (!sdbusp)
+    {
+        // Create a new sdbus connection so it can have a well-known name
+        sd_bus* bus = nullptr;
+        sd_bus_open_system(&bus);
+        if (bus)
+        {
+            sdbusp = std::make_unique<sdbusplus::asio::connection>(
+                *getIoContext(), bus);
+        }
+    }
+    return sdbusp;
+}
diff --git a/systemintfcmds.cpp b/systemintfcmds.cpp
index 00e6c51..aeded06 100644
--- a/systemintfcmds.cpp
+++ b/systemintfcmds.cpp
@@ -146,17 +146,8 @@
     __attribute__((init_priority(101)));
 std::unique_ptr<sdbusplus::server::manager::manager> objManager
     __attribute__((init_priority(101)));
-std::unique_ptr<sdbusplus::asio::connection> sdbusp
-    __attribute__((init_priority(101)));
 } // namespace
 
-// this is used by openpower-host-ipmi-oem
-std::unique_ptr<sdbusplus::asio::connection>& ipmid_get_sdbus_plus_handler()
-{
-    return sdbusp;
-}
-
-#include <unistd.h>
 void register_netfn_app_functions()
 {
 
@@ -179,15 +170,8 @@
     // Create new xyz.openbmc_project.host object on the bus
     auto objPath = std::string{CONTROL_HOST_OBJ_MGR} + '/' + HOST_NAME + '0';
 
-    // Create a new sdbus connection so it can have a well-known name
-    sd_bus* bus = nullptr;
-    sd_bus_open_system(&bus);
-    if (!bus)
-    {
-        return;
-    }
-    auto io = getIoService();
-    sdbusp = std::make_unique<sdbusplus::asio::connection>(*io, bus);
+    std::unique_ptr<sdbusplus::asio::connection>& sdbusp =
+        ipmid_get_sdbus_plus_handler();
 
     // Add sdbusplus ObjectManager.
     objManager = std::make_unique<sdbusplus::server::manager::manager>(