| Kea 3.0.2-git

process_spawn.h
Go to the documentation of this file.
1// Copyright (C) 2015-2024 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 namespace isc {
19 namespace asiolink {
20
22 class ProcessSpawnError : public Exception {
23 public:
24 ProcessSpawnError(const char* file, size_t line, const char* what) :
25 isc::Exception(file, line, what) { }
26};
27
30 class ProcessSpawnImpl;
31
33 typedef boost::shared_ptr<ProcessSpawnImpl> ProcessSpawnImplPtr;
34
37 typedef std::vector<std::string> ProcessArgs;
38
41 typedef std::vector<std::string> ProcessEnvVars;
42
61 class ProcessSpawn : boost::noncopyable {
62 public:
63
65 enum SpawnMode {
66 ASYNC, // thread continues without waiting for the child to finish.
67 SYNC // thread waits for the child to finish.
68 };
69
78 ProcessSpawn(const SpawnMode mode,
79 const std::string& executable,
80 const ProcessArgs& args = ProcessArgs(),
81 const ProcessEnvVars& vars = ProcessEnvVars(),
82 const bool inherit_env = false);
83
85 ~ProcessSpawn() = default;
86
88 std::string getCommandLine() const;
89
103 pid_t spawn(bool dismiss = false);
104
114 bool isRunning(const pid_t pid) const;
115
119 bool isAnyRunning() const;
120
133 int getExitStatus(const pid_t pid) const;
134
147 void clearState(const pid_t pid);
148
152 static isc::asiolink::IOServicePtr getIOService() {
153 return (io_service_);
154 }
155
159 static void setIOService(isc::asiolink::IOServicePtr io_service) {
160 io_service_ = io_service;
161 }
162
163 private:
164
166 static isc::asiolink::IOServicePtr io_service_;
167
169 ProcessSpawnImplPtr impl_;
170};
171
172} // namespace asiolink
173} // namespace isc
174
175#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:24
isc::asiolink::ProcessSpawn::isAnyRunning
bool isAnyRunning() const
Checks if any of the spawned processes is still running.
Definition process_spawn.cc:507
isc::asiolink::ProcessSpawn::getExitStatus
int getExitStatus(const pid_t pid) const
Returns exit status of the process.
Definition process_spawn.cc:512
isc::asiolink::ProcessSpawn::clearState
void clearState(const pid_t pid)
Removes the status of the process with a specified PID.
Definition process_spawn.cc:517
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:483
isc::asiolink::ProcessSpawn::isRunning
bool isRunning(const pid_t pid) const
Checks if the process is still running.
Definition process_spawn.cc:502
isc::asiolink::ProcessSpawn::getCommandLine
std::string getCommandLine() const
Returns full command line, including arguments, for the process.
Definition process_spawn.cc:492
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:497
isc::asiolink::ProcessSpawn::getIOService
static isc::asiolink::IOServicePtr getIOService()
Get the I/O service.
Definition process_spawn.h:152
isc::asiolink::ProcessSpawn::setIOService
static void setIOService(isc::asiolink::IOServicePtr io_service)
Set the I/O service.
Definition process_spawn.h:159
isc::asiolink::ProcessSpawn::SpawnMode
SpawnMode
The spawn type.
Definition process_spawn.h:65
isc::asiolink::ProcessSpawn::SYNC
@ SYNC
Definition process_spawn.h:67
isc::asiolink::ProcessSpawn::ASYNC
@ ASYNC
Definition process_spawn.h:66
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:37
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:41
isc::asiolink::ProcessSpawnImplPtr
boost::shared_ptr< ProcessSpawnImpl > ProcessSpawnImplPtr
Pointer to a ProcessSpawnImpl class.
Definition process_spawn.h:33
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:28
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148