Kea  1.9.9-git
isc::asiolink::ProcessSpawn Class Reference

Utility class for spawning new processes. More...

#include <process_spawn.h>

+ Inheritance diagram for isc::asiolink::ProcessSpawn:

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...
 

Detailed Description

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.

Note
The ProcessSpawn uses full path for the program to execute.

Definition at line 61 of file process_spawn.h.

Constructor & Destructor Documentation

isc::asiolink::ProcessSpawn::ProcessSpawn ( isc::asiolink::IOServicePtr  io_service,
const std::string &  executable,
const ProcessArgs args = ProcessArgs(),
const ProcessEnvVars vars = ProcessEnvVars() 
)

Constructor.

Parameters
io_serviceThe IOService which handles signal handlers.
executableA full path to the program to be executed.
argsArguments for the program to be executed.
varsEnvironment variables for the program to be executed.

Definition at line 364 of file process_spawn.cc.

isc::asiolink::ProcessSpawn::~ProcessSpawn ( )
default

Destructor.

Member Function Documentation

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.

Parameters
pidA 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.

Note
ProcessSpawn::isRunning should be called and have returned false before using ProcessSpawn::getExitStatus.
Parameters
pidID of the child process for which exit status should be returned.
Returns
Exit code of the process.

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.

Returns
true if at least one child process 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.

Parameters
pidID of the child processes for which state should be checked.
Returns
true if the child process is running, false otherwise.

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.

Parameters
dismissThe flag which indicated if the process status can be disregarded.
Exceptions
ProcessSpawnErrorif forking a current process failed.

Definition at line 377 of file process_spawn.cc.


The documentation for this class was generated from the following files: