Changeset 22177
- Timestamp:
- 2010-05-26T23:52:09+12:00 (14 years ago)
- Location:
- main/trunk/greenstone2/runtime-src/src/recpt
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/runtime-src/src/recpt/os_process.cpp
r22173 r22177 26 26 #include "os_process.h" 27 27 28 osprocess::osprocess(char* cmd)29 {}30 28 osprocess::osprocess(char* cmdline, OSProcessPipeMode mode) 31 29 : mode_(mode) … … 36 34 } 37 35 38 /* 39 bool osprocess::eop() 36 bool osprocess::close() 40 37 { 38 // close any file handles that are still open 39 40 bool write_close_rv = close_write_pipe(withoutWarning); 41 bool read_close_rv = close_read_pipe(withoutWarning); 42 43 return (write_close_rv && read_close_rv); 41 44 } 42 45 43 bool osprocess::eop(OSProcessPipeMode) 46 47 48 49 #if 1 50 51 #include "os_process_windows.h" 52 #include "os_process_unix.h" 53 #include <iostream> 54 using namespace std; 55 56 void test_uni_write_pipe() 44 57 { 58 #ifdef __WIN32__ 59 char* prog_name = "c:\\Windows\\system32\\java.exe"; 60 char* cmd_line = "java"; 61 62 //char* input_line = "echo hi there\n"; 63 //char* prog_name = "c:\\cygwin\\bin\\cat.exe"; 64 //char* cmd_line = "cat"; 65 #else 66 char* cmd_line = "/usr/bin/java"; 67 //char* cmd_line = "java"; 68 #endif 69 70 #ifdef __WIN32__ 71 osprocesswindows* process = new osprocesswindows(cmd_line,uniRead); 72 #else 73 osprocessunix* process = new osprocessunix(cmd_line,uniRead); 74 #endif 75 76 const int BufferSize = 1024; 77 char buffer[BufferSize]; 78 79 int bytes_read = 0; 80 do { 81 bytes_read = process->read(buffer,BufferSize); 82 if (bytes_read>0) { 83 fwrite(buffer,1,bytes_read,stdout); 84 } 85 } while (bytes_read==BufferSize); 86 87 88 delete process; 89 45 90 } 46 */47 91 48 int osprocess::read(char* buffer, const int buffer_len)92 int main() 49 93 { 94 printf("Content-type: text/html\n\n"); 95 96 printf( "<html>\n"); 97 printf( " <head>\n"); 98 printf( " <title>Testing</title>\n"); 99 printf( " </head>\n"); 100 printf( " <body>\n"); 101 102 test_uni_write_pipe(); 103 104 printf( " </body>\n"); 105 printf( "</html>\n"); 106 50 107 return 0; 51 108 } 52 109 53 int osprocess::write(char* buffer, const int buffer_len) 54 { 55 return 0; 56 } 57 58 bool osprocess::close() 59 { 60 return true; 61 } 110 #endif 62 111 63 bool osprocess::close(OSProcessPipeMode mode)64 {65 return true;66 }67 -
main/trunk/greenstone2/runtime-src/src/recpt/os_process.h
r22173 r22177 33 33 #define OS_PROCESS_H 34 34 35 35 #include <stdlib.h> 36 36 37 enum OSProcessPipeMode { uniRead=0, uniWrite=1, biReadWrite=2, noPipe=3 }; 37 enum OSProcessPipeMode { uniRead=0, uniWrite=1, biReadWrite=2 }; 38 enum OSProcessWarnStatus { withoutWarning, withWarning }; 38 39 39 40 class osprocess … … 41 42 42 43 public: 43 osprocess(char* cmdline);44 44 osprocess(char* cmdline, OSProcessPipeMode mode); 45 45 virtual ~osprocess(); 46 46 47 /* 48 virtual bool eop(); // end of pipe 49 virtual bool eop(OSProcessPipeMode); // end of pipe, for designated pipe 47 /* one day might want these 48 virtual bool end_of_pipe(); 49 virtual bool end_of_read_pipe(); 50 virtual bool end_of_write_pipe(); 50 51 */ 51 52 52 virtual int write(char* buffer, const int buffer_len) ;53 virtual int read(char* buffer, const int buffer_len) ;53 virtual int write(char* buffer, const int buffer_len)=0; 54 virtual int read(char* buffer, const int buffer_len)=0; 54 55 56 virtual bool close_write_pipe(OSProcessWarnStatus warn_status=withWarning)=0; 57 virtual bool close_read_pipe(OSProcessWarnStatus warn_status=withWarning)=0; 55 58 virtual bool close(); 56 virtual bool close(OSProcessPipeMode mode); 59 57 60 58 61 protected: 59 62 OSProcessPipeMode mode_; 60 bool read_pipe_open_;61 bool write_pipe_open_;62 63 63 64 }; -
main/trunk/greenstone2/runtime-src/src/recpt/os_process_unix.cpp
r22174 r22177 41 41 #include "os_process_unix.h" 42 42 43 #define READ_PIPE_INDEX 044 #define WRITE_PIPE_INDEX 145 46 #define FD_STDIN 047 #define FD_STDOUT 148 #define FD_STDERR 249 50 osprocessunix::osprocessunix(char* cmdline)51 : osprocess(cmdline)52 {}53 54 43 osprocessunix::osprocessunix(char* cmdline, OSProcessPipeMode mode) 55 44 : child_stdout_read_(-1), child_stdin_write_(-1), … … 81 70 82 71 83 pid_ t pid= fork();84 if (pid < 0) {72 pid_ = fork(); 73 if (pid_ < 0) { 85 74 cerr << "osprocessunix::osprocessunix(): Failed to create child process" << endl; 86 75 return; 87 76 } 88 77 89 if (pid == 0) {78 if (pid_ == 0) { 90 79 // Child process 91 80 92 81 // Sort out input pipe 93 82 // child has no buisness accessing write end of input pipe => close 94 ::close(child_stdin_write_); 83 ::close(child_stdin_write_); 84 child_stdin_write_=-1; 95 85 96 86 if ((mode == uniWrite) || (mode == biReadWrite)) { … … 99 89 } 100 90 else { 101 // noPipe or (parent is doing) uniRead (which means we're not interested102 // in the child reading any input from the parent )91 // Parent is doing uniRead, which means we're not interested 92 // in the child reading any input from the parent 103 93 // => child input remains coming from stdin 104 94 ::close(child_stdin_read); … … 108 98 // child has no buisness accessing read end of output pipe => close 109 99 ::close(child_stdout_read_); 100 child_stdout_read_=-1; 101 110 102 if ((mode == uniRead) || (mode == biReadWrite)) { 111 103 // wire up child's stdout write so it is send down the pipe to the parent … … 113 105 } 114 106 else { 115 // noPipe or (parent is doing) uniWrite (which means we're not interested116 // in any output produced by the child process )107 // Parent is doing uniWrite, which means we're not interested 108 // in any output produced by the child process 117 109 // => child output remains going to stdout 118 110 ::close(child_stdout_write); 119 111 } 120 112 121 /* 122 // execve ... 123 int rv = CreateProcess(NULL, // no application name 124 cmdline, 125 NULL, 126 NULL, // no process or thread security attribues 127 TRUE, // Inherit handles 128 0, // Creation flag 129 NULL, // No environment block 130 ".", // current working directory 131 &si, 132 &pi_); // process info filled out as a result 133 if (!rv) { 134 fprintf(stderr,"os_process_windows(): Error creating process"); 135 } 136 */ 113 114 // execvp? 115 cerr << "Away to execve: " << cmdline << endl; 116 117 execve(cmdline, NULL, NULL); 137 118 138 119 … … 154 135 case uniRead: 155 136 ::close(child_stdin_write_); 137 child_stdin_write_ = -1; 156 138 break; 157 139 case uniWrite: 158 140 ::close(child_stdout_read_); 141 child_stdout_read_ = -1; 159 142 break; 160 143 case biReadWrite: … … 162 145 // the pipes are set up just the way we want them 163 146 break; 164 case noPipe:165 ::close(child_stdin_write_);166 ::close(child_stdout_read_);167 break;168 147 } 169 148 } … … 171 150 172 151 osprocessunix::~osprocessunix() 173 {} 174 175 /* 176 bool osprocessunix::eop() 177 {} 178 179 bool osprocessunix::eop(OSProcessPipeMode) 180 {} 181 */ 152 { 153 // close any file handles that are still open 154 close(); 155 } 156 182 157 183 158 int osprocessunix::write(char* buffer, const int buffer_len) … … 206 181 207 182 208 bool osprocessunix::close() 209 { 210 return true; 211 } 212 213 bool osprocessunix::close(OSProcessPipeMode mode) 214 { 215 return true; 216 } 183 bool osprocessunix::close_write_pipe(OSProcessWarnStatus warn_status) 184 { 185 int write_close_rv = 0; 186 187 if (child_stdin_write_ != -1) { 188 write_close_rv = ::close(child_stdin_write_); 189 child_stdin_write_ = -1; 190 } 191 else if (warn_status = withWarning) { 192 cerr << "osprocessunix::close_write_pipe(): Warning - Tried to close already closed pipe" << endl; 193 } 194 195 return (write_close_rv==0); 196 } 197 198 199 bool osprocessunix::close_read_pipe(OSProcessWarnStatus warn_status) 200 { 201 int read_close_rv = 0; 202 203 204 if (child_stdout_read_ != -1) { 205 read_close_rv = ::close(child_stdout_read_); 206 child_stdout_read_ = -1; 207 } 208 else if (warn_status == withWarning) { 209 cerr << "osprocessunix::close_read_pipe(): Warning - Tried to close already closed pipe" << endl; 210 } 211 212 return (read_close_rv==0); 213 } 214 215 216 217 217 218 218 -
main/trunk/greenstone2/runtime-src/src/recpt/os_process_unix.h
r22174 r22177 38 38 39 39 public: 40 osprocessunix(char* cmd);41 40 osprocessunix(char* cmd, OSProcessPipeMode mode); 42 41 virtual ~osprocessunix(); 43 42 44 /*45 virtual bool eop(); // end of pipe46 virtual bool eop(OSProcessPipeMode); // end of pipe, for designated pipe47 */48 49 43 virtual int read(char* buffer, const int buffer_len); 50 44 virtual int write(char* buffer, const int buffer_len); 51 45 52 virtual bool close ();53 virtual bool close (OSProcessPipeMode mode);46 virtual bool close_write_pipe(OSProcessWarnStatus warn_status=withWarning); 47 virtual bool close_read_pipe(OSProcessWarnStatus warn_status=withWarning); 54 48 55 49 protected: 50 51 static const int READ_PIPE_INDEX=0; 52 static const int WRITE_PIPE_INDEX=1; 53 54 static const int FD_STDIN=0; 55 static const int FD_STDOUT=1; 56 static const int FD_STDERR=2; 57 58 pid_t pid_; 59 56 60 int child_stdin_write_; 57 61 int child_stdout_read_; -
main/trunk/greenstone2/runtime-src/src/recpt/os_process_windows.cpp
r22173 r22177 130 130 osprocesswindows::~osprocesswindows() 131 131 { 132 // close any file handles that are still open 133 close(); 134 132 135 // Close process and thread handles 133 136 CloseHandle( pi_.hProcess ); … … 137 140 pi_.hThread = NULL; 138 141 } 139 140 141 /*142 bool osprocesswindows::eop()143 {144 }145 146 bool osprocesswindows::eop(OSProcessPipeMode)147 {148 }149 */150 142 151 143 … … 178 170 179 171 180 bool osprocesswindows::close() { 181 return true; 182 } 183 184 bool osprocesswindows::close(OSProcessPipeMode mode) 185 { 186 // Close the pipe handle so the child process stops reading. 187 188 if (!CloseHandle(child_stdin_write_)) { 189 cerr << "osprocesswindows::close(): Failed to close stdin write handle on pipe to child process" << endl; 190 return false; 191 } 192 193 if (!CloseHandle(child_stdout_read_)) { 194 cerr << "osprocesswindows::close(): Failed to close handle stdout read on pipe to chlild process" << endl; 195 return false; 196 } 197 198 199 return true; 200 } 201 202 203 204 205 206 //#ifdef TEST_OS_PROCESS_WINDOWS 207 #if 0 208 209 int main () 210 { 211 char* input_line = NULL; 212 char* prog_name = "c:\\Windows\\system32\\java.exe"; 213 char* cmd_line = "java"; 214 215 //char* input_line = "echo hi there\n"; 216 //char* prog_name = "c:\\cygwin\\bin\\cat.exe"; 217 //char* cmd_line = "cat"; 218 219 osprocesswindows osprocess(cmd_line,uniRead); 220 221 printf("Content-type: text/html\n\n"); 222 223 printf( "<html>\n"); 224 printf( " <head>\n"); 225 printf( " <title>Testing</title>\n"); 226 printf( " </head>\n"); 227 printf( " <body>\n"); 228 229 230 const int BufferSize = 1024; 231 char buffer[BufferSize]; 232 233 int bytes_read = 0; 234 do { 235 bytes_read = osprocess.read(buffer,BufferSize); 236 if (bytes_read>0) { 237 fwrite(buffer,1,bytes_read,stdout); 238 } 239 } while (bytes_read==BufferSize); 240 241 242 printf( " </body>\n"); 243 printf( "</html>\n"); 244 245 return 0; 246 } 172 173 174 bool osprocessunix::close_write_pipe(OSProcessWarnStatus warn_status) 175 { 176 bool write_close_ok = true; 177 178 if (child_stdout_read_ != NULL) { 179 180 write_close_ok = CloseHandle(child_stdin_write_); 181 182 if (write_close_ok) { 183 child_stdin_write_ = NULL; 184 } 185 else { 186 // not OK 187 cerr << "osprocesswindows::close(): Error - Failed to close stdin write handle on pipe to child process" << endl; 188 } 189 } 190 else if (warning_status == withWarning) { 191 cerr << "osprocesswindows::close_write_pipe(): Warning - Tried to close already closed pipe" << endl; 192 } 193 194 return write_close_ok; 195 } 196 197 198 bool osprocessunix::close_read_pipe(OSProcessWarnStatus warn_status) 199 { 200 bool read_close_ok = true; 201 202 if (child_stdout_read_ != NULL) { 203 204 read_close_ok = CloseHandle(child_stdout_read_); 205 206 if (read_close_ok) { 207 child_stdout_read_ = NULL; 208 } 209 else { 210 cerr << "osprocesswindows::close(): Error - Failed to close handle stdout read on pipe to chlild process" << endl; 211 } 212 } 213 else if (warn_status == withWarning) { 214 cerr << "osprocesswindows::close_read_pipe(): Warning - Tried to close already closed pipe" << endl; 215 } 216 217 return read_close_ok; 218 } 219 220 221 222 223 224 225 247 226 248 227 #endif 249 250 #endif -
main/trunk/greenstone2/runtime-src/src/recpt/os_process_windows.h
r22173 r22177 38 38 { 39 39 public: 40 osprocesswindows(char* cmdline);41 40 osprocesswindows(char* cmdline, OSProcessPipeMode mode); 42 41 virtual ~osprocesswindows(); 43 44 /*45 virtual bool eop(); // end of pipe46 virtual bool eop(OSProcessPipeMode); // end of pipe, for designated pipe47 */48 42 49 43 virtual int read(char* buffer, const int buffer_len); 50 44 virtual int write(const char* buffer, const int buffer_len); 51 45 52 virtual bool close ();53 virtual bool close (OSProcessPipeMode mode);46 virtual bool close_write_pipe(OSProcessWarnStatus warn_status); 47 virtual bool close_read_pipe(OSProcessWarnStatus warn_status); 54 48 55 49 protected:
Note:
See TracChangeset
for help on using the changeset viewer.