Merge pull request #38 from causten/reduceconnections

ipmid taking too many dbus connections
diff --git a/ipmid-api.h b/ipmid-api.h
index b3117d5..34d3bbe 100644
--- a/ipmid-api.h
+++ b/ipmid-api.h
@@ -1,6 +1,7 @@
 #ifndef __HOST_IPMID_IPMI_COMMON_H__
 #define __HOST_IPMID_IPMI_COMMON_H__
 #include <stdlib.h>
+#include <systemd/sd-bus.h>
 
 // length of Completion Code and its ALWAYS _1_
 #define IPMI_CC_LEN 1
@@ -93,4 +94,5 @@
     IPMI_CC_UNSPECIFIED_ERROR = 0xFF,
 };
 
+sd_bus *ipmid_get_sd_bus_connection(void);
 #endif
diff --git a/ipmid.C b/ipmid.C
index 0c7be7b..6b1eacc 100644
--- a/ipmid.C
+++ b/ipmid.C
@@ -15,6 +15,7 @@
 
 
 sd_bus *bus = NULL;
+
 FILE *ipmiio, *ipmidbus, *ipmicmddetails;
 
 void print_usage(void) {
@@ -367,6 +368,10 @@
     return;
 }
 
+sd_bus *ipmid_get_sd_bus_connection(void) {
+    return bus;
+}
+
 int main(int argc, char *argv[])
 {
     sd_bus_slot *slot = NULL;
@@ -402,9 +407,6 @@
         }
 
 
-    // Register all the handlers that provider implementation to IPMI commands.
-    ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH);
-
     /* Connect to system bus */
     r = sd_bus_open_system(&bus);
     if (r < 0) {
@@ -413,6 +415,9 @@
         goto finish;
     }
 
+    // Register all the handlers that provider implementation to IPMI commands.
+    ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH);
+
     r = sd_bus_add_match(bus, &slot, FILTER, handle_ipmi_command, NULL);
     if (r < 0) {
         fprintf(stderr, "Failed: sd_bus_add_match: %s : %s\n", strerror(-r), FILTER);
diff --git a/storageaddsel.C b/storageaddsel.C
index 94e037c..64b0e6a 100644
--- a/storageaddsel.C
+++ b/storageaddsel.C
@@ -159,15 +159,8 @@
     uint16_t *pty;
     int r;
 
+    mbus = ipmid_get_sd_bus_connection();
 
-
-    /* Connect to system bus */
-    r = sd_bus_open_system(&mbus);
-    if (r < 0) {
-        fprintf(stderr, "Failed to connect to system bus: %s\n",
-                strerror(-r));
-        goto finish;
-    }
     r = sd_bus_message_new_method_call(mbus,&m,
     									"org.openbmc.records.events",
     									"/org/openbmc/records/events",
@@ -208,10 +201,8 @@
     sd_bus_error_free(&error);
     sd_bus_message_unref(m);
     sd_bus_message_unref(reply);
-    sd_bus_unref(mbus);
 
     return r;
-
 }