1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <dlfcn.h>
#include <cerrno>
#include <string>
#include <boost/filesystem.hpp>
#include <drizzled/plugin.h>
#include <drizzled/definitions.h>
#include <drizzled/error.h>
#include <drizzled/errmsg_print.h>
#include <drizzled/module/library.h>
using namespace std;
namespace fs=boost::filesystem;
namespace drizzled
{
module::Library::Library(const std::string &name_arg,
void *handle_arg,
const Manifest *manifest_arg)
: name(name_arg), handle(handle_arg), manifest(manifest_arg)
{}
module::Library::~Library()
{
/**
* @TODO: This breaks valgrind at the moment.
if (handle)
dlclose(handle);
*/
}
const fs::path module::Library::getLibraryPath(const string &plugin_name)
{
string plugin_lib_name("lib");
plugin_lib_name.append(plugin_name);
plugin_lib_name.append("_plugin");
#if defined(TARGET_OS_OSX)
plugin_lib_name.append(".dylib");
#else
plugin_lib_name.append(".so");
#endif
/* Compile dll path */
return plugin_dir / plugin_lib_name;
}
module::Library *module::Library::loadLibrary(const string &plugin_name, bool builtin)
{
/*
Ensure that the dll doesn't have a path.
This is done to ensure that only approved libraries from the
plugin directory are used (to make this even remotely secure).
*/
size_t found= plugin_name.find(FN_LIBCHAR);
if (found != string::npos)
{
errmsg_printf(error::ERROR, "%s",ER(ER_PLUGIN_NO_PATHS));
return NULL;
}
void *dl_handle= NULL;
string dlpath;
if (builtin)
{
dlpath= "<builtin>";
dl_handle= dlopen(NULL, RTLD_NOW|RTLD_LOCAL);
if (dl_handle == NULL)
{
const char *errmsg= dlerror();
errmsg_printf(error::ERROR, ER(ER_CANT_OPEN_LIBRARY),
dlpath.c_str(), errno, errmsg);
(void)dlerror();
return NULL;
}
}
else
{
/* Open new dll handle */
dlpath= Library::getLibraryPath(plugin_name).file_string();
dl_handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
if (dl_handle == NULL)
{
const char *errmsg= dlerror();
uint32_t dlpathlen= dlpath.length();
if (not dlpath.compare(0, dlpathlen, errmsg))
{ // if errmsg starts from dlpath, trim this prefix.
errmsg+= dlpathlen;
if (*errmsg == ':') errmsg++;
if (*errmsg == ' ') errmsg++;
}
errmsg_printf(error::ERROR, ER(ER_CANT_OPEN_LIBRARY),
dlpath.c_str(), errno, errmsg);
// This, in theory, should cause dlerror() to deallocate the error
// message. Found this via Google'ing :)
(void)dlerror();
return NULL;
}
}
string plugin_decl_sym("_drizzled_");
plugin_decl_sym.append(plugin_name);
plugin_decl_sym.append("_plugin_");
/* Find plugin declarations */
void *sym= dlsym(dl_handle, plugin_decl_sym.c_str());
if (sym == NULL)
{
const char* errmsg= dlerror();
errmsg_printf(error::ERROR, "%s", errmsg);
errmsg_printf(error::ERROR, ER(ER_CANT_FIND_DL_ENTRY),
plugin_decl_sym.c_str(), dlpath.c_str());
(void)dlerror();
dlclose(dl_handle);
return NULL;
}
const Manifest *module_manifest= static_cast<module::Manifest *>(sym);
if (module_manifest->drizzle_version != DRIZZLE_VERSION_ID)
{
errmsg_printf(error::ERROR,
_("Plugin module %s was compiled for version %" PRIu64 ", "
"which does not match the current running version of "
"Drizzle: %" PRIu64"."),
dlpath.c_str(), module_manifest->drizzle_version,
static_cast<uint64_t>(DRIZZLE_VERSION_ID));
return NULL;
}
return new module::Library(plugin_name, dl_handle, module_manifest);
}
} /* namespace drizzled */
|