1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
#include "drizzled/plugin.h"
28
#include "drizzled/definitions.h"
29
#include "drizzled/error.h"
30
#include "drizzled/errmsg_print.h"
31
#include "drizzled/plugin/library.h"
38
plugin::Library::Library(const std::string &name_arg,
40
const Manifest *manifest_arg)
41
: name(name_arg), handle(handle_arg), manifest(manifest_arg)
44
plugin::Library::~Library()
50
plugin::Library *plugin::Library::loadLibrary(const string &plugin_name)
53
Ensure that the dll doesn't have a path.
54
This is done to ensure that only approved libraries from the
55
plugin directory are used (to make this even remotely secure).
57
size_t found= plugin_name.find(FN_LIBCHAR);
58
if (found != string::npos)
60
errmsg_printf(ERRMSG_LVL_ERROR, "%s",ER(ER_PLUGIN_NO_PATHS));
64
/* Compile dll path */
66
dlpath.reserve(FN_REFLEN);
67
dlpath.append(opt_plugin_dir);
70
dlpath.append(plugin_name);
71
dlpath.append("_plugin.so");
73
/* Open new dll handle */
74
void *handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
77
const char *errmsg= dlerror();
78
uint32_t dlpathlen= dlpath.length();
79
if (!dlpath.compare(0, dlpathlen, errmsg))
80
{ // if errmsg starts from dlpath, trim this prefix.
82
if (*errmsg == ':') errmsg++;
83
if (*errmsg == ' ') errmsg++;
85
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
86
dlpath.c_str(), errno, errmsg);
88
// This is, in theory, should cause dlerror() to deallocate the error
89
// message. Found this via Google'ing :)
96
string plugin_decl_sym("_drizzled_");
97
plugin_decl_sym.append(plugin_name);
98
plugin_decl_sym.append("_plugin_");
100
/* Find plugin declarations */
101
void *sym= dlsym(handle, plugin_decl_sym.c_str());
104
const char* errmsg= dlerror();
105
errmsg_printf(ERRMSG_LVL_ERROR, errmsg);
106
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_FIND_DL_ENTRY),
107
plugin_decl_sym.c_str(), dlpath.c_str());
113
const Manifest *manifest= static_cast<plugin::Manifest *>(sym);
114
if (manifest->drizzle_version != DRIZZLE_VERSION_ID)
116
errmsg_printf(ERRMSG_LVL_ERROR,
117
_("Plugin module %s was compiled for version %" PRIu64 ", "
118
"which does not match the current running version of "
119
"Drizzle: %" PRIu64"."),
120
dlpath.c_str(), manifest->drizzle_version,
125
return new (nothrow) plugin::Library(plugin_name, handle, manifest);
128
} /* namespace drizzled */