root/main/trunk/greenstone2/runtime-src/src/recpt/os_process_unix.cpp @ 22174

Revision 22174, 5.8 KB (checked in by davidb, 10 years ago)

Syntax errors corrected

  • Property svn:executable set to *
Line 
1/**********************************************************************
2 *
3 * os_process_unix.cpp -- Unix version of osprocess.  See os_process.h
4 *                 for more details
5 *
6 * Copyright (C) 2010  The New Zealand Digital Library Project
7 *
8 * A component of the Greenstone digital library software
9 * from the New Zealand Digital Library Project at the
10 * University of Waikato, New Zealand.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *********************************************************************/
27
28#ifndef __WIN32__ // i.e Unix
29
30#if defined(GSDL_USE_OBJECTSPACE)
31#  include <ospace/std/iostream>
32#elif defined(GSDL_USE_IOS_H)
33#  include <iostream.h>
34#else
35#  include <iostream>
36using namespace std;
37#endif
38
39#include <unistd.h>
40
41#include "os_process_unix.h"
42
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
50osprocessunix::osprocessunix(char* cmdline)
51  : osprocess(cmdline)
52{}
53
54osprocessunix::osprocessunix(char* cmdline, OSProcessPipeMode mode)
55  : child_stdout_read_(-1), child_stdin_write_(-1),
56    osprocess(cmdline,mode)
57{
58 
59  // Create a pipe for the child process's STDOUT.
60  int stdout_pipe_fd[2];
61
62  if (pipe(stdout_pipe_fd)!=0) {
63    cerr << "osprocessunix::osprocessunix(): Failed to create stdout pipe for child process" << endl;
64    return;
65  }
66
67  child_stdout_read_ = stdout_pipe_fd[READ_PIPE_INDEX];
68  int child_stdout_write = stdout_pipe_fd[WRITE_PIPE_INDEX];
69 
70
71  // Create a pipe for the child process's STDIN.
72  int stdin_pipe_fd[2];
73
74  if (pipe(stdin_pipe_fd)!=0) {
75    cerr << "osprocessunix::osprocessunix(): Failed to create stdin pipe for child process" << endl;
76    return;
77  }
78
79  int child_stdin_read = stdin_pipe_fd[READ_PIPE_INDEX];
80  child_stdin_write_ = stdin_pipe_fd[WRITE_PIPE_INDEX];
81
82 
83  pid_t pid = fork();
84  if (pid < 0) {
85    cerr << "osprocessunix::osprocessunix(): Failed to create child process" << endl;
86    return;
87  }
88
89  if (pid == 0) {
90    // Child process
91
92    // Sort out input pipe
93    //   child has no buisness accessing write end of input pipe => close
94    ::close(child_stdin_write_);   
95
96    if ((mode == uniWrite) || (mode == biReadWrite)) {
97      //   wire up child's stdin read so its input comes from the parent's pipe
98      dup2(child_stdin_read, FD_STDIN);
99    }
100    else {
101      // noPipe or (parent is doing) uniRead (which means we're not interested
102      // in the child reading any input from the parent)
103      // => child input remains coming from stdin
104      ::close(child_stdin_read);
105    }
106
107    // Sort out output pipe
108    //   child has no buisness accessing read end of output pipe => close
109    ::close(child_stdout_read_);
110    if ((mode == uniRead) || (mode == biReadWrite)) {
111      //   wire up child's stdout write so it is send down the pipe to the parent
112      dup2(child_stdout_write, FD_STDOUT);
113    }
114    else {
115      // noPipe or (parent is doing) uniWrite (which means we're not interested
116      // in any output produced by the child process)
117      // => child output remains going to stdout
118      ::close(child_stdout_write);
119    }
120
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    */
137
138   
139  }
140  else {
141    // Parent process
142
143    // Sort out input pipe
144    //    parent has no buisness accessing read end of input pipe => close
145    ::close(child_stdin_read);
146
147    // Sort out output pipe
148    //    parent has no buisness accessing write end of output pipe => close
149    ::close(child_stdout_write);
150
151
152    switch(mode)
153    {
154      case uniRead:
155    ::close(child_stdin_write_);
156    break;
157      case uniWrite:
158    ::close(child_stdout_read_);
159    break;
160      case biReadWrite:
161    // nothing to do
162    // the pipes are set up just the way we want them
163    break;
164      case noPipe:
165    ::close(child_stdin_write_);
166    ::close(child_stdout_read_);
167    break;
168    }
169  }
170}
171
172osprocessunix::~osprocessunix()
173{}
174
175/* 
176bool osprocessunix::eop()
177{}
178
179bool osprocessunix::eop(OSProcessPipeMode)
180{}
181*/
182
183int osprocessunix::write(char* buffer, const int buffer_len)
184{
185
186  int actual_write_len = ::write(child_stdin_write_, buffer, buffer_len);
187 
188  if (actual_write_len<0) {
189    cerr << "osproessunix::write() Error: failed to write data" << endl;
190  }
191
192  return actual_write_len;
193}
194
195int osprocessunix::read(char* buffer, const int buffer_len)
196{
197  int actual_read_len = ::read(child_stdout_read_, buffer, buffer_len);
198
199  if (actual_read_len<0) {
200    cerr << "osproessunix::read() Error: failed to read data" << endl;
201  }
202
203  return actual_read_len;
204}
205
206
207 
208bool osprocessunix::close()
209{
210  return true;
211}
212
213bool osprocessunix::close(OSProcessPipeMode mode)
214{
215  return true;
216}
217
218
219
220#endif
Note: See TracBrowser for help on using the browser.