kea_controller.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // If there's no logging element, we'll just pass NULL pointer,
  58. // which will be handled by configureLogger().
  59. Daemon::configureLogger(json->get("Logging"),
  60. CfgMgr::instance().getStagingCfg());
  61. // Get Dhcp4 component from the config
  62. dhcp4 = json->get("Dhcp4");
  63. if (!dhcp4) {
  64. isc_throw(isc::BadValue, "no mandatory 'Dhcp4' entry in"
  65. " the configuration");
  66. }
  67. // Use parsed JSON structures to configure the server
  68. result = ControlledDhcpv4Srv::processCommand("config-reload", dhcp4);
  69. if (!result) {
  70. // Undetermined status of the configuration. This should never
  71. // happen, but as the configureDhcp4Server returns a pointer, it is
  72. // theoretically possible that it will return NULL.
  73. isc_throw(isc::BadValue, "undefined result of "
  74. "processCommand(\"config-reload\", dhcp4)");
  75. }
  76. // Now check is the returned result is successful (rcode=0) or not
  77. // (see @ref isc::config::parseAnswer).
  78. int rcode;
  79. isc::data::ConstElementPtr comment =
  80. isc::config::parseAnswer(rcode, result);
  81. if (rcode != 0) {
  82. string reason = comment ? comment->stringValue() :
  83. "no details available";
  84. isc_throw(isc::BadValue, reason);
  85. }
  86. // If configuration was parsed successfully, apply the new logger
  87. // configuration to log4cplus. It is done before commit in case
  88. // something goes wrong.
  89. CfgMgr::instance().getStagingCfg()->applyLoggingCfg();
  90. // Use new configuration.
  91. CfgMgr::instance().commit();
  92. } catch (const std::exception& ex) {
  93. // If configuration failed at any stage, we drop the staging
  94. // configuration and continue to use the previous one.
  95. CfgMgr::instance().rollback();
  96. LOG_ERROR(dhcp4_logger, DHCP4_CONFIG_LOAD_FAIL)
  97. .arg(file_name).arg(ex.what());
  98. isc_throw(isc::BadValue, "configuration error using file '"
  99. << file_name << "': " << ex.what());
  100. }
  101. }
  102. /// @brief Signals handler for DHCPv4 server.
  103. ///
  104. /// This signal handler handles the following signals received by the DHCPv4
  105. /// server process:
  106. /// - SIGHUP - triggers server's dynamic reconfiguration.
  107. /// - SIGTERM - triggers server's shut down.
  108. /// - SIGINT - triggers server's shut down.
  109. ///
  110. /// @param signo Signal number received.
  111. void signalHandler(int signo) {
  112. // SIGHUP signals a request to reconfigure the server.
  113. if (signo == SIGHUP) {
  114. // Get configuration file name.
  115. std::string file = ControlledDhcpv4Srv::getInstance()->getConfigFile();
  116. try {
  117. LOG_INFO(dhcp4_logger, DHCP4_DYNAMIC_RECONFIGURATION).arg(file);
  118. configure(file);
  119. } catch (const std::exception& ex) {
  120. // Log the unsuccessful reconfiguration. The reason for failure
  121. // should be already logged. Don't rethrow an exception so as
  122. // the server keeps working.
  123. LOG_ERROR(dhcp4_logger, DHCP4_DYNAMIC_RECONFIGURATION_FAIL)
  124. .arg(file);
  125. }
  126. } else if ((signo == SIGTERM) || (signo == SIGINT)) {
  127. isc::data::ElementPtr params(new isc::data::MapElement());
  128. ControlledDhcpv4Srv::processCommand("shutdown", params);
  129. }
  130. }
  131. }
  132. namespace isc {
  133. namespace dhcp {
  134. void
  135. ControlledDhcpv4Srv::init(const std::string& file_name) {
  136. // Call parent class's init to initialize file name.
  137. Daemon::init(file_name);
  138. // Configure the server using JSON file.
  139. configure(file_name);
  140. // We don't need to call openActiveSockets() or startD2() as these
  141. // methods are called in processConfig() which is called by
  142. // processCommand("reload-config", ...)
  143. // Set signal handlers. When the SIGHUP is received by the process
  144. // the server reconfiguration will be triggered. When SIGTERM or
  145. // SIGINT will be received, the server will start shutting down.
  146. signal_set_.reset(new isc::util::SignalSet(SIGINT, SIGHUP, SIGTERM));
  147. // Set the pointer to the handler function.
  148. signal_handler_ = signalHandler;
  149. }
  150. void ControlledDhcpv4Srv::cleanup() {
  151. // Nothing to do here. No need to disconnect from anything.
  152. }
  153. };
  154. };