Kea 3.3.4
library_manager_collection.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2025 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
10#include <hooks/hooks_manager.h>
13#include <boost/range/adaptor/reversed.hpp>
14
15namespace isc {
16namespace hooks {
17
18// Return callout manager for the loaded libraries. This call is only valid
19// after one has been created for the loaded libraries (which includes the
20// case of no loaded libraries).
21//
22// Note that there is no real connection between the callout manager and the
23// libraries, other than it knows the number of libraries so can do sanity
24// checks on values passed to it. However, this may change in the future,
25// so the hooks framework is written such that a callout manager is used only
26// with the LibraryManagerCollection that created it. It is also the reason
27// why each LibraryManager contains a pointer to this CalloutManager.
28
29boost::shared_ptr<CalloutManager>
31
32 // Only return a pointer if we have a CalloutManager created.
33 if (!callout_manager_) {
34 isc_throw(LoadLibrariesNotCalled, "must load hooks libraries before "
35 "attempting to retrieve a CalloutManager for them");
36 }
37
38 return (callout_manager_);
39}
40
42 : library_info_(libraries) {
43
44 // We need to split hook libs into library names and library parameters.
45 for (auto const& it : libraries) {
46 library_names_.push_back(it.libname_);
47 }
48}
49
50// Load a set of libraries
51
52bool
53LibraryManagerCollection::loadLibraries(bool multi_threading_enabled) {
54 std::string info;
55 return (loadLibraries(multi_threading_enabled, info));
56}
57
58bool
59LibraryManagerCollection::loadLibraries(bool multi_threading_enabled,
60 std::string& info) {
61
62 info.clear();
63 // There must be no libraries still in memory.
64 if (!lib_managers_.empty()) {
65 isc_throw(LibrariesStillOpened, "some libraries are still opened");
66 }
67
68 // Access the callout manager, (re)creating it if required.
69 //
70 // A pointer to the callout manager is maintained by each as well as by
71 // the HooksManager itself. Note that the callout manager does not hold any
72 // memory allocated by a library: although a library registers a callout
73 // (and so causes the creation of an entry in the CalloutManager's callout
74 // list), that creation is done by the CalloutManager itself. The
75 // CalloutManager is created within the server. The upshot of this is that
76 // it is therefore safe for the CalloutManager to be deleted after all
77 // associated libraries are deleted, hence this link (LibraryManager ->
78 // CalloutManager) is safe.
79 //
80 // The call of this function will result in re-creating the callout manager.
81 // This deletes all callouts (including the pre-library and post-
82 // library) ones. It is up to the libraries to re-register their callouts.
83 // The pre-library and post-library callouts will also need to be
84 // re-registered.
85 callout_manager_.reset(new CalloutManager(library_names_.size()));
86
87 // Now iterate through the libraries are load them one by one.
88 for (size_t i = 0; i < library_names_.size(); ++i) {
89 // Create a pointer to the new library manager. The index of this
90 // library is determined by the number of library managers currently
91 // loaded: note that the library indexes run from 1 to (number of loaded
92 // libraries).
93 boost::shared_ptr<LibraryManager> manager(
94 new LibraryManager(library_names_[i], lib_managers_.size() + 1,
95 callout_manager_));
96
97 // Load the library. On success, add it to the list of loaded
98 // libraries. On failure, unload all currently loaded libraries,
99 // leaving the object in the state it was in before loadLibraries was
100 // called.
101 if (manager->loadLibrary(multi_threading_enabled)) {
102 lib_managers_.push_back(manager);
103 } else {
104 std::ostringstream ss;
105 ss << "Error loading library: " << library_names_[i]
106 << " (index: " << i << ").";
107 info = ss.str();
108 static_cast<void>(unloadLibraries());
109 return (false);
110 }
111 }
112
113 return (true);
114}
115
116// Unload the libraries.
117
118void
120
121 // Delete the library managers in the reverse order to which they were
122 // created, then clear the library manager vector.
123 while (!lib_managers_.empty()) {
124 lib_managers_.pop_back();
125 }
126
127 // Get rid of the callout manager. (The other member, the list of library
128 // names, was cleared when the libraries were loaded.)
129 callout_manager_.reset();
130}
131
132// Prepare the unloading of libraries.
133bool
135 bool result = true;
136 // Iterate on library managers in reverse order.
137 for (auto const& lm : boost::adaptors::reverse(lib_managers_)) {
138 result = lm->prepareUnloadLibrary() && result;
139 }
140 return (result);
141}
142
143// Return number of loaded libraries.
144int
146 return (lib_managers_.size());
147}
148
149// Validate the libraries.
150std::vector<std::string>
151LibraryManagerCollection::validateLibraries(const std::vector<std::string>& libraries,
152 bool multi_threading_enabled) {
153
154 std::vector<std::string> failures;
155 for (size_t i = 0; i < libraries.size(); ++i) {
156 if (!LibraryManager::validateLibrary(libraries[i], multi_threading_enabled)) {
157 failures.push_back(libraries[i]);
158 }
159 }
160
161 return (failures);
162}
163
164} // namespace hooks
165} // namespace isc
Libraries still opened.
LibraryManagerCollection(const HookLibsCollection &libraries)
Constructor.
boost::shared_ptr< CalloutManager > getCalloutManager() const
Get callout manager.
bool prepareUnloadLibraries()
Prepare libaries unloading.
int getLoadedLibraryCount() const
Get number of loaded libraries.
static std::vector< std::string > validateLibraries(const std::vector< std::string > &libraries, bool multi_threading_enabled=false)
Validate libraries.
bool loadLibraries(bool multi_threading_enabled=false)
Load libraries.
static bool validateLibrary(const std::string &name, bool multi_threading_enabled=false)
Validate library.
#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.