Move D-Bus finder class constructors
Three classes in this repository search for D-Bus interfaces and call
a callback when interfaces are found:
- DBusInterfacesFinder
- CompatibleSystemTypesFinder
- DeviceFinder
The constructors of these three classes may immediately call the
callback. For this reason, these constructors should not be used in the
initialization list of constructors in other classes. Otherwise the
callback may be called before the other class is fully initialized,
leading to unpredictable behavior.
Move usage of these three constructors from initialization lists to
constructor bodies.
Tested:
+ CompatibleSystemTypesFinder
+ Test where compatible system types interface already exists on D-Bus
+ Verify callback is called immediately
+ Test where compatible system types interface does not already exist
on D-Bus
+ Verify callback is called later when interface is added
+ Verify callback is passed the correct data
+ DeviceFinder
+ Test where device interface already exists on D-Bus
+ Verify callback is called immediately
+ Test where device interface does not already exist on D-Bus
+ Verify callback is called later when interface is added
+ Verify callback is passed the correct data
Change-Id: If089f4720012d837b464137b5d3c8fba5dbdc335
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
diff --git a/compatible_system_types_finder.cpp b/compatible_system_types_finder.cpp
index 4306942..98db28b 100644
--- a/compatible_system_types_finder.cpp
+++ b/compatible_system_types_finder.cpp
@@ -40,13 +40,14 @@
CompatibleSystemTypesFinder::CompatibleSystemTypesFinder(sdbusplus::bus_t& bus,
Callback callback) :
- callback{std::move(callback)},
- interfaceFinder{
+ callback{std::move(callback)}
+{
+ interfaceFinder = std::make_unique<DBusInterfacesFinder>(
bus, compatibleInterfaceService,
std::vector<std::string>{compatibleInterface},
std::bind_front(&CompatibleSystemTypesFinder::interfaceFoundCallback,
- this)}
-{}
+ this));
+}
void CompatibleSystemTypesFinder::interfaceFoundCallback(
[[maybe_unused]] const std::string& path,
diff --git a/compatible_system_types_finder.hpp b/compatible_system_types_finder.hpp
index e365679..f24617d 100644
--- a/compatible_system_types_finder.hpp
+++ b/compatible_system_types_finder.hpp
@@ -21,6 +21,7 @@
#include <sdbusplus/bus.hpp>
#include <functional>
+#include <memory>
#include <string>
#include <vector>
@@ -70,6 +71,12 @@
/**
* Constructor.
*
+ * Note: The callback function may be called immediately by this
+ * constructor. For this reason, do not use this constructor in the
+ * initialization list of constructors in other classes. Otherwise the
+ * callback may be called before the other class is fully initialized,
+ * leading to unpredictable behavior.
+ *
* @param bus D-Bus bus object
* @param callback Callback function that is called each time a list of
* compatible system types is found
@@ -98,7 +105,7 @@
/**
* Class used to find instances of the D-Bus Compatible interface.
*/
- DBusInterfacesFinder interfaceFinder;
+ std::unique_ptr<DBusInterfacesFinder> interfaceFinder;
};
} // namespace phosphor::power::util
diff --git a/dbus_interfaces_finder.hpp b/dbus_interfaces_finder.hpp
index 35a80c7..db89e26 100644
--- a/dbus_interfaces_finder.hpp
+++ b/dbus_interfaces_finder.hpp
@@ -73,6 +73,12 @@
/**
* Constructor.
*
+ * Note: The callback function may be called immediately by this
+ * constructor. For this reason, do not use this constructor in the
+ * initialization list of constructors in other classes. Otherwise the
+ * callback may be called before the other class is fully initialized,
+ * leading to unpredictable behavior.
+ *
* @param bus D-Bus bus object
* @param service D-Bus service that owns the object paths implementing
* the specified interfaces
diff --git a/phosphor-power-sequencer/src/device_finder.cpp b/phosphor-power-sequencer/src/device_finder.cpp
index 65d3651..343b5e5 100644
--- a/phosphor-power-sequencer/src/device_finder.cpp
+++ b/phosphor-power-sequencer/src/device_finder.cpp
@@ -40,11 +40,12 @@
const std::string addressProperty = "Address";
DeviceFinder::DeviceFinder(sdbusplus::bus_t& bus, Callback callback) :
- callback{std::move(callback)},
- interfacesFinder{
+ callback{std::move(callback)}
+{
+ interfacesFinder = std::make_unique<DBusInterfacesFinder>(
bus, deviceInterfacesService, deviceInterfaces,
- std::bind_front(&DeviceFinder::interfaceFoundCallback, this)}
-{}
+ std::bind_front(&DeviceFinder::interfaceFoundCallback, this));
+}
void DeviceFinder::interfaceFoundCallback(
[[maybe_unused]] const std::string& path, const std::string& interface,
diff --git a/phosphor-power-sequencer/src/device_finder.hpp b/phosphor-power-sequencer/src/device_finder.hpp
index f09d164..52fc1c6 100644
--- a/phosphor-power-sequencer/src/device_finder.hpp
+++ b/phosphor-power-sequencer/src/device_finder.hpp
@@ -22,6 +22,7 @@
#include <cstdint>
#include <functional>
+#include <memory>
#include <string>
namespace phosphor::power::sequencer
@@ -72,6 +73,12 @@
/**
* Constructor.
*
+ * Note: The callback function may be called immediately by this
+ * constructor. For this reason, do not use this constructor in the
+ * initialization list of constructors in other classes. Otherwise the
+ * callback may be called before the other class is fully initialized,
+ * leading to unpredictable behavior.
+ *
* @param bus D-Bus bus object
* @param callback Callback function that is called each time a power
* sequencer device is found
@@ -113,7 +120,7 @@
* Class used to find D-Bus interfaces that contain power sequencer device
* properties.
*/
- DBusInterfacesFinder interfacesFinder;
+ std::unique_ptr<DBusInterfacesFinder> interfacesFinder;
};
} // namespace phosphor::power::sequencer