factory.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include "factory.h"
  15. #include "exceptions.h"
  16. #include "database.h"
  17. #include "sqlite3_accessor.h"
  18. #include "datasrc_config.h"
  19. #include <datasrc/logger.h>
  20. #include <exceptions/exceptions.h>
  21. #include <dlfcn.h>
  22. #include <cstdlib>
  23. using namespace std;
  24. using namespace isc::data;
  25. using namespace isc::datasrc;
  26. namespace {
  27. // This helper function takes the 'type' string as passed to
  28. // the DataSourceClient container below, and, unless it
  29. // already specifies a specific loadable .so file, will
  30. // convert the short-name to the full file.
  31. // I.e. it will add '_ds.so' (if necessary), and prepend
  32. // it with an absolute path (if necessary).
  33. // Returns the resulting string to use with LibraryContainer.
  34. const std::string
  35. getDataSourceLibFile(const std::string& type) {
  36. if (type.empty()) {
  37. isc_throw(DataSourceLibraryError,
  38. "DataSourceClient container called with empty type value");
  39. }
  40. if (type == ".so") {
  41. isc_throw(DataSourceLibraryError, "DataSourceClient container called "
  42. "with bad type or file name");
  43. }
  44. // Type can be either a short name, in which case we need to
  45. // append "_ds.so", or it can be a direct .so library.
  46. std::string lib_file = type;
  47. const int ext_pos = lib_file.rfind(".so");
  48. if (ext_pos == std::string::npos || ext_pos + 3 != lib_file.length()) {
  49. lib_file.append("_ds.so");
  50. }
  51. // And if it is not an absolute path, prepend it with our
  52. // loadable backend library path
  53. if (type[0] != '/') {
  54. // When running from the build tree, we do NOT want
  55. // to load the installed loadable library
  56. if (getenv("B10_FROM_BUILD") != NULL) {
  57. lib_file = std::string(getenv("B10_FROM_BUILD")) +
  58. "/src/lib/datasrc/.libs/" + lib_file;
  59. } else {
  60. lib_file = isc::datasrc::BACKEND_LIBRARY_PATH + lib_file;
  61. }
  62. }
  63. return (lib_file);
  64. }
  65. } // end anonymous namespace
  66. namespace isc {
  67. namespace datasrc {
  68. LibraryContainer::LibraryContainer(const std::string& name) {
  69. // use RTLD_GLOBAL so that shared symbols (e.g. exceptions)
  70. // are recognized as such
  71. ds_lib_ = dlopen(name.c_str(), RTLD_NOW | RTLD_GLOBAL);
  72. if (ds_lib_ == NULL) {
  73. // This may cause the filename to appear twice in the actual
  74. // error, but the output of dlerror is implementation-dependent
  75. isc_throw(DataSourceLibraryError, "dlopen failed for " << name <<
  76. ": " << dlerror());
  77. }
  78. }
  79. LibraryContainer::~LibraryContainer() {
  80. dlclose(ds_lib_);
  81. }
  82. void*
  83. LibraryContainer::getSym(const char* name) {
  84. // Since dlsym can return NULL on success, we check for errors by
  85. // first clearing any existing errors with dlerror(), then calling dlsym,
  86. // and finally checking for errors with dlerror()
  87. dlerror();
  88. void *sym = dlsym(ds_lib_, name);
  89. const char* dlsym_error = dlerror();
  90. if (dlsym_error != NULL) {
  91. isc_throw(DataSourceLibrarySymbolError, dlsym_error);
  92. }
  93. return (sym);
  94. }
  95. DataSourceClientContainer::DataSourceClientContainer(const std::string& type,
  96. ConstElementPtr config)
  97. : ds_lib_(getDataSourceLibFile(type))
  98. {
  99. // We are casting from a data to a function pointer here
  100. // Some compilers (rightfully) complain about that, but
  101. // c-style casts are accepted the most here. If we run
  102. // into any that also don't like this, we might need to
  103. // use some form of union cast or memory copy to get
  104. // from the void* to the function pointer.
  105. ds_creator* ds_create = (ds_creator*)ds_lib_.getSym("createInstance");
  106. destructor_ = (ds_destructor*)ds_lib_.getSym("destroyInstance");
  107. std::string error;
  108. try {
  109. instance_ = ds_create(config, error);
  110. if (instance_ == NULL) {
  111. isc_throw(DataSourceError, error);
  112. }
  113. } catch (const std::exception& exc) {
  114. isc_throw(DataSourceError, "Unknown uncaught exception from " + type +
  115. " createInstance: " + exc.what());
  116. } catch (...) {
  117. isc_throw(DataSourceError, "Unknown uncaught exception from " + type);
  118. }
  119. }
  120. DataSourceClientContainer::~DataSourceClientContainer() {
  121. destructor_(instance_);
  122. }
  123. } // end namespace datasrc
  124. } // end namespace isc