Kea 3.3.4
hooks_manager.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2023 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
13#include <hooks/hooks_manager.h>
14#include <hooks/server_hooks.h>
15
16#include <boost/shared_ptr.hpp>
17#include <boost/weak_ptr.hpp>
18
19#include <string>
20#include <vector>
21
22using namespace std;
23
24namespace isc {
25namespace hooks {
26
27// Constructor
28
29HooksManager::HooksManager() : test_mode_(false) {
30 // Nothing present, so create the collection with any empty set of
31 // libraries, and get the CalloutManager.
32 HookLibsCollection libraries;
33 lm_collection_.reset(new LibraryManagerCollection(libraries));
34 lm_collection_->loadLibraries(false);
35 callout_manager_ = lm_collection_->getCalloutManager();
36}
37
38// Return reference to singleton hooks manager.
39
40HooksManager&
41HooksManager::getHooksManager() {
42 static HooksManager manager;
43 return (manager);
44}
45
46// Are callouts present?
47
48bool
49HooksManager::calloutsPresentInternal(int index) {
50 return (callout_manager_->calloutsPresent(index));
51}
52
53bool
55 return (getHooksManager().calloutsPresentInternal(index));
56}
57
58bool
59HooksManager::commandHandlersPresentInternal(const std::string& command_name) {
60 return (callout_manager_->commandHandlersPresent(command_name));
61}
62
63bool
64HooksManager::commandHandlersPresent(const std::string& command_name) {
65 return (getHooksManager().commandHandlersPresentInternal(command_name));
66}
67
68// Call the callouts
69
70void
71HooksManager::callCalloutsInternal(int index, CalloutHandle& handle) {
72 callout_manager_->callCallouts(index, handle);
73}
74
75void
77 getHooksManager().callCalloutsInternal(index, handle);
78}
79
80void
81HooksManager::callCommandHandlersInternal(const std::string& command_name,
82 CalloutHandle& handle) {
83 callout_manager_->callCommandHandlers(command_name, handle);
84}
85
86void
87HooksManager::callCommandHandlers(const std::string& command_name,
88 CalloutHandle& handle) {
89 getHooksManager().callCommandHandlersInternal(command_name, handle);
90}
91
92// Load the libraries. This will delete the previously-loaded libraries
93// (if present) and load new ones. If loading libraries fails, initialize with
94// empty list.
95
96bool
97HooksManager::loadLibrariesInternal(const HookLibsCollection& libraries,
98 bool multi_threading_enabled,
99 string& info) {
100 info.clear();
101 if (test_mode_) {
102 return (true);
103 }
104
105 ServerHooks::getServerHooks().getParkingLotsPtr()->clear();
106
107 // Keep a weak pointer on the existing library manager collection.
108 boost::weak_ptr<LibraryManagerCollection> weak_lmc(lm_collection_);
109
110 // Create the library manager collection.
111 lm_collection_.reset(new LibraryManagerCollection(libraries));
112
113 // If there was another owner the previous library manager collection
114 // was not destroyed and libraries not closed.
115 if (!weak_lmc.expired()) {
116 isc_throw(LibrariesStillOpened, "some libraries are still opened");
117 }
118
119 // Load the libraries.
120 bool status = lm_collection_->loadLibraries(multi_threading_enabled, info);
121
122 if (status) {
123 // ... and obtain the callout manager for them if successful.
124 callout_manager_ = lm_collection_->getCalloutManager();
125 } else {
126 // Unable to load libraries, reset to state before this function was
127 // called.
128 static_cast<void>(unloadLibrariesInternal());
129 }
130
131 return (status);
132}
133
134bool
136 bool multi_threading_enabled) {
137 string info;
138 return (getHooksManager().loadLibrariesInternal(libraries,
139 multi_threading_enabled,
140 info));
141}
142
143bool
145 bool multi_threading_enabled,
146 string& info) {
147 return (getHooksManager().loadLibrariesInternal(libraries,
148 multi_threading_enabled,
149 info));
150}
151
152// Unload the libraries. This just deletes all internal objects (which will
153// cause the libraries to be unloaded) and initializes them with empty list if
154// requested.
155
156bool
157HooksManager::unloadLibrariesInternal() {
158 if (test_mode_) {
159 return (true);
160 }
161
162 ServerHooks::getServerHooks().getParkingLotsPtr()->clear();
163
164 // Keep a weak pointer on the existing library manager collection.
165 boost::weak_ptr<LibraryManagerCollection> weak_lmc(lm_collection_);
166
167 // Create the collection with an empty set of libraries.
168 HookLibsCollection libraries;
169 lm_collection_.reset(new LibraryManagerCollection(libraries));
170
171 // If there was another owner the previous library manager collection
172 // was not destroyed and libraries not closed.
173 boost::shared_ptr<LibraryManagerCollection> still_here = weak_lmc.lock();
174 if (still_here) {
175 // Restore the library manager collection.
176 lm_collection_ = still_here;
177 return (false);
178 }
179
180 // Load the empty set of libraries.
181 lm_collection_->loadLibraries(false);
182
183 // Get the CalloutManager.
184 callout_manager_ = lm_collection_->getCalloutManager();
185
186 return (true);
187}
188
189bool
191 return (getHooksManager().unloadLibrariesInternal());
192}
193
194void
195HooksManager::prepareUnloadLibrariesInternal() {
196 if (test_mode_) {
197 return;
198 }
199
200 static_cast<void>(lm_collection_->prepareUnloadLibraries());
201}
202
203void
205 getHooksManager().prepareUnloadLibrariesInternal();
206}
207
208// Create a callout handle
209
210boost::shared_ptr<CalloutHandle>
211HooksManager::createCalloutHandleInternal() {
212 return (boost::make_shared<CalloutHandle>(callout_manager_, lm_collection_));
213}
214
215boost::shared_ptr<CalloutHandle>
217 return (getHooksManager().createCalloutHandleInternal());
218}
219
220// Get the list of the names of loaded libraries.
221
222std::vector<std::string>
223HooksManager::getLibraryNamesInternal() const {
224 return (lm_collection_->getLibraryNames());
225}
226
227HookLibsCollection
228HooksManager::getLibraryInfoInternal() const {
229 return (lm_collection_->getLibraryInfo());
230}
231
232std::vector<std::string>
234 return (getHooksManager().getLibraryNamesInternal());
235}
236
239 return (getHooksManager().getLibraryInfoInternal());
240}
241
242// Shell around ServerHooks::registerHook()
243
244int
245HooksManager::registerHook(const std::string& name) {
247}
248
249// Return pre- and post- library handles.
250
252HooksManager::preCalloutsLibraryHandleInternal() {
253 return (callout_manager_->getPreLibraryHandle());
254}
255
258 return (getHooksManager().preCalloutsLibraryHandleInternal());
259}
260
262HooksManager::postCalloutsLibraryHandleInternal() {
263 return (callout_manager_->getPostLibraryHandle());
264}
265
268 return (getHooksManager().postCalloutsLibraryHandleInternal());
269}
270
271// Validate libraries
272
273std::vector<std::string>
274HooksManager::validateLibraries(const std::vector<std::string>& libraries,
275 bool multi_threading_enabled) {
277 multi_threading_enabled));
278}
279
280// Test mode
281
282void
284 getHooksManager().test_mode_ = mode;
285}
286
287bool
289 return (getHooksManager().test_mode_);
290}
291
292} // namespace util
293} // namespace isc
Per-packet callout handle.
static int registerHook(const std::string &name)
Register Hook.
static LibraryHandle & postCalloutsLibraryHandle()
Return post-callouts library handle.
static bool calloutsPresent(int index)
Are callouts present?
static void setTestMode(bool mode)
Set test mode.
static std::vector< std::string > getLibraryNames()
Return list of loaded libraries.
static void callCommandHandlers(const std::string &command_name, CalloutHandle &handle)
Calls the callouts/command handlers for a given command name.
static std::vector< std::string > validateLibraries(const std::vector< std::string > &libraries, bool multi_threading_enabled=false)
Validate library list.
static bool unloadLibraries()
Unload libraries.
static bool loadLibraries(const HookLibsCollection &libraries, bool multi_threading_enabled=false)
Load and reload libraries.
static LibraryHandle & preCalloutsLibraryHandle()
Return pre-callouts library handle.
static bool commandHandlersPresent(const std::string &command_name)
Checks if control command handlers are present for the specified command.
static boost::shared_ptr< CalloutHandle > createCalloutHandle()
Return callout handle.
static void callCallouts(int index, CalloutHandle &handle)
Calls the callouts for a given hook.
static bool getTestMode()
Get test mode.
static HookLibsCollection getLibraryInfo()
Return list of loaded libraries with its parameters.
static void prepareUnloadLibraries()
Prepare the unloading of libraries.
static std::vector< std::string > validateLibraries(const std::vector< std::string > &libraries, bool multi_threading_enabled=false)
Validate libraries.
static ServerHooks & getServerHooks()
Return ServerHooks object.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition libinfo.h:51
Defines the logger used by the top-level component of kea-lfc.