17 #include <kea_version.h> 18 #include <boost/bind.hpp> 31 DControllerBase::DControllerBase(
const char* app_name,
const char* bin_name)
32 : app_name_(app_name), bin_name_(bin_name),
33 verbose_(false), check_only_(false),
44 "Multiple controller instances attempted.");
47 controller_ = controller;
94 }
catch (
const std::exception& ex) {
96 .arg(app_name_).arg(ex.what());
104 .arg(bin_name_).arg(ex.
what());
106 }
catch (
const std::exception& ex) {
108 .arg(app_name_).arg(ex.what());
114 .arg(app_name_).arg(getpid()).arg(VERSION);
118 }
catch (
const std::exception& ex) {
120 .arg(app_name_).arg(ex.what());
122 "Application Process initialization failed: " << ex.
what());
133 .arg(app_name_).arg(comment->stringValue());
135 << comment->stringValue());
144 }
catch (
const std::exception& ex) {
146 .arg(app_name_).arg(ex.what());
148 "Application process event loop failed: " << ex.
what());
153 .arg(app_name_).arg(getpid()).arg(VERSION);
162 setenv(
"KEA_LOCKFILE_DIR",
"none", 0);
169 if (config_file.empty()) {
179 std::cerr <<
"Syntax check OK" << std::endl;
184 module_config = whole_config->get(
getAppName());
185 if (!module_config) {
187 " does not include '" <<
getAppName() <<
"' entry");
198 << answer->stringValue());
204 }
catch (
const std::exception& ex) {
225 while ((ch = getopt(argc, argv, opts.c_str())) != -1) {
253 if (optarg == NULL) {
267 << static_cast<char>(optopt) <<
"] " 268 << (!optarg ?
"" : optarg));
278 << static_cast<char>(ch) <<
"] " 279 << (!optarg ?
"" : optarg));
306 }
catch (
const std::exception& ex) {
334 if (config_file.empty()) {
337 "use -c command line option.");
345 whole_config = Element::fromJSONFile(config_file,
true);
352 ConfigPtr storage = process_->getCfgMgr()->getContext();
359 module_config = whole_config->get(
getAppName());
360 if (!module_config) {
362 " does not include '" <<
372 storage->applyLoggingCfg();
375 }
catch (
const std::exception& ex) {
382 std::string(
"Configuration parsing failed: ") + ex.what());
406 return (process_->configure(new_config,
false));
412 return (process_->configure(new_config,
true));
418 ConstElementPtr config = process_->getCfgMgr()->getContext()->toElement();
426 std::string filename;
429 if (args->getType() != Element::map) {
430 return (
createAnswer(COMMAND_ERROR,
"Argument must be a map"));
433 if (filename_param) {
434 if (filename_param->getType() != Element::string) {
436 "passed parameter 'filename' " 439 filename = filename_param->stringValue();
443 if (filename.empty()) {
447 if (filename.empty()) {
449 "Unable to determine filename." 450 "Please specify filename explicitly."));
457 ElementPtr cfg = process_->getCfgMgr()->getContext()->toElement();
469 if (!cfg->contains(
"Logging")) {
470 ConfigPtr base_cfg = process_->getCfgMgr()->getContext();
475 loginfo = loginfo->get(
"Logging");
479 cfg->set(
"Logging", loginfo);
487 std::string(
"Error during write-config:")
492 "Error writing configuration to " + filename));
497 params->set(
"size", Element::create(static_cast<long long>(size)));
498 params->set(
"filename", Element::create(filename));
501 + filename +
" successful", params));
506 const int status_code = COMMAND_ERROR;
516 message =
"Missing mandatory 'arguments' parameter.";
518 module_config = args->get(app_name);
519 if (!module_config) {
520 message =
"Missing mandatory '" + app_name +
"' parameter.";
521 }
else if (module_config->getType() != Element::map) {
522 message =
"'" + app_name +
"' parameter expected to be a map.";
526 if (!message.empty()) {
550 arguments->set(
"extended", extended);
572 return (process_->shutdown(args));
578 return (
createAnswer(COMMAND_SUCCESS,
"Process has not been initialized"));
612 IOSignalPtr io_signal = io_signal_queue_->popSignal(sequence_id);
629 .arg(comment->stringValue());
639 DCTL_SHUTDOWN_SIGNAL_RECVD).arg(signum);
655 std::cerr <<
"Usage error: " << text << std::endl;
658 std::cerr <<
"Usage: " << bin_name_ << std::endl
659 <<
" -v: print version number and exit" << std::endl
660 <<
" -V: print extended version information and exit" 662 <<
" -W: display the configuration report and exit" 664 <<
" -d: optional, verbose output " << std::endl
665 <<
" -c <config file name> : mandatory," 666 <<
" specify name of configuration file" << std::endl
667 <<
" -t <config file name> : check the" 668 <<
" configuration file and exit" << std::endl;
682 std::stringstream tmp;
686 tmp << std::endl << EXTENDED_VERSION << std::endl;
687 tmp <<
"linked with:" << std::endl;
bool osSignalHandler(int signum)
Handler for processing OS-level signals.
virtual isc::data::ConstElementPtr parseFile(const std::string &file_name)
Parse a given file into Elements.
boost::shared_ptr< DControllerBase > DControllerBasePtr
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
std::string getVersion(bool extended)
returns Kea version on stdout and exit.
isc::data::ConstElementPtr configTestHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-test command
virtual isc::data::ConstElementPtr configFromFile()
Reconfigures the process from a configuration file.
void usage(const std::string &text)
Prints the program usage text to std error.
static void setOnReceiptHandler(BoolSignalHandler handler)
Registers a handler as the onreceipt signal handler.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
Creates and manages IOSignals.
Exception thrown when the application process fails.
virtual void processSignal(int signum)
Application-level signal processing method.
void parseArgs(int argc, char *argv[])
Processes the command line arguments.
void checkConfigFile() const
Checks the configuration file name.
virtual ~DControllerBase()
Destructor.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
isc::log::Logger dctl_logger("dctl")
Defines the logger used within libkea-process library.
boost::shared_ptr< Element > ElementPtr
isc::util::SignalSetPtr signal_set_
A pointer to the object installing custom signal handlers.
void runProcess()
Invokes the application process's event loop,(DBaseProcess::run).
virtual bool customOption(int option, char *optarg)
Virtual method that provides derivations the opportunity to support additional command line options.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
isc::data::ConstElementPtr shutdownHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for 'shutdown' command
std::string getConfigFile() const
Returns config file name.
virtual void launch(int argc, char *argv[], const bool test_mode)
Acts as the primary entry point into the controller execution and provides the outermost application ...
Exception used to convey version info upwards.
virtual std::string getVersionAddendum()
Fetches text containing additional version specifics.
static void setDefaultLoggerName(const std::string &logger)
Sets the default logger name.
isc::data::ConstElementPtr shutdownProcess(isc::data::ConstElementPtr args)
Initiates shutdown procedure.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Exception thrown when the controller encounters an operational error.
void setProcName(const std::string &proc_name)
Sets the process name.
virtual isc::data::ConstElementPtr checkConfig(isc::data::ConstElementPtr new_config)
Instance method invoked by the configuration event handler and which processes the actual configurati...
void setConfigFile(const std::string &config_file)
Sets the configuration file name.
boost::shared_ptr< const Element > ConstElementPtr
isc::data::ConstElementPtr configWriteHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-write command
void checkConfigOnly()
Check the configuration.
static void setVerbose(const bool verbose)
Sets or clears verbose mode.
virtual isc::data::ConstElementPtr updateConfig(isc::data::ConstElementPtr new_config)
Instance method invoked by the configuration event handler and which processes the actual configurati...
Represents a collection of signals handled in a customized way.
Exception thrown when the command line is invalid.
This is a base class for exceptions thrown from the DNS library module.
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
static void setController(const DControllerBasePtr &controller)
Static setter which sets the singleton instance.
Defines the logger used by the top-level component of kea-dhcp-ddns.
const char *const config_report[]
isc::data::ConstElementPtr configGetHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for config-get command
static void loggerInit(const char *log_name, bool verbose)
Initializes logger.
Logging initialization functions.
void ioSignalHandler(IOSignalId sequence_id)
Handler for processing IOSignals.
virtual size_t writeConfigFile(const std::string &config_file, isc::data::ConstElementPtr cfg=isc::data::ConstElementPtr()) const
Writes current configuration to specified file.
const char *const * d2_config_report
void initProcess()
Instantiates the application process and then initializes it.
This file contains several functions and constants that are used for handling commands and responses ...
Exception thrown when the controller launch fails.
A wrapper interface for the ASIO library.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
boost::shared_ptr< IOSignal > IOSignalPtr
Defines a pointer to an IOSignal.
virtual const std::string getUsageText() const
Virtual method which can be used to contribute derivation specific usage text.
static std::string getVersion()
Version.
std::string getConfigReport()
#define LOG_FATAL(LOGGER, MESSAGE)
Macro to conveniently test fatal output and log it.
isc::data::ConstElementPtr buildReportHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for 'build-report' command
Exception thrown when a the PID file points to a live PID.
virtual const std::string getCustomOpts() const
Virtual method which returns a string containing the option letters for any custom command line optio...
Exception thrown when the application process encounters an operation in its event loop (i....
void initSignalHandling()
Initializes signal handling.
void createPIDFile(int pid=0)
Creates the PID file.
const int DBGLVL_START_SHUT
This is given a value of 0 as that is the level selected if debugging is enabled without giving a lev...
bool isCheckOnly() const
Supplies whether or not check only mode is enabled.
virtual DProcessBase * createProcess()=0
Abstract method that is responsible for instantiating the application process object.
std::string getAppName() const
Fetches the name of the application under control.
isc::data::ConstElementPtr versionGetHandler(const std::string &command, isc::data::ConstElementPtr args)
handler for version-get command
uint64_t IOSignalId
Defines a unique identifier type for IOSignal.
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the SrvConfig.
static void configureLogger(const isc::data::ConstElementPtr &log_config, const isc::process::ConfigPtr &storage)
Configures logger.