~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/library.cc

  • Committer: Brian Aker
  • Date: 2010-05-21 18:25:00 UTC
  • mfrom: (1530.2.10)
  • Revision ID: brian@gaz-20100521182500-c30dbqd2ko56cf56
Merge Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include "drizzled/definitions.h"
29
29
#include "drizzled/error.h"
30
30
#include "drizzled/errmsg_print.h"
31
 
#include "drizzled/plugin/library.h"
 
31
#include "drizzled/module/library.h"
32
32
 
33
33
using namespace std;
34
34
 
35
35
namespace drizzled
36
36
{
37
37
 
38
 
plugin::Library::Library(const std::string &name_arg,
 
38
module::Library::Library(const std::string &name_arg,
39
39
                         void *handle_arg,
40
40
                         const Manifest *manifest_arg)
41
41
 : name(name_arg), handle(handle_arg), manifest(manifest_arg)
42
42
{}
43
43
 
44
 
plugin::Library::~Library()
 
44
module::Library::~Library()
45
45
{
46
46
  /**
47
47
   * @TODO: This breaks valgrind at the moment. 
50
50
  */
51
51
}
52
52
 
53
 
plugin::Library *plugin::Library::loadLibrary(const string &plugin_name)
 
53
const string module::Library::getLibraryPath(const string &plugin_name)
54
54
{
55
 
  /*
56
 
    Ensure that the dll doesn't have a path.
57
 
    This is done to ensure that only approved libraries from the
58
 
    plugin directory are used (to make this even remotely secure).
59
 
  */
60
 
  size_t found= plugin_name.find(FN_LIBCHAR);
61
 
  if (found != string::npos)
62
 
  {
63
 
    errmsg_printf(ERRMSG_LVL_ERROR, "%s",ER(ER_PLUGIN_NO_PATHS));
64
 
    return NULL;
65
 
  }
66
 
 
67
55
  /* Compile dll path */
68
56
  string dlpath;
69
57
  dlpath.reserve(FN_REFLEN);
77
65
#else
78
66
  dlpath.append(".so");
79
67
#endif
80
 
 
 
68
  return dlpath;
 
69
}
 
70
 
 
71
module::Library *module::Library::loadLibrary(const string &plugin_name, bool builtin)
 
72
{
 
73
  /*
 
74
    Ensure that the dll doesn't have a path.
 
75
    This is done to ensure that only approved libraries from the
 
76
    plugin directory are used (to make this even remotely secure).
 
77
  */
 
78
  size_t found= plugin_name.find(FN_LIBCHAR);
 
79
  if (found != string::npos)
 
80
  {
 
81
    errmsg_printf(ERRMSG_LVL_ERROR, "%s",ER(ER_PLUGIN_NO_PATHS));
 
82
    return NULL;
 
83
  }
 
84
 
 
85
  void *handle= NULL;
 
86
  string dlpath("");
 
87
 
 
88
  if (builtin)
 
89
  {
 
90
    dlpath.assign("<builtin>");
 
91
    handle= dlopen(NULL, RTLD_NOW|RTLD_GLOBAL);
 
92
    if (handle == NULL)
 
93
    {
 
94
      const char *errmsg= dlerror();
 
95
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
 
96
                    dlpath.c_str(), errno, errmsg);
 
97
      (void)dlerror();
 
98
 
 
99
      return NULL;
 
100
    }
 
101
  }
 
102
  else
 
103
  {
81
104
  /* Open new dll handle */
82
 
  void *handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
83
 
  if (handle == NULL)
84
 
  {
85
 
    const char *errmsg= dlerror();
86
 
    uint32_t dlpathlen= dlpath.length();
87
 
    if (!dlpath.compare(0, dlpathlen, errmsg))
88
 
    { // if errmsg starts from dlpath, trim this prefix.
89
 
      errmsg+=dlpathlen;
90
 
      if (*errmsg == ':') errmsg++;
91
 
      if (*errmsg == ' ') errmsg++;
 
105
    dlpath.assign(Library::getLibraryPath(plugin_name));
 
106
    handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
 
107
    if (handle == NULL)
 
108
    {
 
109
      const char *errmsg= dlerror();
 
110
      uint32_t dlpathlen= dlpath.length();
 
111
      if (not dlpath.compare(0, dlpathlen, errmsg))
 
112
      { // if errmsg starts from dlpath, trim this prefix.
 
113
        errmsg+= dlpathlen;
 
114
        if (*errmsg == ':') errmsg++;
 
115
        if (*errmsg == ' ') errmsg++;
 
116
      }
 
117
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
 
118
                    dlpath.c_str(), errno, errmsg);
 
119
 
 
120
      // This, in theory, should cause dlerror() to deallocate the error
 
121
      // message. Found this via Google'ing :)
 
122
      (void)dlerror();
 
123
 
 
124
      return NULL;
92
125
    }
93
 
    errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
94
 
                  dlpath.c_str(), errno, errmsg);
95
 
 
96
 
    // This is, in theory, should cause dlerror() to deallocate the error
97
 
    // message. Found this via Google'ing :)
98
 
    (void)dlerror();
99
 
 
100
 
    return NULL;
101
126
  }
102
127
 
103
 
 
104
128
  string plugin_decl_sym("_drizzled_");
105
129
  plugin_decl_sym.append(plugin_name);
106
130
  plugin_decl_sym.append("_plugin_");
118
142
    return NULL;
119
143
  }
120
144
 
121
 
  const Manifest *manifest= static_cast<plugin::Manifest *>(sym); 
 
145
  const Manifest *manifest= static_cast<module::Manifest *>(sym); 
122
146
  if (manifest->drizzle_version != DRIZZLE_VERSION_ID)
123
147
  {
124
148
    errmsg_printf(ERRMSG_LVL_ERROR,
130
154
    return NULL;
131
155
  }
132
156
 
133
 
  return new (nothrow) plugin::Library(plugin_name, handle, manifest);
 
157
  return new (nothrow) module::Library(plugin_name, handle, manifest);
134
158
}
135
159
 
136
160
} /* namespace drizzled */