kea_controller.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /// This signal handler handles the following signals received by the DHCPv6
  115. /// server process:
  116. /// - SIGHUP - triggers server's dynamic reconfiguration.
  117. /// - SIGTERM - triggers server's shut down.
  118. /// - SIGINT - triggers server's shut down.
  119. ///
  120. /// @param signo Signal number received.
  121. void signalHandler(int signo) {
  122. // SIGHUP signals a request to reconfigure the server.
  123. if (signo == SIGHUP) {
  124. // Get configuration file name.
  125. std::string file = ControlledDhcpv6Srv::getInstance()->getConfigFile();
  126. try {
  127. LOG_INFO(dhcp6_logger, DHCP6_DYNAMIC_RECONFIGURATION).arg(file);
  128. configure(file);
  129. } catch (const std::exception& ex) {
  130. // Log the unsuccessful reconfiguration. The reason for failure
  131. // should be already logged. Don't rethrow an exception so as
  132. // the server keeps working.
  133. LOG_ERROR(dhcp6_logger, DHCP6_DYNAMIC_RECONFIGURATION_FAIL)
  134. .arg(file);
  135. }
  136. } else if ((signo == SIGTERM) || (signo == SIGINT)) {
  137. ElementPtr params(new isc::data::MapElement());
  138. ControlledDhcpv6Srv::processCommand("shutdown", params);
  139. }
  140. }
  141. }
  142. namespace isc {
  143. namespace dhcp {
  144. void
  145. ControlledDhcpv6Srv::init(const std::string& file_name) {
  146. // Call parent class's init to initialize file name.
  147. Daemon::init(file_name);
  148. // Configure the server using JSON file.
  149. configure(file_name);
  150. // We don't need to call openActiveSockets() or startD2() as these
  151. // methods are called in processConfig() which is called by
  152. // processCommand("reload-config", ...)
  153. // Set signal handlers. When the SIGHUP is received by the process
  154. // the server reconfiguration will be triggered. When SIGTERM or
  155. // SIGINT will be received, the server will start shutting down.
  156. signal(SIGHUP, signalHandler);
  157. signal(SIGTERM, signalHandler);
  158. signal(SIGINT, signalHandler);
  159. }
  160. void ControlledDhcpv6Srv::cleanup() {
  161. // Nothing to do here. No need to disconnect from anything.
  162. }
  163. /// This is a logger initialization for JSON file backend.
  164. /// For now, it's just setting log messages to be printed on stdout.
  165. /// @todo: Implement this properly (see #3427)
  166. void Daemon::loggerInit(const char*, bool verbose, bool ) {
  167. setenv("B10_LOCKFILE_DIR_FROM_BUILD", "/tmp", 1);
  168. setenv("B10_LOGGER_ROOT", "kea", 0);
  169. setenv("B10_LOGGER_SEVERITY", (verbose ? "DEBUG":"INFO"), 0);
  170. setenv("B10_LOGGER_DBGLEVEL", "99", 0);
  171. setenv("B10_LOGGER_DESTINATION", "stdout", 0);
  172. isc::log::initLogger();
  173. }
  174. };
  175. };