xorg-gtest 0.1
Xorg testing extension to Google Test
|
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. | |
void | Start (const std::string &program, va_list args) |
Starts a program as a child process. | |
void | Start (const std::string &program,...) |
Starts a program as a child process. | |
bool | Terminate () |
Terminates (SIGTERM) this child process. | |
bool | Kill () |
Kills (SIGKILL) this child process. | |
pid_t | Pid () const |
Accesses the pid of the child process. | |
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. | |
static std::string | GetEnv (const std::string &name, bool *exists=NULL) |
Helper function to query the environment of the current process. |
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; }
xorg::testing::Process::Process | ( | ) |
Creates a child-process that is in a terminated state.
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.
[in] | name | The name of the environment variable. |
[out] | exists | If not NULL, the variable will be set to true if the environment variable exists and to false otherwise. |
bool xorg::testing::Process::Kill | ( | ) |
Kills (SIGKILL) this child process.
std::runtime_error | if child tries to kill itself. |
pid_t xorg::testing::Process::Pid | ( | ) | const |
Accesses the pid of the child process.
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.
[in] | name | Name of the environment variable. |
[in] | value | Value of the environment variable. |
[in] | overwrite | Whether to overwrite the value of existing env variables. |
std::runtime_error | if adjusting the environment does not succeed. |
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.
program | The program to start. |
std::runtime_error | on failure. |
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.
program | The program to start. |
args | Variadic list of arguments passed to the program. |
std::runtime_error | on failure. |
bool xorg::testing::Process::Terminate | ( | ) |
Terminates (SIGTERM) this child process.
std::runtime_error | if child tries to terminate itself. |