wibble  0.1.28
childprocess.h
Go to the documentation of this file.
1 #ifndef WIBBLE_SYS_CHILDPROCESS_H
2 #define WIBBLE_SYS_CHILDPROCESS_H
3 
4 /*
5  * OO base class for process functions and child processes
6  *
7  * Copyright (C) 2003--2006 Enrico Zini <enrico@debian.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <sys/types.h>
25 #include <wibble/exception.h>
26 #include <wibble/sys/macros.h>
27 
28 struct rusage;
29 
30 namespace wibble {
31 namespace sys {
32 
37 {
38 protected:
39  pid_t _pid;
40  int m_status;
41  void waitError();
42 
46  // TODO: since the destructor is called twice (one in the parent and one in
47  // the child), it could be useful to add a bool isChild() method to let the
48  // destructor and other functions know where they are operating. The value
49  // returned can be set by ChildProcess::fork.
50  virtual int main() = 0;
51 
52 public:
53  ChildProcess() : _pid(-1) {}
54  virtual ~ChildProcess() {}
55 
57  pid_t fork();
58 
62  pid_t forkAndRedirect(int* stdinfd = 0, int* stdoutfd = 0, int* stderrfd = 0);
63 
71  pid_t pid() const { return _pid; }
72 
76  int wait();
77 
78  bool running();
79  int exitStatus();
80  void waitForSuccess();
81 
85  int wait(struct rusage* ru);
86 
88  void kill(int signal);
89 };
90 
91 }
92 }
93 
94 // vim:set ts=4 sw=4:
95 #endif