wibble  1.1
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
wibble::sys::ChildProcess Class Referenceabstract

Fork a child process. More...

#include <childprocess.h>

Inheritance diagram for wibble::sys::ChildProcess:
Inheritance graph
[legend]

Public Member Functions

 ChildProcess ()
 
virtual ~ChildProcess ()
 
void setExec (std::string command)
 Instead of calling the main() function of this class, execute an external command. More...
 
pid_t fork ()
 For a subprocess to run proc. More...
 
void setupRedirects (int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
 
pid_t forkAndRedirect (int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
 
pid_t pid () const
 Get the pid of the child process or (pid_t)-1 if no child is running. More...
 
bool running ()
 
int exitStatus ()
 
void waitForSuccess ()
 
int wait (struct rusage *ru=0)
 Wait for the child to finish, returning its exit status and optionally storing resource usage informations in `ru'. More...
 
void kill (int signal)
 Send the given signal to the process. More...
 

Protected Member Functions

virtual int main ()=0
 Main function to be called in the child process after it has forked. More...
 
virtual void spawnChild ()
 On Windows, it's impossible to fork(), but if you were to fork+exec, it's not all lost. More...
 
void waitError ()
 
void setupPipes ()
 
void setupPrefork ()
 
void setupChild ()
 
void setupParent ()
 

Protected Attributes

pid_t _pid
 
int pipes [3][2]
 
int * _stdin
 
int * _stdout
 
int * _stderr
 
int m_status
 
bool m_doExec
 
std::string m_command
 

Detailed Description

Fork a child process.

Constructor & Destructor Documentation

wibble::sys::ChildProcess::ChildProcess ( )
inline
virtual wibble::sys::ChildProcess::~ChildProcess ( )
inlinevirtual

Member Function Documentation

int wibble::sys::ChildProcess::exitStatus ( )
pid_t wibble::sys::ChildProcess::fork ( )

For a subprocess to run proc.

To redirect stdio of the child process to pipes, call setupRedirects first. NB. This currently works on Windows only when setExec has been called first (any main() overrides have no effect on Windows).

Referenced by forkAndRedirect(), TestChildprocess::kill(), and wibble::sys::PipeThrough::run().

pid_t wibble::sys::ChildProcess::forkAndRedirect ( int *  stdinfd = 0,
int *  stdoutfd = 0,
int *  stderrfd = 0 
)
inline
void wibble::sys::ChildProcess::kill ( int  signal)

Send the given signal to the process.

References assert_die.

Referenced by TestChildprocess::kill().

virtual int wibble::sys::ChildProcess::main ( )
protectedpure virtual

Main function to be called in the child process after it has forked.

Implemented in wibble::sys::Exec, TestChild, and EndlessChild.

pid_t wibble::sys::ChildProcess::pid ( ) const
inline

Get the pid of the child process or (pid_t)-1 if no child is running.

Note: while ChildProcess::kill() has a safeguard against killing pid -1, if you are going to run ::kill on the output of pid() make sure to check what is the semanthics of kill() when pid is -1.

References _pid.

bool wibble::sys::ChildProcess::running ( )

References assert_die.

void wibble::sys::ChildProcess::setExec ( std::string  command)
inline

Instead of calling the main() function of this class, execute an external command.

The command is passed to the shell interpreter of the system (/bin/sh on UNIX, CreateProcess on Windows).

References m_command, and m_doExec.

void wibble::sys::ChildProcess::setupChild ( )
protected
void wibble::sys::ChildProcess::setupParent ( )
protected
void wibble::sys::ChildProcess::setupPipes ( )
protected
void wibble::sys::ChildProcess::setupPrefork ( )
protected
void wibble::sys::ChildProcess::setupRedirects ( int *  stdinfd = 0,
int *  stdoutfd = 0,
int *  stderrfd = 0 
)
void wibble::sys::ChildProcess::spawnChild ( )
protectedvirtual

On Windows, it's impossible to fork(), but if you were to fork+exec, it's not all lost.

You can implement spawnChild() instead of main(), which needs to call CreateProcess, spawn or similar. The redirections requested by setupRedirects are respected. Exec and ShellProcess implement spawnChild on Windows.

NB. For wait() to work, the si/pi member variables need to be filled in by the implementation.

Reimplemented in wibble::sys::Exec.

References assert_die.

int wibble::sys::ChildProcess::wait ( struct rusage *  ru = 0)

Wait for the child to finish, returning its exit status and optionally storing resource usage informations in `ru'.

Return -1 if no child is running. TODO: gracefully handle the EINTR error code

Referenced by TestChildprocess::inout(), TestChildprocess::kill(), TestChildprocess::output(), TestChildprocess::redirect(), and TestChildprocess::shellCommand().

void wibble::sys::ChildProcess::waitError ( )
protected
void wibble::sys::ChildProcess::waitForSuccess ( )

References wibble::str::fmtf().

Member Data Documentation

pid_t wibble::sys::ChildProcess::_pid
protected

Referenced by pid().

int * wibble::sys::ChildProcess::_stderr
protected
int* wibble::sys::ChildProcess::_stdin
protected
int * wibble::sys::ChildProcess::_stdout
protected
std::string wibble::sys::ChildProcess::m_command
protected

Referenced by setExec().

bool wibble::sys::ChildProcess::m_doExec
protected

Referenced by setExec().

int wibble::sys::ChildProcess::m_status
protected
int wibble::sys::ChildProcess::pipes[3][2]
protected

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