-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessBuff.cpp
More file actions
337 lines (267 loc) · 7.16 KB
/
ProcessBuff.cpp
File metadata and controls
337 lines (267 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/***************************************************************************
* Copyright (C) 2009 by Mushthofa, Thomas Krennwallner *
* Mushthofa.Mushthofa@UGent. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/* Modified from dlvhex's ProcessBuff.cpp */
/**
* @file ProcessBuf.cpp
* @author Thomas Krennwallner
* @date Sun May 21 13:22:23 2006
*
* @brief Process buffer for external program input/output pipeline
*
*
*/
#include "ProcessBuff.h"
#include <iostream>
#include <sstream>
#include <cerrno>
#include <cstdio>
#include <csignal>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
ProcessBuf::ProcessBuf()
: std::streambuf(), bufsize(256)
{
// ignore SIGPIPE
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (::sigaction(SIGPIPE, &sa, 0))
{
::perror("sigaction");
::exit(1);
}
initBuffers(); // don't call virtual methods in the ctor
opened = false;
//std::cout<<"pb init"<<std::endl;
}
ProcessBuf::ProcessBuf(const ProcessBuf& sb)
: std::streambuf(),
process(sb.process),
status(sb.status),
bufsize(sb.bufsize)
{
::memcpy(inpipes, sb.inpipes, 2);
::memcpy(outpipes, sb.outpipes, 2);
initBuffers(); // don't call virtual methods in the ctor
opened = sb.opened;
}
ProcessBuf::~ProcessBuf()
{
close();
if (ibuf)
{
delete[] ibuf;
ibuf = 0;
}
if (obuf)
{
delete[] obuf;
obuf = 0;
}
//std::cout<<"pb term"<<std::endl;
}
void ProcessBuf::initBuffers()
{
obuf = new std::streambuf::char_type[bufsize];
ibuf = new std::streambuf::char_type[bufsize];
setp(obuf, obuf + bufsize);
setg(ibuf, ibuf, ibuf);
}
void ProcessBuf::open(const std::vector<std::string>& av)
{
// we want a full-duplex stream -> create two pairs of pipes
//std::cout<<"pb open"<<std::endl;
if (::pipe(outpipes) < 0)
{
::perror("pipes");
return;
}
if (::pipe(inpipes) < 0)
{
::perror("pipes");
return;
}
// create a new process
process = ::fork();
switch (process)
{
case -1: // error
::perror("fork");
::exit(process);
break;
case 0: // child
{
// setup argv
char* argv[av.size() + 1];
int i = 0;
//std::cerr<<"Opening process buffer with command :";
for (std::vector<std::string>::const_iterator it = av.begin();
it != av.end(); it++)
{
//std::cerr<<*it<<" ";
std::string::size_type size = it->size();
argv[i] = new char[size + 1];
it->copy(argv[i], size);
argv[i][size] = '\0';
i++;
}
*argv[i] = '\0';
// redirect stdin and stdout and stderr
if (::dup2(outpipes[1], STDOUT_FILENO) < 0)
{
::perror("dup2");
::exit(1);
}
if (::dup2(outpipes[1], STDERR_FILENO) < 0)
{
::perror("dup2");
::exit(1);
}
if (::dup2(inpipes[0], STDIN_FILENO) < 0)
{
::perror("dup2");
::exit(1);
}
// stdout and stdin is redirected, close unneeded filedescr.
::close(outpipes[0]);
::close(outpipes[1]);
::close(inpipes[0]);
::close(inpipes[1]);
// execute command, should not return
::execvp(*argv, argv);
// just in case we couldn't execute the command
::exit(127);
}
break;
default: // parent
// close writing end of the output pipe
::close(outpipes[1]);
outpipes[1] = -1;
// close reading end of the input pipe
::close(inpipes[0]);
inpipes[0] = -1;
break;
}
opened = true;
}
void ProcessBuf::endoffile()
{
if (inpipes[1] != -1)
{
::close(inpipes[1]); // send EOF to stdin of child process
inpipes[1] = -1;
}
}
int ProcessBuf::close()
{
//std::cout<<"pb closed"<<std::endl;
// we're done writing
endoffile();
// we're done reading
if (outpipes[0] != -1)
{
::close(outpipes[0]);
outpipes[0] = -1;
}
// obviously we do not want to leave zombies around, so get status
// code of the process
::waitpid(process, &status, 0);
// exit code of process
opened = false;
return WEXITSTATUS(status);
}
std::streambuf::int_type ProcessBuf::overflow(std::streambuf::int_type c)
{
if (pptr() >= epptr()) // full obuf -> write buffer
{
if (sync() == -1)
{
return traits_type::eof();
}
}
// if c != EOF, put c into output buffer so next call to sync() will
// write it
if (!traits_type::eq_int_type(c, traits_type::eof()))
{
*pptr() = traits_type::to_char_type(c);
pbump(1); // increase put pointer by one
}
return traits_type::not_eof(c);
}
std::streambuf::int_type ProcessBuf::underflow()
{
if (gptr() >= egptr()) // empty ibuf -> get data
{
// try to receive at most bufsize bytes
ssize_t n = ::read(outpipes[0], ibuf, bufsize);
if (n == 0) // EOF
{
return traits_type::eof();
}
// a failure occured while receiving from the stream
if (n < 0)
{
throw std::ios_base::failure("Process prematurely closed pipe.");
}
setg(ibuf, ibuf, ibuf + n); // set new input buffer boundaries
}
return traits_type::to_int_type(*gptr());
}
std::streambuf::int_type ProcessBuf::sync()
{
// reset input buffer
setg(ibuf, ibuf, ibuf);
if (pptr() != pbase()) // non-empty obuf -> send data
{
errno = 0;
// loops until whole obuf is sent
//
// Warning: when peer disconnects during the sending we receive
// a SIGPIPE and the default signal handler exits the program.
// Therefore we have to ignore SIGPIPE (in ctor) and reset the
// obuf followed by an error return value. See chapter 5.13 of
// W.R. Stevens: Unix Network Programming Vol.1.
ssize_t len = pptr() - pbase();
ssize_t ret = 0;
for (ssize_t written = 0;
written < len;
written += ret)
{
ret = ::write (inpipes[1], pbase() + written, len - written);
if (ret == -1 || ret == 0) break;
}
// reset output buffer right after sending to the stream
setp(obuf, obuf + bufsize);
if (ret == 0) // EOF
{
return -1;
}
if (ret < 0 || errno == EPIPE) // failure
{
std::ostringstream oss;
oss << "Couldn't write to process pipe (errno = " << errno << ").";
throw std::ios_base::failure(oss.str());
}
}
return 0;
}
//End