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 
34 struct rusage;
35 
36 namespace wibble {
37 namespace sys {
38 
43 {
44 protected:
45  pid_t _pid;
46  int pipes[3][2];
47 
49  int m_status;
50  bool m_doExec;
51  std::string m_command;
52 
53 #ifdef _WIN32
54  int backups[3];
55  STARTUPINFO si;
56  PROCESS_INFORMATION pi;
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 
87 public:
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 
103  pid_t fork();
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) {
108  setupRedirects(stdinfd, stdoutfd, stderrfd);
109  return fork();
110  }
111 
119  pid_t pid() const { return _pid; }
120 
121  bool running();
122  int exitStatus();
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
wibble::sys::ChildProcess::_stdout
int * _stdout
Definition: childprocess.h:48
wibble::sys::mkpipe
void mkpipe(int *fds, int *infd, int *outfd, const char *err)
Definition: childprocess.cpp:123
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::setupPipes
void setupPipes()
wibble::sys::funlockfile
void funlockfile(FILE *)
Definition: childprocess.cpp:53
wibble::sys::ChildProcess::waitForSuccess
void waitForSuccess()
Definition: childprocess.cpp:287
wibble::sys::ChildProcess::pipes
int pipes[3][2]
Definition: childprocess.h:46
macros.h
wibble::sys::ChildProcess::setupParent
void setupParent()
Definition: childprocess.cpp:216
wibble::sys::flockfile
void flockfile(FILE *)
Definition: childprocess.cpp:54
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
assert_die
#define assert_die()
Definition: test.h:52
wibble::sys::ChildProcess::ChildProcess
ChildProcess()
Definition: childprocess.h:88
wibble::sys::ChildProcess::waitError
void waitError()
Definition: childprocess.cpp:240
main
int main(int argc, char **argv)
Definition: test-main.h:257
wibble::sys::ChildProcess::m_command
std::string m_command
Definition: childprocess.h:51
wibble::sys::ChildProcess::kill
void kill(int signal)
Send the given signal to the process.
Definition: childprocess.cpp:310
childprocess.h
wibble::sys::ChildProcess::spawnChild
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
signal.h
wibble::sys::ChildProcess::~ChildProcess
virtual ~ChildProcess()
Definition: childprocess.h:89
wibble::sys::ChildProcess::setupChild
void setupChild()
Definition: childprocess.cpp:187
wibble::sys::ChildProcess
Fork a child process.
Definition: childprocess.h:42
wibble::sys::ChildProcess::_stdin
int * _stdin
Definition: childprocess.h:48
wibble::sys::ChildProcess::m_doExec
bool m_doExec
Definition: childprocess.h:50
wibble::exception::System
Base class for system exceptions.
Definition: exception.h:396
wibble::exception::Generic
Base class for all exceptions.
Definition: exception.h:179
wibble::sys::ChildProcess::setupRedirects
void setupRedirects(int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
Definition: childprocess.cpp:145
wibble::sys::ChildProcess::fork
pid_t fork()
For a subprocess to run proc.
wibble::sys::ChildProcess::running
bool running()
Definition: childprocess.cpp:247
wibble::sys::ChildProcess::m_status
int m_status
Definition: childprocess.h:49
wibble::sys::renamefd
void renamefd(int _old, int _new, const char *err="...")
Definition: childprocess.cpp:137
wibble
Definition: amorph.h:17
wibble::sys::ChildProcess::_pid
pid_t _pid
Definition: childprocess.h:45
exception.h
wibble::sys::ChildProcess::exitStatus
int exitStatus()
wibble::str::fmtf
std::string fmtf(const char *f,...)
Definition: string.cpp:113
wibble::sys::ChildProcess::setupPrefork
void setupPrefork()
Definition: childprocess.cpp:164
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::exception::Interrupted
Exception thrown when some long event is interrupted by an external event (like a system call being i...
Definition: exception.h:224
wibble::exception::Consistency
Exception thrown when some consistency check fails.
Definition: exception.h:254
wibble::sys::ChildProcess::main
virtual int main()=0
Main function to be called in the child process after it has forked.
wibble::exception
Definition: core.h:11