main.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (C) 2011-2016 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/ctrl_dhcp4_srv.h>
  8. #include <dhcp4/dhcp4_log.h>
  9. #include <dhcpsrv/cfgmgr.h>
  10. #include <log/logger_support.h>
  11. #include <log/logger_manager.h>
  12. #include <cfgrpt/config_report.h>
  13. #include <boost/lexical_cast.hpp>
  14. #include <iostream>
  15. using namespace isc::dhcp;
  16. using namespace std;
  17. /// This file contains entry point (main() function) for standard DHCPv4 server
  18. /// component of Kea software suite. It parses command-line arguments and
  19. /// instantiates ControlledDhcpv4Srv class that is responsible for establishing
  20. /// connection with msgq (receiving commands and configuration) and also
  21. /// creating Dhcpv4 server object as well.
  22. namespace {
  23. const char* const DHCP4_NAME = "kea-dhcp4";
  24. /// @brief Prints Kea Usage and exits
  25. ///
  26. /// Note: This function never returns. It terminates the process.
  27. void
  28. usage() {
  29. cerr << "Kea DHCPv4 server, version " << VERSION << endl;
  30. cerr << endl;
  31. cerr << "Usage: " << DHCP4_NAME
  32. << " -[v|V|W] [-d] [-c cfgfile] [-p number]" << endl;
  33. cerr << " -v: print version number and exit" << endl;
  34. cerr << " -V: print extended version and exit" << endl;
  35. cerr << " -W: display the configuration report and exit" << endl;
  36. cerr << " -d: debug mode with extra verbosity (former -v)" << endl;
  37. cerr << " -c file: specify configuration file" << endl;
  38. cerr << " -p number: specify non-standard port number 1-65535 "
  39. << "(useful for testing only)" << endl;
  40. exit(EXIT_FAILURE);
  41. }
  42. } // end of anonymous namespace
  43. int
  44. main(int argc, char* argv[]) {
  45. int ch;
  46. int port_number = DHCP4_SERVER_PORT; // The default. any other values are
  47. // useful for testing only.
  48. bool verbose_mode = false; // Should server be verbose?
  49. // The standard config file
  50. std::string config_file("");
  51. while ((ch = getopt(argc, argv, "dvVWc:p:")) != -1) {
  52. switch (ch) {
  53. case 'd':
  54. verbose_mode = true;
  55. break;
  56. case 'v':
  57. cout << Dhcpv4Srv::getVersion(false) << endl;
  58. return (EXIT_SUCCESS);
  59. case 'V':
  60. cout << Dhcpv4Srv::getVersion(true) << endl;
  61. return (EXIT_SUCCESS);
  62. case 'W':
  63. cout << isc::detail::getConfigReport() << endl;
  64. return (EXIT_SUCCESS);
  65. case 'c': // config file
  66. config_file = optarg;
  67. break;
  68. case 'p':
  69. try {
  70. port_number = boost::lexical_cast<int>(optarg);
  71. } catch (const boost::bad_lexical_cast &) {
  72. cerr << "Failed to parse port number: [" << optarg
  73. << "], 1-65535 allowed." << endl;
  74. usage();
  75. }
  76. if (port_number <= 0 || port_number > 65535) {
  77. cerr << "Failed to parse port number: [" << optarg
  78. << "], 1-65535 allowed." << endl;
  79. usage();
  80. }
  81. break;
  82. default:
  83. usage();
  84. }
  85. }
  86. // Check for extraneous parameters.
  87. if (argc > optind) {
  88. usage();
  89. }
  90. // Configuration file is required.
  91. if (config_file.empty()) {
  92. cerr << "Configuration file not specified." << endl;
  93. usage();
  94. }
  95. int ret = EXIT_SUCCESS;
  96. try {
  97. // It is important that we set a default logger name because this name
  98. // will be used when the user doesn't provide the logging configuration
  99. // in the Kea configuration file.
  100. CfgMgr::instance().setDefaultLoggerName(DHCP4_ROOT_LOGGER_NAME);
  101. // Initialize logging. If verbose, we'll use maximum verbosity.
  102. Daemon::loggerInit(DHCP4_ROOT_LOGGER_NAME, verbose_mode);
  103. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_START_INFO)
  104. .arg(getpid()).arg(port_number).arg(verbose_mode ? "yes" : "no");
  105. LOG_INFO(dhcp4_logger, DHCP4_STARTING).arg(VERSION);
  106. // Create the server instance.
  107. ControlledDhcpv4Srv server(port_number);
  108. // Remember verbose-mode
  109. server.setVerbose(verbose_mode);
  110. // Create our PID file.
  111. server.setProcName(DHCP4_NAME);
  112. server.setConfigFile(config_file);
  113. server.createPIDFile();
  114. try {
  115. // Initialize the server.
  116. server.init(config_file);
  117. } catch (const std::exception& ex) {
  118. try {
  119. // Let's log out what went wrong.
  120. isc::log::LoggerManager log_manager;
  121. log_manager.process();
  122. LOG_ERROR(dhcp4_logger, DHCP4_INIT_FAIL).arg(ex.what());
  123. } catch (...) {
  124. // The exception thrown during the initialization could
  125. // originate from logger subsystem. Therefore LOG_ERROR()
  126. // may fail as well.
  127. cerr << "Failed to initialize server: " << ex.what() << endl;
  128. }
  129. return (EXIT_FAILURE);
  130. }
  131. // Tell the admin we are ready to process packets
  132. LOG_INFO(dhcp4_logger, DHCP4_STARTED).arg(VERSION);
  133. // And run the main loop of the server.
  134. server.run();
  135. LOG_INFO(dhcp4_logger, DHCP4_SHUTDOWN);
  136. } catch (const isc::dhcp::DaemonPIDExists& ex) {
  137. // First, we print the error on stderr (that should always work)
  138. cerr << DHCP4_NAME << " already running? " << ex.what()
  139. << endl;
  140. // Let's also try to log it using logging system, but we're not
  141. // sure if it's usable (the exception may have been thrown from
  142. // the logger subsystem)
  143. try {
  144. LOG_FATAL(dhcp4_logger, DHCP4_ALREADY_RUNNING)
  145. .arg(DHCP4_NAME).arg(ex.what());
  146. } catch (...) {
  147. // Already logged so ignore
  148. }
  149. ret = EXIT_FAILURE;
  150. } catch (const std::exception& ex) {
  151. // First, we print the error on stderr (that should always work)
  152. cerr << DHCP4_NAME << ": Fatal error during start up: " << ex.what()
  153. << endl;
  154. // Let's also try to log it using logging system, but we're not
  155. // sure if it's usable (the exception may have been thrown from
  156. // the logger subsystem)
  157. try {
  158. LOG_FATAL(dhcp4_logger, DHCP4_SERVER_FAILED).arg(ex.what());
  159. } catch (...) {
  160. // Already logged so ignore
  161. }
  162. ret = EXIT_FAILURE;
  163. }
  164. return (ret);
  165. }