callout_manager.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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_handle.h>
  15. #include <hooks/callout_manager.h>
  16. #include <hooks/hooks_log.h>
  17. #include <hooks/pointer_converter.h>
  18. #include <boost/static_assert.hpp>
  19. #include <algorithm>
  20. #include <climits>
  21. #include <functional>
  22. #include <utility>
  23. using namespace std;
  24. namespace isc {
  25. namespace hooks {
  26. // Constructor
  27. CalloutManager::CalloutManager(int num_libraries)
  28. : server_hooks_(ServerHooks::getServerHooks()),
  29. current_hook_(-1), current_library_(-1),
  30. hook_vector_(ServerHooks::getServerHooks().getCount()),
  31. library_handle_(this), pre_library_handle_(this, 0),
  32. post_library_handle_(this, INT_MAX), num_libraries_(num_libraries)
  33. {
  34. if (num_libraries < 0) {
  35. isc_throw(isc::BadValue, "number of libraries passed to the "
  36. "CalloutManager must be >= 0");
  37. }
  38. }
  39. // Check that the index of a library is valid. It can range from 1 - n
  40. // (n is the number of libraries), 0 (pre-user library callouts), or INT_MAX
  41. // (post-user library callouts). It can also be -1 to indicate an invalid
  42. // value.
  43. void
  44. CalloutManager::checkLibraryIndex(int library_index) const {
  45. if (((library_index >= -1) && (library_index <= num_libraries_)) ||
  46. (library_index == INT_MAX)) {
  47. return;
  48. }
  49. isc_throw(NoSuchLibrary, "library index " << library_index <<
  50. " is not valid for the number of loaded libraries (" <<
  51. num_libraries_ << ")");
  52. }
  53. // Register a callout for the current library.
  54. void
  55. CalloutManager::registerCallout(const std::string& name, CalloutPtr callout) {
  56. // Note the registration.
  57. LOG_DEBUG(hooks_logger, HOOKS_DBG_CALLS, HOOKS_CALLOUT_REGISTRATION)
  58. .arg(current_library_).arg(name);
  59. // Sanity check that the current library index is set to a valid value.
  60. checkLibraryIndex(current_library_);
  61. // Get the index associated with this hook (validating the name in the
  62. // process).
  63. int hook_index = server_hooks_.getIndex(name);
  64. // Iterate through the callout vector for the hook from start to end,
  65. // looking for the first entry where the library index is greater than
  66. // the present index.
  67. for (CalloutVector::iterator i = hook_vector_[hook_index].begin();
  68. i != hook_vector_[hook_index].end(); ++i) {
  69. if (i->first > current_library_) {
  70. // Found an element whose library index number is greater than the
  71. // current index, so insert the new element ahead of this one.
  72. hook_vector_[hook_index].insert(i, make_pair(current_library_,
  73. callout));
  74. return;
  75. }
  76. }
  77. // Reached the end of the vector, so there is no element in the (possibly
  78. // empty) set of callouts with a library index greater than the current
  79. // library index. Inset the callout at the end of the list.
  80. hook_vector_[hook_index].push_back(make_pair(current_library_, callout));
  81. }
  82. // Check if callouts are present for a given hook index.
  83. bool
  84. CalloutManager::calloutsPresent(int hook_index) const {
  85. // Validate the hook index.
  86. if ((hook_index < 0) || (hook_index >= hook_vector_.size())) {
  87. isc_throw(NoSuchHook, "hook index " << hook_index <<
  88. " is not valid for the list of registered hooks");
  89. }
  90. // Valid, so are there any callouts associated with that hook?
  91. return (!hook_vector_[hook_index].empty());
  92. }
  93. // Call all the callouts for a given hook.
  94. void
  95. CalloutManager::callCallouts(int hook_index, CalloutHandle& callout_handle) {
  96. // Clear the "skip" flag so we don't carry state from a previous call.
  97. // This is done regardless of whether callouts are present to avoid passing
  98. // any state from the previous call of callCallouts().
  99. callout_handle.setSkip(false);
  100. // Only initialize and iterate if there are callouts present. This check
  101. // also catches the case of an invalid index.
  102. if (calloutsPresent(hook_index)) {
  103. // Set the current hook index. This is used should a callout wish to
  104. // determine to what hook it is attached.
  105. current_hook_ = hook_index;
  106. // Duplicate the callout vector for this hook and work through that.
  107. // This step is needed because we allow dynamic registration and
  108. // deregistration of callouts. If a callout attached to a hook modified
  109. // the list of callouts on that hook, the underlying CalloutVector would
  110. // change and potentially affect the iteration through that vector.
  111. CalloutVector callouts(hook_vector_[hook_index]);
  112. // Call all the callouts.
  113. for (CalloutVector::const_iterator i = callouts.begin();
  114. i != callouts.end(); ++i) {
  115. // In case the callout tries to register or deregister a callout,
  116. // set the current library index to the index associated with the
  117. // library that registered the callout being called.
  118. current_library_ = i->first;
  119. // Call the callout
  120. try {
  121. int status = (*i->second)(callout_handle);
  122. if (status == 0) {
  123. LOG_DEBUG(hooks_logger, HOOKS_DBG_EXTENDED_CALLS,
  124. HOOKS_CALLOUT_CALLED).arg(current_library_)
  125. .arg(server_hooks_.getName(current_hook_))
  126. .arg(PointerConverter(i->second).dlsymPtr());
  127. } else {
  128. LOG_ERROR(hooks_logger, HOOKS_CALLOUT_ERROR)
  129. .arg(current_library_)
  130. .arg(server_hooks_.getName(current_hook_))
  131. .arg(PointerConverter(i->second).dlsymPtr());
  132. }
  133. } catch (const std::exception& e) {
  134. // Any exception, not just ones based on isc::Exception
  135. LOG_ERROR(hooks_logger, HOOKS_CALLOUT_EXCEPTION)
  136. .arg(current_library_)
  137. .arg(server_hooks_.getName(current_hook_))
  138. .arg(PointerConverter(i->second).dlsymPtr())
  139. .arg(e.what());
  140. }
  141. }
  142. // Reset the current hook and library indexs to an invalid value to
  143. // catch any programming errors.
  144. current_hook_ = -1;
  145. current_library_ = -1;
  146. }
  147. }
  148. // Deregister a callout registered by the current library on a particular hook.
  149. bool
  150. CalloutManager::deregisterCallout(const std::string& name, CalloutPtr callout) {
  151. // Sanity check that the current library index is set to a valid value.
  152. checkLibraryIndex(current_library_);
  153. // Get the index associated with this hook (validating the name in the
  154. // process).
  155. int hook_index = server_hooks_.getIndex(name);
  156. /// Construct a CalloutEntry matching the current library and the callout
  157. /// we want to remove.
  158. CalloutEntry target(current_library_, callout);
  159. /// To decide if any entries were removed, we'll record the initial size
  160. /// of the callout vector for the hook, and compare it with the size after
  161. /// the removal.
  162. size_t initial_size = hook_vector_[hook_index].size();
  163. // The next bit is standard STL (see "Item 33" in "Effective STL" by
  164. // Scott Meyers).
  165. //
  166. // remove_if reorders the hook vector so that all items not matching
  167. // the predicate are at the start of the vector and returns a pointer
  168. // to the next element. (In this case, the predicate is that the item
  169. // is equal to the value of the passed callout.) The erase() call
  170. // removes everything from that element to the end of the vector, i.e.
  171. // all the matching elements.
  172. hook_vector_[hook_index].erase(remove_if(hook_vector_[hook_index].begin(),
  173. hook_vector_[hook_index].end(),
  174. bind1st(equal_to<CalloutEntry>(),
  175. target)),
  176. hook_vector_[hook_index].end());
  177. // Return an indication of whether anything was removed.
  178. bool removed = initial_size != hook_vector_[hook_index].size();
  179. if (removed) {
  180. LOG_DEBUG(hooks_logger, HOOKS_DBG_EXTENDED_CALLS,
  181. HOOKS_CALLOUT_DEREGISTERED).arg(current_library_).arg(name);
  182. }
  183. return (removed);
  184. }
  185. // Deregister all callouts on a given hook.
  186. bool
  187. CalloutManager::deregisterAllCallouts(const std::string& name) {
  188. // Get the index associated with this hook (validating the name in the
  189. // process).
  190. int hook_index = server_hooks_.getIndex(name);
  191. /// Construct a CalloutEntry matching the current library (the callout
  192. /// pointer is NULL as we are not checking that).
  193. CalloutEntry target(current_library_, NULL);
  194. /// To decide if any entries were removed, we'll record the initial size
  195. /// of the callout vector for the hook, and compare it with the size after
  196. /// the removal.
  197. size_t initial_size = hook_vector_[hook_index].size();
  198. // Remove all callouts matching this library.
  199. hook_vector_[hook_index].erase(remove_if(hook_vector_[hook_index].begin(),
  200. hook_vector_[hook_index].end(),
  201. bind1st(CalloutLibraryEqual(),
  202. target)),
  203. hook_vector_[hook_index].end());
  204. // Return an indication of whether anything was removed.
  205. bool removed = initial_size != hook_vector_[hook_index].size();
  206. if (removed) {
  207. LOG_DEBUG(hooks_logger, HOOKS_DBG_EXTENDED_CALLS,
  208. HOOKS_ALL_CALLOUTS_DEREGISTERED).arg(current_library_)
  209. .arg(name);
  210. }
  211. return (removed);
  212. }
  213. } // namespace util
  214. } // namespace isc