kea_controller.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <dhcp4/json_config_parser.h>
  16. #include <dhcp4/ctrl_dhcp4_srv.h>
  17. #include <dhcp4/dhcp4_log.h>
  18. #include <dhcpsrv/cfgmgr.h>
  19. #include <exceptions/exceptions.h>
  20. #include <string>
  21. using namespace isc::asiolink;
  22. using namespace isc::dhcp;
  23. using namespace std;
  24. namespace {
  25. /// @brief Configure DHCPv4 server using the configuration file specified.
  26. ///
  27. /// This function is used to both configure the DHCP server on its startup
  28. /// and dynamically reconfigure the server when SIGHUP signal is received.
  29. ///
  30. /// It fetches DHCPv6 server's configuration from the 'Dhcp4' section of
  31. /// the JSON configuration file.
  32. ///
  33. /// @param file_name Configuration file location.
  34. void configure(const std::string& file_name) {
  35. // This is a configuration backend implementation that reads the
  36. // configuration from a JSON file.
  37. isc::data::ConstElementPtr json;
  38. isc::data::ConstElementPtr dhcp4;
  39. isc::data::ConstElementPtr logger;
  40. isc::data::ConstElementPtr result;
  41. // Basic sanity check: file name must not be empty.
  42. try {
  43. if (file_name.empty()) {
  44. // Basic sanity check: file name must not be empty.
  45. isc_throw(isc::BadValue, "JSON configuration file not specified."
  46. " Please use -c command line option.");
  47. }
  48. // Read contents of the file and parse it as JSON
  49. json = isc::data::Element::fromJSONFile(file_name, true);
  50. if (!json) {
  51. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL)
  52. .arg("Config file " + file_name + " missing or empty.");
  53. isc_throw(isc::BadValue, "Unable to process JSON configuration"
  54. " file: " << file_name);
  55. }
  56. // Let's configure logging before applying the configuration,
  57. // so we can log things during configuration process.
  58. // If there's no logging element, we'll just pass NULL pointer,
  59. // which will be handled by configureLogger().
  60. Daemon::configureLogger(json->get("Logging"),
  61. CfgMgr::instance().getConfiguration(),
  62. ControlledDhcpv4Srv::getInstance()->getVerbose());
  63. // Get Dhcp4 component from the config
  64. dhcp4 = json->get("Dhcp4");
  65. if (!dhcp4) {
  66. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL)
  67. .arg("Config file " + file_name + " does not include 'Dhcp4'"
  68. " entry.");
  69. isc_throw(isc::BadValue, "Unable to process JSON configuration"
  70. " file: " << file_name);
  71. }
  72. // Use parsed JSON structures to configure the server
  73. result = ControlledDhcpv4Srv::processCommand("config-reload", dhcp4);
  74. } catch (const std::exception& ex) {
  75. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL).arg(ex.what());
  76. isc_throw(isc::BadValue, "Unable to process JSON configuration file: "
  77. << file_name);
  78. }
  79. if (!result) {
  80. // Undetermined status of the configuration. This should never happen,
  81. // but as the configureDhcp4Server returns a pointer, it is
  82. // theoretically possible that it will return NULL.
  83. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL)
  84. .arg("Configuration failed: Undefined result of processCommand("
  85. "config-reload, " + file_name + ")");
  86. isc_throw(isc::BadValue, "Configuration failed: Undefined result of "
  87. "processCommand('config-reload', " << file_name << ")");
  88. }
  89. // Now check is the returned result is successful (rcode=0) or not
  90. isc::data::ConstElementPtr comment; /// see @ref isc::config::parseAnswer
  91. int rcode;
  92. comment = isc::config::parseAnswer(rcode, result);
  93. if (rcode != 0) {
  94. string reason = "";
  95. if (comment) {
  96. reason = comment->stringValue();
  97. }
  98. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL).arg(reason);
  99. isc_throw(isc::BadValue, "Failed to apply configuration: " << reason);
  100. }
  101. }
  102. /// @brief Signals handler for DHCPv4 server.
  103. ///
  104. /// This signal handler handles the following signals received by the DHCPv4
  105. /// server process:
  106. /// - SIGHUP - triggers server's dynamic reconfiguration.
  107. /// - SIGTERM - triggers server's shut down.
  108. /// - SIGINT - triggers server's shut down.
  109. ///
  110. /// @param signo Signal number received.
  111. void signalHandler(int signo) {
  112. // SIGHUP signals a request to reconfigure the server.
  113. if (signo == SIGHUP) {
  114. // Get configuration file name.
  115. std::string file = ControlledDhcpv4Srv::getInstance()->getConfigFile();
  116. try {
  117. LOG_INFO(dhcp4_logger, DHCP4_DYNAMIC_RECONFIGURATION).arg(file);
  118. configure(file);
  119. } catch (const std::exception& ex) {
  120. // Log the unsuccessful reconfiguration. The reason for failure
  121. // should be already logged. Don't rethrow an exception so as
  122. // the server keeps working.
  123. LOG_ERROR(dhcp4_logger, DHCP4_DYNAMIC_RECONFIGURATION_FAIL)
  124. .arg(file);
  125. }
  126. } else if ((signo == SIGTERM) || (signo == SIGINT)) {
  127. isc::data::ElementPtr params(new isc::data::MapElement());
  128. ControlledDhcpv4Srv::processCommand("shutdown", params);
  129. }
  130. }
  131. }
  132. namespace isc {
  133. namespace dhcp {
  134. void
  135. ControlledDhcpv4Srv::init(const std::string& file_name) {
  136. // Call parent class's init to initialize file name.
  137. Daemon::init(file_name);
  138. // Configure the server using JSON file.
  139. configure(file_name);
  140. // We don't need to call openActiveSockets() or startD2() as these
  141. // methods are called in processConfig() which is called by
  142. // processCommand("reload-config", ...)
  143. // Set signal handlers. When the SIGHUP is received by the process
  144. // the server reconfiguration will be triggered. When SIGTERM or
  145. // SIGINT will be received, the server will start shutting down.
  146. signal_set_.reset(new isc::util::SignalSet(SIGINT, SIGHUP, SIGTERM));
  147. // Set the pointer to the handler function.
  148. signal_handler_ = signalHandler;
  149. }
  150. void ControlledDhcpv4Srv::cleanup() {
  151. // Nothing to do here. No need to disconnect from anything.
  152. }
  153. /// This is a logger initialization for JSON file backend.
  154. /// For now, it's just setting log messages to be printed on stdout.
  155. /// @todo: Implement this properly (see #3427)
  156. void Daemon::loggerInit(const char* logger_name, bool verbose) {
  157. setenv("KEA_LOCKFILE_DIR_FROM_BUILD", "/tmp", 1);
  158. setenv("KEA_LOGGER_ROOT", logger_name, 0);
  159. setenv("KEA_LOGGER_SEVERITY", (verbose ? "DEBUG":"INFO"), 0);
  160. setenv("KEA_LOGGER_DBGLEVEL", "99", 0);
  161. setenv("KEA_LOGGER_DESTINATION", "stdout", 0);
  162. isc::log::initLogger();
  163. }
  164. };
  165. };