wibble 1.1
childprocess.h
Go to the documentation of this file.
1// -*- C++ -*-
2
3#ifndef WIBBLE_SYS_CHILDPROCESS_H
4#define WIBBLE_SYS_CHILDPROCESS_H
5
6/*
7 * OO base class for process functions and child processes
8 *
9 * Copyright (C) 2003--2006 Enrico Zini <enrico@debian.org>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26#include <sys/types.h>
27#include <wibble/exception.h>
28#include <wibble/sys/macros.h>
29
30#ifdef _WIN32
31#include <windows.h>
32#endif
33
34struct rusage;
35
36namespace wibble {
37namespace sys {
38
43{
44protected:
46 int pipes[3][2];
47
51 std::string m_command;
52
53#ifdef _WIN32
54 int backups[3];
57#endif
58
62 // TODO: since the destructor is called twice (one in the parent and one in
63 // the child), it could be useful to add a bool isChild() method to let the
64 // destructor and other functions know where they are operating. The value
65 // returned can be set by ChildProcess::fork.
66
67 virtual int main() = 0;
68
79 virtual void spawnChild();
80
81 void waitError();
82 void setupPipes();
83 void setupPrefork();
84 void setupChild();
85 void setupParent();
86
87public:
88 ChildProcess() : _pid(-1), _stdin( 0 ), _stdout( 0 ), _stderr( 0 ) {}
89 virtual ~ChildProcess() {}
90
94 void setExec( std::string command ) {
95 m_doExec = true;
96 m_command = command;
97 }
98
104
105 void setupRedirects(int* stdinfd = 0, int* stdoutfd = 0, int* stderrfd = 0);
106
107 pid_t forkAndRedirect(int* stdinfd = 0, int* stdoutfd = 0, int* stderrfd = 0) {
109 return fork();
110 }
111
119 pid_t pid() const { return _pid; }
120
121 bool running();
123 void waitForSuccess();
124
128 int wait(struct rusage* ru = 0);
129
131 void kill(int signal);
132};
133
134}
135}
136
137// vim:set ts=4 sw=4:
138#endif
Fork a child process.
Definition childprocess.h:43
ChildProcess()
Definition childprocess.h:88
void setupChild()
Definition childprocess.cpp:187
void setExec(std::string command)
Instead of calling the main() function of this class, execute an external command.
Definition childprocess.h:94
bool running()
Definition childprocess.cpp:247
void setupPrefork()
Definition childprocess.cpp:164
virtual void spawnChild()
On Windows, it's impossible to fork(), but if you were to fork+exec, it's not all lost.
Definition childprocess.cpp:48
pid_t fork()
For a subprocess to run proc.
void setupParent()
Definition childprocess.cpp:216
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
void waitError()
Definition childprocess.cpp:240
int * _stderr
Definition childprocess.h:48
void kill(int signal)
Send the given signal to the process.
Definition childprocess.cpp:310
pid_t pid() const
Get the pid of the child process or (pid_t)-1 if no child is running.
Definition childprocess.h:119
void setupRedirects(int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
Definition childprocess.cpp:145
pid_t forkAndRedirect(int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
Definition childprocess.h:107
int pipes[3][2]
Definition childprocess.h:46
bool m_doExec
Definition childprocess.h:50
std::string m_command
Definition childprocess.h:51
int * _stdout
Definition childprocess.h:48
virtual int main()=0
Main function to be called in the child process after it has forked.
int * _stdin
Definition childprocess.h:48
void waitForSuccess()
Definition childprocess.cpp:287
virtual ~ChildProcess()
Definition childprocess.h:89
pid_t _pid
Definition childprocess.h:45
int m_status
Definition childprocess.h:49
Definition amorph.h:17
Definition amorph.h:30