class ExternalPluginInterface;
-extern "C" {
-typedef ExternalPluginInterface *(*createPlugin)(void);
-}
+/**
+ * Type of function used for creating objects implementing ExternalPluginInterface.
+ * Inside plugin library function with create_t signature should have symbol
+ * named "create".
+ */
+typedef ExternalPluginInterface *(*create_t)(void);
+/**
+ * Type of function used for destroying objects created with "create".
+ * Inside plugin library function with destroy_t signature should have symbol
+ * named "destroy".
+ */
+typedef void (*destroy_t)(ExternalPluginInterface *);
-//These typedefs will be defined in external headers
+// These typedefs will be defined in external headers
typedef std::string PluginData;
typedef std::string AgentType;
typedef std::vector<PolicyType> PolicyTypes;
loadPlugins();
}
+PluginManager::~PluginManager(void) {
+ // We have to be sure, that external objects will be destroyed
+ // before handles to libraries are closed.
+ for (auto &plugin : m_plugins) {
+ plugin.second.reset();
+ }
+}
+
ExternalPluginPtr PluginManager::getPlugin(PolicyType pType) {
return m_plugins[pType];
}
//Flush any previous errors
dlerror();
- createPlugin func = reinterpret_cast<createPlugin>(dlsym(handle, "create"));
+ create_t creator = reinterpret_cast<create_t>(dlsym(handle, "create"));
char *error;
if ((error = dlerror()) != NULL) {
return;
}
- ExternalPluginPtr pluginPtr(func());
+ destroy_t destroyer = reinterpret_cast<destroy_t>(dlsym(handle, "destroy"));
+ if ((error = dlerror()) != NULL) {
+ LOGE("Couldn't resolve symbol <destroy> from lib <%s> : <%s>", path.c_str(), error);
+ return;
+ }
+
+ ExternalPluginPtr pluginPtr(creator(), destroyer);
if (!pluginPtr) {
LOGE("Couldn't create plugin for <%s>", path.c_str());
#ifndef SRC_SERVICE_PLUGIN_PLUGINMANAGER_H_
#define SRC_SERVICE_PLUGIN_PLUGINMANAGER_H_
+#include <functional>
#include <list>
#include <map>
#include <memory>
public:
PluginManager(const std::string &pluginDir);
ExternalPluginPtr getPlugin(PolicyType pType);
- ~PluginManager() {}
+ ~PluginManager();
private:
typedef std::unique_ptr<void, std::function<void (void*)>> PluginLibPtr;