xorg-gtest  0.1
Xorg testing extension to Google Test
 All Classes Functions Pages
xorg::testing::Process Class Reference

Class that abstracts child process creation and termination. More...

#include <xorg/gtest/process.h>

Public Member Functions

 Process ()
 Creates a child-process that is in a terminated state. More...
 
void Start (const std::string &program, va_list args)
 Starts a program as a child process. More...
 
void Start (const std::string &program,...)
 Starts a program as a child process. More...
 
bool Terminate ()
 Terminates (SIGTERM) this child process. More...
 
bool Kill ()
 Kills (SIGKILL) this child process. More...
 
pid_t Pid () const
 Accesses the pid of the child process. More...
 

Static Public Member Functions

static void SetEnv (const std::string &name, const std::string &value, bool overwrite)
 Helper function to adjust the environment of the current process. More...
 
static std::string GetEnv (const std::string &name, bool *exists=NULL)
 Helper function to query the environment of the current process. More...
 

Detailed Description

Class that abstracts child process creation and termination.

This class allows for forking, running and terminating child processes. In addition, manipulation of the child process' environment is supported. For example, starting an X server instance on display port 133 as a child process can be realized with the following code snippet:

Process xorgServer;
try {
xorgServer.Start("Xorg", "Xorg", ":133");
} catch (const std::runtime_error&e) {
std::cerr << "Problem starting the X server: " << e.what() << std::endl;
}
...
if (!xorgServer.Terminate()) {
std::cerr << "Problem terminating server ... killing now ..." << std::endl;
if (!xorgServer.Kill())
std::cerr << "Problem killing server" << std::endl;
}

Constructor & Destructor Documentation

xorg::testing::Process::Process ( )

Creates a child-process that is in a terminated state.

Member Function Documentation

static std::string xorg::testing::Process::GetEnv ( const std::string &  name,
bool *  exists = NULL 
)
static

Helper function to query the environment of the current process.

Parameters
[in]nameThe name of the environment variable.
[out]existsIf not NULL, the variable will be set to true if the environment variable exists and to false otherwise.
Returns
The value of the environment variable, or an empty string.
bool xorg::testing::Process::Kill ( )

Kills (SIGKILL) this child process.

Exceptions
std::runtime_errorif child tries to kill itself.
Returns
true if kill succeeded, false otherwise.
Postcondition
If successful: Child process killed.
If successful: Subsequent calls to Pid() return -1.
pid_t xorg::testing::Process::Pid ( ) const

Accesses the pid of the child process.

Returns
The pid of the child process or -1.
static void xorg::testing::Process::SetEnv ( const std::string &  name,
const std::string &  value,
bool  overwrite 
)
static

Helper function to adjust the environment of the current process.

Parameters
[in]nameName of the environment variable.
[in]valueValue of the environment variable.
[in]overwriteWhether to overwrite the value of existing env variables.
Exceptions
std::runtime_errorif adjusting the environment does not succeed.
void xorg::testing::Process::Start ( const std::string &  program,
va_list  args 
)

Starts a program as a child process.

See 'man execvp' for further information on the variadic argument list.

Parameters
programThe program to start.
argsVariadic list of arguments passed to the program.
Exceptions
std::runtime_erroron failure.
Postcondition
If successful: Child process forked and program started.
If successful: Subsequent calls to Pid() return child process pid.
void xorg::testing::Process::Start ( const std::string &  program,
  ... 
)

Starts a program as a child process.

Takes a variadic list of arguments passed to the program. See 'man execvp' for further information on the variadic argument list.

Parameters
programThe program to start.
Exceptions
std::runtime_erroron failure.
Postcondition
If successful: Child process forked and program started.
If successful: Subsequent calls to Pid() return child process pid.
bool xorg::testing::Process::Terminate ( )

Terminates (SIGTERM) this child process.

Exceptions
std::runtime_errorif child tries to terminate itself.
Returns
true if termination succeeded, false otherwise.
Postcondition
If successful: Child process terminated.
If successful: Subsequent calls to Pid() return -1.

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