kea_controller.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <asiolink/asiolink.h>
  16. #include <dhcp/iface_mgr.h>
  17. #include <dhcpsrv/dhcp_config_parser.h>
  18. #include <dhcpsrv/cfgmgr.h>
  19. #include <dhcp6/json_config_parser.h>
  20. #include <dhcp6/ctrl_dhcp6_srv.h>
  21. #include <dhcp6/dhcp6_log.h>
  22. #include <dhcp6/spec_config.h>
  23. #include <log/logger_level.h>
  24. #include <log/logger_name.h>
  25. #include <log/logger_manager.h>
  26. #include <log/logger_specification.h>
  27. #include <log/logger_support.h>
  28. #include <log/output_option.h>
  29. #include <exceptions/exceptions.h>
  30. #include <util/buffer.h>
  31. #include <cassert>
  32. #include <iostream>
  33. #include <signal.h>
  34. #include <string>
  35. #include <vector>
  36. using namespace isc::asiolink;
  37. using namespace isc::cc;
  38. using namespace isc::config;
  39. using namespace isc::data;
  40. using namespace isc::dhcp;
  41. using namespace isc::log;
  42. using namespace isc::util;
  43. using namespace std;
  44. namespace {
  45. /// @brief Configure DHCPv6 server using the configuration file specified.
  46. ///
  47. /// This function is used to both configure the DHCP server on its startup
  48. /// and dynamically reconfigure the server when SIGHUP signal is received.
  49. ///
  50. /// It fetches DHCPv6 server's configuration from the 'Dhcp6' section of
  51. /// the JSON configuration file.
  52. ///
  53. /// @param file_name Configuration file location.
  54. void configure(const std::string& file_name) {
  55. // This is a configuration backend implementation that reads the
  56. // configuration from a JSON file.
  57. isc::data::ConstElementPtr json;
  58. isc::data::ConstElementPtr dhcp6;
  59. isc::data::ConstElementPtr result;
  60. // Basic sanity check: file name must not be empty.
  61. try {
  62. if (file_name.empty()) {
  63. // Basic sanity check: file name must not be empty.
  64. isc_throw(isc::BadValue, "JSON configuration file not specified. Please "
  65. "use -c command line option.");
  66. }
  67. // Read contents of the file and parse it as JSON
  68. json = Element::fromJSONFile(file_name, true);
  69. if (!json) {
  70. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL)
  71. .arg("Config file " + file_name + " missing or empty.");
  72. isc_throw(isc::BadValue, "Unable to process JSON configuration file:"
  73. + file_name);
  74. }
  75. // Get Dhcp6 component from the config
  76. dhcp6 = json->get("Dhcp6");
  77. if (!dhcp6) {
  78. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL)
  79. .arg("Config file " + file_name + " does not include 'Dhcp6' entry.");
  80. isc_throw(isc::BadValue, "Unable to process JSON configuration file:"
  81. + file_name);
  82. }
  83. // Use parsed JSON structures to configure the server
  84. result = ControlledDhcpv6Srv::processCommand("config-reload", dhcp6);
  85. } catch (const std::exception& ex) {
  86. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL).arg(ex.what());
  87. isc_throw(isc::BadValue, "Unable to process JSON configuration file:"
  88. + file_name);
  89. }
  90. if (!result) {
  91. // Undetermined status of the configuration. This should never happen,
  92. // but as the configureDhcp6Server returns a pointer, it is theoretically
  93. // possible that it will return NULL.
  94. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL)
  95. .arg("Configuration failed: Undefined result of configureDhcp6Server"
  96. "() function after attempting to read " + file_name);
  97. return;
  98. }
  99. // Now check is the returned result is successful (rcode=0) or not
  100. ConstElementPtr comment; /// see @ref isc::config::parseAnswer
  101. int rcode;
  102. comment = parseAnswer(rcode, result);
  103. if (rcode != 0) {
  104. string reason = "";
  105. if (comment) {
  106. reason = string(" (") + comment->stringValue() + string(")");
  107. }
  108. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL).arg(reason);
  109. isc_throw(isc::BadValue, "Failed to apply configuration:" << reason);
  110. }
  111. }
  112. /// @brief Signals handler for DHCPv6 server.
  113. ///
  114. /// Currently, this function handles SIGHUP signal only. When this signal
  115. /// is received it triggeres DHCSP server reconfiguration.
  116. ///
  117. /// @param signo Signal number received.
  118. void signalHandler(int signo) {
  119. // SIGHUP signals a request to reconfigure the server.
  120. if (signo == SIGHUP) {
  121. try {
  122. LOG_INFO(dhcp6_logger, DHCP6_DYNAMIC_RECONFIGURATION);
  123. configure(ControlledDhcpv6Srv::getInstance()->getConfigFile());
  124. } catch (const std::exception& ex) {
  125. // Log the unsuccessful reconfiguration. The reason for failure
  126. // should be already logged. Don't rethrow an exception so as
  127. // the server keeps working.
  128. LOG_ERROR(dhcp6_logger, DHCP6_DYNAMIC_RECONFIGURATION_FAIL);
  129. }
  130. }
  131. }
  132. }
  133. namespace isc {
  134. namespace dhcp {
  135. void
  136. ControlledDhcpv6Srv::init(const std::string& file_name) {
  137. // Call parent class's init to initialize file name.
  138. Daemon::init(file_name);
  139. // Configure the server using JSON file.
  140. configure(file_name);
  141. // We don't need to call openActiveSockets() or startD2() as these
  142. // methods are called in processConfig() which is called by
  143. // processCommand("reload-config", ...)
  144. // Set signal handler function for SIGHUP. When the SIGHUP is received
  145. // by the process the server reconfiguration will be triggered.
  146. signal(SIGHUP, signalHandler);
  147. }
  148. void ControlledDhcpv6Srv::cleanup() {
  149. // Nothing to do here. No need to disconnect from anything.
  150. }
  151. /// This is a logger initialization for JSON file backend.
  152. /// For now, it's just setting log messages to be printed on stdout.
  153. /// @todo: Implement this properly (see #3427)
  154. void Daemon::loggerInit(const char*, bool verbose, bool ) {
  155. setenv("B10_LOCKFILE_DIR_FROM_BUILD", "/tmp", 1);
  156. setenv("B10_LOGGER_ROOT", "kea", 0);
  157. setenv("B10_LOGGER_SEVERITY", (verbose ? "DEBUG":"INFO"), 0);
  158. setenv("B10_LOGGER_DBGLEVEL", "99", 0);
  159. setenv("B10_LOGGER_DESTINATION", "stdout", 0);
  160. isc::log::initLogger();
  161. }
  162. };
  163. };