daemon.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright (C) 2014-2015,2017 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 implementing 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 Destructor
  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 Writes current configuration to specified file
  123. ///
  124. /// This method writes the current configuration to specified file.
  125. /// @todo: this logically more belongs to CPL process file. Once
  126. /// Daemon is merged with CPL architecture, it will be a better
  127. /// fit.
  128. ///
  129. /// If cfg is not specified, the current config (as returned by
  130. /// CfgMgr::instance().getCurrentCfg() will be returned.
  131. ///
  132. /// @param config_file name of the file to write the configuration to
  133. /// @param cfg configuration to write (optional)
  134. /// @return number of files written
  135. /// @throw Unexpected if CfgMgr can't retrieve configuation or file cannot
  136. /// be written
  137. virtual size_t
  138. writeConfigFile(const std::string& config_file,
  139. isc::data::ConstElementPtr cfg = isc::data::ConstElementPtr()) const;
  140. /// @brief returns the process name
  141. /// This value is used as when forming the default PID file name
  142. /// @return text string
  143. std::string getProcName() const;
  144. /// @brief Sets the process name
  145. /// @param proc_name name the process by which the process is recognized
  146. void setProcName(const std::string& proc_name);
  147. /// @brief Returns the directory used when forming default PID file name
  148. /// @return text string
  149. std::string getPIDFileDir() const;
  150. /// @brief Sets the PID file directory
  151. /// @param pid_file_dir path into which the PID file should be written
  152. /// Note the value should not include a trailing slash, '/'
  153. void setPIDFileDir(const std::string& pid_file_dir);
  154. /// @brief Returns the current PID file name
  155. /// @return text string
  156. std::string getPIDFileName() const;
  157. /// @brief Sets PID file name
  158. ///
  159. /// If this method is called prior to calling createPIDFile,
  160. /// the value passed in will be treated as the full file name
  161. /// for the PID file. This provides a means to override the
  162. /// default file name with an explicit value.
  163. ///
  164. /// @param pid_file_name file name to be used as the PID file
  165. void setPIDFileName(const std::string& pid_file_name);
  166. /// @brief Creates the PID file
  167. ///
  168. /// If the PID file name has not been previously set, the method
  169. /// uses manufacturePIDFileName() to set it. If the PID file
  170. /// name refers to an existing file whose contents are a PID whose
  171. /// process is still alive, the method will throw a DaemonPIDExists
  172. /// exception. Otherwise, the file created (or truncated) and
  173. /// the given pid (if not zero) is written to the file.
  174. ///
  175. /// @param pid PID to write to the file if not zero, otherwise the
  176. /// PID of the current process is used.
  177. void createPIDFile(int pid = 0);
  178. protected:
  179. /// @brief Invokes handler for the next received signal.
  180. ///
  181. /// This function provides a default implementation for the function
  182. /// handling next signal received by the process. It checks if a pointer
  183. /// to @c isc::util::SignalSet object and the signal handler function
  184. /// have been set. If they have been set, the signal handler is invoked for
  185. /// the the next signal registered in the @c SignalSet object.
  186. ///
  187. /// This function should be received in the main loop of the process.
  188. virtual void handleSignal();
  189. /// @brief A pointer to the object installing custom signal handlers.
  190. ///
  191. /// This pointer needs to be initialized to point to the @c SignalSet
  192. /// object in the derived classes which need to handle signals received
  193. /// by the process.
  194. isc::util::SignalSetPtr signal_set_;
  195. /// @brief Pointer to the common signal handler invoked by the handleSignal
  196. /// function.
  197. ///
  198. /// This pointer needs to be initialized to point to the signal handler
  199. /// function for signals being handled by the process. If signal handler
  200. /// it not initialized, the signals will not be handled.
  201. isc::util::SignalHandler signal_handler_;
  202. /// @brief Manufacture the pid file name
  203. std::string makePIDFileName() const;
  204. private:
  205. /// @brief Config file name or empty if config file not used.
  206. std::string config_file_;
  207. /// @brief Name of this process, used when creating its pid file
  208. std::string proc_name_;
  209. /// @brief Pointer to the directory where PID file(s) are written
  210. /// It defaults to --localstatedir
  211. std::string pid_file_dir_;
  212. /// @brief Pointer to the PID file for this process
  213. isc::util::PIDFilePtr pid_file_;
  214. /// @brief Flag indicating if this instance created the file
  215. bool am_file_author_;
  216. };
  217. }; // end of isc::dhcp namespace
  218. }; // end of isc namespace
  219. #endif