Kea
1.9.9-git
|
Utility class for spawning new processes. More...
#include <process_spawn.h>
Public Member Functions | |
ProcessSpawn (isc::asiolink::IOServicePtr io_service, const std::string &executable, const ProcessArgs &args=ProcessArgs(), const ProcessEnvVars &vars=ProcessEnvVars()) | |
Constructor. More... | |
~ProcessSpawn ()=default | |
Destructor. More... | |
void | clearState (const pid_t pid) |
Removes the status of the process with a specified PID. More... | |
std::string | getCommandLine () const |
Returns full command line, including arguments, for the process. More... | |
int | getExitStatus (const pid_t pid) const |
Returns exit status of the process. More... | |
bool | isAnyRunning () const |
Checks if any of the spawned processes is still running. More... | |
bool | isRunning (const pid_t pid) const |
Checks if the process is still running. More... | |
pid_t | spawn (bool dismiss=false) |
Spawn the new process. More... | |
Utility class for spawning new processes.
This class is used to spawn new process by Kea. It forks the current process and then uses the execve
function to execute the specified binary with parameters. The ProcessSpawn
installs the handler for the SIGCHLD signal, which is executed when the child process ends. The handler checks the exit code returned by the process and records it. The exit code can be retrieved by the caller using the ProcessSpawn::getExitStatus
method.
This class is made noncopyable so that we don't have attempts to make multiple copies of an object. This avoid problems with multiple copies of objects for a single global resource such as the SIGCHLD signal handler. In addition making it noncopyable keeps the static check code from flagging the lack of a copy constructor as an issue.
Definition at line 61 of file process_spawn.h.
isc::asiolink::ProcessSpawn::ProcessSpawn | ( | isc::asiolink::IOServicePtr | io_service, |
const std::string & | executable, | ||
const ProcessArgs & | args = ProcessArgs() , |
||
const ProcessEnvVars & | vars = ProcessEnvVars() |
||
) |
Constructor.
io_service | The IOService which handles signal handlers. |
executable | A full path to the program to be executed. |
args | Arguments for the program to be executed. |
vars | Environment variables for the program to be executed. |
Definition at line 364 of file process_spawn.cc.
|
default |
Destructor.
void isc::asiolink::ProcessSpawn::clearState | ( | const pid_t | pid | ) |
Removes the status of the process with a specified PID.
This method removes the status of the process with a specified PID. If the process is still running, the status is not removed and the exception is thrown.
Note ProcessSpawn::isRunning
must be called and have returned false before using clearState(). And of course ProcessSpawn::getExitStatus
should be called first, if there is some interest in the status.
pid | A process pid. |
Definition at line 397 of file process_spawn.cc.
std::string isc::asiolink::ProcessSpawn::getCommandLine | ( | ) | const |
Returns full command line, including arguments, for the process.
Definition at line 372 of file process_spawn.cc.
int isc::asiolink::ProcessSpawn::getExitStatus | ( | const pid_t | pid | ) | const |
Returns exit status of the process.
If the process is still running, the previous status is returned or 0, if the process is being ran for the first time.
ProcessSpawn::isRunning
should be called and have returned false before using ProcessSpawn::getExitStatus
.pid | ID of the child process for which exit status should be returned. |
Definition at line 392 of file process_spawn.cc.
bool isc::asiolink::ProcessSpawn::isAnyRunning | ( | ) | const |
Checks if any of the spawned processes is still running.
Definition at line 387 of file process_spawn.cc.
bool isc::asiolink::ProcessSpawn::isRunning | ( | const pid_t | pid | ) | const |
Checks if the process is still running.
Note that only a negative (false) result is reliable as the child process can exit between the time its state is checked and this function returns.
pid | ID of the child processes for which state should be checked. |
Definition at line 382 of file process_spawn.cc.
pid_t isc::asiolink::ProcessSpawn::spawn | ( | bool | dismiss = false | ) |
Spawn the new process.
This method forks the current process and executes the specified binary with arguments within the child process.
The child process will return EXIT_FAILURE if the method was unable to start the executable, e.g. as a result of insufficient permissions or when the executable does not exist. If the process ends successfully the EXIT_SUCCESS is returned.
dismiss | The flag which indicated if the process status can be disregarded. |
ProcessSpawnError | if forking a current process failed. |
Definition at line 377 of file process_spawn.cc.