Kea 3.1.1
forensic_log/load_unload.cc
Go to the documentation of this file.
1// Copyright (C) 2016-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
8
9#include <config.h>
10
11#include <asiolink/io_service.h>
13#include <cc/data.h>
14#include <hooks/hooks.h>
16#include <dhcpsrv/cfgmgr.h>
17#include <process/daemon.h>
18#include <legal_log_log.h>
19#include <legal_syslog.h>
21#include <rotating_file.h>
22
23#include <boost/lexical_cast.hpp>
24
25#include <sstream>
26#include <string>
27
28using namespace isc;
29using namespace isc::asiolink;
30using namespace isc::data;
31using namespace isc::db;
32using namespace isc::dhcp;
33using namespace isc::hooks;
34using namespace isc::process;
35using namespace isc::legal_log;
36using namespace std;
37
38// Functions accessed by the hooks framework use C linkage to avoid the name
39// mangling that accompanies use of the C++ compiler as well as to avoid
40// issues related to namespaces.
41extern "C" {
42
49int load(LibraryHandle& handle) {
50 try {
51 // Make the hook library not loadable by d2 or ca.
52 uint16_t family = CfgMgr::instance().getFamily();
53 const string& proc_name = Daemon::getProcName();
54 if (family == AF_INET) {
55 if (proc_name != "kea-dhcp4") {
56 isc_throw(isc::Unexpected, "Bad process name: " << proc_name
57 << ", expected kea-dhcp4");
58 }
59 } else {
60 if (proc_name != "kea-dhcp6") {
61 isc_throw(isc::Unexpected, "Bad process name: " << proc_name
62 << ", expected kea-dhcp6");
63 }
64 }
65
68
69 // Get and decode parameters.
70 ConstElementPtr const& parameters(handle.getParameters());
72
73 try {
74 LegalLogMgr::parseConfig(parameters, map);
76 } catch (const isc::db::DbOpenErrorWithRetry& err) {
77 string redacted;
78 try {
80 } catch (...) {
81 }
83 .arg(redacted).arg(err.what());
84 }
85
86 } catch (const std::exception& ex) {
87 // Log the error and return failure.
89 .arg(ex.what());
90 return (1);
91 }
92
93 return (0);
94}
95
102int unload() {
103 try {
104 // Since it's "global" Let's explicitly destroy it now rather
105 // than indeterminately. Note, LegalLogMgr destructor will close
106 // the store.
108
111 } catch (const std::exception& ex) {
112 // On the off chance something goes awry, catch it and log it.
113 // @todo Not sure if we should return a non-zero result or not.
115 .arg(ex.what());
116 }
117
118 return (0);
119}
120
125 return (1);
126}
127
128}
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when an unexpected error condition occurs.
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Exception thrown on failure to open database but permit retries.
uint16_t getFamily() const
Returns address family.
Definition cfgmgr.h:246
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
static bool unregisterBackendFactory(const std::string &db_type, bool no_log=false)
Unregisters the backend factory function for a given backend type.
static void addBackend(db::DatabaseConnection::ParameterMap &parameters, ManagerID id=0)
Create an instance of a forensic log backend.
static void delAllBackends()
Removes all backends from the pool.
static bool registerBackendFactory(const std::string &db_type, const Factory &factory, bool no_log=false, DBVersion db_version=DBVersion())
Registers new backend factory function for a given backend type.
static void parseConfig(const isc::data::ConstElementPtr &parameters, isc::db::DatabaseConnection::ParameterMap &map)
Parse database specification.
int getLibraryIndex() const
Get library index.
isc::data::ConstElementPtr getParameters()
Get configuration parameter common code.
static isc::dhcp::LegalLogMgrPtr factory(const isc::db::DatabaseConnection::ParameterMap &parameters)
Factory class method.
static isc::dhcp::LegalLogMgrPtr factory(const isc::db::DatabaseConnection::ParameterMap &parameters)
Factory class method.
static std::string getProcName()
returns the process name This value is used as when forming the default PID file name
Definition daemon.cc:151
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
int multi_threading_compatible()
This function is called to retrieve the multi-threading compatibility.
int unload()
Called by the Hooks library manager when the library is unloaded.
int load(LibraryHandle &handle)
Called by the Hooks library manager when the library is loaded.
const isc::log::MessageID LEGAL_LOG_UNLOAD_ERROR
const isc::log::MessageID LEGAL_LOG_LOAD_ERROR
const isc::log::MessageID LEGAL_LOG_DB_OPEN_CONNECTION_WITH_RETRY_FAILED
Defines the abstract class for backend stores.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::log::Logger legal_log_logger("legal-log-hooks")
Legal Log Logger.
Defines the logger used by the top-level component of kea-lfc.
Defines the class, RotatingFile, which implements an appending text file that rotates to a new file o...