| 1 |
/*******************************************************************\ |
| 2 |
|
| 3 |
Copyright (C) 2004 Joseph Coffland |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License |
| 7 |
as published by the Free Software Foundation; either version 2 |
| 8 |
of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program; if not, write to the Free Software |
| 17 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 18 |
02111-1307, USA. |
| 19 |
|
| 20 |
For information regarding this software email |
| 21 |
jcofflan@users.sourceforge.net |
| 22 |
|
| 23 |
\*******************************************************************/ |
| 24 |
#ifndef PROCESS_H |
| 25 |
#define PROCESS_H |
| 26 |
|
| 27 |
#include <iostream> |
| 28 |
#include <string> |
| 29 |
#include <vector> |
| 30 |
#include <list> |
| 31 |
|
| 32 |
#include <protomol/debug/Pipe.h> |
| 33 |
|
| 34 |
namespace ProtoMol { |
| 35 |
|
| 36 |
class ProcessFunctor { |
| 37 |
public: |
| 38 |
virtual ~ProcessFunctor() {} |
| 39 |
virtual void child() = 0; |
| 40 |
virtual void parent() = 0; |
| 41 |
}; |
| 42 |
|
| 43 |
class PipeProcessFunctor : public ProcessFunctor { |
| 44 |
Pipe pipe; |
| 45 |
int direction; |
| 46 |
int fd; |
| 47 |
public: |
| 48 |
|
| 49 |
PipeProcessFunctor(int direction, int fd) : direction(direction), fd(fd) {} |
| 50 |
virtual ~PipeProcessFunctor() {} |
| 51 |
|
| 52 |
virtual void child(); |
| 53 |
virtual void parent(); |
| 54 |
|
| 55 |
Pipe *getPipe() {return &pipe;} |
| 56 |
}; |
| 57 |
|
| 58 |
class FDReplaceProcessFunctor : public ProcessFunctor { |
| 59 |
int fd; |
| 60 |
int replacement; |
| 61 |
|
| 62 |
public: |
| 63 |
FDReplaceProcessFunctor(int fd, int replacement) : |
| 64 |
fd(fd), replacement(replacement) {} |
| 65 |
virtual ~FDReplaceProcessFunctor() {} |
| 66 |
|
| 67 |
virtual void child(); |
| 68 |
virtual void parent() {} |
| 69 |
}; |
| 70 |
|
| 71 |
class Process { |
| 72 |
typedef std::vector<ProcessFunctor *> functors_t; |
| 73 |
functors_t functors; |
| 74 |
|
| 75 |
int pid; |
| 76 |
bool running; |
| 77 |
int returnCode; |
| 78 |
|
| 79 |
public: |
| 80 |
Process(); |
| 81 |
~Process(); |
| 82 |
|
| 83 |
typedef enum {TO_CHILD, FROM_CHILD} dir_t; |
| 84 |
|
| 85 |
void exec(std::list<std::string> &args); |
| 86 |
void exec(const char *args); |
| 87 |
void exec(char *args); |
| 88 |
void exec(char *argv[]); |
| 89 |
|
| 90 |
static void parseArgs(char *args, int &argc, char *argv[], int n); |
| 91 |
|
| 92 |
Pipe *getChildPipe(dir_t dir, int childFD = -1); |
| 93 |
void replaceChildFD(int fd, int replacement); |
| 94 |
|
| 95 |
int getPID() {return pid;} |
| 96 |
void kill(int sig); |
| 97 |
int wait(int options = 0); |
| 98 |
|
| 99 |
bool isRunning(); |
| 100 |
int getReturnCode() {return returnCode;} |
| 101 |
}; |
| 102 |
} |
| 103 |
#endif // PROCESS_H |