Kea 3.1.3
lfc_controller.cc
Go to the documentation of this file.
1// Copyright (C) 2015-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#include <kea_version.h>
9
10#include <lfc/lfc_controller.h>
11#include <lfc/lfc_log.h>
12#include <util/pid_file.h>
18#include <dhcpsrv/lease_mgr.h>
20#include <log/logger_manager.h>
21#include <log/logger_name.h>
23
24#include <iostream>
25#include <sstream>
26#include <unistd.h>
27#include <stdlib.h>
28#include <cerrno>
29
30using namespace std;
31using namespace isc::util;
32using namespace isc::dhcp;
33using namespace isc::log;
34
35namespace {
37const uint32_t MAX_LEASE_ERRORS = 100;
38} // namespace anonymous
39
40namespace isc {
41namespace lfc {
42
45const char* LFCController::lfc_app_name_ = "DhcpLFC";
46
48const char* LFCController::lfc_bin_name_ = "kea-lfc";
49
51 : protocol_version_(0), verbose_(false), config_file_(""), previous_file_(""),
52 copy_file_(""), output_file_(""), finish_file_(""), pid_file_("") {
53}
54
57
58void
59LFCController::launch(int argc, char* argv[], const bool test_mode) {
60 bool do_rotate = true;
61
62 // It would be nice to set up the logger as the first step
63 // in the process, but we don't know where to send logging
64 // info until after we have parsed our arguments. As we
65 // don't currently log anything when trying to parse the
66 // arguments we do the parse before the logging setup. If
67 // we do decide to log something then the code will need
68 // to move around a bit.
69
70 try {
71 parseArgs(argc, argv);
72 } catch (const InvalidUsage& ex) {
73 usage(ex.what());
74 throw; // rethrow it
75 }
76
77 // Start up the logging system.
78 startLogger(test_mode);
79
81
82 // verify we are the only instance
83 PIDFile pid_file(pid_file_);
84
85 try {
86 // Acquire a lock for check and write operations.
87 PIDLock pid_lock(pid_file.getLockname(), true);
88
89 if (!pid_lock.isLocked()) {
90 isc_throw(Unexpected, "failed to acquire the lock?");
91 }
92
93 int existing = pid_file.check();
94
95 if ((existing != 0) && (existing != getpid())) {
96 // Already running instance, bail out
98 return;
99 }
100
101 // create the pid file for this instance
102 if (existing == 0) {
103 pid_file.write();
104 }
105 } catch (const PIDFileError& pid_ex) {
107 return;
108 }
109
110 // If we don't have a finish file do the processing. We
111 // don't know the exact type of the finish file here but
112 // all we care about is if it exists so that's okay
113 CSVFile lf_finish(getFinishFile());
114 if (!lf_finish.exists()) {
116 .arg(previous_file_)
117 .arg(copy_file_);
118
119 try {
120 if (getProtocolVersion() == 4) {
121 processLeases<Lease4, CSVLeaseFile4, Lease4Storage>();
122 } else {
123 processLeases<Lease6, CSVLeaseFile6, Lease6Storage>();
124 }
125 } catch (const std::exception& proc_ex) {
126 // We don't want to do the cleanup but do want to get rid of the pid
127 do_rotate = false;
128 LOG_FATAL(lfc_logger, LFC_FAIL_PROCESS).arg(proc_ex.what());
129 }
130 }
131
132 // If do_rotate is true We either already had a finish file or
133 // were able to create one. We now want to do the file cleanup,
134 // we don't want to return after the catch as we
135 // still need to cleanup the pid file
136 if (do_rotate) {
138
139 try {
140 fileRotate();
141 } catch (const RunTimeFail& run_ex) {
143 }
144 }
145
146 // delete the pid file for this instance
147 try {
148 pid_file.deleteFile();
149 } catch (const PIDFileError& pid_ex) {
151 }
152
154}
155
156void
157LFCController::parseArgs(int argc, char* argv[]) {
158 int ch;
159
160 opterr = 0;
161 optind = 1;
162 while ((ch = getopt(argc, argv, ":46dhvVWp:x:i:o:c:f:")) != -1) {
163 switch (ch) {
164 case '4':
165 // Process DHCPv4 lease files.
166 protocol_version_ = 4;
167 break;
168
169 case '6':
170 // Process DHCPv6 lease files.
171 protocol_version_ = 6;
172 break;
173
174 case 'v':
175 // Print just Kea version and exit.
176 std::cout << getVersion(false) << std::endl;
177 exit(EXIT_SUCCESS);
178
179 case 'V':
180 // Print extended Kea version and exit.
181 std::cout << getVersion(true) << std::endl;
182 exit(EXIT_SUCCESS);
183
184 case 'W':
185 // Display the configuration report and exit.
186 std::cout << isc::detail::getConfigReport() << std::endl;
187 exit(EXIT_SUCCESS);
188
189 case 'd':
190 // Verbose output.
191 verbose_ = true;
192 break;
193
194 case 'p':
195 // PID file name.
196 if (optarg == NULL) {
197 isc_throw(InvalidUsage, "PID file name missing");
198 }
199 pid_file_ = optarg;
200 break;
201
202 case 'x':
203 // Previous (or ex) file name.
204 if (optarg == NULL) {
205 isc_throw(InvalidUsage, "Previous (ex) file name missing");
206 }
207 previous_file_ = optarg;
208 break;
209
210 case 'i':
211 // Copy file name.
212 if (optarg == NULL) {
213 isc_throw(InvalidUsage, "Copy file name missing");
214 }
215 copy_file_ = optarg;
216 break;
217
218 case 'o':
219 // Output file name.
220 if (optarg == NULL) {
221 isc_throw(InvalidUsage, "Output file name missing");
222 }
223 output_file_ = optarg;
224 break;
225
226 case 'f':
227 // Finish file name.
228 if (optarg == NULL) {
229 isc_throw(InvalidUsage, "Finish file name missing");
230 }
231 finish_file_ = optarg;
232 break;
233
234 case 'c':
235 // Configuration file name
236 if (optarg == NULL) {
237 isc_throw(InvalidUsage, "Configuration file name missing");
238 }
239 config_file_ = optarg;
240 break;
241
242 case 'h':
243 usage("");
244 exit(EXIT_SUCCESS);
245
246 case '?':
247 // Unknown argument
248 // note this will catch all the previous ... name missing
249 isc_throw(InvalidUsage, "Unknown argument");
250
251 case ':':
252 // Missing option argument
253 isc_throw(InvalidUsage, "Missing option argument");
254
255 default:
256 // I don't think we should get here as the unknown arguments
257 // and missing options cases should cover everything else
258 isc_throw(InvalidUsage, "Invalid command line");
259 }
260 }
261
262 // Check for extraneous parameters.
263 if (argc > optind) {
264 isc_throw(InvalidUsage, "Extraneous parameters.");
265 }
266
267 if (protocol_version_ == 0) {
268 isc_throw(InvalidUsage, "DHCP version required");
269 }
270
271 if (pid_file_.empty()) {
272 isc_throw(InvalidUsage, "PID file not specified");
273 }
274
275 if (previous_file_.empty()) {
276 isc_throw(InvalidUsage, "Previous file not specified");
277 }
278
279 if (copy_file_.empty()) {
280 isc_throw(InvalidUsage, "Copy file not specified");
281 }
282
283 if (output_file_.empty()) {
284 isc_throw(InvalidUsage, "Output file not specified");
285 }
286
287 if (finish_file_.empty()) {
288 isc_throw(InvalidUsage, "Finish file not specified");
289 }
290
291 if (config_file_.empty()) {
292 isc_throw(InvalidUsage, "Config file not specified");
293 }
294
295 // If verbose is set echo the input information
296 if (verbose_) {
297 std::cout << "Protocol version: DHCPv" << protocol_version_ << std::endl
298 << "Previous or ex lease file: " << previous_file_ << std::endl
299 << "Copy lease file: " << copy_file_ << std::endl
300 << "Output lease file: " << output_file_ << std::endl
301 << "Finish file: " << finish_file_ << std::endl
302 << "Config file: " << config_file_ << std::endl
303 << "PID file: " << pid_file_ << std::endl
304 << std::endl;
305 }
306}
307
308void
309LFCController::usage(const std::string& text) {
310 if (!text.empty()) {
311 std::cerr << "Usage error: " << text << std::endl;
312 }
313
314 std::cerr << "Usage: " << lfc_bin_name_ << std::endl
315 << " [-4|-6] -p file -x file -i file -o file -f file -c file" << std::endl
316 << " -4 or -6 clean a set of v4 or v6 lease files" << std::endl
317 << " -p <file>: PID file" << std::endl
318 << " -x <file>: previous or ex lease file" << std::endl
319 << " -i <file>: copy of lease file" << std::endl
320 << " -o <file>: output lease file" << std::endl
321 << " -f <file>: finish file" << std::endl
322 << " -c <file>: configuration file" << std::endl
323 << " -v: print version number and exit" << std::endl
324 << " -V: print extended version information and exit" << std::endl
325 << " -d: optional, verbose output " << std::endl
326 << " -h: print this message " << std::endl
327 << std::endl;
328}
329
330std::string
331LFCController::getVersion(const bool extended) const{
332 std::stringstream version_stream;
333
334 version_stream << VERSION;
335 if (extended) {
336 std::string db_version;
337 if (protocol_version_ == 4) {
339 } else if (protocol_version_ == 6) {
341 }
342
343 version_stream << " (" << EXTENDED_VERSION << ")";
344 if (!db_version.empty()) {
345 db_version = "backend: " + db_version;
346 version_stream << std::endl << db_version;
347 }
348 }
349
350 return (version_stream.str());
351}
352
353template<typename LeaseObjectType, typename LeaseFileType, typename StorageType>
354void
355LFCController::processLeases() const {
356 StorageType storage;
357
358 // If a previous file exists read the entries into storage
359 LeaseFileType lf_prev(getPreviousFile());
360 if (lf_prev.exists()) {
362 MAX_LEASE_ERRORS);
363 }
364
365 // Follow that with the copy of the current lease file
366 LeaseFileType lf_copy(getCopyFile());
367 if (lf_copy.exists()) {
369 MAX_LEASE_ERRORS);
370 }
371
372 // Write the result out to the output file
373 LeaseFileType lf_output(getOutputFile());
374 LeaseFileLoader::write<LeaseObjectType>(lf_output, storage);
375
376 // If desired log the stats
378 .arg(lf_prev.getReadLeases() + lf_copy.getReadLeases())
379 .arg(lf_prev.getReads() + lf_copy.getReads())
380 .arg(lf_prev.getReadErrs() + lf_copy.getReadErrs());
381
383 .arg(lf_output.getWriteLeases())
384 .arg(lf_output.getWrites())
385 .arg(lf_output.getWriteErrs());
386
387 // Once we've finished the output file move it to the complete file
388 if (rename(getOutputFile().c_str(), getFinishFile().c_str()) != 0) {
389 isc_throw(RunTimeFail, "Unable to move output (" << output_file_
390 << ") to complete (" << finish_file_
391 << ") error: " << strerror(errno));
392 }
393}
394
395void
397 // Remove the old previous file
398 if ((remove(getPreviousFile().c_str()) != 0) &&
399 (errno != ENOENT)) {
400 isc_throw(RunTimeFail, "Unable to delete previous file '"
401 << previous_file_ << "' error: " << strerror(errno));
402 }
403
404 // Remove the copy file
405 if ((remove(getCopyFile().c_str()) != 0) &&
406 (errno != ENOENT)) {
407 isc_throw(RunTimeFail, "Unable to delete copy file '"
408 << copy_file_ << "' error: " << strerror(errno));
409 }
410
411 // Rename the finish file to be the previous file
412 if (rename(finish_file_.c_str(), previous_file_.c_str()) != 0) {
413 isc_throw(RunTimeFail, "Unable to move finish (" << finish_file_
414 << ") to previous (" << previous_file_
415 << ") error: " << strerror(errno));
416 }
417}
418
419void
420LFCController::startLogger(const bool test_mode) const {
421 // If we are running in test mode use the environment variables
422 // else use our defaults
423 if (test_mode) {
424 initLogger();
425 } else {
426 OutputOption option;
427 LoggerManager manager;
428
429 initLogger(lfc_app_name_, INFO, 0, NULL, false);
430
431 // Prepare the objects to define the logging specification
435
436 // If we are running in verbose (debugging) mode
437 // we send the output to the console, otherwise
438 // by default we send it to the SYSLOG
439 if (verbose_) {
441 } else {
443 }
444
445 // ... and set the destination
446 spec.addOutputOption(option);
447
448 manager.process(spec);
449 }
450}
451
452} // namespace isc::lfc
453} // namespace isc
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 void write(LeaseFileType &lease_file, const StorageType &storage)
Write leases from the storage into a lease file.
static void load(LeaseFileType &lease_file, StorageType &storage, const uint32_t max_errors=0, const bool close_file_on_exit=true)
Load leases from the lease file into the specified storage.
static std::string getDBVersionInternal(Universe const &u)
Local version of getDBVersion() class method.
Exception thrown when the command line is invalid.
void fileRotate() const
Rotate files.
void launch(int argc, char *argv[], const bool test_mode)
Acts as the primary entry point to start execution of the process.
int getProtocolVersion() const
Gets the protocol version of the leases files.
std::string getFinishFile() const
Gets the finish file name.
std::string getCopyFile() const
Gets the copy file name.
static const char * lfc_bin_name_
Defines the executable name, by convention this should match the executable name.
void parseArgs(int argc, char *argv[])
Process the command line arguments.
static const char * lfc_app_name_
Defines the application name, it may be used to locate configuration data and appears in log statemen...
std::string getPreviousFile() const
Gets the previous file name.
std::string getOutputFile() const
Gets the output file name.
Exceptions thrown when a method is unable to manipulate (remove or rename) a file.
void process(T start, T finish)
Process Specifications.
Provides input/output access to CSV files.
Definition csv_file.h:358
bool exists() const
Checks if the CSV file exists and can be opened for reading.
Definition csv_file.cc:133
Exception thrown when an error occurs during PID file processing.
Definition pid_file.h:20
Class to help with processing PID files.
Definition pid_file.h:40
void write(int) const
Write the PID to the file.
Definition pid_file.cc:60
void deleteFile() const
Delete the PID file.
Definition pid_file.cc:81
std::string getLockname() const
Returns the path to the lock file.
Definition pid_file.h:97
int check() const
Read the PID in from the file and check it.
Definition pid_file.cc:23
RAII device to handle a lock file to avoid race conditions.
Definition pid_file.h:115
bool isLocked()
Return the lock status.
Definition pid_file.h:131
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
An abstract API for lease database.
void usage()
Print Usage.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
#define LOG_FATAL(LOGGER, MESSAGE)
Macro to conveniently test fatal output and log it.
Definition macros.h:38
std::string getConfigReport()
Definition cfgrpt.cc:20
const isc::log::MessageID LFC_FAIL_PROCESS
const isc::log::MessageID LFC_START
const isc::log::MessageID LFC_TERMINATE
const isc::log::MessageID LFC_FAIL_PID_DEL
const isc::log::MessageID LFC_FAIL_ROTATE
const isc::log::MessageID LFC_RUNNING
const isc::log::MessageID LFC_WRITE_STATS
const isc::log::MessageID LFC_FAIL_PID_CREATE
isc::log::Logger lfc_logger("DhcpLFC")
Defines the logger used within LFC.
Definition lfc_log.h:18
const isc::log::MessageID LFC_PROCESSING
const isc::log::MessageID LFC_ROTATING
const isc::log::MessageID LFC_READ_STATS
const std::string & getRootLoggerName()
Get root logger name.
void initLogger(const string &root, isc::log::Severity severity, int dbglevel, const char *file, bool buffer)
Run-time initialization.
int keaLoggerDbglevel(int defdbglevel)
Obtains logging debug level from KEA_LOGGER_DBGLEVEL.
isc::log::Severity keaLoggerSeverity(isc::log::Severity defseverity)
Obtains logging severity from KEA_LOGGER_SEVERITY.
Defines the logger used by the top-level component of kea-lfc.
Destination destination
Members.