~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/library.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems
 
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
24
24
#include <cerrno>
25
25
#include <string>
26
26
 
 
27
#include <boost/filesystem.hpp>
 
28
 
27
29
#include "drizzled/plugin.h"
28
30
#include "drizzled/definitions.h"
29
31
#include "drizzled/error.h"
31
33
#include "drizzled/module/library.h"
32
34
 
33
35
using namespace std;
 
36
namespace fs=boost::filesystem;
34
37
 
35
38
namespace drizzled
36
39
{
50
53
  */
51
54
}
52
55
 
53
 
const string module::Library::getLibraryPath(const string &plugin_name)
 
56
const fs::path module::Library::getLibraryPath(const string &plugin_name)
54
57
{
55
 
  /* Compile dll path */
56
 
  string dlpath;
57
 
  dlpath.reserve(FN_REFLEN);
58
 
  dlpath.append(opt_plugin_dir);
59
 
  dlpath.append("/");
60
 
  dlpath.append("lib");
61
 
  dlpath.append(plugin_name);
62
 
  dlpath.append("_plugin");
 
58
  string plugin_lib_name("lib");
 
59
  plugin_lib_name.append(plugin_name);
 
60
  plugin_lib_name.append("_plugin");
63
61
#if defined(TARGET_OS_OSX)
64
 
  dlpath.append(".dylib");
 
62
  plugin_lib_name.append(".dylib");
65
63
#else
66
 
  dlpath.append(".so");
 
64
  plugin_lib_name.append(".so");
67
65
#endif
68
 
  return dlpath;
 
66
 
 
67
  /* Compile dll path */
 
68
  return plugin_dir / plugin_lib_name;
69
69
}
70
70
 
71
71
module::Library *module::Library::loadLibrary(const string &plugin_name, bool builtin)
78
78
  size_t found= plugin_name.find(FN_LIBCHAR);
79
79
  if (found != string::npos)
80
80
  {
81
 
    errmsg_printf(ERRMSG_LVL_ERROR, "%s",ER(ER_PLUGIN_NO_PATHS));
 
81
    errmsg_printf(error::ERROR, "%s",ER(ER_PLUGIN_NO_PATHS));
82
82
    return NULL;
83
83
  }
84
84
 
85
 
  void *handle= NULL;
 
85
  void *dl_handle= NULL;
86
86
  string dlpath("");
87
87
 
88
88
  if (builtin)
89
89
  {
90
90
    dlpath.assign("<builtin>");
91
 
    handle= dlopen(NULL, RTLD_NOW|RTLD_LOCAL);
92
 
    if (handle == NULL)
 
91
    dl_handle= dlopen(NULL, RTLD_NOW|RTLD_LOCAL);
 
92
    if (dl_handle == NULL)
93
93
    {
94
94
      const char *errmsg= dlerror();
95
 
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
 
95
      errmsg_printf(error::ERROR, ER(ER_CANT_OPEN_LIBRARY),
96
96
                    dlpath.c_str(), errno, errmsg);
97
97
      (void)dlerror();
98
98
 
102
102
  else
103
103
  {
104
104
  /* Open new dll handle */
105
 
    dlpath.assign(Library::getLibraryPath(plugin_name));
106
 
    handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
107
 
    if (handle == NULL)
 
105
    dlpath.assign(Library::getLibraryPath(plugin_name).file_string());
 
106
    dl_handle= dlopen(dlpath.c_str(), RTLD_NOW|RTLD_GLOBAL);
 
107
    if (dl_handle == NULL)
108
108
    {
109
109
      const char *errmsg= dlerror();
110
110
      uint32_t dlpathlen= dlpath.length();
114
114
        if (*errmsg == ':') errmsg++;
115
115
        if (*errmsg == ' ') errmsg++;
116
116
      }
117
 
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_OPEN_LIBRARY),
 
117
      errmsg_printf(error::ERROR, ER(ER_CANT_OPEN_LIBRARY),
118
118
                    dlpath.c_str(), errno, errmsg);
119
119
 
120
120
      // This, in theory, should cause dlerror() to deallocate the error
130
130
  plugin_decl_sym.append("_plugin_");
131
131
 
132
132
  /* Find plugin declarations */
133
 
  void *sym= dlsym(handle, plugin_decl_sym.c_str());
 
133
  void *sym= dlsym(dl_handle, plugin_decl_sym.c_str());
134
134
  if (sym == NULL)
135
135
  {
136
136
    const char* errmsg= dlerror();
137
 
    errmsg_printf(ERRMSG_LVL_ERROR, errmsg);
138
 
    errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_CANT_FIND_DL_ENTRY),
 
137
    errmsg_printf(error::ERROR, errmsg);
 
138
    errmsg_printf(error::ERROR, ER(ER_CANT_FIND_DL_ENTRY),
139
139
                  plugin_decl_sym.c_str(), dlpath.c_str());
140
140
    (void)dlerror();
141
 
    dlclose(handle);
 
141
    dlclose(dl_handle);
142
142
    return NULL;
143
143
  }
144
144
 
145
 
  const Manifest *manifest= static_cast<module::Manifest *>(sym); 
146
 
  if (manifest->drizzle_version != DRIZZLE_VERSION_ID)
 
145
  const Manifest *module_manifest= static_cast<module::Manifest *>(sym); 
 
146
  if (module_manifest->drizzle_version != DRIZZLE_VERSION_ID)
147
147
  {
148
 
    errmsg_printf(ERRMSG_LVL_ERROR,
 
148
    errmsg_printf(error::ERROR,
149
149
                  _("Plugin module %s was compiled for version %" PRIu64 ", "
150
150
                    "which does not match the current running version of "
151
151
                    "Drizzle: %" PRIu64"."),
152
 
                 dlpath.c_str(), manifest->drizzle_version,
153
 
                 DRIZZLE_VERSION_ID);
 
152
                 dlpath.c_str(), module_manifest->drizzle_version,
 
153
                 static_cast<uint64_t>(DRIZZLE_VERSION_ID));
154
154
    return NULL;
155
155
  }
156
156
 
157
 
  return new (nothrow) module::Library(plugin_name, handle, manifest);
 
157
  return new (nothrow) module::Library(plugin_name, dl_handle, module_manifest);
158
158
}
159
159
 
160
160
} /* namespace drizzled */