00001 #ifndef WIBBLE_SYS_CHILDPROCESS_H 00002 #define WIBBLE_SYS_CHILDPROCESS_H 00003 00004 /* 00005 * OO base class for process functions and child processes 00006 * 00007 * Copyright (C) 2003--2006 Enrico Zini <enrico@debian.org> 00008 * 00009 * This library is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with this library; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 */ 00023 00024 #include <sys/types.h> 00025 #include <wibble/exception.h> 00026 00027 struct rusage; 00028 00029 namespace wibble { 00030 namespace sys { 00031 00035 class ChildProcess 00036 { 00037 protected: 00038 pid_t _pid; 00039 int m_status; 00040 void waitError(); 00041 00045 // TODO: since the destructor is called twice (one in the parent and one in 00046 // the child), it could be useful to add a bool isChild() method to let the 00047 // destructor and other functions know where they are operating. The value 00048 // returned can be set by ChildProcess::fork. 00049 virtual int main() = 0; 00050 00051 public: 00052 ChildProcess() : _pid(-1) {} 00053 virtual ~ChildProcess() {} 00054 00056 pid_t fork(); 00057 00061 pid_t forkAndRedirect(int* stdinfd = 0, int* stdoutfd = 0, int* stderrfd = 0); 00062 00064 pid_t pid() const { return _pid; } 00065 00069 int wait(); 00070 00071 bool running(); 00072 int exitStatus(); 00073 void waitForSuccess(); 00074 00078 int wait(struct rusage* ru); 00079 00081 void kill(int signal); 00082 }; 00083 00084 } 00085 } 00086 00087 // vim:set ts=4 sw=4: 00088 #endif