[22173] | 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>
|
---|
| 36 | using 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 |
|
---|
[22174] | 46 | #define FD_STDIN 0
|
---|
| 47 | #define FD_STDOUT 1
|
---|
| 48 | #define FD_STDERR 2
|
---|
[22173] | 49 |
|
---|
| 50 | osprocessunix::osprocessunix(char* cmdline)
|
---|
| 51 | : osprocess(cmdline)
|
---|
| 52 | {}
|
---|
| 53 |
|
---|
| 54 | osprocessunix::osprocessunix(char* cmdline, OSProcessPipeMode mode)
|
---|
[22174] | 55 | : child_stdout_read_(-1), child_stdin_write_(-1),
|
---|
[22173] | 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
|
---|
[22174] | 94 | ::close(child_stdin_write_);
|
---|
[22173] | 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
|
---|
[22174] | 104 | ::close(child_stdin_read);
|
---|
[22173] | 105 | }
|
---|
| 106 |
|
---|
| 107 | // Sort out output pipe
|
---|
| 108 | // child has no buisness accessing read end of output pipe => close
|
---|
[22174] | 109 | ::close(child_stdout_read_);
|
---|
[22173] | 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
|
---|
[22174] | 118 | ::close(child_stdout_write);
|
---|
[22173] | 119 | }
|
---|
| 120 |
|
---|
[22174] | 121 | /*
|
---|
[22173] | 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 | }
|
---|
[22174] | 136 | */
|
---|
| 137 |
|
---|
[22173] | 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
|
---|
[22174] | 145 | ::close(child_stdin_read);
|
---|
[22173] | 146 |
|
---|
| 147 | // Sort out output pipe
|
---|
| 148 | // parent has no buisness accessing write end of output pipe => close
|
---|
[22174] | 149 | ::close(child_stdout_write);
|
---|
[22173] | 150 |
|
---|
| 151 |
|
---|
| 152 | switch(mode)
|
---|
| 153 | {
|
---|
| 154 | case uniRead:
|
---|
[22174] | 155 | ::close(child_stdin_write_);
|
---|
[22173] | 156 | break;
|
---|
| 157 | case uniWrite:
|
---|
[22174] | 158 | ::close(child_stdout_read_);
|
---|
[22173] | 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:
|
---|
[22174] | 165 | ::close(child_stdin_write_);
|
---|
| 166 | ::close(child_stdout_read_);
|
---|
[22173] | 167 | break;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | osprocessunix::~osprocessunix()
|
---|
| 173 | {}
|
---|
| 174 |
|
---|
| 175 | /*
|
---|
| 176 | bool osprocessunix::eop()
|
---|
| 177 | {}
|
---|
| 178 |
|
---|
| 179 | bool osprocessunix::eop(OSProcessPipeMode)
|
---|
| 180 | {}
|
---|
| 181 | */
|
---|
| 182 |
|
---|
| 183 | int osprocessunix::write(char* buffer, const int buffer_len)
|
---|
| 184 | {
|
---|
| 185 |
|
---|
[22174] | 186 | int actual_write_len = ::write(child_stdin_write_, buffer, buffer_len);
|
---|
[22173] | 187 |
|
---|
[22174] | 188 | if (actual_write_len<0) {
|
---|
[22173] | 189 | cerr << "osproessunix::write() Error: failed to write data" << endl;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | return actual_write_len;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | int osprocessunix::read(char* buffer, const int buffer_len)
|
---|
| 196 | {
|
---|
[22174] | 197 | int actual_read_len = ::read(child_stdout_read_, buffer, buffer_len);
|
---|
[22173] | 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 |
|
---|
| 208 | bool osprocessunix::close()
|
---|
[22174] | 209 | {
|
---|
| 210 | return true;
|
---|
| 211 | }
|
---|
[22173] | 212 |
|
---|
| 213 | bool osprocessunix::close(OSProcessPipeMode mode)
|
---|
[22174] | 214 | {
|
---|
| 215 | return true;
|
---|
| 216 | }
|
---|
[22173] | 217 |
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | #endif
|
---|