Kea 3.1.3
daemon.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2025 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
9#include <cc/data.h>
10#include <process/daemon.h>
11#include <process/log_parser.h>
13#include <log/logger_name.h>
14#include <log/logger_support.h>
15#include <process/config_base.h>
17#include <util/filesystem.h>
18
19#include <functional>
20#include <sstream>
21#include <fstream>
22
23#include <errno.h>
24
25using namespace isc::data;
26using namespace isc::util;
27using namespace isc::util::file;
28
33namespace isc {
34namespace process {
35
36bool Daemon::verbose_ = false;
37
38std::string Daemon::proc_name_("");
39
40std::string Daemon::default_logger_name_("kea");
41
43 : signal_set_(), config_file_(""),
44 pid_file_dir_(PIDFILE_DIR), pid_file_(), am_file_author_(false),
45 exit_value_(EXIT_SUCCESS) {
46
47 // The pid_file_dir can be overridden via environment variable
48 // This is primarily intended to simplify testing
49 const char* const env = getenv("KEA_PIDFILE_DIR");
50 if (env) {
51 pid_file_dir_ = env;
52 }
53}
54
56 if (pid_file_ && am_file_author_) {
57 pid_file_->deleteFile();
58 }
59}
60
62}
63
65}
66
68 const ConfigPtr& storage) {
69
70 if (log_config) {
71 ConstElementPtr loggers = log_config->get("loggers");
72 if (loggers) {
73 LogConfigParser parser(storage);
74 parser.parseConfiguration(loggers, verbose_);
75 }
76 }
77}
78
79void
80Daemon::setVerbose(bool verbose) {
81 verbose_ = verbose;
82}
83
84bool
86 return (verbose_);
87}
88
89void Daemon::loggerInit(const char* name, bool verbose) {
90
91 setenv("KEA_LOGGER_DESTINATION", "stdout", 0);
92
93 // Initialize logger system
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 Path instance from the config_file_ pathname, and
121 // check the file name component.
122 Path file(config_file_);
123 if (file.stem().empty()) {
124 isc_throw(isc::BadValue, "config file:" << config_file_
125 << " is missing file name");
126 }
127}
128
129void
131 Path path(file);
132 // from checkConfigFile().
133 if (path.stem().empty()) {
134 isc_throw(isc::BadValue, "config file:" << file
135 << " is missing file name");
136 }
137 Path current(config_file_);
138 if (current.parentDirectory() == path.parentDirectory()) {
139 // Same parent directories!
140 return;
141 }
142 if (path.parentDirectory().empty()) {
143 // Note the current parent directory can't be empty here.
144 file = current.parentDirectory() + file;
145 return;
146 }
147 isc_throw(isc::BadValue, "file " << file << " must be in the same "
148 << "directory as the config file (" << config_file_ << ")");
149}
150
151std::string
153 return (proc_name_);
154}
155
156void
157Daemon::setProcName(const std::string& proc_name) {
158 proc_name_ = proc_name;
159}
160
161std::string
163 return(pid_file_dir_);
164}
165
166void
167Daemon::setPIDFileDir(const std::string& pid_file_dir) {
168 pid_file_dir_ = pid_file_dir;
169}
170
171std::string
173 if (pid_file_) {
174 return (pid_file_->getFilename());
175 }
176
177 return ("");
178}
179
180std::string
182 if (pid_file_) {
183 return (pid_file_->getLockname());
184 }
185
186 return ("");
187}
188
189void
190Daemon::setPIDFileName(const std::string& pid_file_name) {
191 if (pid_file_) {
192 isc_throw(isc::InvalidOperation, "Daemon::setConfigFile"
193 " file name already set to:" << pid_file_->getFilename());
194 }
195
196 if (pid_file_name.empty()) {
197 isc_throw(isc::BadValue, "Daemon::setPIDFileName"
198 " file name may not be empty");
199 }
200
201 pid_file_.reset(new PIDFile(pid_file_name));
202}
203
204std::string
206 if (config_file_.empty()) {
208 "Daemon::makePIDFileName config file name is not set");
209 }
210
211 // Create Path instance from the config_file_ pathname, so we can
212 // extract the fname component.
213 Path file(config_file_);
214 if (file.stem().empty()) {
215 isc_throw(isc::BadValue, "Daemon::makePIDFileName config file:"
216 << config_file_ << " is missing file name");
217 }
218
219 if (proc_name_.empty()) {
221 "Daemon::makePIDFileName process name is not set");
222 }
223
224 // Make the pathname for the PID file from the runtime directory,
225 // configuration name and process name.
226 std::ostringstream stream;
227 stream << pid_file_dir_ << "/" << file.stem()
228 << "." << proc_name_ << ".pid";
229
230 return(stream.str());
231};
232
233void
235 // If pid_file_ hasn't been instantiated explicitly, then do so
236 // using the default name.
237 if (!pid_file_) {
239 }
240
241 // Acquire a lock for check and write operations.
242 PIDLock pid_lock(getPIDLockName());
243 if (!pid_lock.isLocked()) {
244 isc_throw(DaemonPIDExists, "Daemon::createPIDFile: can't lock, "
245 "PID lock file: " << getPIDLockName());
246 }
247
248 // If we find a pre-existing file containing a live PID we bail.
249 int chk_pid = pid_file_->check();
250 if (chk_pid > 0) {
251 isc_throw(DaemonPIDExists, "Daemon::createPIDFile: PID: " << chk_pid
252 << " exists, PID file: " << getPIDFileName());
253 }
254
255 if (pid == 0) {
256 // Write the PID of the current process
257 pid_file_->write();
258 } else {
259 // Write the PID we were given
260 pid_file_->write(pid);
261 }
262
263 am_file_author_ = true;
264}
265
266size_t
267Daemon::writeConfigFile(const std::string& config_file,
268 ConstElementPtr cfg) const {
269 if (!cfg) {
270 isc_throw(Unexpected, "Can't write configuration: conversion to JSON failed");
271 }
272
273 std::ofstream out(config_file, std::ios::trunc);
274 if (!out.good()) {
275 isc_throw(Unexpected, "Unable to open file " + config_file + " for writing");
276 }
277
278 // Write the actual content using pretty printing.
279 prettyPrint(cfg, out);
280
281 size_t bytes = static_cast<size_t>(out.tellp());
282
283 out.close();
284
285 return (bytes);
286}
287
288std::list<std::list<std::string>>
290 static std::list<std::list<std::string>> const list;
291 return list;
292}
293
297 for (std::list<std::string>& json_path : jsonPathsToRedact()) {
298 result = isc::process::redactConfig(result, json_path);
299 }
300 return result;
301}
302
303} // namespace process
304} // namespace isc
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 the PID file points to a live PID.
Definition daemon.h:25
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:267
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:80
std::string getPIDFileName() const
Returns the current PID file name.
Definition daemon.cc:172
virtual std::list< std::list< std::string > > jsonPathsToRedact() const
Return a list of all paths that contain passwords or secrets.
Definition daemon.cc:289
Daemon()
Default constructor.
Definition daemon.cc:42
virtual void shutdown()
Initiates shutdown procedure for the whole server.
Definition daemon.cc:64
std::string getPIDFileDir() const
Returns the directory used when forming default PID file name.
Definition daemon.cc:162
virtual ~Daemon()
Destructor.
Definition daemon.cc:55
isc::asiolink::IOSignalSetPtr signal_set_
A pointer to the object installing custom signal handlers.
Definition daemon.h:272
static void configureLogger(const isc::data::ConstElementPtr &log_config, const isc::process::ConfigPtr &storage)
Configures logger.
Definition daemon.cc:67
static bool getVerbose()
Returns if running in verbose mode.
Definition daemon.cc:85
static void loggerInit(const char *log_name, bool verbose)
Initializes logger.
Definition daemon.cc:89
void checkWriteConfigFile(std::string &file)
Checks the to-be-written configuration file name.
Definition daemon.cc:130
static std::string getProcName()
returns the process name This value is used as when forming the default PID file name
Definition daemon.cc:152
virtual void cleanup()
Performs final deconfiguration.
Definition daemon.cc:61
isc::data::ConstElementPtr redactConfig(isc::data::ConstElementPtr const &config)
Redact a configuration.
Definition daemon.cc:295
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:190
static void setProcName(const std::string &proc_name)
Sets the process name.
Definition daemon.cc:157
void createPIDFile(int pid=0)
Creates the PID file.
Definition daemon.cc:234
void setPIDFileDir(const std::string &pid_file_dir)
Sets the PID file directory.
Definition daemon.cc:167
std::string getPIDLockName() const
Returns the current PID lock file name.
Definition daemon.cc:181
std::string makePIDFileName() const
Manufacture the pid file name.
Definition daemon.cc:205
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:38
Class to help with processing PID files.
Definition pid_file.h:40
RAII device to handle a lock file to avoid race conditions.
Definition pid_file.h:115
bool isLocked()
Return the lock status.
Definition pid_file.h:131
#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:1549
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
void initLogger(const string &root, isc::log::Severity severity, int dbglevel, const char *file, bool buffer)
Run-time initialization.
const int MAX_DEBUG_LEVEL
void setDefaultLoggingOutput(bool verbose)
Reset root logger characteristics.
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
ConstElementPtr redactConfig(ConstElementPtr const &element, list< string > const &json_path, string obscure)
Redact a configuration.
Defines the logger used by the top-level component of kea-lfc.
Paths on a filesystem.
Definition filesystem.h:107
std::string parentDirectory() const
Get the parent directory.
std::string stem() const
Get the base name of the file without the extension.