diff options
Diffstat (limited to 'lib/MiddleWare')
-rw-r--r-- | lib/MiddleWare/Application.h | 35 | ||||
-rw-r--r-- | lib/MiddleWare/CrashCatcherPlugin.cpp | 154 | ||||
-rw-r--r-- | lib/MiddleWare/CrashCatcherPlugin.h | 63 | ||||
-rw-r--r-- | lib/MiddleWare/Database.h | 98 | ||||
-rw-r--r-- | lib/MiddleWare/DynamicLibrary.cpp | 57 | ||||
-rw-r--r-- | lib/MiddleWare/DynamicLibrary.h | 42 | ||||
-rw-r--r-- | lib/MiddleWare/Language.h | 36 | ||||
-rw-r--r-- | lib/MiddleWare/Makefile.am | 16 | ||||
-rw-r--r-- | lib/MiddleWare/MiddleWare.cpp | 87 | ||||
-rw-r--r-- | lib/MiddleWare/MiddleWare.h | 51 | ||||
-rw-r--r-- | lib/MiddleWare/Plugin.h | 86 | ||||
-rw-r--r-- | lib/MiddleWare/PluginManager.cpp | 254 | ||||
-rw-r--r-- | lib/MiddleWare/PluginManager.h | 78 | ||||
-rw-r--r-- | lib/MiddleWare/Reporter.h | 35 | ||||
-rw-r--r-- | lib/MiddleWare/test.cpp | 58 |
15 files changed, 1150 insertions, 0 deletions
diff --git a/lib/MiddleWare/Application.h b/lib/MiddleWare/Application.h new file mode 100644 index 00000000..b68f9d50 --- /dev/null +++ b/lib/MiddleWare/Application.h @@ -0,0 +1,35 @@ +/* + Application.h - header file for application plugin + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef APPLICATION_H_ +#define APPLICATION_H_ + +#include <string> +#include "Plugin.h" + +class CApplication : public CPlugin +{ + public: + virtual ~CApplication() {} + virtual std::string GetReport(void* pData) = 0; +}; + +#endif /*APPLICATION_H_*/ diff --git a/lib/MiddleWare/CrashCatcherPlugin.cpp b/lib/MiddleWare/CrashCatcherPlugin.cpp new file mode 100644 index 00000000..e50ba535 --- /dev/null +++ b/lib/MiddleWare/CrashCatcherPlugin.cpp @@ -0,0 +1,154 @@ +/* + CrashCatcherPlugin.cpp + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "CrashCatcherPlugin.h" + +CCrashCatcherPlugin::CCrashCatcherPlugin(const std::string& pLibPath) : + m_pDynamicLibrary(NULL), + m_pPluginInfo(NULL), + m_pFnPluginNew(NULL), + m_bEnabled(false) +{ + try + { + m_pDynamicLibrary = new CDynamicLibrary(pLibPath); + if (m_pDynamicLibrary == NULL) + { + throw std::string("Not enought memory."); + } + m_pPluginInfo = (p_plugin_info_t) m_pDynamicLibrary->FindSymbol("plugin_info"); + m_pFnPluginNew = (p_fn_plugin_new_t) m_pDynamicLibrary->FindSymbol("plugin_new"); + } + catch (...) + { + throw; + } +} + +CCrashCatcherPlugin::~CCrashCatcherPlugin() +{ + if (m_pDynamicLibrary != NULL) + { + delete m_pDynamicLibrary; + } +} + +void CCrashCatcherPlugin::LoadSettings(const std::string& pPath) +{ + std::ifstream fIn; + fIn.open(pPath.c_str()); + if (fIn.is_open()) + { + std::string line; + while (!fIn.eof()) + { + getline(fIn, line); + + int ii; + bool is_value = false; + std::string key = ""; + std::string value = ""; + for (ii = 0; ii < line.length(); ii++) + { + if (!isspace(line[ii])) + { + if (line[ii] == '#') + { + break; + } + else if (line[ii] == '=') + { + is_value = true; + } + else if (line[ii] == '=' && is_value) + { + key = ""; + value = ""; + break; + } + else if (!is_value) + { + key += line[ii]; + } + else + { + value += line[ii]; + } + } + } + if (key != "") + { + m_mapSettings[key] = value; + } + } + fIn.close(); + } +} + +const bool CCrashCatcherPlugin::IsEnabled() +{ + return m_mapSettings["Enabled"] == "yes"; +} + +const std::string& CCrashCatcherPlugin::GetVersion() +{ + return m_pPluginInfo->m_sVersion; +} + +const int CCrashCatcherPlugin::GetMagicNumber() +{ + return m_pPluginInfo->m_nMagicNumber; +} + +const std::string& CCrashCatcherPlugin::GetName() +{ + return m_pPluginInfo->m_sName; +} + +const std::string& CCrashCatcherPlugin::GetDescription() +{ + return m_pPluginInfo->m_sDescription; +} + +const std::string& CCrashCatcherPlugin::GetEmail() +{ + return m_pPluginInfo->m_sEmail; +} + +const std::string& CCrashCatcherPlugin::GetWWW() +{ + return m_pPluginInfo->m_sWWW; +} + +const plugin_type_t CCrashCatcherPlugin::GetType() +{ + return m_pPluginInfo->m_Type; +} + +CPlugin* CCrashCatcherPlugin::PluginNew() +{ + return m_pFnPluginNew(); +} + +const map_settings_t& CCrashCatcherPlugin::GetSettings() +{ + return m_mapSettings; +} diff --git a/lib/MiddleWare/CrashCatcherPlugin.h b/lib/MiddleWare/CrashCatcherPlugin.h new file mode 100644 index 00000000..0dcf4af3 --- /dev/null +++ b/lib/MiddleWare/CrashCatcherPlugin.h @@ -0,0 +1,63 @@ +/* + CrashCatcherPlugin.h - header file for CrashCatcher plugin. It takes care + of reporting thinks which has loaded plugin. + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef CRASHCATCHERPLUGIN_H_ +#define CRASHCATCHERPLUGIN_H_ + +#include <string> +#include "DynamicLibrary.h" +#include "Plugin.h" + +class CCrashCatcherPlugin +{ + private: + + typedef const plugin_info_t* p_plugin_info_t; + typedef CPlugin* (*p_fn_plugin_new_t)(); + + CDynamicLibrary* m_pDynamicLibrary; + p_plugin_info_t m_pPluginInfo; + p_fn_plugin_new_t m_pFnPluginNew; + + bool m_bEnabled; + + map_settings_t m_mapSettings; + public: + CCrashCatcherPlugin(const std::string& pLibPath); + ~CCrashCatcherPlugin(); + + void LoadSettings(const std::string& pPath); + const bool IsEnabled(); + const std::string& GetVersion(); + const int GetMagicNumber(); + const std::string& GetName(); + const std::string& GetDescription(); + const std::string& GetEmail(); + const std::string& GetWWW(); + const plugin_type_t GetType(); + + CPlugin* PluginNew(); + const map_settings_t& GetSettings(); +}; + +#endif /*CRASHCATCHERPLUGIN_H_*/ diff --git a/lib/MiddleWare/Database.h b/lib/MiddleWare/Database.h new file mode 100644 index 00000000..da8567ab --- /dev/null +++ b/lib/MiddleWare/Database.h @@ -0,0 +1,98 @@ +/* + Database.h - header file for database plugin + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef DATABASE_H_ +#define DATABASE_H_ + +#include <string> +#include <vector> +#include "Plugin.h" + +/* + * Table + * ===== + * UUID | DebugDumpPath | Architecture | Kernel | ProgramPath | Package | + * UID | Time | Count | Reported | BackTrace | TextData1 + * + * in the future we can add another TextData if we need it + */ + +#define DATABASE_COLUMN_UUID "UUID" +#define DATABASE_COLUMN_DEBUG_DUMP_PATH "DebugDumpPath" +#define DATABASE_COLUMN_ARCHITECTURE "Architecture" +#define DATABASE_COLUMN_KERNEL "Kernel" +#define DATABASE_COLUMN_EXECUTABLE "Executable" +#define DATABASE_COLUMN_PACKAGE "Package" +#define DATABASE_COLUMN_UID "UID" +#define DATABASE_COLUMN_TIME "Time" +#define DATABASE_COLUMN_COUNT "Count" +#define DATABASE_COLUMN_REPORTED "Reported" +#define DATABASE_COLUMN_BACKTRACE "BackTrace" +#define DATABASE_COLUMN_TEXTDATA1 "TextData1" + +typedef struct SDatabaseRow +{ + std::string m_sUUID; + std::string m_sDebugDumpPath; + std::string m_sArchitecture; + std::string m_sKernel; + std::string m_sExecutable; + std::string m_sPackage; + std::string m_sUID; + std::string m_sTime; + std::string m_sCount; + std::string m_sReported; + std::string m_sBackTrace; + std::string m_sTextData1; +} database_row_t; + +// <column_name, <array of values in all selected rows> > +typedef std::vector<database_row_t> vector_database_rows_t; + +class CDatabase : public CPlugin +{ + public: + virtual ~CDatabase() {} + + virtual void Connect() = 0; + virtual void DisConnect() = 0; + virtual void Insert(const std::string& pUUID, + const std::string& pDebugDumpPath, + const std::string& pArch, + const std::string& pKernel, + const std::string& pExecutable, + const std::string& pPackage, + const std::string& pUID, + const std::string& pTime) = 0; + + virtual void InsertBackTrace(const std::string& pUUID, + const std::string& pBackTrace) = 0; + + virtual void InsertTextData1(const std::string& pUUID, + const std::string& pData) = 0; + + virtual void Delete(const std::string& pUUID) = 0; + + const vector_database_rows_t GetUIDData(const std::string& pUID); +}; + +#endif /* DATABASE_H_ */ diff --git a/lib/MiddleWare/DynamicLibrary.cpp b/lib/MiddleWare/DynamicLibrary.cpp new file mode 100644 index 00000000..1c7c1cbd --- /dev/null +++ b/lib/MiddleWare/DynamicLibrary.cpp @@ -0,0 +1,57 @@ +/* + DynamicLybrary.cpp + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "DynamicLibrary.h" +#include <iostream> + +CDynamicLibrary::CDynamicLibrary(const std::string& pPath) : + m_pHandle(NULL) +{ + Load(pPath); +} + +CDynamicLibrary::~CDynamicLibrary() +{ + if (m_pHandle != NULL) + { + dlclose(m_pHandle); + m_pHandle = NULL; + } +} + +void CDynamicLibrary::Load(const std::string& pPath) +{ + m_pHandle = dlopen(pPath.c_str(), RTLD_NOW); + if (m_pHandle == NULL) + { + throw "CDynamicLibrary::Load(): Cannot load " + pPath + " : " + std::string(dlerror()); + } +} + +void* CDynamicLibrary::FindSymbol(const std::string& pName) +{ + void* sym = dlsym(m_pHandle, pName.c_str()); + if (sym == NULL) + { + throw "CDynamicLibrary::Load(): Cannot find symbol '" + pName + "'"; + } + return sym; +} diff --git a/lib/MiddleWare/DynamicLibrary.h b/lib/MiddleWare/DynamicLibrary.h new file mode 100644 index 00000000..4962068c --- /dev/null +++ b/lib/MiddleWare/DynamicLibrary.h @@ -0,0 +1,42 @@ +/* + DynamicLybrary.h - header file for dynamic lybrarby wraper. It uses libdl. + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef DYNAMICLIBRARYH_ +#define DYNAMICLIBRARYH_ + +#include <string> +#include <dlfcn.h> + +class CDynamicLibrary +{ + private: + void* m_pHandle; + + void Load(const std::string& pPath); + public: + CDynamicLibrary(const std::string& pPath); + ~CDynamicLibrary(); + + void* FindSymbol(const std::string& pName); +}; + +#endif /*DYNAMICLIBRARYH_*/ diff --git a/lib/MiddleWare/Language.h b/lib/MiddleWare/Language.h new file mode 100644 index 00000000..81024a53 --- /dev/null +++ b/lib/MiddleWare/Language.h @@ -0,0 +1,36 @@ +/* + Language.h - header file for language plugin + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef LANGUAGE_H_ +#define LANGUAGE_H_ + +#include <string> +#include "Plugin.h" + +class CLanguage : public CPlugin +{ + public: + virtual ~CLanguage() {} + virtual std::string GetUUID(void* pData) = 0; + virtual std::string GetReport(void* pData) = 0; +}; + +#endif /*LANGUAGE_H_*/ diff --git a/lib/MiddleWare/Makefile.am b/lib/MiddleWare/Makefile.am new file mode 100644 index 00000000..a6aa22e2 --- /dev/null +++ b/lib/MiddleWare/Makefile.am @@ -0,0 +1,16 @@ +lib_LTLIBRARIES = libMiddleWare.la +libMiddleWare_la_SOURCES = MiddleWare.cpp MiddleWare.h PluginManager.cpp \ + PluginManager.h CrashCatcherPlugin.cpp \ + CrashCatcherPlugin.h DynamicLibrary.cpp \ + DynamicLibrary.h +libMiddleWare_la_LIBADD = $(DL_LIBS) +libMiddleWare_la_LDFLAGS = -version-info 0:1:0 + + +check_PROGRAMS = test +test_SOURCES = test.cpp +test_LDADD = ../DebugDump/libDebugDump.la libMiddleWare.la $(DL_LIBS) +test_CPPFLAGS = -I$(srcdir)/../DebugDump \ + -DPLUGINS_LIB_DIR=\"$(PLUGINS_LIB_DIR)\" \ + -DPLUGINS_CONF_DIR=\"$(PLUGINS_CONF_DIR)\" \ + -DDEBUG_DUMPS_DIR=\"$(DEBUG_DUMPS_DIR)\"
\ No newline at end of file diff --git a/lib/MiddleWare/MiddleWare.cpp b/lib/MiddleWare/MiddleWare.cpp new file mode 100644 index 00000000..85b88c9c --- /dev/null +++ b/lib/MiddleWare/MiddleWare.cpp @@ -0,0 +1,87 @@ +/* + MiddleWare.cpp + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "MiddleWare.h" + +CMiddleWare::CMiddleWare(const std::string& pPlugisConfDir, + const std::string& pPlugisLibDir) : + m_PluginManager(NULL) +{ + m_PluginManager = new CPluginManager(pPlugisConfDir, pPlugisLibDir); + if (m_PluginManager == NULL) + { + throw std::string("Not enought memory."); + } +} + +CMiddleWare::~CMiddleWare() +{ + if (m_PluginManager != NULL) + { + delete m_PluginManager; + } +} + +void CMiddleWare::LoadPlugins() +{ + m_PluginManager->LoadPlugins(); +} + +void CMiddleWare::LoadPlugin(const std::string& pName) +{ + m_PluginManager->LoadPlugin(pName); +} + +void CMiddleWare::UnLoadPlugin(const std::string& pName) +{ + m_PluginManager->UnLoadPlugin(pName); +} + +std::string CMiddleWare::GetUUID(const std::string& pLanguage, void* pData) +{ + CLanguage* language = m_PluginManager->GetLanguage(pLanguage); + if (language == NULL) + { + return ""; + } + return language->GetUUID(pData); +} + +std::string CMiddleWare::GetReport(const std::string& pLanguage, void* pData) +{ + CLanguage* language = m_PluginManager->GetLanguage(pLanguage); + if (language == NULL) + { + return ""; + } + return language->GetReport(pData); +} + +int CMiddleWare::Report(const std::string& pReporter, const std::string& pDebugDumpPath) +{ + CReporter* reporter = m_PluginManager->GetReporter(pReporter); + if (reporter == NULL) + { + return -1; + } + reporter->Report(pDebugDumpPath); +} + diff --git a/lib/MiddleWare/MiddleWare.h b/lib/MiddleWare/MiddleWare.h new file mode 100644 index 00000000..105266e2 --- /dev/null +++ b/lib/MiddleWare/MiddleWare.h @@ -0,0 +1,51 @@ +/* + MiddleWare.h - header file for MiddleWare library. It wraps plugins and + take case of them. + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef MIDDLEWARE_H_ +#define MIDDLEWARE_H_ + +#include "PluginManager.h" + +class CMiddleWare +{ + private: + + CPluginManager* m_PluginManager; + + public: + CMiddleWare(const std::string& pPlugisConfDir, + const std::string& pPlugisLibDir); + + ~CMiddleWare(); + + void LoadPlugins(); + void LoadPlugin(const std::string& pName); + void UnLoadPlugin(const std::string& pName); + + std::string GetUUID(const std::string& pLanguage, void* pData); + std::string GetReport(const std::string& pLanguage, void* pData); + int Report(const std::string& pReporter, const std::string& pDebugDumpPath); + //void SaveDebugDumpToDataBase(const std::string& pPath); +}; + +#endif /*MIDDLEWARE_H_*/ diff --git a/lib/MiddleWare/Plugin.h b/lib/MiddleWare/Plugin.h new file mode 100644 index 00000000..7b4168c4 --- /dev/null +++ b/lib/MiddleWare/Plugin.h @@ -0,0 +1,86 @@ +/* + Plugin.h - header file for plugin. It contains mandatory macros + and common function for plugins + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef PLUGIN_H_ +#define PLUGIN_H_ + +#include <string> +#include <map> +#include <fstream> + +#define PLUGINS_MAGIC_NUMBER 1 + +#define PLUGINS_CONF_EXTENSION "conf" +#define PLUGINS_LIB_EXTENSIONS "so" + +typedef std::map<std::string, std::string> map_settings_t; + +class CPlugin +{ + public: + virtual ~CPlugin() {} + + virtual void Init(const map_settings_t& pSettings) = 0; + virtual void DeInit() = 0; +}; + +typedef enum { LANGUAGE, REPORTER, APPLICATION, DATABASE } plugin_type_t; + +typedef struct SPluginInfo +{ + const plugin_type_t m_Type; + const std::string m_sName; + const std::string m_sVersion; + const std::string m_sDescription; + const std::string m_sEmail; + const std::string m_sWWW; + const int m_nMagicNumber; +} plugin_info_t; + +#define PLUGIN_IFACE extern "C" + +#define PLUGIN_INIT(plugin_class)\ + PLUGIN_IFACE CPlugin* plugin_new()\ + {\ + plugin_class* plugin = new plugin_class();\ + if (plugin == NULL)\ + {\ + throw std::string("Not enought memory");\ + }\ + return plugin;\ + }\ + + +#define PLUGIN_INFO(type, name, version,\ + description, email, www)\ + PLUGIN_IFACE const plugin_info_t plugin_info =\ + {\ + type,\ + name,\ + version,\ + description,\ + email,\ + www,\ + PLUGINS_MAGIC_NUMBER,\ + }; + +#endif /* PLUGIN_H_ */ diff --git a/lib/MiddleWare/PluginManager.cpp b/lib/MiddleWare/PluginManager.cpp new file mode 100644 index 00000000..4e4c56da --- /dev/null +++ b/lib/MiddleWare/PluginManager.cpp @@ -0,0 +1,254 @@ +/* + PluginManager.cpp + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <iostream> +#include "PluginManager.h" +#include <dirent.h> +#include <stdio.h> +#include <sys/types.h> + +CPluginManager::CPluginManager(const std::string& pPlugisConfDir, + const std::string& pPlugisLibDir) : + m_sPlugisConfDir(pPlugisConfDir), + m_sPlugisLibDir(pPlugisLibDir) +{} + +CPluginManager::~CPluginManager() +{ + map_crash_catcher_plugins_t::iterator it_p; + while ((it_p = m_mapCrashCatcherPlugins.begin()) != m_mapCrashCatcherPlugins.end()) + { + std::string pluginName = it_p->first; + UnLoadPlugin(pluginName); + } +} + +void CPluginManager::LoadPlugins() +{ + DIR *dir = opendir(m_sPlugisConfDir.c_str()); + struct dirent *dent = NULL; + if (dir != NULL) + { + while ((dent = readdir(dir)) != NULL) + { + if (dent->d_type == DT_REG) + { + std::string name = dent->d_name; + std::string extension = name.substr(name.length()-sizeof(PLUGINS_CONF_EXTENSION)+1); + if (extension == PLUGINS_CONF_EXTENSION) + { + name.erase(name.length()-sizeof(PLUGINS_CONF_EXTENSION)); + LoadPlugin(name); + } + } + } + closedir(dir); + } +} + +void CPluginManager::LoadPlugin(const std::string& pName) +{ + if (m_mapCrashCatcherPlugins.find(pName) == m_mapCrashCatcherPlugins.end()) + { + CCrashCatcherPlugin* crashCatcherPlugin = NULL; + CPlugin* plugin = NULL; + try + { + std::string libPath = m_sPlugisLibDir + "/lib" + pName + "." + PLUGINS_LIB_EXTENSIONS; + crashCatcherPlugin = new CCrashCatcherPlugin(libPath); + if (crashCatcherPlugin->GetMagicNumber() != PLUGINS_MAGIC_NUMBER) + { + throw std::string("non-compatible plugin"); + } + crashCatcherPlugin->LoadSettings(m_sPlugisConfDir + "/" + pName + "." + PLUGINS_CONF_EXTENSION); + std::cerr << "Loaded Plugin " << pName << " (" << crashCatcherPlugin->GetVersion() << ") " << "succesfully loaded." << std::endl; + std::cerr << " Description: " << crashCatcherPlugin->GetDescription() << std::endl; + std::cerr << " Email: " << crashCatcherPlugin->GetEmail() << std::endl; + std::cerr << " WWW: " << crashCatcherPlugin->GetWWW() << std::endl; + m_mapCrashCatcherPlugins[pName] = crashCatcherPlugin; + + if (crashCatcherPlugin->IsEnabled()) + { + plugin = crashCatcherPlugin->PluginNew(); + plugin->Init(crashCatcherPlugin->GetSettings()); + RegisterPlugin(plugin, pName, crashCatcherPlugin->GetType()); + } + + } + catch (std::string sError) + { + if (plugin != NULL) + { + delete plugin; + } + if (crashCatcherPlugin != NULL) + { + delete plugin; + } + std::cerr << "Failed to load plugin " << pName << " (" << sError << ")." << std::endl; + } + } +} + +void CPluginManager::UnLoadPlugin(const std::string& pName) +{ + if (m_mapCrashCatcherPlugins.find(pName) != m_mapCrashCatcherPlugins.end()) + { + UnRegisterPlugin(pName, m_mapCrashCatcherPlugins[pName]->GetType()); + delete m_mapCrashCatcherPlugins[pName]; + m_mapCrashCatcherPlugins.erase(pName); + std::cerr << "Plugin " << pName << " sucessfully unloaded." << std::endl; + } +} + + +void CPluginManager::RegisterPlugin(CPlugin* pPlugin, + const std::string pName, + const plugin_type_t& pPluginType) +{ + switch (pPluginType) + { + case LANGUAGE: + { + m_mapLanguages[pName] = (CLanguage*)pPlugin; + std::cerr << "Registred Language plugin " << pName << std::endl; + } + break; + case REPORTER: + { + m_mapReporters[pName] = (CReporter*)pPlugin; + std::cerr << "Registred Reporter plugin " << pName << std::endl; + } + break; + case APPLICATION: + { + m_mapApplications[pName] = (CApplication*)pPlugin; + std::cerr << "Registred Application plugin " << pName << std::endl; + } + break; + case DATABASE: + { + m_mapDatabases[pName] = (CDatabase*)pPlugin; + std::cerr << "Registred Database plugin " << pName << std::endl; + } + break; + default: + { + std::cerr << "Trying to register unknown type of plugin." << std::endl; + } + break; + } +} + +void CPluginManager::UnRegisterPlugin(const std::string pName, const plugin_type_t& pPluginType) +{ + switch (pPluginType) + { + case LANGUAGE: + { + if (m_mapLanguages.find(pName) != m_mapLanguages.end()) + { + m_mapLanguages[pName]->DeInit(); + delete m_mapLanguages[pName]; + m_mapLanguages.erase(pName); + std::cerr << "UnRegistred Language plugin " << pName << std::endl; + } + } + break; + case REPORTER: + { + if (m_mapReporters.find(pName) != m_mapReporters.end()) + { + m_mapReporters[pName]->DeInit(); + delete m_mapReporters[pName]; + m_mapReporters.erase(pName); + std::cerr << "UnRegistred Reporter plugin " << pName << std::endl; + } + } + break; + case APPLICATION: + { + if (m_mapApplications.find(pName) != m_mapApplications.end()) + { + m_mapApplications[pName]->DeInit(); + delete m_mapApplications[pName]; + m_mapApplications.erase(pName); + std::cerr << "UnRegistred Application plugin " << pName << std::endl; + } + } + break; + case DATABASE: + { + if (m_mapDatabases.find(pName) != m_mapDatabases.end()) + { + m_mapDatabases[pName]->DeInit(); + delete m_mapDatabases[pName]; + m_mapDatabases.erase(pName); + std::cerr << "UnRegistred Database plugin " << pName << std::endl; + } + } + break; + default: + std::cerr << "Trying to unregister unknown type of plugin." << std::endl; + break; + } +} + +CLanguage* CPluginManager::GetLanguage(const std::string& pName) +{ + if (m_mapLanguages.find(pName) != m_mapLanguages.end()) + { + return m_mapLanguages[pName]; + } + + return NULL; +} + +CReporter* CPluginManager::GetReporter(const std::string& pName) +{ + if (m_mapReporters.find(pName) != m_mapReporters.end()) + { + return m_mapReporters[pName]; + } + + return NULL; +} + +CApplication* CPluginManager::GetApplication(const std::string& pName) +{ + if (m_mapApplications.find(pName) != m_mapApplications.end()) + { + return m_mapApplications[pName]; + } + + return NULL; +} + +CDatabase* CPluginManager::GetDatabase(const std::string& pName) +{ + if (m_mapDatabases.find(pName) != m_mapDatabases.end()) + { + return m_mapDatabases[pName]; + } + + return NULL; +} + diff --git a/lib/MiddleWare/PluginManager.h b/lib/MiddleWare/PluginManager.h new file mode 100644 index 00000000..56799833 --- /dev/null +++ b/lib/MiddleWare/PluginManager.h @@ -0,0 +1,78 @@ +/* + PluginManager.h - header file for plugin manager. it takes care about + (un)loading plugins + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef PLUGINMANAGER_H_ +#define PLUGINMANAGER_H_ + +#include <map> +#include <string> +#include "CrashCatcherPlugin.h" +#include "Plugin.h" +#include "Language.h" +#include "Reporter.h" +#include "Database.h" +#include "Application.h" + +class CPluginManager +{ + private: + typedef std::map<std::string, CCrashCatcherPlugin*> map_crash_catcher_plugins_t; + typedef std::map<std::string, CLanguage*> map_languages_t; + typedef std::map<std::string, CReporter*> map_reporters_t; + typedef std::map<std::string, CApplication*> map_applications_t; + typedef std::map<std::string, CDatabase*> map_databases_t; + + + map_crash_catcher_plugins_t m_mapCrashCatcherPlugins; + map_languages_t m_mapLanguages; + map_reporters_t m_mapReporters; + map_applications_t m_mapApplications; + map_databases_t m_mapDatabases; + + std::string m_sPlugisConfDir; + std::string m_sPlugisLibDir; + + void RegisterPlugin(CPlugin* pPlugin, + const std::string pName, + const plugin_type_t& pPluginType); + + void UnRegisterPlugin(const std::string pName, + const plugin_type_t& pPluginType); + + public: + CPluginManager(const std::string& pPlugisConfDir, + const std::string& pPlugisLibDir); + + ~CPluginManager(); + + void LoadPlugins(); + void LoadPlugin(const std::string& pName); + void UnLoadPlugin(const std::string& pName); + + CLanguage* GetLanguage(const std::string& pName); + CReporter* GetReporter(const std::string& pName); + CApplication* GetApplication(const std::string& pName); + CDatabase* GetDatabase(const std::string& pName); + +}; + +#endif /*PLUGINMANAGER_H_*/ diff --git a/lib/MiddleWare/Reporter.h b/lib/MiddleWare/Reporter.h new file mode 100644 index 00000000..cff912bc --- /dev/null +++ b/lib/MiddleWare/Reporter.h @@ -0,0 +1,35 @@ +/* + Reporter.h - header file for reporter plugin + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef REPORTER_H_ +#define REPORTER_H_ + +#include <string> +#include "Plugin.h" + +class CReporter : public CPlugin +{ + public: + virtual ~CReporter() {} + virtual void Report(const std::string& pDebugDumpPath) = 0; +}; + +#endif /* REPORTER_H_ */ diff --git a/lib/MiddleWare/test.cpp b/lib/MiddleWare/test.cpp new file mode 100644 index 00000000..380af469 --- /dev/null +++ b/lib/MiddleWare/test.cpp @@ -0,0 +1,58 @@ +/* + test.cpp - simple library test + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + 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 2 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "MiddleWare.h" +#include "DebugDump.h" +#include <iostream> +#include <sys/types.h> +#include <unistd.h> + + +int main(int argc, char** argv) +{ + + + try + { + CMiddleWare middleWare(PLUGINS_CONF_DIR, PLUGINS_LIB_DIR); + CDebugDump* dd; + + middleWare.LoadPlugins(); + middleWare.UnLoadPlugin("Mailx"); + + dd = new CDebugDump(DEBUG_DUMPS_DIR); + + dd->Delete(); + dd->Create(); + dd->SaveTextFile("UUID", middleWare.GetUUID("CCpp", (void*)"data")); + char pid[100]; + sprintf(pid, "%d", getpid()); + dd->SaveProc(pid); + + delete dd; + } + catch (std::string sError) + { + std::cerr << sError << std::endl; + } + + return 0; +} |