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. : current_hook_(-1), current_library_(-1),
  29. hook_vector_(ServerHooks::getServerHooks().getCount()),
  30. library_handle_(this), pre_library_handle_(this, 0),
  31. post_library_handle_(this, INT_MAX), num_libraries_(num_libraries)
  32. {
  33. if (num_libraries < 0) {
  34. isc_throw(isc::BadValue, "number of libraries passed to the "
  35. "CalloutManager must be >= 0");
  36. }
  37. }
  38. // Check that the index of a library is valid. It can range from 1 - n
  39. // (n is the number of libraries), 0 (pre-user library callouts), or INT_MAX
  40. // (post-user library callouts). It can also be -1 to indicate an invalid
  41. // value.
  42. void
  43. CalloutManager::checkLibraryIndex(int library_index) const {
  44. if (((library_index >= -1) && (library_index <= num_libraries_)) ||
  45. (library_index == INT_MAX)) {
  46. return;
  47. }
  48. isc_throw(NoSuchLibrary, "library index " << library_index <<
  49. " is not valid for the number of loaded libraries (" <<
  50. num_libraries_ << ")");
  51. }
  52. // Register a callout for the current library.
  53. void
  54. CalloutManager::registerCallout(const std::string& name, CalloutPtr callout) {
  55. // Note the registration.
  56. LOG_DEBUG(hooks_logger, HOOKS_DBG_CALLS, HOOKS_CALLOUT_REGISTRATION)
  57. .arg(current_library_).arg(name);
  58. // Sanity check that the current library index is set to a valid value.
  59. checkLibraryIndex(current_library_);
  60. // Get the index associated with this hook (validating the name in the
  61. // process).
  62. int hook_index = ServerHooks::getServerHooks().getIndex(name);
  63. // Iterate through the callout vector for the hook from start to end,
  64. // looking for the first entry where the library index is greater than
  65. // the present index.
  66. for (CalloutVector::iterator i = hook_vector_[hook_index].begin();
  67. i != hook_vector_[hook_index].end(); ++i) {
  68. if (i->first > current_library_) {
  69. // Found an element whose library index number is greater than the
  70. // current index, so insert the new element ahead of this one.
  71. hook_vector_[hook_index].insert(i, make_pair(current_library_,
  72. callout));
  73. return;
  74. }
  75. }
  76. // Reached the end of the vector, so there is no element in the (possibly
  77. // empty) set of callouts with a library index greater than the current
  78. // library index. Inset the callout at the end of the list.
  79. hook_vector_[hook_index].push_back(make_pair(current_library_, callout));
  80. }
  81. // Check if callouts are present for a given hook index.
  82. bool
  83. CalloutManager::calloutsPresent(int hook_index) const {
  84. // Validate the hook index.
  85. if ((hook_index < 0) || (hook_index >= hook_vector_.size())) {
  86. isc_throw(NoSuchHook, "hook index " << hook_index <<
  87. " is not valid for the list of registered hooks");
  88. }
  89. // Valid, so are there any callouts associated with that hook?
  90. return (!hook_vector_[hook_index].empty());
  91. }
  92. // Call all the callouts for a given hook.
  93. void
  94. CalloutManager::callCallouts(int hook_index, CalloutHandle& callout_handle) {
  95. // Clear the "skip" flag so we don't carry state from a previous call.
  96. // This is done regardless of whether callouts are present to avoid passing
  97. // any state from the previous call of callCallouts().
  98. callout_handle.setSkip(false);
  99. // Only initialize and iterate if there are callouts present. This check
  100. // also catches the case of an invalid index.
  101. if (calloutsPresent(hook_index)) {
  102. // Set the current hook index. This is used should a callout wish to
  103. // determine to what hook it is attached.
  104. current_hook_ = hook_index;
  105. // Duplicate the callout vector for this hook and work through that.
  106. // This step is needed because we allow dynamic registration and
  107. // deregistration of callouts. If a callout attached to a hook modified
  108. // the list of callouts on that hook, the underlying CalloutVector would
  109. // change and potentially affect the iteration through that vector.
  110. CalloutVector callouts(hook_vector_[hook_index]);
  111. // Call all the callouts.
  112. for (CalloutVector::const_iterator i = callouts.begin();
  113. i != callouts.end(); ++i) {
  114. // In case the callout tries to register or deregister a callout,
  115. // set the current library index to the index associated with the
  116. // library that registered the callout being called.
  117. current_library_ = i->first;
  118. // Call the callout
  119. try {
  120. int status = (*i->second)(callout_handle);
  121. if (status == 0) {
  122. LOG_DEBUG(hooks_logger, HOOKS_DBG_EXTENDED_CALLS,
  123. HOOKS_CALLOUT_CALLED).arg(current_library_)
  124. .arg(ServerHooks::getServerHooks()
  125. .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(ServerHooks::getServerHooks()
  131. .getName(current_hook_))
  132. .arg(PointerConverter(i->second).dlsymPtr());
  133. }
  134. } catch (...) {
  135. // Any exception, not just ones based on isc::Exception
  136. LOG_ERROR(hooks_logger, HOOKS_CALLOUT_EXCEPTION)
  137. .arg(current_library_)
  138. .arg(ServerHooks::getServerHooks().getName(current_hook_))
  139. .arg(PointerConverter(i->second).dlsymPtr());
  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 = ServerHooks::getServerHooks().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 = ServerHooks::getServerHooks().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