wibble::sys::ChildProcess Class Referenceabstract

Fork a child process. More...

`#include <childprocess.h>`

Inheritance diagram for wibble::sys::ChildProcess:
[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

## ChildProcess()

wibble::sys::ChildProcess::ChildProcess | ( |  | ) |

inline

## ~ChildProcess()

virtual wibble::sys::ChildProcess::~ChildProcess | ( |  | ) |

inline virtual

## Member Function Documentation

## exitStatus()

int wibble::sys::ChildProcess::exitStatus | ( |  | ) |

Referenced by pid().

## fork()

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 wibble::sys::flockfile(), forkAndRedirect(), TestChildprocess::kill(), wibble::sys::PipeThrough::run(), and setExec().

## forkAndRedirect()

pid_t wibble::sys::ChildProcess::forkAndRedirect | ( | int * | stdinfd = `0`,
|  | int * | stdoutfd = `0`,
|  | int * | stderrfd = `0`
| ) |  |

inline

References fork(), and setupRedirects().

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

## kill()

void wibble::sys::ChildProcess::kill | ( | int | signal | ) |

Send the given signal to the process.

References assert_die.

Referenced by TestChildprocess::kill(), and pid().

## main()

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

protected pure virtual

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

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

## pid()

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, exitStatus(), kill(), running(), wait(), and waitForSuccess().

## running()

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

References assert_die.

Referenced by pid().

## setExec()

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 fork(), and setupRedirects().

## setupChild()

void wibble::sys::ChildProcess::setupChild | ( |  | ) |

protected

References wibble::sys::renamefd().

## setupParent()

void wibble::sys::ChildProcess::setupParent | ( |  | ) |

protected

References wibble::sys::funlockfile().

## setupPipes()

void wibble::sys::ChildProcess::setupPipes | ( |  | ) |

protected

## setupPrefork()

void wibble::sys::ChildProcess::setupPrefork | ( |  | ) |

protected

## setupRedirects()

void wibble::sys::ChildProcess::setupRedirects | ( | int * | stdinfd = `0`,
|  | int * | stdoutfd = `0`,
|  | int * | stderrfd = `0`
| ) |  |

References wibble::sys::mkpipe().

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

## spawnChild()

void wibble::sys::ChildProcess::spawnChild | ( |  | ) |

protected virtual

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.

## wait()

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(), pid(), TestChildprocess::redirect(), and TestChildprocess::shellCommand().

## waitError()

void wibble::sys::ChildProcess::waitError | ( |  | ) |

protected

## waitForSuccess()

void wibble::sys::ChildProcess::waitForSuccess | ( |  | ) |

References wibble::str::fmtf().

Referenced by pid().

## Member Data Documentation

## _pid

pid_t wibble::sys::ChildProcess::_pid

protected

Referenced by pid().

## _stderr

int * wibble::sys::ChildProcess::_stderr

protected

## _stdin

int* wibble::sys::ChildProcess::_stdin

protected

## _stdout

int * wibble::sys::ChildProcess::_stdout

protected

## m_command

std::string wibble::sys::ChildProcess::m_command

protected

## m_doExec

bool wibble::sys::ChildProcess::m_doExec

protected

## m_status

int wibble::sys::ChildProcess::m_status

protected

## pipes

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

protected

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

- childprocess.h
- childprocess.cpp