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 00043 // TODO: since the destructor is called twice (one in the parent and one in 00044 // the child), it could be useful to add a bool isChild() method to let the 00045 // destructor and other functions know where they are operating. The value 00046 // returned can be set by ChildProcess::fork. 00047 virtual int main() = 0; 00048 00049 public: 00050 ChildProcess() : _pid(-1) {} 00051 virtual ~ChildProcess() {} 00052 00054 pid_t fork(); 00055 00059 pid_t forkAndRedirect(int* stdinfd = 0, int* stdoutfd = 0, int* stderrfd = 0); 00060 00062 pid_t pid() const { return _pid; } 00063 00067 int wait(); 00068 00072 int wait(struct rusage* ru); 00073 00075 void kill(int signal); 00076 }; 00077 00078 } 00079 } 00080 00081 // vim:set ts=4 sw=4: 00082 #endif