From: Zofia Abramowska Date: Thu, 16 Oct 2014 16:35:48 +0000 (+0200) Subject: Fix dlopened objects management X-Git-Tag: submit/R4/20141115.054144~22 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=aae388640a4fa6b772db232e28d6e4fc5a65278c;p=platform%2Fcore%2Fsecurity%2Fcynara.git Fix dlopened objects management * Add destroy function type, so deletion is compatible with allocation * Remove extern "C" from plugin creation/destruction typedefs * Add proper comments, so expected names of creation/destruction function symbol are known to API user Change-Id: I019f5bf83afc94945c8efc62e99dd324c419cc33 --- diff --git a/src/include/cynara-plugin.h b/src/include/cynara-plugin.h index 83956fe..a9d6da7 100644 --- a/src/include/cynara-plugin.h +++ b/src/include/cynara-plugin.h @@ -34,11 +34,20 @@ namespace Cynara { 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 PolicyTypes; diff --git a/src/service/plugin/PluginManager.cpp b/src/service/plugin/PluginManager.cpp index 3d21b49..c591805 100644 --- a/src/service/plugin/PluginManager.cpp +++ b/src/service/plugin/PluginManager.cpp @@ -53,6 +53,14 @@ PluginManager::PluginManager(const std::string &pluginDir) : m_dir(pluginDir) { 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]; } @@ -90,7 +98,7 @@ void PluginManager::openPlugin(const std::string &path) { //Flush any previous errors dlerror(); - createPlugin func = reinterpret_cast(dlsym(handle, "create")); + create_t creator = reinterpret_cast(dlsym(handle, "create")); char *error; if ((error = dlerror()) != NULL) { @@ -98,7 +106,13 @@ void PluginManager::openPlugin(const std::string &path) { return; } - ExternalPluginPtr pluginPtr(func()); + destroy_t destroyer = reinterpret_cast(dlsym(handle, "destroy")); + if ((error = dlerror()) != NULL) { + LOGE("Couldn't resolve symbol 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()); diff --git a/src/service/plugin/PluginManager.h b/src/service/plugin/PluginManager.h index 6338cad..1abc6e6 100644 --- a/src/service/plugin/PluginManager.h +++ b/src/service/plugin/PluginManager.h @@ -23,6 +23,7 @@ #ifndef SRC_SERVICE_PLUGIN_PLUGINMANAGER_H_ #define SRC_SERVICE_PLUGIN_PLUGINMANAGER_H_ +#include #include #include #include @@ -37,7 +38,7 @@ class PluginManager { public: PluginManager(const std::string &pluginDir); ExternalPluginPtr getPlugin(PolicyType pType); - ~PluginManager() {} + ~PluginManager(); private: typedef std::unique_ptr> PluginLibPtr;