hooks_manager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #ifndef HOOKS_MANAGER_H
  15. #define HOOKS_MANAGER_H
  16. #include <hooks/server_hooks.h>
  17. #include <boost/noncopyable.hpp>
  18. #include <boost/shared_ptr.hpp>
  19. #include <string>
  20. #include <vector>
  21. namespace isc {
  22. namespace hooks {
  23. // Forward declarations
  24. class CalloutHandle;
  25. class CalloutManager;
  26. class LibraryHandle;
  27. class LibraryManagerCollection;
  28. /// @brief Hooks Manager
  29. ///
  30. /// This is the overall manager of the hooks framework and is the main class
  31. /// used by a BIND 10 module when handling hooks. It is responsible for the
  32. /// loading and unloading of user libraries, and for calling the callouts on
  33. /// each hook point.
  34. ///
  35. /// The class is a singleton, the single instance of the object being accessed
  36. /// through the static getHooksManager() method.
  37. class HooksManager : boost::noncopyable {
  38. public:
  39. /// @brief Get singleton hooks manager
  40. ///
  41. /// @return Reference to the singleton hooks manager.
  42. static HooksManager& getHooksManager();
  43. /// @brief Validate library list
  44. ///
  45. /// For each library passed to it, checks that the library can be opened
  46. /// and that the "version" function is present and gives the right answer.
  47. /// Each library is closed afterwards.
  48. ///
  49. /// This is used during the configuration parsing - when the list of hooks
  50. /// libraries is changed, each of the new libraries is checked before the
  51. /// change is committed.
  52. ///
  53. /// @param List of libraries to be validated.
  54. ///
  55. /// @return An empty string if all libraries validated. Otherwise it is
  56. /// the names of the libraries that failed validation, separated
  57. /// by a command and a space. The configuration code can return
  58. /// this to bindctl as an indication of the problem. (Note that
  59. /// validation failures are logged, so more information can be
  60. /// obtained if necessary.)
  61. static std::string validateLibraries(
  62. const std::vector<std::string>& libraries);
  63. /// @brief Load and reload libraries
  64. ///
  65. /// Loads the list of libraries into the server address space. For each
  66. /// library, the "standard" functions (ones with the same names as the
  67. /// hook points) are configured and the libraries' "load" function
  68. /// called.
  69. ///
  70. /// If libraries are already loaded, they are unloaded and the new
  71. /// libraries loaded.
  72. ///
  73. /// If any library fails to load, an error message will be logged. The
  74. /// remaining libraries will be loaded if possible.
  75. ///
  76. /// @param libraries List of libraries to be loaded. The order is
  77. /// important, as it determines the order that callouts on the same
  78. /// hook will be called.
  79. ///
  80. /// @return true if all libraries loaded without a problem, false if one or
  81. /// more libraries failed to load. In the latter case, message will
  82. /// be logged that give the reason.
  83. static bool loadLibraries(const std::vector<std::string>& libraries);
  84. /// @brief Unload libraries
  85. ///
  86. /// Unloads the loaded libraries and leaves the hooks subsystem in the
  87. /// state it was after construction but before loadLibraries() is called.
  88. ///
  89. /// @note: This method should be used with caution - see the notes for
  90. /// the class LibraryManager for pitfalls. In general, a server
  91. /// should not call this method: library unloading will automatically
  92. /// take place when new libraries are loaded, and when appropriate
  93. /// objects are destroyed.
  94. ///
  95. /// @return true if all libraries unloaded successfully, false on an error.
  96. /// In the latter case, an error message will have been output.
  97. static void unloadLibraries();
  98. /// @brief Are callouts present?
  99. ///
  100. /// Checks loaded libraries and returns true if at lease one callout
  101. /// has been registered by them for the given hook.
  102. ///
  103. /// @param index Hooks index for which callouts are checked.
  104. ///
  105. /// @return true if callouts are present, false if not.
  106. /// @throw NoSuchHook Given index does not correspond to a valid hook.
  107. static bool calloutsPresent(int index);
  108. /// @brief Calls the callouts for a given hook
  109. ///
  110. /// Iterates through the libray handles and calls the callouts associated
  111. /// with the given hook index.
  112. ///
  113. /// @note This method invalidates the current library index set with
  114. /// setLibraryIndex().
  115. ///
  116. /// @param index Index of the hook to call.
  117. /// @param handle Reference to the CalloutHandle object for the current
  118. /// object being processed.
  119. static void callCallouts(int index, CalloutHandle& handle);
  120. /// @brief Return pre-callouts library handle
  121. ///
  122. /// Returns a library handle that can be used by the server to register
  123. /// callouts on a hook that are called _before_ any callouts belonging
  124. /// to a library.
  125. ///
  126. /// @note Both the reference returned and the callouts registered with
  127. /// this handle only remain valid until the next loadLibraries() or
  128. /// unloadLibraries() call. If the callouts are to remain registered
  129. /// after this time, a new handle will need to be obtained and the
  130. /// callouts re-registered.
  131. ///
  132. /// @return Reference to library handle associated with pre-library callout
  133. /// registration.
  134. static LibraryHandle& preCalloutsLibraryHandle();
  135. /// @brief Return post-callouts library handle
  136. ///
  137. /// Returns a library handle that can be used by the server to register
  138. /// callouts on a hook that are called _after any callouts belonging
  139. /// to a library.
  140. ///
  141. /// @note Both the reference returned and the callouts registered with
  142. /// this handle only remain valid until the next loadLibraries() or
  143. /// unloadLibraries() call. If the callouts are to remain registered
  144. /// after this time, a new handle will need to be obtained and the
  145. /// callouts re-registered.
  146. ///
  147. /// @return Reference to library handle associated with post-library callout
  148. /// registration.
  149. static LibraryHandle& postCalloutsLibraryHandle();
  150. /// @brief Return callout handle
  151. ///
  152. /// Returns a callout handle to be associated with a request passed round
  153. /// the system.
  154. ///
  155. /// @note This handle is valid only after a loadLibraries() call and then
  156. /// only up to the next loadLibraries() call.
  157. ///
  158. /// @return Shared pointer to a CalloutHandle object.
  159. static boost::shared_ptr<CalloutHandle> createCalloutHandle();
  160. /// @brief Register Hook
  161. ///
  162. /// This is just a convenience shell around the ServerHooks::registerHook()
  163. /// method. It - along with the definitions of the two hook indexes for
  164. /// the context_create and context_destroy methods - means that server
  165. /// authors only need to deal with HooksManager and CalloutHandle, and not
  166. /// include any other hooks framework classes.
  167. ///
  168. /// @param name Name of the hook
  169. ///
  170. /// @return Index of the hook, to be used in subsequent hook-related calls.
  171. /// This will be greater than or equal to zero (so allowing a
  172. /// negative value to indicate an invalid index).
  173. ///
  174. /// @throws DuplicateHook A hook with the same name has already been
  175. /// registered.
  176. static int registerHook(const std::string& name);
  177. /// @brief Return list of loaded libraries
  178. ///
  179. /// Returns the names of the loaded libraries.
  180. ///
  181. /// @return List of loaded library names.
  182. static std::vector<std::string> getLibraryNames();
  183. /// @brief Validate set of libraries
  184. ///
  185. /// Validates the names of the libraries passed to it. The function checks
  186. /// that the libraries exist, that they contain a "version" function and
  187. /// that it returns the right value.
  188. ///
  189. /// This is really just a wrapper around the LibraryManagerCollection
  190. /// static method of the same name, and is supplied so that the server
  191. /// does not have to know about that object.
  192. ///
  193. /// @param libraries Names of the libraries to validate
  194. ///
  195. /// @return Comma-separated list of libraries that failed to validate,
  196. /// empty string if not. (Actually, if the list of failures is
  197. /// more than one, each item is separated by a command and a space.)
  198. static std::string validateLibraries();
  199. /// Index numbers for pre-defined hooks.
  200. static const int CONTEXT_CREATE = ServerHooks::CONTEXT_CREATE;
  201. static const int CONTEXT_DESTROY = ServerHooks::CONTEXT_DESTROY;
  202. private:
  203. /// @brief Constructor
  204. ///
  205. /// This is private as the object is a singleton and can only be addessed
  206. /// through the getHooksManager() static method.
  207. HooksManager();
  208. //@{
  209. /// The following methods correspond to similarly-named static methods,
  210. /// but actually do the work on the singleton instance of the HooksManager.
  211. /// See the descriptions of the static methods for more details.
  212. /// @brief Validate library list
  213. ///
  214. /// @param List of libraries to be validated.
  215. ///
  216. /// @return An empty string if all libraries validated. Otherwise it is
  217. /// the name of the first library that failed validation. The
  218. /// configuration code can return this to bindctl as an indication
  219. /// of the problem.
  220. std::string validateLibrariesInternal(
  221. const std::vector<std::string>& libraries) const;
  222. /// @brief Load and reload libraries
  223. ///
  224. /// @param libraries List of libraries to be loaded. The order is
  225. /// important, as it determines the order that callouts on the same
  226. /// hook will be called.
  227. ///
  228. /// @return true if all libraries loaded without a problem, false if one or
  229. /// more libraries failed to load. In the latter case, message will
  230. /// be logged that give the reason.
  231. bool loadLibrariesInternal(const std::vector<std::string>& libraries);
  232. /// @brief Unload libraries
  233. void unloadLibrariesInternal();
  234. /// @brief Are callouts present?
  235. ///
  236. /// @param index Hooks index for which callouts are checked.
  237. ///
  238. /// @return true if callouts are present, false if not.
  239. /// @throw NoSuchHook Given index does not correspond to a valid hook.
  240. bool calloutsPresentInternal(int index);
  241. /// @brief Calls the callouts for a given hook
  242. ///
  243. /// @param index Index of the hook to call.
  244. /// @param handle Reference to the CalloutHandle object for the current
  245. /// object being processed.
  246. void callCalloutsInternal(int index, CalloutHandle& handle);
  247. /// @brief Return callout handle
  248. ///
  249. /// @return Shared pointer to a CalloutHandle object.
  250. boost::shared_ptr<CalloutHandle> createCalloutHandleInternal();
  251. /// @brief Return pre-callouts library handle
  252. ///
  253. /// @return Reference to library handle associated with pre-library callout
  254. /// registration.
  255. LibraryHandle& preCalloutsLibraryHandleInternal();
  256. /// @brief Return post-callouts library handle
  257. ///
  258. /// @return Reference to library handle associated with post-library callout
  259. /// registration.
  260. LibraryHandle& postCalloutsLibraryHandleInternal();
  261. /// @brief Return list of loaded libraries
  262. ///
  263. /// @return List of loaded library names.
  264. std::vector<std::string> getLibraryNamesInternal() const;
  265. //@}
  266. /// @brief Initialization to No Libraries
  267. ///
  268. /// Initializes the hooks manager with an "empty set" of libraries. This
  269. /// method is called if conditionallyInitialize() determines that such
  270. /// initialization is needed.
  271. void performConditionalInitialization();
  272. /// @brief Conditional initialization of the hooks manager
  273. ///
  274. /// loadLibraries() performs the initialization of the HooksManager,
  275. /// setting up the internal structures and loading libraries. However,
  276. /// in some cases, server authors may not do that. This method is called
  277. /// whenever any hooks execution function is invoked (checking callouts,
  278. /// calling callouts or returning a callout handle). If the HooksManager
  279. /// is unitialised, it will initialize it with an "empty set" of libraries.
  280. ///
  281. /// For speed, the test of whether initialization is required is done
  282. /// in-line here. The actual initialization is performed in
  283. /// performConditionalInitialization().
  284. void conditionallyInitialize() {
  285. if (!lm_collection_) {
  286. performConditionalInitialization();
  287. }
  288. }
  289. // Members
  290. /// Set of library managers.
  291. boost::shared_ptr<LibraryManagerCollection> lm_collection_;
  292. /// Callout manager for the set of library managers.
  293. boost::shared_ptr<CalloutManager> callout_manager_;
  294. };
  295. } // namespace util
  296. } // namespace hooks
  297. #endif // HOOKS_MANAGER_H