factory.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "data_source.h"
  16. #include "database.h"
  17. #include "sqlite3_accessor.h"
  18. #include "memory_datasrc.h"
  19. #include "datasrc_config.h"
  20. #include <datasrc/logger.h>
  21. #include <dlfcn.h>
  22. using namespace isc::data;
  23. using namespace isc::datasrc;
  24. namespace {
  25. // This helper function takes the 'type' string as passed to
  26. // the DataSourceClient container below, and, unless it
  27. // already specifies a specific loadable .so file, will
  28. // convert the short-name to the full file.
  29. // I.e. it will add '_ds.so' (if necessary), and prepend
  30. // it with an absolute path (if necessary).
  31. // Returns the resulting string to use with LibraryContainer.
  32. const std::string
  33. getDataSourceLibFile(const std::string& type) {
  34. if (type.empty()) {
  35. isc_throw(DataSourceLibraryError,
  36. "DataSourceClient container called with empty type value");
  37. }
  38. if (type == ".so") {
  39. isc_throw(DataSourceLibraryError, "DataSourceClient container called"
  40. "with bad type or file name");
  41. }
  42. // Type can be either a short name, in which case we need to
  43. // append "_ds.so", or it can be a direct .so library.
  44. std::string lib_file = type;
  45. const int ext_pos = lib_file.rfind(".so");
  46. if (ext_pos == std::string::npos || ext_pos + 3 != lib_file.length()) {
  47. lib_file.append("_ds.so");
  48. }
  49. // And if it is not an absolute path, prepend it with our
  50. // loadable backend library path
  51. if (type[0] != '/') {
  52. // When running from the build tree, we do NOT want
  53. // to load the installed loadable library
  54. if (getenv("B10_FROM_BUILD") != NULL) {
  55. lib_file = std::string(getenv("B10_FROM_BUILD")) +
  56. "/src/lib/datasrc/.libs/" + lib_file;
  57. } else {
  58. lib_file = isc::datasrc::BACKEND_LIBRARY_PATH + lib_file;
  59. }
  60. }
  61. return (lib_file);
  62. }
  63. } // end anonymous namespace
  64. namespace isc {
  65. namespace datasrc {
  66. LibraryContainer::LibraryContainer(const std::string& name) {
  67. // use RTLD_GLOBAL so that shared symbols (e.g. exceptions)
  68. // are recognized as such
  69. ds_lib_ = dlopen(name.c_str(), RTLD_NOW | RTLD_GLOBAL);
  70. if (ds_lib_ == NULL) {
  71. // This may cause the filename to appear twice in the actual
  72. // error, but the output of dlerror is implementation-dependent
  73. isc_throw(DataSourceLibraryError, "dlopen failed for " << name <<
  74. ": " << dlerror());
  75. }
  76. }
  77. LibraryContainer::~LibraryContainer() {
  78. dlclose(ds_lib_);
  79. }
  80. void*
  81. LibraryContainer::getSym(const char* name) {
  82. // Since dlsym can return NULL on success, we check for errors by
  83. // first clearing any existing errors with dlerror(), then calling dlsym,
  84. // and finally checking for errors with dlerror()
  85. dlerror();
  86. void *sym = dlsym(ds_lib_, name);
  87. const char* dlsym_error = dlerror();
  88. if (dlsym_error != NULL) {
  89. isc_throw(DataSourceLibrarySymbolError, dlsym_error);
  90. }
  91. return (sym);
  92. }
  93. DataSourceClientContainer::DataSourceClientContainer(const std::string& type,
  94. ConstElementPtr config)
  95. : ds_lib_(getDataSourceLibFile(type))
  96. {
  97. // We are casting from a data to a function pointer here
  98. // Some compilers (rightfully) complain about that, but
  99. // c-style casts are accepted the most here. If we run
  100. // into any that also don't like this, we might need to
  101. // use some form of union cast or memory copy to get
  102. // from the void* to the function pointer.
  103. ds_creator* ds_create = (ds_creator*)ds_lib_.getSym("createInstance");
  104. destructor_ = (ds_destructor*)ds_lib_.getSym("destroyInstance");
  105. std::string error;
  106. try {
  107. instance_ = ds_create(config, error);
  108. if (instance_ == NULL) {
  109. isc_throw(DataSourceError, error);
  110. }
  111. } catch (const std::exception& exc) {
  112. isc_throw(DataSourceError, "Unknown uncaught exception from " + type +
  113. " createInstance: " + exc.what());
  114. } catch (...) {
  115. isc_throw(DataSourceError, "Unknown uncaught exception from " + type);
  116. }
  117. }
  118. DataSourceClientContainer::~DataSourceClientContainer() {
  119. destructor_(instance_);
  120. }
  121. } // end namespace datasrc
  122. } // end namespace isc