daemon.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright (C) 2014-2015 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. #ifndef DAEMON_H
  7. #define DAEMON_H
  8. #include <cc/data.h>
  9. #include <dhcpsrv/srv_config.h>
  10. #include <util/pid_file.h>
  11. #include <util/signal_set.h>
  12. #include <boost/noncopyable.hpp>
  13. #include <string>
  14. namespace isc {
  15. namespace dhcp {
  16. /// @brief Exception thrown when a the PID file points to a live PID
  17. class DaemonPIDExists : public Exception {
  18. public:
  19. DaemonPIDExists(const char* file, size_t line, const char* what) :
  20. isc::Exception(file, line, what) { };
  21. };
  22. /// @brief Base class for all services
  23. ///
  24. /// This is the base class that all daemons (DHCPv4, DHCPv6, D2 and possibly
  25. /// others) are derived from. It provides a standard interface for starting up,
  26. /// reconfiguring, shutting down and several other operations. It also covers
  27. /// some common operations.
  28. ///
  29. /// This class is not expected to be instantiated directly, but rather daemon
  30. /// implementations should derive from it.
  31. ///
  32. /// Methods are not pure virtual, as we need to instantiate basic daemons (e.g.
  33. /// Dhcpv6Srv) in tests, without going through the hassles of implemeting stub
  34. /// methods.
  35. ///
  36. /// Classes derived from @c Daemon may install custom signal handlers using
  37. /// @c isc::util::SignalSet class. This base class provides a declaration
  38. /// of the @c SignalSet object that should be initialized in the derived
  39. /// classes to install the custom exception handlers.
  40. ///
  41. /// @note Only one instance of this class is instantiated as it encompasses
  42. /// the whole operation of the server. Nothing, however, enforces the
  43. /// singleton status of the object.
  44. class Daemon : public boost::noncopyable {
  45. public:
  46. /// @brief Default constructor
  47. ///
  48. /// Initializes the object installing custom signal handlers for the
  49. /// process to NULL.
  50. Daemon();
  51. /// @brief Desctructor
  52. ///
  53. /// Having virtual destructor ensures that all derived classes will have
  54. /// virtual destructor as well.
  55. virtual ~Daemon();
  56. /// @brief Performs final deconfiguration.
  57. ///
  58. /// Performs configuration backend specific final clean-up. This is called
  59. /// shortly before the daemon terminates. Depending on backend, it may
  60. /// terminat existing msgq session, close LDAP connection or similar.
  61. ///
  62. /// The daemon is not expected to receive any further commands or
  63. /// configuration updates as it is in final stages of shutdown.
  64. virtual void cleanup();
  65. /// @brief Initiates shutdown procedure for the whole DHCPv6 server.
  66. virtual void shutdown();
  67. /// @brief Initializes logger
  68. ///
  69. /// This method initializes logging system. It also sets the default
  70. /// output to stdout. This is used in early stages of the startup
  71. /// phase before config file and parsed and proper logging details
  72. /// are known.
  73. ///
  74. /// @param log_name name used in logger initialization
  75. /// @param verbose verbose mode (true usually enables DEBUG messages)
  76. static void loggerInit(const char* log_name, bool verbose);
  77. /// @brief Configures logger
  78. ///
  79. /// Applies configuration stored in "Logging" structure in the
  80. /// configuration file. This structure has a "loggers" array that
  81. /// contains 0 or more entries, each configuring one logging source
  82. /// (name, severity, debuglevel), each with zero or more outputs (file,
  83. /// maxsize, maximum number of files).
  84. ///
  85. /// @param log_config JSON structures that describe logging
  86. /// @param storage configuration will be stored here
  87. static void configureLogger(const isc::data::ConstElementPtr& log_config,
  88. const isc::dhcp::SrvConfigPtr& storage);
  89. /// @brief Sets or clears verbose mode
  90. ///
  91. /// Verbose mode (-v in command-line) triggers loggers to log everythin
  92. /// (sets severity to DEBUG and debuglevel to 99). Values specified in the
  93. /// config file are ignored.
  94. ///
  95. /// @param verbose specifies if verbose should be set or not
  96. void setVerbose(const bool verbose);
  97. /// @brief Returns if running in verbose mode
  98. ///
  99. /// @return verbose mode
  100. bool getVerbose() const;
  101. /// @brief returns Kea version on stdout and exits.
  102. ///
  103. /// With extended == false, this method returns a simple string
  104. /// containing version number. With extended == true, it returns
  105. /// also additional information about sources. It is expected to
  106. /// return extra information about dependencies and used DB backends.
  107. ///
  108. /// As there is no static virtual methods in C++ this class method
  109. /// has to be redefined in derived classes and called with the
  110. /// derived class name or a child name.
  111. ///
  112. /// @param extended print additional information?
  113. /// @return text string
  114. static std::string getVersion(bool extended);
  115. /// @brief Returns config file name.
  116. /// @return text string
  117. std::string getConfigFile() const;
  118. /// @brief Sets the configuration file name
  119. ///
  120. /// @param config_file pathname of the configuration file
  121. void setConfigFile(const std::string& config_file);
  122. /// @brief returns the process name
  123. /// This value is used as when forming the default PID file name
  124. /// @return text string
  125. std::string getProcName() const;
  126. /// @brief Sets the process name
  127. /// @param proc_name name the process by which the process is recognized
  128. void setProcName(const std::string& proc_name);
  129. /// @brief Returns the directory used when forming default PID file name
  130. /// @return text string
  131. std::string getPIDFileDir() const;
  132. /// @brief Sets the PID file directory
  133. /// @param pid_file_dir path into which the PID file should be written
  134. /// Note the value should not include a trailing slash, '/'
  135. void setPIDFileDir(const std::string& pid_file_dir);
  136. /// @brief Returns the current PID file name
  137. /// @return text string
  138. std::string getPIDFileName() const;
  139. /// @brief Sets PID file name
  140. ///
  141. /// If this method is called prior to calling createPIDFile,
  142. /// the value passed in will be treated as the full file name
  143. /// for the PID file. This provides a means to override the
  144. /// default file name with an explicit value.
  145. ///
  146. /// @param pid_file_name file name to be used as the PID file
  147. void setPIDFileName(const std::string& pid_file_name);
  148. /// @brief Creates the PID file
  149. ///
  150. /// If the PID file name has not been previously set, the method
  151. /// uses manufacturePIDFileName() to set it. If the PID file
  152. /// name refers to an existing file whose contents are a PID whose
  153. /// process is still alive, the method will throw a DaemonPIDExists
  154. /// exception. Otherwise, the file created (or truncated) and
  155. /// the given pid (if not zero) is written to the file.
  156. ///
  157. /// @param pid PID to write to the file if not zero, otherwise the
  158. /// PID of the current process is used.
  159. void createPIDFile(int pid = 0);
  160. protected:
  161. /// @brief Invokes handler for the next received signal.
  162. ///
  163. /// This function provides a default implementation for the function
  164. /// handling next signal received by the process. It checks if a pointer
  165. /// to @c isc::util::SignalSet object and the signal handler function
  166. /// have been set. If they have been set, the signal handler is invoked for
  167. /// the the next signal registered in the @c SignalSet object.
  168. ///
  169. /// This function should be received in the main loop of the process.
  170. virtual void handleSignal();
  171. /// @brief A pointer to the object installing custom signal handlers.
  172. ///
  173. /// This pointer needs to be initialized to point to the @c SignalSet
  174. /// object in the derived classes which need to handle signals received
  175. /// by the process.
  176. isc::util::SignalSetPtr signal_set_;
  177. /// @brief Pointer to the common signal handler invoked by the handleSignal
  178. /// function.
  179. ///
  180. /// This pointer needs to be initialized to point to the signal handler
  181. /// function for signals being handled by the process. If signal handler
  182. /// it not initialized, the signals will not be handled.
  183. isc::util::SignalHandler signal_handler_;
  184. /// @brief Manufacture the pid file name
  185. std::string makePIDFileName() const;
  186. private:
  187. /// @brief Config file name or empty if config file not used.
  188. std::string config_file_;
  189. /// @brief Name of this process, used when creating its pid file
  190. std::string proc_name_;
  191. /// @brief Pointer to the directory where PID file(s) are written
  192. /// It defaults to --localstatedir
  193. std::string pid_file_dir_;
  194. /// @brief Pointer to the PID file for this process
  195. isc::util::PIDFilePtr pid_file_;
  196. /// @brief Flag indicating if this instance created the file
  197. bool am_file_author_;
  198. };
  199. }; // end of isc::dhcp namespace
  200. }; // end of isc namespace
  201. #endif