daemon.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #ifndef DAEMON_H
  15. #define DAEMON_H
  16. #include <config.h>
  17. #include <cc/data.h>
  18. #include <dhcpsrv/srv_config.h>
  19. #include <util/signal_set.h>
  20. #include <boost/noncopyable.hpp>
  21. #include <string>
  22. namespace isc {
  23. namespace dhcp {
  24. /// @brief Base class for all services
  25. ///
  26. /// This is the base class that all daemons (DHCPv4, DHCPv6, D2 and possibly
  27. /// others) are derived from. It provides a standard interface for starting up,
  28. /// reconfiguring, shutting down and several other operations. It also covers
  29. /// some common operations.
  30. ///
  31. /// This class is not expected to be instantiated directly, but rather daemon
  32. /// implementations should derive from it.
  33. ///
  34. /// Methods are not pure virtual, as we need to instantiate basic daemons (e.g.
  35. /// Dhcpv6Srv) in tests, without going through the hassles of implemeting stub
  36. /// methods.
  37. ///
  38. /// This class comprises a static object holding a location of the configuration
  39. /// file. The object must be static because it is instantiated by the signal
  40. /// handler functions, which are static by their nature. The signal handlers
  41. /// are used to reconfigure a running server and they need access to the
  42. /// configuration file location. They get this access by calling
  43. /// @c Daemon::getConfigFile function.
  44. ///
  45. /// By default, the configuration file location is empty and its actual value
  46. /// is assigned to the static object in @c Daemon::init function.
  47. ///
  48. /// Classes derived from @c Daemon may install custom signal handlers using
  49. /// @c isc::util::SignalSet class. This base class provides a declaration
  50. /// of the @c SignalSet object that should be initialized in the derived
  51. /// classes to install the custom exception handlers.
  52. ///
  53. /// @note Only one instance of this class is instantiated as it encompasses
  54. /// the whole operation of the server. Nothing, however, enforces the
  55. /// singleton status of the object.
  56. class Daemon : public boost::noncopyable {
  57. public:
  58. /// @brief Default constructor
  59. ///
  60. /// Initializes the object installing custom signal handlers for the
  61. /// process to NULL.
  62. Daemon();
  63. /// @brief Desctructor
  64. ///
  65. /// Having virtual destructor ensures that all derived classes will have
  66. /// virtual destructor as well.
  67. virtual ~Daemon();
  68. /// @brief Initializes the server.
  69. ///
  70. /// Depending on the configuration backend, it establishes msgq session,
  71. /// or reads the configuration file.
  72. ///
  73. /// Note: This function may throw to report enountered problems. It may
  74. /// also return false if the initialization was skipped. That may seem
  75. /// redundant, but the idea here is that in some cases the configuration
  76. /// was read, understood and the decision was made to not start. One
  77. /// case where such capability could be needed is when we have a single
  78. /// config file for Kea4 and D2, but the DNS Update is disabled. It is
  79. /// likely that the D2 will be started, it will analyze its config file,
  80. /// decide that it is not needed and will shut down.
  81. ///
  82. /// @note this method may throw
  83. ///
  84. /// @param config_file Config file name (may be empty if unused).
  85. virtual void init(const std::string& config_file);
  86. /// @brief Performs final deconfiguration.
  87. ///
  88. /// Performs configuration backend specific final clean-up. This is called
  89. /// shortly before the daemon terminates. Depending on backend, it may
  90. /// terminat existing msgq session, close LDAP connection or similar.
  91. ///
  92. /// The daemon is not expected to receive any further commands or
  93. /// configuration updates as it is in final stages of shutdown.
  94. virtual void cleanup();
  95. /// @brief Initiates shutdown procedure for the whole DHCPv6 server.
  96. virtual void shutdown();
  97. /// @brief Returns config file name.
  98. static std::string getConfigFile() {
  99. return (config_file_);
  100. }
  101. /// @brief Initializes logger
  102. ///
  103. /// This method initializes logging system. It also sets the default
  104. /// output to stdout. This is used in early stages of the startup
  105. /// phase before config file and parsed and proper logging details
  106. /// are known.
  107. ///
  108. /// @param log_name name used in logger initialization
  109. /// @param verbose verbose mode (true usually enables DEBUG messages)
  110. static void loggerInit(const char* log_name, bool verbose);
  111. /// @brief Configures logger
  112. ///
  113. /// Applies configuration stored in "Logging" structure in the
  114. /// configuration file. This structure has a "loggers" array that
  115. /// contains 0 or more entries, each configuring one logging source
  116. /// (name, severity, debuglevel), each with zero or more outputs (file,
  117. /// maxsize, maximum number of files).
  118. ///
  119. /// @param log_config JSON structures that describe logging
  120. /// @param storage configuration will be stored here
  121. static void configureLogger(const isc::data::ConstElementPtr& log_config,
  122. const isc::dhcp::SrvConfigPtr& storage);
  123. /// @brief Sets or clears verbose mode
  124. ///
  125. /// Verbose mode (-v in command-line) triggers loggers to log everythin
  126. /// (sets severity to DEBUG and debuglevel to 99). Values specified in the
  127. /// config file are ignored.
  128. ///
  129. /// @param verbose specifies if verbose should be set or not
  130. void setVerbose(const bool verbose);
  131. /// @brief Returns if running in verbose mode
  132. ///
  133. /// @return verbose mode
  134. bool getVerbose() const;
  135. /// @brief returns Kea version on stdout and exits.
  136. ///
  137. /// With extended == false, this method returns a simple string
  138. /// containing version number. With extended == true, it returns
  139. /// also additional information about sources. It is expected to
  140. /// return extra information about dependencies and used DB backends.
  141. ///
  142. /// @param extended print additional information?
  143. /// @return text string
  144. static std::string getVersion(bool extended);
  145. protected:
  146. /// @brief Invokes handler for the next received signal.
  147. ///
  148. /// This function provides a default implementation for the function
  149. /// handling next signal received by the process. It checks if a pointer
  150. /// to @c isc::util::SignalSet object and the signal handler function
  151. /// have been set. If they have been set, the signal handler is invoked for
  152. /// the the next signal registered in the @c SignalSet object.
  153. ///
  154. /// This function should be received in the main loop of the process.
  155. virtual void handleSignal();
  156. /// @brief A pointer to the object installing custom signal handlers.
  157. ///
  158. /// This pointer needs to be initialized to point to the @c SignalSet
  159. /// object in the derived classes which need to handle signals received
  160. /// by the process.
  161. isc::util::SignalSetPtr signal_set_;
  162. /// @brief Pointer to the common signal handler invoked by the handleSignal
  163. /// function.
  164. ///
  165. /// This pointer needs to be initialized to point to the signal handler
  166. /// function for signals being handled by the process. If signal handler
  167. /// it not initialized, the signals will not be handled.
  168. isc::util::SignalHandler signal_handler_;
  169. private:
  170. /// @brief Config file name or empty if config file not used.
  171. static std::string config_file_;
  172. };
  173. }; // end of isc::dhcp namespace
  174. }; // end of isc namespace
  175. #endif