Kea  1.5.0
hooks_manager.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2018 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 
7 #include <config.h>
8 
9 #include <hooks/callout_handle.h>
10 #include <hooks/callout_manager.h>
11 #include <hooks/callout_manager.h>
12 #include <hooks/library_handle.h>
14 #include <hooks/hooks_manager.h>
15 #include <hooks/server_hooks.h>
16 
17 #include <boost/shared_ptr.hpp>
18 
19 #include <string>
20 #include <vector>
21 
22 using namespace std;
23 
24 namespace isc {
25 namespace hooks {
26 
27 // Constructor
28 
29 HooksManager::HooksManager() {
30 }
31 
32 // Return reference to singleton hooks manager.
33 
34 HooksManager&
35 HooksManager::getHooksManager() {
36  static HooksManager manager;
37  return (manager);
38 }
39 
40 // Are callouts present?
41 
42 bool
43 HooksManager::calloutsPresentInternal(int index) {
44  conditionallyInitialize();
45  return (callout_manager_->calloutsPresent(index));
46 }
47 
48 bool
49 HooksManager::calloutsPresent(int index) {
50  return (getHooksManager().calloutsPresentInternal(index));
51 }
52 
53 bool
54 HooksManager::commandHandlersPresentInternal(const std::string& command_name) {
55  conditionallyInitialize();
56  return (callout_manager_->commandHandlersPresent(command_name));
57 }
58 
59 bool
60 HooksManager::commandHandlersPresent(const std::string& command_name) {
61  return (getHooksManager().commandHandlersPresentInternal(command_name));
62 }
63 
64 // Call the callouts
65 
66 void
67 HooksManager::callCalloutsInternal(int index, CalloutHandle& handle) {
68  conditionallyInitialize();
69  callout_manager_->callCallouts(index, handle);
70 }
71 
72 void
73 HooksManager::callCallouts(int index, CalloutHandle& handle) {
74  getHooksManager().callCalloutsInternal(index, handle);
75 }
76 
77 void
78 HooksManager::callCommandHandlersInternal(const std::string& command_name,
79  CalloutHandle& handle) {
80  conditionallyInitialize();
81  callout_manager_->callCommandHandlers(command_name, handle);
82 }
83 
84 void
85 HooksManager::callCommandHandlers(const std::string& command_name,
86  CalloutHandle& handle) {
87  getHooksManager().callCommandHandlersInternal(command_name, handle);
88 }
89 
90 
91 // Load the libraries. This will delete the previously-loaded libraries
92 // (if present) and load new ones.
93 
94 bool
95 HooksManager::loadLibrariesInternal(const HookLibsCollection& libraries) {
96  // Unload current set of libraries (if any are loaded).
97  unloadLibrariesInternal();
98 
99  // Create the library manager and load the libraries.
100  lm_collection_.reset(new LibraryManagerCollection(libraries));
101  bool status = lm_collection_->loadLibraries();
102 
103  if (status) {
104  // ... and obtain the callout manager for them if successful.
105  callout_manager_ = lm_collection_->getCalloutManager();
106  } else {
107  // Unable to load libraries, reset to state before this function was
108  // called.
109  lm_collection_.reset();
110  callout_manager_.reset();
111  }
112 
113  return (status);
114 }
115 
116 bool
117 HooksManager::loadLibraries(const HookLibsCollection& libraries) {
118  return (getHooksManager().loadLibrariesInternal(libraries));
119 }
120 
121 // Unload the libraries. This just deletes all internal objects which will
122 // cause the libraries to be unloaded.
123 
124 void
125 HooksManager::unloadLibrariesInternal() {
126  // The order of deletion does not matter here, as each library manager
127  // holds its own pointer to the callout manager. However, we may as
128  // well delete the library managers first: if there are no other references
129  // to the callout manager, the second statement will delete it, which may
130  // ease debugging.
131  lm_collection_.reset();
132  callout_manager_.reset();
133  ServerHooks::getServerHooks().getParkingLotsPtr()->clear();
134 }
135 
136 void HooksManager::unloadLibraries() {
137  getHooksManager().unloadLibrariesInternal();
138 }
139 
140 // Create a callout handle
141 
142 boost::shared_ptr<CalloutHandle>
143 HooksManager::createCalloutHandleInternal() {
144  conditionallyInitialize();
145  return (boost::shared_ptr<CalloutHandle>(
146  new CalloutHandle(callout_manager_, lm_collection_)));
147 }
148 
149 boost::shared_ptr<CalloutHandle>
150 HooksManager::createCalloutHandle() {
151  return (getHooksManager().createCalloutHandleInternal());
152 }
153 
154 // Get the list of the names of loaded libraries.
155 
156 std::vector<std::string>
157 HooksManager::getLibraryNamesInternal() const {
158  return (lm_collection_ ? lm_collection_->getLibraryNames()
159  : std::vector<std::string>());
160 }
161 
163 HooksManager::getLibraryInfoInternal() const {
164  return (lm_collection_ ? lm_collection_->getLibraryInfo()
165  : HookLibsCollection());
166 }
167 
168 std::vector<std::string>
169 HooksManager::getLibraryNames() {
170  return (getHooksManager().getLibraryNamesInternal());
171 }
172 
174 HooksManager::getLibraryInfo() {
175  return (getHooksManager().getLibraryInfoInternal());
176 }
177 
178 // Perform conditional initialization if nothing is loaded.
179 
180 void
181 HooksManager::performConditionalInitialization() {
182 
183  // Nothing present, so create the collection with any empty set of
184  // libraries, and get the CalloutManager.
185  HookLibsCollection libraries;
186  lm_collection_.reset(new LibraryManagerCollection(libraries));
187  lm_collection_->loadLibraries();
188 
189  callout_manager_ = lm_collection_->getCalloutManager();
190 }
191 
192 // Shell around ServerHooks::registerHook()
193 
194 int
195 HooksManager::registerHook(const std::string& name) {
196  return (ServerHooks::getServerHooks().registerHook(name));
197 }
198 
199 // Return pre- and post- library handles.
200 
202 HooksManager::preCalloutsLibraryHandleInternal() {
203  conditionallyInitialize();
204  return (callout_manager_->getPreLibraryHandle());
205 }
206 
208 HooksManager::preCalloutsLibraryHandle() {
209  return (getHooksManager().preCalloutsLibraryHandleInternal());
210 }
211 
213 HooksManager::postCalloutsLibraryHandleInternal() {
214  conditionallyInitialize();
215  return (callout_manager_->getPostLibraryHandle());
216 }
217 
219 HooksManager::postCalloutsLibraryHandle() {
220  return (getHooksManager().postCalloutsLibraryHandleInternal());
221 }
222 
223 // Validate libraries
224 
225 std::vector<std::string>
226 HooksManager::validateLibraries(const std::vector<std::string>& libraries) {
227  return (LibraryManagerCollection::validateLibraries(libraries));
228 }
229 
230 // Shared callout manager
231 boost::shared_ptr<CalloutManager>&
232 HooksManager::getSharedCalloutManager() {
233  return (getHooksManager().shared_callout_manager_);
234 }
235 
236 } // namespace util
237 } // namespace isc
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
Per-packet callout handle.
Defines the logger used by the top-level component of kea-dhcp-ddns.