kea_controller.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <dhcpsrv/cfgmgr.h>
  19. #include <exceptions/exceptions.h>
  20. #include <string>
  21. using namespace isc::asiolink;
  22. using namespace isc::dhcp;
  23. using namespace std;
  24. namespace {
  25. /// @brief Configure DHCPv4 server using the configuration file specified.
  26. ///
  27. /// This function is used to both configure the DHCP server on its startup
  28. /// and dynamically reconfigure the server when SIGHUP signal is received.
  29. ///
  30. /// It fetches DHCPv6 server's configuration from the 'Dhcp4' section of
  31. /// the JSON configuration file.
  32. ///
  33. /// @param file_name Configuration file location.
  34. void configure(const std::string& file_name) {
  35. // This is a configuration backend implementation that reads the
  36. // configuration from a JSON file.
  37. // We are starting the configuration process so we should remove any
  38. // staging configuration that has been created during previous
  39. // configuration attempts.
  40. CfgMgr::instance().rollback();
  41. isc::data::ConstElementPtr json;
  42. isc::data::ConstElementPtr dhcp4;
  43. isc::data::ConstElementPtr logger;
  44. isc::data::ConstElementPtr result;
  45. // Basic sanity check: file name must not be empty.
  46. try {
  47. if (file_name.empty()) {
  48. // Basic sanity check: file name must not be empty.
  49. isc_throw(isc::BadValue, "JSON configuration file not specified."
  50. " Please use -c command line option.");
  51. }
  52. // Read contents of the file and parse it as JSON
  53. json = isc::data::Element::fromJSONFile(file_name, true);
  54. if (!json) {
  55. isc_throw(isc::BadValue, "no configuration found");
  56. }
  57. // Let's do sanity check before we call json->get() which
  58. // works only for map.
  59. if (json->getType() != isc::data::Element::map) {
  60. isc_throw(isc::BadValue, "Configuration file is expected to be "
  61. "a map, i.e., start with { and end with } and contain "
  62. "at least an entry called 'Dhcp4' that itself is a map. "
  63. << file_name
  64. << " is a valid JSON, but its top element is not a map."
  65. " Did you forget to add { } around your configuration?");
  66. }
  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 Dhcp4 component from the config
  72. dhcp4 = json->get("Dhcp4");
  73. if (!dhcp4) {
  74. isc_throw(isc::BadValue, "no mandatory 'Dhcp4' entry in"
  75. " the configuration");
  76. }
  77. // Use parsed JSON structures to configure the server
  78. result = ControlledDhcpv4Srv::processCommand("config-reload", dhcp4);
  79. if (!result) {
  80. // Undetermined status of the configuration. This should never
  81. // happen, but as the configureDhcp4Server 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\", dhcp4)");
  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. /// @todo: This commit should be moved to
  102. /// CtrlDhcp4Srv::commandConfigReloadHandler.
  103. CfgMgr::instance().commit();
  104. } catch (const std::exception& ex) {
  105. // If configuration failed at any stage, we drop the staging
  106. // configuration and continue to use the previous one.
  107. CfgMgr::instance().rollback();
  108. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL)
  109. .arg(file_name).arg(ex.what());
  110. isc_throw(isc::BadValue, "configuration error using file '"
  111. << file_name << "': " << ex.what());
  112. }
  113. }
  114. /// @brief Signals handler for DHCPv4 server.
  115. ///
  116. /// This signal handler handles the following signals received by the DHCPv4
  117. /// server process:
  118. /// - SIGHUP - triggers server's dynamic reconfiguration.
  119. /// - SIGTERM - triggers server's shut down.
  120. /// - SIGINT - triggers server's shut down.
  121. ///
  122. /// @param signo Signal number received.
  123. void signalHandler(int signo) {
  124. // SIGHUP signals a request to reconfigure the server.
  125. if (signo == SIGHUP) {
  126. // Get configuration file name.
  127. std::string file = ControlledDhcpv4Srv::getInstance()->getConfigFile();
  128. try {
  129. LOG_INFO(dhcp4_logger, DHCP4_DYNAMIC_RECONFIGURATION).arg(file);
  130. configure(file);
  131. } catch (const std::exception& ex) {
  132. // Log the unsuccessful reconfiguration. The reason for failure
  133. // should be already logged. Don't rethrow an exception so as
  134. // the server keeps working.
  135. LOG_ERROR(dhcp4_logger, DHCP4_DYNAMIC_RECONFIGURATION_FAIL)
  136. .arg(file);
  137. }
  138. } else if ((signo == SIGTERM) || (signo == SIGINT)) {
  139. isc::data::ElementPtr params(new isc::data::MapElement());
  140. ControlledDhcpv4Srv::processCommand("shutdown", params);
  141. }
  142. }
  143. }
  144. namespace isc {
  145. namespace dhcp {
  146. void
  147. ControlledDhcpv4Srv::init(const std::string& 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_set_.reset(new isc::util::SignalSet(SIGINT, SIGHUP, SIGTERM));
  157. // Set the pointer to the handler function.
  158. signal_handler_ = signalHandler;
  159. }
  160. void ControlledDhcpv4Srv::cleanup() {
  161. // Nothing to do here. No need to disconnect from anything.
  162. }
  163. };
  164. };