kea_controller.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2014-2015 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 <asiolink/asio_wrapper.h>
  8. #include <asiolink/asiolink.h>
  9. #include <dhcpsrv/cfgmgr.h>
  10. #include <dhcpsrv/parsers/dhcp_config_parser.h>
  11. #include <dhcp6/json_config_parser.h>
  12. #include <dhcp6/ctrl_dhcp6_srv.h>
  13. #include <dhcp6/parser_context.h>
  14. #include <dhcp6/dhcp6_log.h>
  15. #include <exceptions/exceptions.h>
  16. #include <signal.h>
  17. #include <string>
  18. using namespace isc::asiolink;
  19. using namespace isc::dhcp;
  20. using namespace std;
  21. namespace {
  22. /// @brief Configure DHCPv6 server using the configuration file specified.
  23. ///
  24. /// This function is used to both configure the DHCP server on its startup
  25. /// and dynamically reconfigure the server when SIGHUP signal is received.
  26. ///
  27. /// It fetches DHCPv6 server's configuration from the 'Dhcp6' section of
  28. /// the JSON configuration file.
  29. ///
  30. /// @param file_name Configuration file location.
  31. void configure(const std::string& file_name) {
  32. // This is a configuration backend implementation that reads the
  33. // configuration from a JSON file.
  34. // We are starting the configuration process so we should remove any
  35. // staging configuration that has been created during previous
  36. // configuration attempts.
  37. CfgMgr::instance().rollback();
  38. isc::data::ConstElementPtr json;
  39. isc::data::ConstElementPtr dhcp6;
  40. isc::data::ConstElementPtr logger;
  41. isc::data::ConstElementPtr result;
  42. // Basic sanity check: file name must not be empty.
  43. try {
  44. if (file_name.empty()) {
  45. // Basic sanity check: file name must not be empty.
  46. isc_throw(isc::BadValue, "JSON configuration file not specified. Please "
  47. "use -c command line option.");
  48. }
  49. // Read contents of the file and parse it as JSON
  50. Parser6Context parser;
  51. json = parser.parseFile(file_name, Parser6Context::PARSER_DHCP6);
  52. if (!json) {
  53. isc_throw(isc::BadValue, "no configuration found");
  54. }
  55. // Let's do sanity check before we call json->get() which
  56. // works only for map.
  57. if (json->getType() != isc::data::Element::map) {
  58. isc_throw(isc::BadValue, "Configuration file is expected to be "
  59. "a map, i.e., start with { and end with } and contain "
  60. "at least an entry called 'Dhcp6' that itself is a map. "
  61. << file_name
  62. << " is a valid JSON, but its top element is not a map."
  63. " Did you forget to add { } around your configuration?");
  64. }
  65. // Let's configure logging before applying the configuration,
  66. // so we can log things during configuration process.
  67. // If there's no logging element, we'll just pass NULL pointer,
  68. // which will be handled by configureLogger().
  69. Daemon::configureLogger(json->get("Logging"),
  70. CfgMgr::instance().getStagingCfg());
  71. // Get Dhcp6 component from the config
  72. dhcp6 = json->get("Dhcp6");
  73. if (!dhcp6) {
  74. isc_throw(isc::BadValue, "no mandatory 'Dhcp6' entry in"
  75. " the configuration");
  76. }
  77. // Use parsed JSON structures to configure the server
  78. result = ControlledDhcpv6Srv::processCommand("config-reload", dhcp6);
  79. if (!result) {
  80. // Undetermined status of the configuration. This should never
  81. // happen, but as the configureDhcp6Server returns a pointer, it is
  82. // theoretically possible that it will return NULL.
  83. isc_throw(isc::BadValue, "undefined result of "
  84. "processCommand(\"config-reload\", dhcp6)");
  85. }
  86. // Now check is the returned result is successful (rcode=0) or not
  87. // (see @ref isc::config::parseAnswer).
  88. int rcode;
  89. isc::data::ConstElementPtr comment =
  90. isc::config::parseAnswer(rcode, result);
  91. if (rcode != 0) {
  92. string reason = comment ? comment->stringValue() :
  93. "no details available";
  94. isc_throw(isc::BadValue, reason);
  95. }
  96. // If configuration was parsed successfully, apply the new logger
  97. // configuration to log4cplus. It is done before commit in case
  98. // something goes wrong.
  99. CfgMgr::instance().getStagingCfg()->applyLoggingCfg();
  100. // Use new configuration.
  101. CfgMgr::instance().commit();
  102. } catch (const std::exception& ex) {
  103. // If configuration failed at any stage, we drop the staging
  104. // configuration and continue to use the previous one.
  105. CfgMgr::instance().rollback();
  106. LOG_ERROR(dhcp6_logger, DHCP6_CONFIG_LOAD_FAIL)
  107. .arg(file_name).arg(ex.what());
  108. isc_throw(isc::BadValue, "configuration error using file '"
  109. << file_name << "': " << ex.what());
  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. isc::data::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. // Configure the server using JSON file.
  147. configure(file_name);
  148. // We don't need to call openActiveSockets() or startD2() as these
  149. // methods are called in processConfig() which is called by
  150. // processCommand("reload-config", ...)
  151. // Set signal handlers. When the SIGHUP is received by the process
  152. // the server reconfiguration will be triggered. When SIGTERM or
  153. // SIGINT will be received, the server will start shutting down.
  154. signal_set_.reset(new isc::util::SignalSet(SIGINT, SIGHUP, SIGTERM));
  155. // Set the pointer to the handler function.
  156. signal_handler_ = signalHandler;
  157. }
  158. void ControlledDhcpv6Srv::cleanup() {
  159. // Nothing to do here. No need to disconnect from anything.
  160. }
  161. };
  162. };