Kea: process_spawn.h Source File

| Kea 3.2.0-git

process_spawn.h
Go to the documentation of this file.
1// Copyright (C) 2015-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#ifndef PROCESS_SPAWN_H
8#define PROCESS_SPAWN_H
9
10#include <asiolink/io_service.h>
11#include <exceptions/exceptions.h>
12#include <boost/noncopyable.hpp>
13#include <string>
14#include <sys/types.h>
15#include <vector>
16#include <boost/shared_ptr.hpp>
17
18#include <unordered_set>
19
20namespace isc {
21namespace asiolink {
22
24 class ProcessSpawnError : public Exception {
25public:
26 ProcessSpawnError(const char* file, size_t line, const char* what) :
27 isc::Exception(file, line, what) { }
28};
29
32class ProcessSpawnImpl;
33
35 typedef boost::shared_ptr<ProcessSpawnImpl> ProcessSpawnImplPtr;
36
39 typedef std::vector<std::string> ProcessArgs;
40
43 typedef std::vector<std::string> ProcessEnvVars;
44
63 class ProcessSpawn : boost::noncopyable {
64public:
65
67 enum SpawnMode {
68 ASYNC, // thread continues without waiting for the child to finish.
69 SYNC // thread waits for the child to finish.
70 };
71
80 ProcessSpawn(const SpawnMode mode,
81 const std::string& executable,
82 const ProcessArgs& args = ProcessArgs(),
83 const ProcessEnvVars& vars = ProcessEnvVars(),
84 const bool inherit_env = false);
85
87 ~ProcessSpawn() = default;
88
92 std::string getCommandLine(std::unordered_set<std::string> redact_args = {}) const;
93
107 pid_t spawn(bool dismiss = false);
108
118 bool isRunning(const pid_t pid) const;
119
123 bool isAnyRunning() const;
124
137 int getExitStatus(const pid_t pid) const;
138
151 void clearState(const pid_t pid);
152
156 static isc::asiolink::IOServicePtr getIOService() {
157 return (io_service_);
158 }
159
163 static void setIOService(isc::asiolink::IOServicePtr io_service) {
164 io_service_ = io_service;
165 }
166
167private:
168
170 static isc::asiolink::IOServicePtr io_service_;
171
173 ProcessSpawnImplPtr impl_;
174};
175
176} // namespace asiolink
177} // namespace isc
178
179#endif // PROCESS_SPAWN_H
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::asiolink::ProcessSpawnError::ProcessSpawnError
ProcessSpawnError(const char *file, size_t line, const char *what)
Definition process_spawn.h:26
isc::asiolink::ProcessSpawn::isAnyRunning
bool isAnyRunning() const
Checks if any of the spawned processes is still running.
Definition process_spawn.cc:520
isc::asiolink::ProcessSpawn::getExitStatus
int getExitStatus(const pid_t pid) const
Returns exit status of the process.
Definition process_spawn.cc:525
isc::asiolink::ProcessSpawn::clearState
void clearState(const pid_t pid)
Removes the status of the process with a specified PID.
Definition process_spawn.cc:530
isc::asiolink::ProcessSpawn::getCommandLine
std::string getCommandLine(std::unordered_set< std::string > redact_args={}) const
Returns full command line, including arguments, for the process.
Definition process_spawn.cc:505
isc::asiolink::ProcessSpawn::ProcessSpawn
ProcessSpawn(const SpawnMode mode, const std::string &executable, const ProcessArgs &args=ProcessArgs(), const ProcessEnvVars &vars=ProcessEnvVars(), const bool inherit_env=false)
Constructor.
Definition process_spawn.cc:496
isc::asiolink::ProcessSpawn::isRunning
bool isRunning(const pid_t pid) const
Checks if the process is still running.
Definition process_spawn.cc:515
isc::asiolink::ProcessSpawn::~ProcessSpawn
~ProcessSpawn()=default
Destructor.
isc::asiolink::ProcessSpawn::spawn
pid_t spawn(bool dismiss=false)
Spawn the new process.
Definition process_spawn.cc:510
isc::asiolink::ProcessSpawn::getIOService
static isc::asiolink::IOServicePtr getIOService()
Get the I/O service.
Definition process_spawn.h:156
isc::asiolink::ProcessSpawn::setIOService
static void setIOService(isc::asiolink::IOServicePtr io_service)
Set the I/O service.
Definition process_spawn.h:163
isc::asiolink::ProcessSpawn::SpawnMode
SpawnMode
The spawn type.
Definition process_spawn.h:67
isc::asiolink::ProcessSpawn::SYNC
@ SYNC
Definition process_spawn.h:69
isc::asiolink::ProcessSpawn::ASYNC
@ ASYNC
Definition process_spawn.h:68
exceptions.h
io_service.h
isc::asiolink
Definition addr_utilities.cc:170
isc::asiolink::ProcessArgs
std::vector< std::string > ProcessArgs
Type of the container holding arguments of the executable being run as a background process.
Definition process_spawn.h:39
isc::asiolink::ProcessEnvVars
std::vector< std::string > ProcessEnvVars
Type of the container holding environment variables of the executable being run as a background proce...
Definition process_spawn.h:43
isc::asiolink::ProcessSpawnImplPtr
boost::shared_ptr< ProcessSpawnImpl > ProcessSpawnImplPtr
Pointer to a ProcessSpawnImpl class.
Definition process_spawn.h:35
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18