~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/library.cc

  • Committer: Jay Pipes
  • Date: 2009-09-21 14:33:44 UTC
  • mfrom: (1126.10.26 dtrace-probes)
  • mto: This revision was merged to the branch mainline in revision 1133.
  • Revision ID: jpipes@serialcoder-20090921143344-jnarp7gcn6zmg19c
Merge fixes from Trond and Padraig on dtrace probes.

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 "config.h"
21
 
 
22
 
#include <dlfcn.h>
23
 
 
24
 
#include <cerrno>
25
 
#include <string>
26
 
 
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"
32
 
 
33
 
using namespace std;
34
 
 
35
 
namespace drizzled
36
 
{
37
 
 
38
 
plugin::Library::Library(const std::string &name_arg,
39
 
                         void *handle_arg,
40
 
                         const Manifest *manifest_arg)
41
 
 : name(name_arg), handle(handle_arg), manifest(manifest_arg)
42
 
{}
43
 
 
44
 
plugin::Library::~Library()
45
 
{
46
 
  if (handle)
47
 
    dlclose(handle);
48
 
}
49
 
 
50
 
plugin::Library *plugin::Library::loadLibrary(const string &plugin_name)
51
 
{
52
 
  /*
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).
56
 
  */
57
 
  size_t found= plugin_name.find(FN_LIBCHAR);
58
 
  if (found != string::npos)
59
 
  {
60
 
    errmsg_printf(ERRMSG_LVL_ERROR, "%s",ER(ER_PLUGIN_NO_PATHS));
61
 
    return NULL;
62
 
  }
63
 
 
64
 
  /* Compile dll path */
65
 
  string dlpath;
66
 
  dlpath.reserve(FN_REFLEN);
67
 
  dlpath.append(opt_plugin_dir);
68
 
  dlpath.append("/");
69
 
  dlpath.append("lib");
70
 
  dlpath.append(plugin_name);
71
 
  dlpath.append("_plugin");
72
 
#if defined(TARGET_OS_OSX)
73
 
  dlpath.append(".dylib");
74
 
#else
75
 
  dlpath.append(".so");
76
 
#endif
77
 
 
78
 
  /* Open new dll handle */
79
 
  void *handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
80
 
  if (handle == NULL)
81
 
  {
82
 
    const char *errmsg= dlerror();
83
 
    uint32_t dlpathlen= dlpath.length();
84
 
    if (!dlpath.compare(0, dlpathlen, errmsg))
85
 
    { // if errmsg starts from dlpath, trim this prefix.
86
 
      errmsg+=dlpathlen;
87
 
      if (*errmsg == ':') errmsg++;
88
 
      if (*errmsg == ' ') errmsg++;
89
 
    }
90
 
    errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
91
 
                  dlpath.c_str(), errno, errmsg);
92
 
 
93
 
    // This is, in theory, should cause dlerror() to deallocate the error
94
 
    // message. Found this via Google'ing :)
95
 
    (void)dlerror();
96
 
 
97
 
    return NULL;
98
 
  }
99
 
 
100
 
 
101
 
  string plugin_decl_sym("_drizzled_");
102
 
  plugin_decl_sym.append(plugin_name);
103
 
  plugin_decl_sym.append("_plugin_");
104
 
 
105
 
  /* Find plugin declarations */
106
 
  void *sym= dlsym(handle, plugin_decl_sym.c_str());
107
 
  if (sym == NULL)
108
 
  {
109
 
    const char* errmsg= dlerror();
110
 
    errmsg_printf(ERRMSG_LVL_ERROR, errmsg);
111
 
    errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_FIND_DL_ENTRY),
112
 
                  plugin_decl_sym.c_str(), dlpath.c_str());
113
 
    (void)dlerror();
114
 
    dlclose(handle);
115
 
    return NULL;
116
 
  }
117
 
 
118
 
  const Manifest *manifest= static_cast<plugin::Manifest *>(sym); 
119
 
  if (manifest->drizzle_version != DRIZZLE_VERSION_ID)
120
 
  {
121
 
    errmsg_printf(ERRMSG_LVL_ERROR,
122
 
                  _("Plugin module %s was compiled for version %" PRIu64 ", "
123
 
                    "which does not match the current running version of "
124
 
                    "Drizzle: %" PRIu64"."),
125
 
                 dlpath.c_str(), manifest->drizzle_version,
126
 
                 DRIZZLE_VERSION_ID);
127
 
    return NULL;
128
 
  }
129
 
 
130
 
  return new (nothrow) plugin::Library(plugin_name, handle, manifest);
131
 
}
132
 
 
133
 
} /* namespace drizzled */