~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/library.cc

  • Committer: Jay Pipes
  • Date: 2009-12-08 17:46:38 UTC
  • mfrom: (1240 staging)
  • mto: This revision was merged to the branch mainline in revision 1245.
  • Revision ID: jpipes@serialcoder-20091208174638-exwaq2vbq0bzvyze
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
 
 
20
#include "drizzled/global.h"
 
21
 
 
22
#include <dlfcn.h>
 
23
 
 
24
#include <string>
 
25
 
 
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"
 
31
 
 
32
using namespace std;
 
33
 
 
34
namespace drizzled
 
35
{
 
36
 
 
37
static const char *plugin_declarations_sym= "_drizzled_plugin_declaration_";
 
38
 
 
39
plugin::Library::Library(const std::string &name_arg,
 
40
                         void *handle_arg,
 
41
                         const Manifest *manifest_arg)
 
42
 : name(name_arg), handle(handle_arg), manifest(manifest_arg)
 
43
{}
 
44
 
 
45
plugin::Library::~Library()
 
46
{
 
47
  if (handle)
 
48
    dlclose(handle);
 
49
}
 
50
 
 
51
plugin::Library *plugin::Library::loadLibrary(const string &plugin_name)
 
52
{
 
53
  /*
 
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).
 
57
  */
 
58
  size_t found= plugin_name.find(FN_LIBCHAR);
 
59
  if (found != string::npos)
 
60
  {
 
61
    errmsg_printf(ERRMSG_LVL_ERROR, "%s",ER(ER_PLUGIN_NO_PATHS));
 
62
    return NULL;
 
63
  }
 
64
 
 
65
  /* Compile dll path */
 
66
  string dlpath;
 
67
  dlpath.reserve(FN_REFLEN);
 
68
  dlpath.append(opt_plugin_dir);
 
69
  dlpath.append("/");
 
70
  dlpath.append("lib");
 
71
  dlpath.append(plugin_name);
 
72
  dlpath.append("_plugin.so");
 
73
 
 
74
  /* Open new dll handle */
 
75
  void *handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
 
76
  if (handle == NULL)
 
77
  {
 
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.
 
82
      errmsg+=dlpathlen;
 
83
      if (*errmsg == ':') errmsg++;
 
84
      if (*errmsg == ' ') errmsg++;
 
85
    }
 
86
    errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
 
87
                  dlpath.c_str(), errno, errmsg);
 
88
 
 
89
    // This is, in theory, should cause dlerror() to deallocate the error
 
90
    // message. Found this via Google'ing :)
 
91
    (void)dlerror();
 
92
 
 
93
    return NULL;
 
94
  }
 
95
 
 
96
 
 
97
  /* Find plugin declarations */
 
98
  void *sym= dlsym(handle, plugin_declarations_sym);
 
99
  if (sym == NULL)
 
100
  {
 
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());
 
105
    (void)dlerror();
 
106
    dlclose(handle);
 
107
    return NULL;
 
108
  }
 
109
 
 
110
  const Manifest *manifest= reinterpret_cast<plugin::Manifest *>(sym); 
 
111
  return new (nothrow) plugin::Library(plugin_name, handle, manifest);
 
112
}
 
113
 
 
114
} /* namespace drizzled */