library_manager_collection.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2013-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <hooks/callout_manager.h>
  7. #include <hooks/library_manager.h>
  8. #include <hooks/library_manager_collection.h>
  9. namespace isc {
  10. namespace hooks {
  11. // Return callout manager for the loaded libraries. This call is only valid
  12. // after one has been created for the loaded libraries (which includes the
  13. // case of no loaded libraries).
  14. //
  15. // Note that there is no real connection between the callout manager and the
  16. // libraries, other than it knows the number of libraries so can do sanity
  17. // checks on values passed to it. However, this may change in the future,
  18. // so the hooks framework is written such that a callout manager is used only
  19. // with the LibraryManagerCollection that created it. It is also the reason
  20. // why each LibraryManager contains a pointer to this CalloutManager.
  21. boost::shared_ptr<CalloutManager>
  22. LibraryManagerCollection::getCalloutManager() const {
  23. // Only return a pointer if we have a CalloutManager created.
  24. if (! callout_manager_) {
  25. isc_throw(LoadLibrariesNotCalled, "must load hooks libraries before "
  26. "attempting to retrieve a CalloutManager for them");
  27. }
  28. return (callout_manager_);
  29. }
  30. LibraryManagerCollection::LibraryManagerCollection(const HookLibsCollection& libraries)
  31. :library_info_(libraries) {
  32. // We need to split hook libs into library names and library parameters.
  33. for (HookLibsCollection::const_iterator it = libraries.begin();
  34. it != libraries.end(); ++it) {
  35. library_names_.push_back(it->first);
  36. }
  37. }
  38. // Load a set of libraries
  39. bool
  40. LibraryManagerCollection::loadLibraries() {
  41. // Unload libraries if any are loaded.
  42. static_cast<void>(unloadLibraries());
  43. // Create the callout manager. A pointer to this is maintained by
  44. // each library. Note that the callout manager does not hold any memory
  45. // allocated by a library: although a library registers a callout (and so
  46. // causes the creation of an entry in the CalloutManager's callout list),
  47. // that creation is done by the CalloutManager itself. The CalloutManager
  48. // is created within the server.
  49. //
  50. // The upshot of this is that it is therefore safe for the CalloutManager
  51. // to be deleted after all associated libraries are deleted, hence this
  52. // link (LibraryManager -> CalloutManager) is safe.
  53. //
  54. // To survive reloads an attempt to re-use the shared manager
  55. // is performed when the list of library names is empty.
  56. if (library_names_.empty()) {
  57. callout_manager_ = CalloutManager::getSharedManager();
  58. }
  59. if (!library_names_.empty() || !callout_manager_) {
  60. callout_manager_.reset(new CalloutManager(library_names_.size()));
  61. }
  62. // Now iterate through the libraries are load them one by one. We'll
  63. for (size_t i = 0; i < library_names_.size(); ++i) {
  64. // Create a pointer to the new library manager. The index of this
  65. // library is determined by the number of library managers currently
  66. // loaded: note that the library indexes run from 1 to (number of loaded
  67. // libraries).
  68. boost::shared_ptr<LibraryManager> manager(
  69. new LibraryManager(library_names_[i], lib_managers_.size() + 1,
  70. callout_manager_));
  71. // Load the library. On success, add it to the list of loaded
  72. // libraries. On failure, unload all currently loaded libraries,
  73. // leaving the object in the state it was in before loadLibraries was
  74. // called.
  75. if (manager->loadLibrary()) {
  76. lib_managers_.push_back(manager);
  77. } else {
  78. static_cast<void>(unloadLibraries());
  79. return (false);
  80. }
  81. }
  82. return (true);
  83. }
  84. // Unload the libraries.
  85. void
  86. LibraryManagerCollection::unloadLibraries() {
  87. // Delete the library managers in the reverse order to which they were
  88. // created, then clear the library manager vector.
  89. for (int i = lib_managers_.size() - 1; i >= 0; --i) {
  90. lib_managers_[i].reset();
  91. }
  92. lib_managers_.clear();
  93. // Get rid of the callout manager. (The other member, the list of library
  94. // names, was cleared when the libraries were loaded.)
  95. callout_manager_.reset();
  96. }
  97. // Return number of loaded libraries.
  98. int
  99. LibraryManagerCollection::getLoadedLibraryCount() const {
  100. return (lib_managers_.size());
  101. }
  102. // Validate the libraries.
  103. std::vector<std::string>
  104. LibraryManagerCollection::validateLibraries(
  105. const std::vector<std::string>& libraries) {
  106. std::vector<std::string> failures;
  107. for (size_t i = 0; i < libraries.size(); ++i) {
  108. if (!LibraryManager::validateLibrary(libraries[i])) {
  109. failures.push_back(libraries[i]);
  110. }
  111. }
  112. return (failures);
  113. }
  114. } // namespace hooks
  115. } // namespace isc