Kea  2.5.2
hooks_config.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-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 
9 #include <hooks/hooks_config.h>
10 #include <hooks/hooks_manager.h>
11 
12 using namespace std;
13 using namespace isc;
14 using namespace isc::data;
15 
16 namespace isc {
17 namespace hooks {
18 
19 void
20 HooksConfig::verifyLibraries(const Element::Position& position,
21  bool multi_threading_enabled) const {
22  // The code used to follow this logic:
23  //
24  // Check if the list of libraries has changed. If not, nothing is done
25  // - the command "DhcpN libreload" is required to reload the same
26  // libraries (this prevents needless reloads when anything else in the
27  // configuration is changed).
28  //
29  // We no longer rely on this. Parameters can change. And even if the
30  // parameters stay the same, they could point to files that could
31  // change. We can skip loading routines only if there were and there still
32  // are no libraries specified.
33  vector<string> current_libraries = HooksManager::getLibraryNames();
34  if (current_libraries.empty() && libraries_.empty()) {
35  return;
36  }
37 
38  // Library list has changed, validate each of the libraries specified.
39  vector<string> lib_names = isc::hooks::extractNames(libraries_);
40  vector<string> error_libs = HooksManager::validateLibraries(lib_names,
41  multi_threading_enabled);
42  if (!error_libs.empty()) {
43 
44  // Construct the list of libraries in error for the message.
45  string error_list = error_libs[0];
46  for (size_t i = 1; i < error_libs.size(); ++i) {
47  error_list += (string(", ") + error_libs[i]);
48  }
50  "hooks libraries failed to validate - "
51  "library or libraries in error are: "
52  << error_list << " (" << position << ")");
53  }
54 }
55 
56 void
57 HooksConfig::loadLibraries(bool multi_threading_enabled) const {
62  if (!HooksManager::loadLibraries(libraries_, multi_threading_enabled)) {
64  "One or more hook libraries failed to load");
65  }
66 }
67 
68 bool
69 HooksConfig::equal(const HooksConfig& other) const {
70 
79  for (isc::hooks::HookLibsCollection::const_iterator this_it = libraries_.begin();
80  this_it != libraries_.end(); ++this_it) {
81  bool match = false;
82  for (isc::hooks::HookLibsCollection::const_iterator other_it =
83  other.libraries_.begin(); other_it != other.libraries_.end(); ++other_it) {
84  if (this_it->first != other_it->first) {
85  continue;
86  }
87  if (isNull(this_it->second) && isNull(other_it->second)) {
88  match = true;
89  break;
90  }
91  if (isNull(this_it->second) || isNull(other_it->second)) {
92  continue;
93  }
94  if (this_it->second->equals(*other_it->second)) {
95  match = true;
96  break;
97  }
98  }
99  // No match found for the particular hooks library so return false.
100  if (!match) {
101  return (false);
102  }
103  }
104  return (true);
105 }
106 
108 HooksConfig::toElement() const {
109  // hooks-libraries is a list of maps
110  ElementPtr result = Element::createList();
111  // Iterate through libraries
112  for (HookLibsCollection::const_iterator hl = libraries_.begin();
113  hl != libraries_.end(); ++hl) {
114  // Entries are maps
115  ElementPtr map = Element::createMap();
116  // Set the library name
117  map->set("library", Element::create(hl->first));
118  // Set parameters (not set vs set empty map)
119  if (!isNull(hl->second)) {
120  map->set("parameters", hl->second);
121  }
122  // Push to the list
123  result->add(map);
124  }
125  return (result);
126 }
127 
128 };
129 };
Wrapper class that holds hooks libraries configuration.
Definition: hooks_config.h:36
Exception thrown when a library failed to validate.
Definition: hooks_config.h:19
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a NULL pointer.
Definition: data.cc:1123
boost::shared_ptr< Element > ElementPtr
Definition: data.h:26
std::vector< std::string > extractNames(const isc::hooks::HookLibsCollection &libraries)
Extracts names from HookLibsCollection.
Definition: libinfo.cc:19
Defines the logger used by the top-level component of kea-lfc.
Represents the position of the data element within a configuration string.
Definition: data.h:94