kea_controller.cc 6.8 KB

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