diff options
author | randomdan <randomdan@localhost> | 2013-09-13 17:34:05 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2013-09-13 17:34:05 +0000 |
commit | cd292163113134ccb7e037da70718bb6aaf6567c (patch) | |
tree | 5b5c4b24b6fed32d243c16303055d3b9bdbc9bcd | |
parent | Explicit daemon destruction in order (diff) | |
download | project2-cd292163113134ccb7e037da70718bb6aaf6567c.tar.bz2 project2-cd292163113134ccb7e037da70718bb6aaf6567c.tar.xz project2-cd292163113134ccb7e037da70718bb6aaf6567c.zip |
Add a module for preloading libraries, which supersedes that in daemon
-rw-r--r-- | project2/basics/Jamfile.jam | 2 | ||||
-rw-r--r-- | project2/basics/preload.cpp | 52 | ||||
-rw-r--r-- | project2/daemon/p2daemonAppEngine.cpp | 18 | ||||
-rw-r--r-- | project2/daemon/p2daemonAppEngine.h | 4 |
4 files changed, 56 insertions, 20 deletions
diff --git a/project2/basics/Jamfile.jam b/project2/basics/Jamfile.jam index b276ba1..02de4c3 100644 --- a/project2/basics/Jamfile.jam +++ b/project2/basics/Jamfile.jam @@ -3,6 +3,7 @@ alias glibmm : : : : <linkflags>"`pkg-config --libs glibmm-2.4`" ; lib boost_filesystem : : <name>boost_filesystem ; +lib dl ; cpp-pch pch : pch.hpp : <include>../../libmisc @@ -16,6 +17,7 @@ lib p2basics : <include>. <include>../../libmisc <library>glibmm + <library>dl <library>boost_filesystem <library>../common//p2common ; diff --git a/project2/basics/preload.cpp b/project2/basics/preload.cpp new file mode 100644 index 0000000..e8aa6e9 --- /dev/null +++ b/project2/basics/preload.cpp @@ -0,0 +1,52 @@ +#include <pch.hpp> +#include <options.h> +#include <boost/filesystem/path.hpp> +#include <map> +#include <dlfcn.h> + +using namespace boost::filesystem; + +class Preload { + public: + INITOPTIONS; + + private: + typedef boost::shared_ptr<void> Library; + typedef std::map<path, Library> Libraries; + + static void LoadLibrary(const VariableType & librarypath) + { + const auto beforeOpts = *Plugable::ComponentType<Options>::components(); + + void * handle = dlopen(librarypath, RTLD_NOW); + if (handle) { + Logger()->messagebf(LOG_DEBUG, "Loaded library '%s'", librarypath.as<std::string>()); + } + else { + Logger()->messagebf(LOG_ERR, "Failed to load library '%s' (%s)", librarypath.as<std::string>(), dlerror()); + throw std::runtime_error("module load failed"); + } + libs[librarypath.as<std::string>()] = boost::shared_ptr<void>(handle, &dlclose); + + const auto afterOpts = Plugable::ComponentType<Options>::components(); + BOOST_FOREACH(const auto & opt, *afterOpts) { + if (std::find(beforeOpts.begin(), beforeOpts.end(), opt) == beforeOpts.end()) { + opt->reset(); + } + } + } + + static void UnloadLibraries() + { + libs.clear(); + } + + static Libraries libs; +}; + +Preload::Libraries Preload::libs; + +DECLARE_OPTIONS(Preload, "Library preload options") +("library", Options::functions(Preload::LoadLibrary, Preload::UnloadLibraries), "Libraries to preload") +END_OPTIONS(Preload) + diff --git a/project2/daemon/p2daemonAppEngine.cpp b/project2/daemon/p2daemonAppEngine.cpp index cb414c5..7daed70 100644 --- a/project2/daemon/p2daemonAppEngine.cpp +++ b/project2/daemon/p2daemonAppEngine.cpp @@ -1,20 +1,18 @@ #include "p2daemonAppEngine.h" #include <logger.h> +#include <optionsSource.h> #include <dlfcn.h> std::string DaemonAppEngine::daemonType; -boost::optional<std::string> DaemonAppEngine::libraryPath; Glib::ustring DaemonAppEngine::reqPlatform; DaemonPtr DaemonAppEngine::daemon; DECLARE_OPTIONS(DaemonAppEngine, "Project2 Daemon options") ("daemon.type", Options::value(&daemonType), "The core daemon module to run") -("daemon.library", Options::value(&libraryPath), "The optional library containing the core daemon to load") ("daemon.platform", Options::value(&reqPlatform), "Platform") END_OPTIONS(DaemonAppEngine); -DaemonAppEngine::DaemonAppEngine(int argc, char ** argv) : - library(!!libraryPath ? loadLibrary(*libraryPath) : boost::shared_ptr<void>()) +DaemonAppEngine::DaemonAppEngine(int argc, char ** argv) { daemon = DaemonLoader::createNew(daemonType, argc, argv); } @@ -54,15 +52,3 @@ DaemonAppEngine::process() const Logger()->message(LOG_INFO, "Daemon exitted."); } -boost::shared_ptr<void> -DaemonAppEngine::loadLibrary(const std::string & path) -{ - void * handle = dlopen(path.c_str(), RTLD_NOW); - if (!handle) { - Logger()->messagebf(LOG_ERR, "Failed to load library '%s' (%s)", path, dlerror()); - throw std::runtime_error("module load failed"); - } - return boost::shared_ptr<void>(handle, &dlclose); -} - - diff --git a/project2/daemon/p2daemonAppEngine.h b/project2/daemon/p2daemonAppEngine.h index 236bef6..e6632f2 100644 --- a/project2/daemon/p2daemonAppEngine.h +++ b/project2/daemon/p2daemonAppEngine.h @@ -16,12 +16,8 @@ class DaemonAppEngine { static Glib::ustring reqPlatform; static std::string daemonType; - static boost::optional<std::string> libraryPath; private: - static boost::shared_ptr<void> loadLibrary(const std::string & path); - - boost::shared_ptr<void> library; static void shutdown(int); static DaemonPtr daemon; }; |