main.cc 5.8 KB

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