hooks_manager.h 12 KB

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