Kea: library_manager_collection.cc Source File

| Kea 2.2.0

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