1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/library.hpp"
4 #include "base/loader.hpp"
5 #include "base/logger.hpp"
6 #include "base/exception.hpp"
7 #include "base/application.hpp"
9 using namespace icinga;
12 * Loads the specified library.
14 * @param name The name of the library.
16 Library::Library(const String& name)
21 #elif defined(__APPLE__)
22 path = "lib" + name + "." + Application::GetAppSpecVersion() + ".dylib";
24 path = "lib" + name + ".so." + Application::GetAppSpecVersion();
27 Log(LogNotice, "Library")
28 << "Loading library '" << path << "'";
31 HMODULE hModule = LoadLibrary(path.CStr());
34 BOOST_THROW_EXCEPTION(win32_error()
35 << boost::errinfo_api_function("LoadLibrary")
36 << errinfo_win32_error(GetLastError())
37 << boost::errinfo_file_name(path));
40 void *hModule = dlopen(path.CStr(), RTLD_NOW | RTLD_GLOBAL);
43 BOOST_THROW_EXCEPTION(std::runtime_error("Could not load library '" + path + "': " + dlerror()));
47 Loader::ExecuteDeferredInitializers();
49 m_Handle.reset(new LibraryHandle(hModule), [](LibraryHandle *handle) {
58 void *Library::GetSymbolAddress(const String& name) const
61 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid library handle"));
64 return GetProcAddress(*m_Handle.get(), name.CStr());
66 return dlsym(*m_Handle.get(), name.CStr());