kea_controller.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <dhcp4/json_config_parser.h>
  8. #include <dhcp4/ctrl_dhcp4_srv.h>
  9. #include <dhcp4/dhcp4_log.h>
  10. #include <dhcp4/parser_context.h>
  11. #include <dhcpsrv/cfgmgr.h>
  12. #include <exceptions/exceptions.h>
  13. #include <string>
  14. using namespace isc::asiolink;
  15. using namespace isc::dhcp;
  16. using namespace std;
  17. namespace {
  18. /// @brief Configure DHCPv4 server using the configuration file specified.
  19. ///
  20. /// This function is used to both configure the DHCP server on its startup
  21. /// and dynamically reconfigure the server when SIGHUP signal is received.
  22. ///
  23. /// It fetches DHCPv6 server's configuration from the 'Dhcp4' section of
  24. /// the JSON configuration file.
  25. ///
  26. /// @param file_name Configuration file location.
  27. void configure(const std::string& file_name) {
  28. // This is a configuration backend implementation that reads the
  29. // configuration from a JSON file.
  30. isc::data::ConstElementPtr json;
  31. isc::data::ConstElementPtr dhcp4;
  32. isc::data::ConstElementPtr logger;
  33. isc::data::ConstElementPtr result;
  34. // Basic sanity check: file name must not be empty.
  35. try {
  36. if (file_name.empty()) {
  37. // Basic sanity check: file name must not be empty.
  38. isc_throw(isc::BadValue, "JSON configuration file not specified."
  39. " Please use -c command line option.");
  40. }
  41. // Read contents of the file and parse it as JSON
  42. Parser4Context parser;
  43. json = parser.parseFile(file_name, Parser4Context::PARSER_DHCP4);
  44. if (!json) {
  45. isc_throw(isc::BadValue, "no configuration found");
  46. }
  47. // Let's do sanity check before we call json->get() which
  48. // works only for map.
  49. if (json->getType() != isc::data::Element::map) {
  50. isc_throw(isc::BadValue, "Configuration file is expected to be "
  51. "a map, i.e., start with { and end with } and contain "
  52. "at least an entry called 'Dhcp4' that itself is a map. "
  53. << file_name
  54. << " is a valid JSON, but its top element is not a map."
  55. " Did you forget to add { } around your configuration?");
  56. }
  57. // Use parsed JSON structures to configure the server
  58. result = ControlledDhcpv4Srv::processCommand("set-config", json);
  59. if (!result) {
  60. // Undetermined status of the configuration. This should never
  61. // happen, but as the configureDhcp4Server returns a pointer, it is
  62. // theoretically possible that it will return NULL.
  63. isc_throw(isc::BadValue, "undefined result of "
  64. "processCommand(\"set-config\", json)");
  65. }
  66. // Now check is the returned result is successful (rcode=0) or not
  67. // (see @ref isc::config::parseAnswer).
  68. int rcode;
  69. isc::data::ConstElementPtr comment =
  70. isc::config::parseAnswer(rcode, result);
  71. if (rcode != 0) {
  72. string reason = comment ? comment->stringValue() :
  73. "no details available";
  74. isc_throw(isc::BadValue, reason);
  75. }
  76. } catch (const std::exception& ex) {
  77. // If configuration failed at any stage, we drop the staging
  78. // configuration and continue to use the previous one.
  79. CfgMgr::instance().rollback();
  80. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL)
  81. .arg(file_name).arg(ex.what());
  82. isc_throw(isc::BadValue, "configuration error using file '"
  83. << file_name << "': " << ex.what());
  84. }
  85. }
  86. /// @brief Signals handler for DHCPv4 server.
  87. ///
  88. /// This signal handler handles the following signals received by the DHCPv4
  89. /// server process:
  90. /// - SIGHUP - triggers server's dynamic reconfiguration.
  91. /// - SIGTERM - triggers server's shut down.
  92. /// - SIGINT - triggers server's shut down.
  93. ///
  94. /// @param signo Signal number received.
  95. void signalHandler(int signo) {
  96. // SIGHUP signals a request to reconfigure the server.
  97. if (signo == SIGHUP) {
  98. // Get configuration file name.
  99. std::string file = ControlledDhcpv4Srv::getInstance()->getConfigFile();
  100. try {
  101. LOG_INFO(dhcp4_logger, DHCP4_DYNAMIC_RECONFIGURATION).arg(file);
  102. configure(file);
  103. } catch (const std::exception& ex) {
  104. // Log the unsuccessful reconfiguration. The reason for failure
  105. // should be already logged. Don't rethrow an exception so as
  106. // the server keeps working.
  107. LOG_ERROR(dhcp4_logger, DHCP4_DYNAMIC_RECONFIGURATION_FAIL)
  108. .arg(file);
  109. }
  110. } else if ((signo == SIGTERM) || (signo == SIGINT)) {
  111. isc::data::ElementPtr params(new isc::data::MapElement());
  112. ControlledDhcpv4Srv::processCommand("shutdown", params);
  113. }
  114. }
  115. }
  116. namespace isc {
  117. namespace dhcp {
  118. void
  119. ControlledDhcpv4Srv::init(const std::string& file_name) {
  120. // Configure the server using JSON file.
  121. configure(file_name);
  122. // We don't need to call openActiveSockets() or startD2() as these
  123. // methods are called in processConfig() which is called by
  124. // processCommand("set-config", ...)
  125. // Set signal handlers. When the SIGHUP is received by the process
  126. // the server reconfiguration will be triggered. When SIGTERM or
  127. // SIGINT will be received, the server will start shutting down.
  128. signal_set_.reset(new isc::util::SignalSet(SIGINT, SIGHUP, SIGTERM));
  129. // Set the pointer to the handler function.
  130. signal_handler_ = signalHandler;
  131. }
  132. void ControlledDhcpv4Srv::cleanup() {
  133. // Nothing to do here. No need to disconnect from anything.
  134. }
  135. };
  136. };