daemon.h 9.4 KB

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