00001 /* 00002 * BEEN: Benchmarking Environment 00003 * ============================== 00004 * 00005 * File author: Branislav Repcek 00006 * 00007 * GNU Lesser General Public License Version 2.1 00008 * --------------------------------------------- 00009 * Copyright (C) 2004-2006 Distributed Systems Research Group, 00010 * Faculty of Mathematics and Physics, Charles University in Prague 00011 * 00012 * This library is free software; you can redistribute it and/or 00013 * modify it under the terms of the GNU Lesser General Public 00014 * License version 2.1, as published by the Free Software Foundation. 00015 * 00016 * This library is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 * Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with this library; if not, write to the Free Software 00023 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00024 * MA 02111-1307 USA 00025 */ 00026 00034 #ifndef FORK_AND_PIPE_INCLUDED 00035 #define FORK_AND_PIPE_INCLUDED 00036 00037 #include <sys/types.h> 00038 #include <sys/wait.h> 00039 #include <unistd.h> 00040 #include <stdio.h> 00041 #include "../common/UnicodeString.h" 00042 #include "../common/MessageReporter.h" 00043 00044 namespace hwdet 00045 { 00048 class ForkAndPipe : public MessageReporter 00049 { 00050 public: 00052 ForkAndPipe(const String &rep_name, MessageReporter *parent_reporter = NULL); 00053 00055 virtual ~ForkAndPipe(void); 00056 00058 bool Run(void); 00059 00067 virtual void ParentProcess(pid_t child_pid, FILE *file) = 0; 00068 00074 virtual void ChildProcess(void) = 0; 00075 00077 virtual void OnError(int error_code, const String &message); 00078 00079 protected: 00080 00085 pid_t GetChildPID(void) const 00086 { 00087 return pid; 00088 } 00089 00096 void WaitForChildProcess(void) 00097 { 00098 while (wait((int *) 0) != pid) ; 00099 } 00100 00106 int GetForkError(void) const 00107 { 00108 return error_num; 00109 } 00110 00112 bool AutoError(int result, const String &message = ""); 00113 00114 private: 00116 int pipe_desc[2]; 00117 00119 pid_t pid; 00120 00122 int error_num; 00123 00125 ForkAndPipe(const ForkAndPipe &) : MessageReporter(TXT(""), NULL) { } 00126 00128 ForkAndPipe &operator =(const ForkAndPipe &); 00129 }; 00130 } 00131 00132 #endif