Kea 1.5.0
daemon.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2018 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
7#include <config.h>
8#include <cc/data.h>
9#include <process/daemon.h>
10#include <process/log_parser.h>
12#include <log/logger_name.h>
13#include <log/logger_support.h>
14#include <process/config_base.h>
15#include <util/filename.h>
16
17#include <boost/bind.hpp>
18
19#include <sstream>
20#include <fstream>
21#include <errno.h>
22
27namespace isc {
28namespace process {
29
30bool Daemon::verbose_ = false;
31
32std::string Daemon::default_logger_name_("kea");
33
35 : signal_set_(), signal_handler_(), config_file_(""), proc_name_(""),
36 pid_file_dir_(DATA_DIR), pid_file_(), am_file_author_(false) {
37
38 // The pid_file_dir can be overridden via environment variable
39 // This is primarily intended to simplify testing
40 const char* const env = getenv("KEA_PIDFILE_DIR");
41 if (env) {
42 pid_file_dir_ = env;
43 }
44}
45
47 if (pid_file_ && am_file_author_) {
48 pid_file_->deleteFile();
49 }
50}
51
53
54}
55
57
58}
59
62 signal_set_->handleNext(boost::bind(signal_handler_, _1));
63 }
64}
65
67 const ConfigPtr& storage) {
68
69 if (log_config) {
70 isc::data::ConstElementPtr loggers = log_config->get("loggers");
71 if (loggers) {
72 LogConfigParser parser(storage);
73 parser.parseConfiguration(loggers, verbose_);
74 }
75 }
76}
77
78void
79Daemon::setVerbose(bool verbose) {
80 verbose_ = verbose;
81}
82
83bool
85 return (verbose_);
86}
87
88void Daemon::loggerInit(const char* name, bool verbose) {
89
90 setenv("KEA_LOGGER_DESTINATION", "stdout", 0);
91
92 // Initialize logger system
94 NULL);
95
96 // Apply default configuration (log INFO or DEBUG to stdout)
98}
99
100std::string Daemon::getVersion(bool /*extended*/) {
101 isc_throw(isc::NotImplemented, "Daemon::getVersion() called");
102}
103
104std::string
106 return (config_file_);
107}
108
109void
110Daemon::setConfigFile(const std::string& config_file) {
111 config_file_ = config_file;
112}
113
114void
116 if (config_file_.empty()) {
117 isc_throw(isc::BadValue, "config file name is not set");
118 }
119
120 // Create Filename instance from the config_file_ pathname, and
121 // check the file name component.
122 isc::util::Filename file(config_file_);
123 if (file.name().empty()) {
124 isc_throw(isc::BadValue, "config file:" << config_file_
125 << " is missing file name");
126 }
127}
128
129std::string
131 return (proc_name_);
132};
133
134void
135Daemon::setProcName(const std::string& proc_name) {
136 proc_name_ = proc_name;
137}
138
139std::string
141 return(pid_file_dir_);
142}
143
144void
145Daemon::setPIDFileDir(const std::string& pid_file_dir) {
146 pid_file_dir_ = pid_file_dir;
147}
148
149std::string
151 if (pid_file_) {
152 return (pid_file_->getFilename());
153 }
154
155 return ("");
156};
157
158void
159Daemon::setPIDFileName(const std::string& pid_file_name) {
160 if (pid_file_) {
161 isc_throw(isc::InvalidOperation, "Daemon::setConfigFile"
162 " file name already set to:" << pid_file_->getFilename());
163 }
164
165 if (pid_file_name.empty()) {
166 isc_throw(isc::BadValue, "Daemon::setPIDFileName"
167 " file name may not be empty");
168 }
169
170 pid_file_.reset(new util::PIDFile(pid_file_name));
171};
172
173std::string
175 if (config_file_.empty()) {
177 "Daemon::makePIDFileName config file name is not set");
178 }
179
180 // Create Filename instance from the config_file_ pathname, so we can
181 // extract the fname component.
182 isc::util::Filename file(config_file_);
183 if (file.name().empty()) {
184 isc_throw(isc::BadValue, "Daemon::makePIDFileName config file:"
185 << config_file_ << " is missing file name");
186 }
187
188 if (proc_name_.empty()) {
190 "Daemon::makePIDFileName process name is not set");
191 }
192
193 // Make the pathname for the PID file from the runtime directory,
194 // configuration name and process name.
195 std::ostringstream stream;
196 stream << pid_file_dir_ << "/" << file.name()
197 << "." << proc_name_ << ".pid";
198
199 return(stream.str());
200};
201
202void
204 // If pid_file_ hasn't been instantiated explicitly, then do so
205 // using the default name.
206 if (!pid_file_) {
208 }
209
210 // If we find a pre-existing file containing a live PID we bail.
211 int chk_pid = pid_file_->check();
212 if (chk_pid > 0) {
213 isc_throw(DaemonPIDExists, "Daemon::createPIDFile: PID: " << chk_pid
214 << " exists, PID file: " << getPIDFileName());
215 }
216
217 if (pid == 0) {
218 // Write the PID of the current process
219 pid_file_->write();
220 } else {
221 // Write the PID we were given
222 pid_file_->write(pid);
223 }
224
225 am_file_author_ = true;
226}
227
228size_t
229Daemon::writeConfigFile(const std::string& config_file,
230 isc::data::ConstElementPtr cfg) const {
231 if (!cfg) {
232 isc_throw(Unexpected, "Can't write configuration: conversion to JSON failed");
233 }
234
235 std::ofstream out(config_file, std::ios::trunc);
236 if (!out.good()) {
237 isc_throw(Unexpected, "Unable to open file " + config_file + " for writing");
238 }
239
240 // Write the actual content using pretty printing.
241 isc::data::prettyPrint(cfg, out);
242
243 size_t bytes = static_cast<size_t>(out.tellp());
244
245 out.close();
246
247 return (bytes);
248}
249
250};
251};
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
A generic exception that is thrown when a function is not implemented.
A generic exception that is thrown when an unexpected error condition occurs.
Exception thrown when a the PID file points to a live PID.
Definition: daemon.h:21
std::string getConfigFile() const
Returns config file name.
Definition: daemon.cc:105
virtual size_t writeConfigFile(const std::string &config_file, isc::data::ConstElementPtr cfg=isc::data::ConstElementPtr()) const
Writes current configuration to specified file.
Definition: daemon.cc:229
isc::util::SignalSetPtr signal_set_
A pointer to the object installing custom signal handlers.
Definition: daemon.h:238
static std::string getVersion(bool extended)
returns Kea version on stdout and exits.
Definition: daemon.cc:100
static void setVerbose(const bool verbose)
Sets or clears verbose mode.
Definition: daemon.cc:79
isc::util::SignalHandler signal_handler_
Pointer to the common signal handler invoked by the handleSignal function.
Definition: daemon.h:246
std::string getPIDFileName() const
Returns the current PID file name.
Definition: daemon.cc:150
Daemon()
Default constructor.
Definition: daemon.cc:34
virtual void shutdown()
Initiates shutdown procedure for the whole DHCPv6 server.
Definition: daemon.cc:56
std::string getPIDFileDir() const
Returns the directory used when forming default PID file name.
Definition: daemon.cc:140
virtual ~Daemon()
Destructor.
Definition: daemon.cc:46
static void configureLogger(const isc::data::ConstElementPtr &log_config, const isc::process::ConfigPtr &storage)
Configures logger.
Definition: daemon.cc:66
virtual void handleSignal()
Invokes handler for the next received signal.
Definition: daemon.cc:60
static bool getVerbose()
Returns if running in verbose mode.
Definition: daemon.cc:84
static void loggerInit(const char *log_name, bool verbose)
Initializes logger.
Definition: daemon.cc:88
virtual void cleanup()
Performs final deconfiguration.
Definition: daemon.cc:52
void checkConfigFile() const
Checks the configuration file name.
Definition: daemon.cc:115
void setPIDFileName(const std::string &pid_file_name)
Sets PID file name.
Definition: daemon.cc:159
void setProcName(const std::string &proc_name)
Sets the process name.
Definition: daemon.cc:135
void createPIDFile(int pid=0)
Creates the PID file.
Definition: daemon.cc:203
void setPIDFileDir(const std::string &pid_file_dir)
Sets the PID file directory.
Definition: daemon.cc:145
std::string getProcName() const
returns the process name This value is used as when forming the default PID file name
Definition: daemon.cc:130
std::string makePIDFileName() const
Manufacture the pid file name.
Definition: daemon.cc:174
void setConfigFile(const std::string &config_file)
Sets the configuration file name.
Definition: daemon.cc:110
Configures log4cplus by translating Kea configuration structures.
Definition: log_parser.h:43
void parseConfiguration(const isc::data::ConstElementPtr &log_config, bool verbose=false)
Parses specified configuration.
Definition: log_parser.cc:31
Class to Manipulate Filenames.
Definition: filename.h:50
std::string name() const
Definition: filename.h:89
Class to help with processing PID files.
Definition: pid_file.h:40
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Logging initialization functions.
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
Definition: data.cc:1256
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
void initLogger(const string &root, isc::log::Severity severity, int dbglevel, const char *file, bool buffer)
Run-time initialization.
const int MAX_DEBUG_LEVEL
Definition: logger_level.h:36
void setDefaultLoggingOutput(bool verbose)
Reset root logger characteristics.
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the SrvConfig.
Definition: config_base.h:119
Defines the logger used by the top-level component of kea-dhcp-ddns.