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

Further development of the os_process classes

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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