library_manager_collection.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2013 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 <hooks/callout_manager.h>
  15. #include <hooks/library_manager.h>
  16. #include <hooks/library_manager_collection.h>
  17. namespace isc {
  18. namespace hooks {
  19. // Return callout manager for the loaded libraries. This call is only valid
  20. // after one has been created for the loaded libraries (which includes the
  21. // case of no loaded libraries).
  22. //
  23. // Note that there is no real connection between the callout manager and the
  24. // libraries, other than it knows the number of libraries so can do sanity
  25. // checks on values passed to it. However, this may change in the future,
  26. // so the hooks framework is written such that a callout manager is used only
  27. // with the LibraryManagerCollection that created it. It is also the reason
  28. // why each LibraryManager contains a pointer to this CalloutManager.
  29. boost::shared_ptr<CalloutManager>
  30. LibraryManagerCollection::getCalloutManager() const {
  31. // Only return a pointer if we have a CalloutManager created.
  32. if (! callout_manager_) {
  33. isc_throw(LoadLibrariesNotCalled, "must load hooks libraries before "
  34. "attempting to retrieve a CalloutManager for them");
  35. }
  36. return (callout_manager_);
  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. callout_manager_.reset(new CalloutManager(library_names_.size()));
  54. // Now iterate through the libraries are load them one by one. We'll
  55. for (int i = 0; i < library_names_.size(); ++i) {
  56. // Create a pointer to the new library manager. The index of this
  57. // library is determined by the number of library managers currently
  58. // loaded: note that the library indexes run from 1 to (number of loaded
  59. // libraries).
  60. boost::shared_ptr<LibraryManager> manager(
  61. new LibraryManager(library_names_[i], lib_managers_.size() + 1,
  62. callout_manager_));
  63. // Load the library. On success, add it to the list of loaded
  64. // libraries. On failure, unload all currently loaded libraries,
  65. // leaving the object in the state it was in before loadLibraries was
  66. // called.
  67. if (manager->loadLibrary()) {
  68. lib_managers_.push_back(manager);
  69. } else {
  70. static_cast<void>(unloadLibraries());
  71. return (false);
  72. }
  73. }
  74. return (true);
  75. }
  76. // Unload the libraries.
  77. void
  78. LibraryManagerCollection::unloadLibraries() {
  79. // Delete the library managers in the reverse order to which they were
  80. // created, then clear the library manager vector.
  81. for (int i = lib_managers_.size() - 1; i >= 0; --i) {
  82. lib_managers_[i].reset();
  83. }
  84. lib_managers_.clear();
  85. // Get rid of the callout manager. (The other member, the list of library
  86. // names, was cleared when the libraries were loaded.)
  87. callout_manager_.reset();
  88. }
  89. // Return number of loaded libraries.
  90. int
  91. LibraryManagerCollection::getLoadedLibraryCount() const {
  92. return (lib_managers_.size());
  93. }
  94. // Validate the libraries.
  95. std::vector<std::string>
  96. LibraryManagerCollection::validateLibraries(
  97. const std::vector<std::string>& libraries) {
  98. std::vector<std::string> failures;
  99. for (int i = 0; i < libraries.size(); ++i) {
  100. if (!LibraryManager::validateLibrary(libraries[i])) {
  101. failures.push_back(libraries[i]);
  102. }
  103. }
  104. return (failures);
  105. }
  106. } // namespace hooks
  107. } // namespace isc