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