Changeset 22177

Show
Ignore:
Timestamp:
26.05.2010 23:52:09 (9 years ago)
Author:
davidb
Message:

Further development of the os_process classes

Location:
main/trunk/greenstone2/runtime-src/src/recpt
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process.cpp

    r22173 r22177  
    2626#include "os_process.h" 
    2727 
    28 osprocess::osprocess(char* cmd) 
    29 {} 
    3028osprocess::osprocess(char* cmdline, OSProcessPipeMode mode) 
    3129  : mode_(mode) 
     
    3634} 
    3735   
    38 /* 
    39 bool osprocess::eop() 
     36bool osprocess::close() 
    4037{ 
     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); 
    4144} 
    4245 
    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> 
     54using namespace std; 
     55 
     56void test_uni_write_pipe() 
    4457{ 
     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 
    4590} 
    46 */ 
    4791 
    48 int osprocess::read(char* buffer, const int buffer_len) 
     92int main() 
    4993{ 
     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   
    50107  return 0; 
    51108} 
    52109 
    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 
    62111 
    63 bool osprocess::close(OSProcessPipeMode mode) 
    64 { 
    65   return true; 
    66 } 
    67  
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process.h

    r22173 r22177  
    3333#define OS_PROCESS_H 
    3434 
    35  #include <stdlib.h> 
     35#include <stdlib.h> 
    3636 
    37 enum OSProcessPipeMode { uniRead=0, uniWrite=1, biReadWrite=2, noPipe=3 }; 
     37enum OSProcessPipeMode   { uniRead=0, uniWrite=1, biReadWrite=2 }; 
     38enum OSProcessWarnStatus { withoutWarning, withWarning }; 
    3839 
    3940class osprocess  
     
    4142   
    4243 public: 
    43   osprocess(char* cmdline); 
    4444  osprocess(char* cmdline, OSProcessPipeMode mode); 
    4545  virtual ~osprocess(); 
    4646   
    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(); 
    5051  */ 
    5152 
    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; 
    5455   
     56  virtual bool close_write_pipe(OSProcessWarnStatus warn_status=withWarning)=0; 
     57  virtual bool close_read_pipe(OSProcessWarnStatus warn_status=withWarning)=0; 
    5558  virtual bool close(); 
    56   virtual bool close(OSProcessPipeMode mode); 
     59 
    5760   
    5861 protected: 
    5962  OSProcessPipeMode mode_; 
    60   bool read_pipe_open_; 
    61   bool write_pipe_open_; 
    6263 
    6364}; 
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process_unix.cpp

    r22174 r22177  
    4141#include "os_process_unix.h" 
    4242 
    43 #define READ_PIPE_INDEX 0 
    44 #define WRITE_PIPE_INDEX 1 
    45  
    46 #define FD_STDIN 0 
    47 #define FD_STDOUT 1 
    48 #define FD_STDERR 2 
    49  
    50 osprocessunix::osprocessunix(char* cmdline) 
    51   : osprocess(cmdline) 
    52 {} 
    53  
    5443osprocessunix::osprocessunix(char* cmdline, OSProcessPipeMode mode) 
    5544  : child_stdout_read_(-1), child_stdin_write_(-1), 
     
    8170 
    8271   
    83   pid_t pid = fork(); 
    84   if (pid < 0) { 
     72  pid_ = fork(); 
     73  if (pid_ < 0) { 
    8574    cerr << "osprocessunix::osprocessunix(): Failed to create child process" << endl;  
    8675    return; 
    8776  } 
    8877 
    89   if (pid == 0) { 
     78  if (pid_ == 0) { 
    9079    // Child process 
    9180 
    9281    // Sort out input pipe 
    9382    //   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; 
    9585 
    9686    if ((mode == uniWrite) || (mode == biReadWrite)) { 
     
    9989    } 
    10090    else { 
    101       // noPipe or (parent is doing) uniRead (which means we're not interested 
    102       // 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 
    10393      // => child input remains coming from stdin 
    10494      ::close(child_stdin_read); 
     
    10898    //   child has no buisness accessing read end of output pipe => close 
    10999    ::close(child_stdout_read_); 
     100    child_stdout_read_=-1; 
     101 
    110102    if ((mode == uniRead) || (mode == biReadWrite)) { 
    111103      //   wire up child's stdout write so it is send down the pipe to the parent 
     
    113105    } 
    114106    else { 
    115       // noPipe or (parent is doing) uniWrite (which means we're not interested 
    116       // 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 
    117109      // => child output remains going to stdout 
    118110      ::close(child_stdout_write); 
    119111    } 
    120112 
    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); 
    137118 
    138119     
     
    154135      case uniRead: 
    155136    ::close(child_stdin_write_); 
     137    child_stdin_write_ = -1; 
    156138    break; 
    157139      case uniWrite: 
    158140    ::close(child_stdout_read_); 
     141    child_stdout_read_ = -1; 
    159142    break; 
    160143      case biReadWrite: 
     
    162145    // the pipes are set up just the way we want them 
    163146    break; 
    164       case noPipe: 
    165     ::close(child_stdin_write_); 
    166     ::close(child_stdout_read_); 
    167     break; 
    168147    } 
    169148  } 
     
    171150 
    172151osprocessunix::~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 
    182157 
    183158int osprocessunix::write(char* buffer, const int buffer_len) 
     
    206181 
    207182   
    208 bool osprocessunix::close() 
    209 { 
    210   return true; 
    211 } 
    212  
    213 bool osprocessunix::close(OSProcessPipeMode mode) 
    214 { 
    215   return true; 
    216 } 
     183bool 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 
     199bool 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 
    217217 
    218218 
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process_unix.h

    r22174 r22177  
    3838    
    3939 public: 
    40   osprocessunix(char* cmd); 
    4140  osprocessunix(char* cmd, OSProcessPipeMode mode); 
    4241  virtual ~osprocessunix(); 
    4342   
    44   /* 
    45   virtual bool eop();                  // end of pipe 
    46   virtual bool eop(OSProcessPipeMode); // end of pipe, for designated pipe 
    47   */ 
    48  
    4943  virtual int read(char* buffer, const int buffer_len); 
    5044  virtual int write(char* buffer, const int buffer_len); 
    5145   
    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); 
    5448 
    5549 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 
    5660  int child_stdin_write_; 
    5761  int child_stdout_read_; 
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process_windows.cpp

    r22173 r22177  
    130130osprocesswindows::~osprocesswindows()  
    131131{ 
     132  // close any file handles that are still open 
     133  close(); 
     134 
    132135  // Close process and thread handles 
    133136  CloseHandle( pi_.hProcess );   
     
    137140  pi_.hThread = NULL; 
    138141} 
    139  
    140  
    141 /* 
    142 bool osprocesswindows::eop() 
    143 { 
    144 } 
    145  
    146 bool osprocesswindows::eop(OSProcessPipeMode) 
    147 { 
    148 } 
    149 */ 
    150142 
    151143 
     
    178170 
    179171 
    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   
     174bool 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 
     198bool 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 
    247226 
    248227#endif 
    249  
    250 #endif 
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process_windows.h

    r22173 r22177  
    3838{ 
    3939public: 
    40   osprocesswindows(char* cmdline); 
    4140  osprocesswindows(char* cmdline, OSProcessPipeMode mode); 
    4241  virtual ~osprocesswindows(); 
    43  
    44   /* 
    45   virtual bool eop();                  // end of pipe 
    46   virtual bool eop(OSProcessPipeMode); // end of pipe, for designated pipe 
    47   */ 
    4842 
    4943  virtual int read(char* buffer, const int buffer_len); 
    5044  virtual int write(const char* buffer, const int buffer_len); 
    5145 
    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); 
    5448 
    5549protected: