Commit 2f15047c authored by Tristan Matthews's avatar Tristan Matthews

* #7150: remove unused pluginmanager

parent a906eff2
......@@ -50,8 +50,6 @@ AC_CONFIG_FILES([src/Makefile \
src/audio/codecs/Makefile \
src/config/Makefile \
src/dbus/Makefile \
src/plug-in/Makefile \
src/plug-in/test/Makefile \
src/hooks/Makefile \
src/history/Makefile])
......
......@@ -5,7 +5,7 @@ libexec_PROGRAMS = sflphoned
# all: indent
SUBDIRS = dbus audio config plug-in hooks history sip iax im
SUBDIRS = dbus audio config hooks history sip iax im
sflphoned_SOURCES = main.cpp
......@@ -58,7 +58,6 @@ libsflphone_la_LIBADD = \
./audio/libaudio.la \
./dbus/libdbus.la \
./config/libconfig.la \
./plug-in/libplugin.la \
./hooks/libhooks.la \
./history/libhistory.la
......
include ../../globals.mak
SUBDIRS=test
noinst_LTLIBRARIES = libplugin.la
libplugin_la_SOURCES = \
pluginmanager.cpp \
librarymanager.cpp \
pluginmanager.h librarymanager.h plugin.h
libplugin_la_CXXFLAGS = -I $(top_srcdir)/src/plug-in
/*
* Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "librarymanager.h"
LibraryManager::LibraryManager(const std::string &filename)
: _filename(filename), _handlePtr(NULL)
{
_handlePtr = loadLibrary(filename);
}
LibraryManager::~LibraryManager(void)
{
unloadLibrary();
}
LibraryManager::LibraryHandle LibraryManager::loadLibrary(const std::string &filename)
{
LibraryHandle pluginHandlePtr = NULL;
const char *error;
_debug("Loading dynamic library %s", filename.c_str());
/* Load the library */
pluginHandlePtr = dlopen(filename.c_str(), RTLD_LAZY);
if (!pluginHandlePtr) {
error = dlerror();
_debug("Error while opening plug-in: %s", error);
return NULL;
}
dlerror();
return pluginHandlePtr;
}
int LibraryManager::unloadLibrary()
{
if (_handlePtr == NULL)
return 1;
_debug("Unloading dynamic library ...");
dlclose(_handlePtr);
if (dlerror()) {
_debug("Error unloading the library : %s...", dlerror());
return 1;
}
return 0;
}
int LibraryManager::resolveSymbol(const std::string &symbol, SymbolHandle *symbolPtr)
{
SymbolHandle sy = 0;
if (_handlePtr) {
try {
sy = dlsym(_handlePtr, symbol.c_str());
if (sy != NULL) {
*symbolPtr = sy;
return 0;
}
} catch (...) {}
throw LibraryManagerException(_filename, symbol, LibraryManagerException::symbolNotFound);
} else
return 1;
}
/************************************************************************************************/
LibraryManagerException::LibraryManagerException(const std::string &libraryName, const std::string &details, Reason reason) :
std::runtime_error(""), _reason(reason), _details("")
{
if (_reason == loadingFailed)
_details = "Error when loading " + libraryName + "" + details;
else
_details = "Error when resolving symbol " + details + " in " + libraryName;
}
const char* LibraryManagerException::what() const throw()
{
return _details.c_str();
}
/*
* Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#ifndef LIBRARY_MANAGER_H
#define LIBRARY_MANAGER_H
#include "dlfcn.h"
#include <stdexcept>
#include "global.h"
class LibraryManager {
public:
typedef void* LibraryHandle;
typedef void* SymbolHandle;
LibraryManager(const std::string &filename);
~LibraryManager(void);
int resolveSymbol(const std::string &symbol, SymbolHandle *ptr);
int unloadLibrary(void);
protected:
LibraryHandle loadLibrary(const std::string &filename);
private:
std::string _filename;
LibraryHandle _handlePtr;
};
class LibraryManagerException : public std::runtime_error {
public:
typedef enum Reason {
loadingFailed = 0,
symbolNotFound
} Reason;
LibraryManagerException(const std::string &libraryName, const std::string &details, Reason reason);
~LibraryManagerException(void) throw() {}
Reason getReason(void) const {
return _reason;
}
const char* what() const throw();
private:
Reason _reason;
std::string _details;
};
#endif // LIBRARY_MANAGER_H
/*
* Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#ifndef PLUGIN_H
#define PLUGIN_H
#include "global.h"
#include "pluginmanager.h"
/*
* @file plugin.h
* @brief Define a plugin object
*/
class Plugin {
public:
Plugin(const std::string &name) {
_name = name;
}
virtual ~Plugin() {}
std::string getPluginName(void) const {
return _name;
}
/**
* Return the minimal core version required so that the plugin could work
* @return int The version required
*/
virtual int initFunc(PluginInfo **info) = 0;
private:
Plugin &operator = (const Plugin &plugin);
std::string _name;
};
typedef Plugin* createFunc(void);
typedef void destroyFunc(Plugin*);
#endif //PLUGIN_H
/*
* Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include <dirent.h>
#include <dlfcn.h>
#include "pluginmanager.h"
PluginManager* PluginManager::_instance = 0;
PluginManager*
PluginManager::instance()
{
if (!_instance) {
return new PluginManager();
}
return _instance;
}
PluginManager::PluginManager()
:_loadedPlugins()
{
_instance = this;
}
PluginManager::~PluginManager()
{
_instance = 0;
}
namespace {
bool hasSharedExtension(const std::string &fn)
{
size_t dot_position = fn.rfind(".");
return (dot_position != std::string::npos and
fn.substr(dot_position) == ".so");
}
}
int
PluginManager::loadPlugins(const std::string &path)
{
std::string pluginDir, current;
DIR *dir;
dirent *dirStruct;
LibraryManager *library;
Plugin *plugin;
const std::string pDir = "..";
const std::string cDir = ".";
/* The directory in which plugins are dropped. Default: /usr/lib/sflphone/plugins/ */
if (path.empty())
pluginDir = std::string(PLUGINS_DIR).append("/");
else
pluginDir = path;
_debug("Loading plugins from %s...", pluginDir.c_str());
dir = opendir(pluginDir.c_str());
/* Test if the directory exists or is readable */
if (!dir)
return 1;
/* Read the directory */
while ((dirStruct=readdir(dir))) {
/* Get the name of the current item in the directory */
current = dirStruct->d_name;
/* Test if the current item is not the parent or the current directory and that it ends with .so*/
if (current == pDir || current == cDir || !hasSharedExtension(current))
continue;
/* Load the dynamic library */
library = loadDynamicLibrary(pluginDir + current);
/* Instanciate the plugin object */
if (instanciatePlugin(library, &plugin) != 0) {
_debug("Error instanciating the plugin ...");
closedir(dir);
return 1;
}
/* Regitering the current plugin */
if (registerPlugin(plugin, library) != 0) {
_debug("Error registering the plugin ...");
closedir(dir);
return 1;
}
}
/* Close the directory */
closedir(dir);
return 0;
}
int
PluginManager::unloadPlugins(void)
{
PluginInfo *info;
if (_loadedPlugins.empty()) return 0;
/* Use an iterator on the loaded plugins map */
pluginMap::iterator iter;
iter = _loadedPlugins.begin();
while (iter != _loadedPlugins.end()) {
info = iter->second;
if (deletePlugin(info) != 0) {
_debug("Error deleting the plugin ... ");
return 1;
}
unloadDynamicLibrary(info->_libraryPtr);
if (unregisterPlugin(info) != 0) {
_debug("Error unregistering the plugin ... ");
return 1;
}
iter++;
}
return 0;
}
bool
PluginManager::isPluginLoaded(const std::string &name)
{
if (_loadedPlugins.empty()) return false;
/* Use an iterator on the loaded plugins map */
pluginMap::iterator iter;
iter = _loadedPlugins.find(name);
/* Returns map::end if the specified key has not been found */
if (iter==_loadedPlugins.end())
return false;
/* Returns the plugin pointer */
return true;
}
LibraryManager*
PluginManager::loadDynamicLibrary(const std::string& filename)
{
/* Load the library through the library manager */
return new LibraryManager(filename);
}
int
PluginManager::unloadDynamicLibrary(LibraryManager *libraryPtr)
{
_debug("Unloading dynamic library ...");
/* Close it */
return libraryPtr->unloadLibrary();
}
int
PluginManager::instanciatePlugin(LibraryManager *libraryPtr, Plugin **plugin)
{
createFunc *createPlugin;
LibraryManager::SymbolHandle symbol;
if (libraryPtr->resolveSymbol("createPlugin", &symbol) != 0)
return 1;
createPlugin = (createFunc*) symbol;
*plugin = createPlugin();
return 0;
}
int
PluginManager::deletePlugin(PluginInfo *plugin)
{
destroyFunc *destroyPlugin;
LibraryManager::SymbolHandle symbol;
if (plugin->_libraryPtr->resolveSymbol("destroyPlugin", &symbol) != 0)
return 1;
destroyPlugin = (destroyFunc*) symbol;
/* Call it */
destroyPlugin(plugin->_plugin);
return 0;
}
int
PluginManager::registerPlugin(Plugin *plugin, LibraryManager *library)
{
std::string key;
PluginInfo *p_info;
if (plugin==0)
return 1;
p_info = new PluginInfo();
/* Retrieve information from the plugin */
plugin->initFunc(&p_info);
key = p_info->_name;
//p_info->_plugin = plugin;
p_info->_libraryPtr = library;
/* Add the data in the loaded plugin map */
_loadedPlugins[ key ] = p_info;
return 0;
}
int
PluginManager::unregisterPlugin(PluginInfo *plugin)
{
pluginMap::iterator iter;
std::string key;
key = plugin->_name;
if (!isPluginLoaded(key))
return 1;
iter = _loadedPlugins.find(key);
_loadedPlugins.erase(iter);
return 0;
}
/*
* Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#ifndef PLUGIN_MANAGER_H
#define PLUGIN_MANAGER_H
/*
* @file pluginmanager.h
* @brief Base class of the plugin manager
*/
#include "librarymanager.h"
#include "global.h"
#include <map>
#include <string>
#include <vector>
class Plugin;
typedef struct PluginInfo {
std::string _name;
LibraryManager *_libraryPtr;
Plugin *_plugin;
int _major_version;
int _minor_version;
} PluginInfo;
#include "plugin.h"
class PluginManager {
public:
/**
* Destructor
*/
~PluginManager();
/**
* Returns the unique instance of the plugin manager
*/
static PluginManager* instance();
/**
* Load all the plugins found in a specific directory
* @param path The absolute path to the directory
* @return int The number of items loaded
*/
int loadPlugins(const std::string &path = "");
int unloadPlugins(void);
int instanciatePlugin(LibraryManager* libraryPtr, Plugin** plugin);
/**
* Check if a plugin has been already loaded
* @param name The name of the plugin looked for
* @return bool The pointer on the plugin or NULL if not found
*/
bool isPluginLoaded(const std::string &name);
int registerPlugin(Plugin *plugin, LibraryManager *library);
int unregisterPlugin(PluginInfo *plugin);
int deletePlugin(PluginInfo *plugin);
/**
* Load a unix dynamic/shared library
* @param filename The path to the dynamic/shared library
* @return LibraryManager* A pointer on the library
*/
LibraryManager* loadDynamicLibrary(const std::string &filename);
/**
* Unload a unix dynamic/shared library
* @param LibraryManager* The pointer on the loaded library
*/
int unloadDynamicLibrary(LibraryManager* libraryPtr);
private:
/**
* Default constructor
*/
PluginManager();
/* Map of plugins associated by their string name */
typedef std::map<std::string, PluginInfo*> pluginMap;
pluginMap _loadedPlugins;
/* The unique static instance */
static PluginManager* _instance;
};
#endif //PLUGIN_MANAGER_H
include $(top_srcdir)/globals.mak
PLUGIN_LIB = libplugintest.so
libplugintest_so_SOURCES = pluginTest.cpp
LLIBS=$(top_builddir)/src/plug-in/libplugin.la
libplugintest_so_CXXFLAGS = -fPIC -g -Wall -I $(top_srcdir)/src/plug-in
libplugintest_so_LDFLAGS = --shared -lc
INSTALL_PLUGIN_RULE = install-libplugintest_so
check_PROGRAMS = libplugintest.so
install-exec-local: install-libplugintest_so
uninstall-local: uninstall-libplugintest_so
install-libplugintest_so: libplugintest.so
mkdir -p $(sflplugindir)
$(INSTALL_PROGRAM) libplugintest.so $(sflplugindir)
uninstall-libplugintest_so:
rm -f $(sflplugindir)/libplugintest.so
/*
* Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Savoir-Faire Linux Inc.
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of