bus: Add function for checking open status

We have a workaround in sdevent for dealing with match expressions
that leak memory. If we keep trying to process bus events after
sd_bus_close(), we will encounter errors. This allows us to stop
processing when the bus should be closed.

Change-Id: I0bda95e76db8e81b56527cd1d5687a5d0b14f259
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include/sdbusplus/bus.hpp b/include/sdbusplus/bus.hpp
index c30d0d8..294db22 100644
--- a/include/sdbusplus/bus.hpp
+++ b/include/sdbusplus/bus.hpp
@@ -159,6 +159,17 @@
         _intf->sd_bus_close(_bus.get());
     }
 
+    /** @brief Determine if the bus is open to the broker */
+    bool is_open()
+    {
+        int r = _intf->sd_bus_is_open(_bus.get());
+        if (r < 0)
+        {
+            throw exception::SdBusError(-r, "sd_bus_is_open");
+        }
+        return r;
+    }
+
     /** @brief Wait for new dbus messages or signals.
      *
      *  @param[in] timeout_us - Timeout in usec.
diff --git a/include/sdbusplus/sdbus.hpp b/include/sdbusplus/sdbus.hpp
index bd618d5..37b7635 100644
--- a/include/sdbusplus/sdbus.hpp
+++ b/include/sdbusplus/sdbus.hpp
@@ -164,6 +164,7 @@
 
     virtual int sd_bus_flush(sd_bus* bus) = 0;
     virtual void sd_bus_close(sd_bus* bus) = 0;
+    virtual int sd_bus_is_open(sd_bus* bus) = 0;
 
     virtual int sd_bus_wait(sd_bus* bus, uint64_t timeout_usec) = 0;
 };
@@ -532,6 +533,11 @@
         ::sd_bus_close(bus);
     }
 
+    int sd_bus_is_open(sd_bus* bus) override
+    {
+        return ::sd_bus_is_open(bus);
+    }
+
     int sd_bus_wait(sd_bus* bus, uint64_t timeout_usec) override
     {
         return ::sd_bus_wait(bus, timeout_usec);
diff --git a/include/sdbusplus/test/sdbus_mock.hpp b/include/sdbusplus/test/sdbus_mock.hpp
index 13bf2ba..5f419b9 100644
--- a/include/sdbusplus/test/sdbus_mock.hpp
+++ b/include/sdbusplus/test/sdbus_mock.hpp
@@ -150,6 +150,7 @@
     MOCK_METHOD(sd_bus*, sd_bus_flush_close_unref, (sd_bus*), (override));
     MOCK_METHOD(int, sd_bus_flush, (sd_bus*), (override));
     MOCK_METHOD(void, sd_bus_close, (sd_bus*), (override));
+    MOCK_METHOD(int, sd_bus_is_open, (sd_bus*), (override));
     MOCK_METHOD(int, sd_bus_wait, (sd_bus*, uint64_t), (override));
 };