Kea: load_unload.cc Source File

| Kea 3.2.0-git

forensic_log/load_unload.cc
Go to the documentation of this file.
1// Copyright (C) 2016-2026 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>
12#include <asiolink/io_service_mgr.h>
13#include <cc/data.h>
14#include <hooks/hooks.h>
15#include <database/database_connection.h>
16#include <dhcpsrv/cfgmgr.h>
17#include <process/daemon.h>
18#include <legal_log_log.h>
19#include <legal_syslog.h>
20#include <dhcpsrv/legal_log_mgr.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
49 int load(LibraryHandle& handle) {
50 try {
51 // Make the hook library not loadable by d2.
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
66 LegalLogMgrFactory::registerBackendFactory("logfile", RotatingFile::factory);
67 LegalLogMgrFactory::registerBackendFactory("syslog", LegalSyslog::factory);
68
69 // Get and decode parameters.
70 ConstElementPtr const& parameters(handle.getParameters());
71 DatabaseConnection::ParameterMap map;
72
73 try {
74 LegalLogMgr::parseConfig(parameters, map);
75 LegalLogMgrFactory::addBackend(map, handle.getLibraryIndex());
76 } catch (const isc::db::DbOpenErrorWithRetry& err) {
77 string redacted;
78 try {
79 redacted = DatabaseConnection::redactedAccessString(map);
80 } catch (...) {
81 }
82 LOG_INFO(legal_log_logger, LEGAL_LOG_DB_OPEN_CONNECTION_WITH_RETRY_FAILED)
83 .arg(redacted).arg(err.what());
84 }
85
86 } catch (const std::exception& ex) {
87 // Log the error and return failure.
88 LOG_ERROR(legal_log_logger, LEGAL_LOG_LOAD_ERROR)
89 .arg(ex.what());
90 return (1);
91 }
92
93 return (0);
94}
95
102 int 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.
107 LegalLogMgrFactory::delAllBackends();
108
109 LegalLogMgrFactory::unregisterBackendFactory("logfile");
110 LegalLogMgrFactory::unregisterBackendFactory("syslog");
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.
114 LOG_ERROR(legal_log_logger, LEGAL_LOG_UNLOAD_ERROR)
115 .arg(ex.what());
116 }
117
118 return (0);
119}
120
124 int multi_threading_compatible() {
125 return (1);
126}
127
128}
cfgmgr.h
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition exceptions/exceptions.h:153
isc::db::DatabaseConnection::redactedAccessString
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
Definition database_connection.cc:108
isc::db::DatabaseConnection::ParameterMap
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Definition database_connection.h:137
isc::db::DbOpenErrorWithRetry
Exception thrown on failure to open database but permit retries.
Definition database_connection.h:38
isc::dhcp::CfgMgr::getFamily
uint16_t getFamily() const
Returns address family.
Definition cfgmgr.h:246
isc::dhcp::CfgMgr::instance
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
isc::dhcp::LegalLogMgrFactory::unregisterBackendFactory
static bool unregisterBackendFactory(const std::string &db_type, bool no_log=false)
Unregisters the backend factory function for a given backend type.
Definition legal_log_mgr_factory.cc:53
isc::dhcp::LegalLogMgrFactory::addBackend
static void addBackend(db::DatabaseConnection::ParameterMap &parameters, ManagerID id=0)
Create an instance of a forensic log backend.
Definition legal_log_mgr_factory.cc:72
isc::dhcp::LegalLogMgrFactory::delAllBackends
static void delAllBackends()
Removes all backends from the pool.
Definition legal_log_mgr_factory.cc:125
isc::dhcp::LegalLogMgrFactory::registerBackendFactory
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.
Definition legal_log_mgr_factory.cc:23
isc::dhcp::LegalLogMgr::parseConfig
static void parseConfig(const isc::data::ConstElementPtr &parameters, isc::db::DatabaseConnection::ParameterMap &map)
Parse database specification.
Definition legal_log_mgr.cc:45
isc::hooks::LibraryHandle
Library handle.
Definition library_handle.h:60
isc::hooks::LibraryHandle::getLibraryIndex
int getLibraryIndex() const
Get library index.
Definition library_handle.h:214
isc::hooks::LibraryHandle::getParameters
isc::data::ConstElementPtr getParameters()
Get configuration parameter common code.
Definition library_handle.cc:74
isc::legal_log::LegalSyslog::factory
static isc::dhcp::LegalLogMgrPtr factory(const isc::db::DatabaseConnection::ParameterMap &parameters)
Factory class method.
Definition legal_syslog.cc:76
isc::legal_log::RotatingFile::factory
static isc::dhcp::LegalLogMgrPtr factory(const isc::db::DatabaseConnection::ParameterMap &parameters)
Factory class method.
Definition rotating_file.cc:427
isc::process::Daemon::getProcName
static std::string getProcName()
returns the process name This value is used as when forming the default PID file name
Definition daemon.cc:154
daemon.h
data.h
database_connection.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
multi_threading_compatible
int multi_threading_compatible()
This function is called to retrieve the multi-threading compatibility.
Definition forensic_log/load_unload.cc:124
unload
int unload()
Called by the Hooks library manager when the library is unloaded.
Definition forensic_log/load_unload.cc:102
load
int load(LibraryHandle &handle)
Called by the Hooks library manager when the library is loaded.
Definition forensic_log/load_unload.cc:49
hooks.h
io_service.h
io_service_mgr.h
legal_log_log.h
LEGAL_LOG_UNLOAD_ERROR
const isc::log::MessageID LEGAL_LOG_UNLOAD_ERROR
LEGAL_LOG_LOAD_ERROR
const isc::log::MessageID LEGAL_LOG_LOAD_ERROR
LEGAL_LOG_DB_OPEN_CONNECTION_WITH_RETRY_FAILED
const isc::log::MessageID LEGAL_LOG_DB_OPEN_CONNECTION_WITH_RETRY_FAILED
legal_log_mgr.h
Defines the abstract class for backend stores.
legal_syslog.h
LOG_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
LOG_INFO
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
isc::asiolink
Definition addr_utilities.cc:170
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::db
Definition audit_entry.cc:14
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::hooks
Definition callout_handle.cc:21
isc::legal_log
Definition legal_log_log.cc:13
isc::legal_log::legal_log_logger
isc::log::Logger legal_log_logger("legal-log-hooks")
Legal Log Logger.
Definition legal_log_log.h:22
isc::process
Definition cb_ctl_base.h:22
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
rotating_file.h
Defines the class, RotatingFile, which implements an appending text file that rotates to a new file o...