27
#include <boost/filesystem.hpp>
29
27
#include "drizzled/plugin.h"
30
28
#include "drizzled/definitions.h"
31
29
#include "drizzled/error.h"
32
30
#include "drizzled/errmsg_print.h"
33
#include "drizzled/module/library.h"
31
#include "drizzled/plugin/library.h"
35
33
using namespace std;
36
namespace fs=boost::filesystem;
41
module::Library::Library(const std::string &name_arg,
38
plugin::Library::Library(const std::string &name_arg,
43
40
const Manifest *manifest_arg)
44
41
: name(name_arg), handle(handle_arg), manifest(manifest_arg)
47
module::Library::~Library()
44
plugin::Library::~Library()
50
47
* @TODO: This breaks valgrind at the moment.
56
const fs::path module::Library::getLibraryPath(const string &plugin_name)
58
string plugin_lib_name("lib");
59
plugin_lib_name.append(plugin_name);
60
plugin_lib_name.append("_plugin");
61
#if defined(TARGET_OS_OSX)
62
plugin_lib_name.append(".dylib");
64
plugin_lib_name.append(".so");
67
/* Compile dll path */
68
return plugin_dir / plugin_lib_name;
71
module::Library *module::Library::loadLibrary(const string &plugin_name, bool builtin)
53
plugin::Library *plugin::Library::loadLibrary(const string &plugin_name)
74
56
Ensure that the dll doesn't have a path.
85
void *dl_handle= NULL;
90
dlpath.assign("<builtin>");
91
dl_handle= dlopen(NULL, RTLD_NOW|RTLD_LOCAL);
92
if (dl_handle == NULL)
94
const char *errmsg= dlerror();
95
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
96
dlpath.c_str(), errno, errmsg);
67
/* Compile dll path */
69
dlpath.reserve(FN_REFLEN);
70
dlpath.append(opt_plugin_dir);
73
dlpath.append(plugin_name);
74
dlpath.append("_plugin");
75
#if defined(TARGET_OS_OSX)
76
dlpath.append(".dylib");
104
81
/* Open new dll handle */
105
dlpath.assign(Library::getLibraryPath(plugin_name).file_string());
106
dl_handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
107
if (dl_handle == NULL)
109
const char *errmsg= dlerror();
110
uint32_t dlpathlen= dlpath.length();
111
if (not dlpath.compare(0, dlpathlen, errmsg))
112
{ // if errmsg starts from dlpath, trim this prefix.
114
if (*errmsg == ':') errmsg++;
115
if (*errmsg == ' ') errmsg++;
117
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
118
dlpath.c_str(), errno, errmsg);
120
// This, in theory, should cause dlerror() to deallocate the error
121
// message. Found this via Google'ing :)
82
void *handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
85
const char *errmsg= dlerror();
86
uint32_t dlpathlen= dlpath.length();
87
if (!dlpath.compare(0, dlpathlen, errmsg))
88
{ // if errmsg starts from dlpath, trim this prefix.
90
if (*errmsg == ':') errmsg++;
91
if (*errmsg == ' ') errmsg++;
93
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
94
dlpath.c_str(), errno, errmsg);
96
// This is, in theory, should cause dlerror() to deallocate the error
97
// message. Found this via Google'ing :)
128
104
string plugin_decl_sym("_drizzled_");
129
105
plugin_decl_sym.append(plugin_name);
130
106
plugin_decl_sym.append("_plugin_");
132
108
/* Find plugin declarations */
133
void *sym= dlsym(dl_handle, plugin_decl_sym.c_str());
109
void *sym= dlsym(handle, plugin_decl_sym.c_str());
136
112
const char* errmsg= dlerror();
138
114
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_FIND_DL_ENTRY),
139
115
plugin_decl_sym.c_str(), dlpath.c_str());
145
const Manifest *module_manifest= static_cast<module::Manifest *>(sym);
146
if (module_manifest->drizzle_version != DRIZZLE_VERSION_ID)
121
const Manifest *manifest= static_cast<plugin::Manifest *>(sym);
122
if (manifest->drizzle_version != DRIZZLE_VERSION_ID)
148
124
errmsg_printf(ERRMSG_LVL_ERROR,
149
125
_("Plugin module %s was compiled for version %" PRIu64 ", "
150
126
"which does not match the current running version of "
151
127
"Drizzle: %" PRIu64"."),
152
dlpath.c_str(), module_manifest->drizzle_version,
153
static_cast<uint64_t>(DRIZZLE_VERSION_ID));
128
dlpath.c_str(), manifest->drizzle_version,
157
return new (nothrow) module::Library(plugin_name, dl_handle, module_manifest);
133
return new (nothrow) plugin::Library(plugin_name, handle, manifest);
160
136
} /* namespace drizzled */