childprocess.h
Go to the documentation of this file.
// -*- C++ -*-
#ifndef WIBBLE_SYS_CHILDPROCESS_H
#define WIBBLE_SYS_CHILDPROCESS_H
/*
* OO base class for process functions and child processes
*
* Copyright (C) 2003--2006 Enrico Zini <enrico@debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <wibble/exception.h>
#include <wibble/sys/macros.h>
#ifdef _WIN32
#include <windows.h>
#endif
struct rusage;
namespace wibble {
namespace sys {
class ChildProcess
{
protected:
pid_t _pid;
int pipes[3][2];
int *_stdin, *_stdout, *_stderr;
int m_status;
bool m_doExec;
std::string m_command;
#ifdef _WIN32
int backups[3];
STARTUPINFO si;
PROCESS_INFORMATION pi;
#endif
// TODO: since the destructor is called twice (one in the parent and one in
// the child), it could be useful to add a bool isChild() method to let the
// destructor and other functions know where they are operating. The value
// returned can be set by ChildProcess::fork.
virtual int main() = 0;
virtual void spawnChild();
void waitError();
void setupPipes();
void setupPrefork();
void setupChild();
void setupParent();
public:
ChildProcess() : _pid(-1), _stdin( 0 ), _stdout( 0 ), _stderr( 0 ) {}
virtual ~ChildProcess() {}
void setExec( std::string command ) {
m_doExec = true;
m_command = command;
}
pid_t fork();
void setupRedirects(int* stdinfd = 0, int* stdoutfd = 0, int* stderrfd = 0);
pid_t forkAndRedirect(int* stdinfd = 0, int* stdoutfd = 0, int* stderrfd = 0) {
setupRedirects(stdinfd, stdoutfd, stderrfd);
return fork();
}
pid_t pid() const { return _pid; }
bool running();
int exitStatus();
void waitForSuccess();
int wait(struct rusage* ru = 0);
void kill(int signal);
};
}
}
// vim:set ts=4 sw=4:
#endif
wibble::sys::ChildProcess::setupParent
void setupParent()
Definition: childprocess.cpp:216
wibble::sys::ChildProcess::pipes
int pipes[3][2]
Definition: childprocess.h:46
wibble::sys::ChildProcess
Fork a child process.
Definition: childprocess.h:42
wibble::sys::ChildProcess::setExec
void setExec(std::string command)
Instead of calling the main() function of this class, execute an external command.
Definition: childprocess.h:94
wibble::sys::ChildProcess::waitForSuccess
void waitForSuccess()
Definition: childprocess.cpp:287
wibble::sys::ChildProcess::setupPipes
void setupPipes()
wibble::sys::ChildProcess::ChildProcess
ChildProcess()
Definition: childprocess.h:88
wibble::sys::ChildProcess::m_command
std::string m_command
Definition: childprocess.h:51
exception.h
macros.h
wibble::sys::ChildProcess::_stderr
int * _stderr
Definition: childprocess.h:48
wibble::sys::ChildProcess::wait
int wait(struct rusage *ru=0)
Wait for the child to finish, returning its exit status and optionally storing resource usage informa...
Definition: childprocess.cpp:270
wibble::sys::ChildProcess::forkAndRedirect
pid_t forkAndRedirect(int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
Definition: childprocess.h:107
wibble::sys::ChildProcess::m_doExec
bool m_doExec
Definition: childprocess.h:50
wibble::sys::ChildProcess::kill
void kill(int signal)
Send the given signal to the process.
Definition: childprocess.cpp:310
wibble::sys::ChildProcess::waitError
void waitError()
Definition: childprocess.cpp:240
wibble::sys::ChildProcess::m_status
int m_status
Definition: childprocess.h:49
wibble::sys::ChildProcess::fork
pid_t fork()
For a subprocess to run proc.
wibble::sys::ChildProcess::exitStatus
int exitStatus()
wibble::sys::ChildProcess::spawnChild
virtual void spawnChild()
On Windows, it&#39;s impossible to fork(), but if you were to fork+exec, it&#39;s not all lost...
Definition: childprocess.cpp:48
wibble::sys::ChildProcess::~ChildProcess
virtual ~ChildProcess()
Definition: childprocess.h:89
wibble::sys::ChildProcess::setupChild
void setupChild()
Definition: childprocess.cpp:187
wibble::sys::ChildProcess::setupRedirects
void setupRedirects(int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
Definition: childprocess.cpp:145
wibble
Definition: amorph.h:17
wibble::sys::ChildProcess::_pid
pid_t _pid
Definition: childprocess.h:45
wibble::sys::ChildProcess::pid
pid_t pid() const
Get the pid of the child process or (pid_t)-1 if no child is running.
Definition: childprocess.h:119
wibble::sys::ChildProcess::_stdin
int * _stdin
Definition: childprocess.h:48
wibble::sys::ChildProcess::setupPrefork
void setupPrefork()
Definition: childprocess.cpp:164
wibble::sys::ChildProcess::running
bool running()
Definition: childprocess.cpp:247
wibble::sys::ChildProcess::main
virtual int main()=0
Main function to be called in the child process after it has forked.
wibble::sys::ChildProcess::_stdout
int * _stdout
Definition: childprocess.h:48