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
20
#include "drizzled/global.h"
26
#include "drizzled/plugin.h"
27
#include "drizzled/definitions.h"
28
#include "drizzled/error.h"
29
#include "drizzled/errmsg_print.h"
30
#include "drizzled/plugin/library.h"
37
static const char *plugin_declarations_sym= "_drizzled_plugin_declaration_";
39
plugin::Library::Library(const std::string &name_arg,
41
const Manifest *manifest_arg)
42
: name(name_arg), handle(handle_arg), manifest(manifest_arg)
45
plugin::Library::~Library()
51
plugin::Library *plugin::Library::loadLibrary(const string &plugin_name)
54
Ensure that the dll doesn't have a path.
55
This is done to ensure that only approved libraries from the
56
plugin directory are used (to make this even remotely secure).
58
size_t found= plugin_name.find(FN_LIBCHAR);
59
if (found != string::npos)
61
errmsg_printf(ERRMSG_LVL_ERROR, "%s",ER(ER_PLUGIN_NO_PATHS));
65
/* Compile dll path */
67
dlpath.reserve(FN_REFLEN);
68
dlpath.append(opt_plugin_dir);
71
dlpath.append(plugin_name);
72
dlpath.append("_plugin.so");
74
/* Open new dll handle */
75
void *handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
78
const char *errmsg= dlerror();
79
uint32_t dlpathlen= dlpath.length();
80
if (!dlpath.compare(0, dlpathlen, errmsg))
81
{ // if errmsg starts from dlpath, trim this prefix.
83
if (*errmsg == ':') errmsg++;
84
if (*errmsg == ' ') errmsg++;
86
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
87
dlpath.c_str(), errno, errmsg);
89
// This is, in theory, should cause dlerror() to deallocate the error
90
// message. Found this via Google'ing :)
97
/* Find plugin declarations */
98
void *sym= dlsym(handle, plugin_declarations_sym);
101
const char* errmsg= dlerror();
102
errmsg_printf(ERRMSG_LVL_ERROR, errmsg);
103
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_FIND_DL_ENTRY),
104
plugin_declarations_sym, dlpath.c_str());
110
const Manifest *manifest= reinterpret_cast<plugin::Manifest *>(sym);
111
return new (nothrow) plugin::Library(plugin_name, handle, manifest);
114
} /* namespace drizzled */