/* Copyright 2010 sss * This file is part of evil_core. * * evil_core 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. * * evil_core 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 evil_core. If not, see .*/ #include "commonheaders.h" extern std::list plugins; extern PLUGINLINK pluglink; void load_modules() { std::string path = boost::filesystem::initial_path().directory_string(); //need some workaround for windows than called indirectly path.append("/plugins"); logger.log(LM_DEBUG, "Plugin directory is %s \n", path.c_str()); logger.log(LM_DEBUG, "Loading plugins...\n"); boost::filesystem::path pth(path); if(!boost::filesystem::is_directory(pth)) return; boost::filesystem::directory_iterator i(pth), end; while(i != end) { if(boost::filesystem::is_directory(*i)) //we not look in subdirectories { logger.log(LM_DEBUG, "found subdirectory under plugins directory, skipping.\n"); ++i; continue; } if(!boost::filesystem::status_known((*i).status())) //worng data { logger.log(LM_DEBUG, "found invalid filesystem object, skipping.\n"); ++i; continue; } bool is_plugin = true; plugin::exported_functions_s *funcs = new plugin::exported_functions_s; ACE_DLL *dll = new ACE_DLL; boost::filesystem::path pth = i->path(); if(dll->open(pth.string().c_str()) != -1) { if((funcs->Load = (load)dll->symbol("load")) == NULL) { is_plugin = false; logger.log(LM_DEBUG, "\'load\' not found in library, this is not plugin.\n"); } if((funcs->OnModulesLoaded = (on_modules_loaded)dll->symbol("on_modules_loaded")) == NULL) { is_plugin = false; logger.log(LM_DEBUG, "\'on_modules_loaded\' not found in library, this is not plugin.\n"); } if((funcs->Unload = (unload)dll->symbol("unload")) == NULL) { is_plugin = false; logger.log(LM_DEBUG, "\'unload\' not found in library, this is not plugin.\n"); } if((funcs->SetPluginInfo = (set_plugin_info)dll->symbol("set_plugin_info")) == NULL) { is_plugin = false; logger.log(LM_DEBUG, "\'set_plugin_info\' not found in library, this is not plugin.\n"); } } if(!is_plugin) { delete funcs; delete dll; continue; } PLUGININFO *info = funcs->SetPluginInfo(); plugins.push_back(new plugin(dll, info, funcs)); ++i; } } void run_plugins() { //now for testing only if(!plugins.empty()) { std::list::iterator end = plugins.end(); for(std::list::iterator i = plugins.begin(); i != end; ++i) { boost::thread *thr = new boost::thread(boost::bind((*i)->get_exported_functions()->Load, &pluglink)); if(!thr->timed_join(boost::posix_time::seconds(10))) logger.log(LM_DEBUG, "Thread execution timeout, plugin \"%s\" basic initialisation failed.\n", toUTF8((*i)->get_plugininfo()->name).c_str()); else logger.log(LM_DEBUG, "plugin \"%s\" basic initialisation success.\n", toUTF8((*i)->get_plugininfo()->name).c_str()); delete thr; } for(std::list::iterator i = plugins.begin(); i != end; ++i) { boost::thread *thr = new boost::thread(boost::bind((*i)->get_exported_functions()->OnModulesLoaded)); if(!thr->timed_join(boost::posix_time::seconds(15))) logger.log(LM_DEBUG, "Thread execution timeout, plugin \"%s\" main initialisation failed.\n", toUTF8((*i)->get_plugininfo()->name).c_str()); else logger.log(LM_DEBUG, "plugin \"%s\" main initialisation success.\n", toUTF8((*i)->get_plugininfo()->name).c_str()); delete thr; } } } plugin::plugin(ACE_DLL *lib, PLUGININFO *info, exported_functions_s *funcs) { if(lib) plug = lib; else plug = NULL; if(info) plugininfo = info; else plugininfo = NULL; if(funcs) exported_funcs = funcs; else exported_funcs = NULL; } plugin::~plugin() { if(plug) delete plug; if(exported_funcs) delete exported_funcs; } const PLUGININFO* plugin::get_plugininfo() { return plugininfo; } const plugin::exported_functions_s* plugin::get_exported_functions() { return exported_funcs; }