[ecfbcc]: libutil++ / child_reader.cpp  Maximize  Restore  History

Download this file

276 lines (220 with data), 5.2 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
/**
* @file child_reader.cpp
* Facility for reading from child processes
*
* @remark Copyright 2002 OProfile authors
* @remark Read the file COPYING
*
* @author Philippe Elie
* @author John Levon
*/
#include <unistd.h>
#include <sys/wait.h>
#include <limits.h>
#include <cerrno>
#include <sstream>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include "op_libiberty.h"
#include "child_reader.h"
using namespace std;
child_reader::child_reader(string const & cmd, vector<string> const & args)
:
fd1(-1), fd2(-1),
pos1(0), end1(0),
pos2(0), end2(0),
pid(0),
first_error(0),
buf2(0), sz_buf2(0),
buf1(new char[PIPE_BUF]),
process_name(cmd),
is_terminated(true),
terminate_on_exception(false),
forked(false)
{
exec_command(cmd, args);
}
child_reader::~child_reader()
{
terminate_process();
delete [] buf1;
if (buf2) {
// allocated through C alloc
free(buf2);
}
}
void child_reader::exec_command(string const & cmd, vector<string> const & args)
{
int pstdout[2];
int pstderr[2];
if (pipe(pstdout) == -1 || pipe(pstderr) == -1) {
first_error = errno;
return;
}
pid = fork();
switch (pid) {
case -1:
first_error = errno;
return;
case 0: {
char const ** argv = new char const *[args.size() + 2];
size_t i;
argv[0] = cmd.c_str();
for (i = 1 ; i <= args.size() ; ++i)
argv[i] = args[i - 1].c_str();
argv[i] = 0;
// child: we can cleanup a few fd
close(pstdout[0]);
dup2(pstdout[1], STDOUT_FILENO);
close(pstdout[1]);
close(pstderr[0]);
dup2(pstderr[1], STDERR_FILENO);
close(pstderr[1]);
execvp(cmd.c_str(), (char * const *)argv);
int ret_code = errno;
// we can communicate with parent by writing to stderr
// and by returning a non zero error code. Setting
// first_error in the child is a non-sense
// we are in the child process: so this error message
// is redirect to the parent process
cerr << "Couldn't exec \"" << cmd << "\" : "
<< strerror(errno) << endl;
exit(ret_code);
}
default:;
// parent: we do not write on these fd.
close(pstdout[1]);
close(pstderr[1]);
forked = true;
break;
}
fd1 = pstdout[0];
fd2 = pstderr[0];
is_terminated = false;
return;
}
bool child_reader::block_read()
{
fd_set read_fs;
FD_ZERO(&read_fs);
FD_SET(fd1, &read_fs);
FD_SET(fd2, &read_fs);
if (select(max(fd1, fd2) + 1, &read_fs, 0, 0, 0) >= 0) {
if (FD_ISSET(fd1, &read_fs)) {
ssize_t temp = read(fd1, buf1, PIPE_BUF);
if (temp >= 0)
end1 = temp;
else
end1 = 0;
}
if (FD_ISSET(fd2, &read_fs)) {
if (end2 >= sz_buf2) {
sz_buf2 = sz_buf2 ? sz_buf2 * 2 : PIPE_BUF;
buf2 = (char *)xrealloc(buf2, sz_buf2);
}
ssize_t temp = read(fd2, buf2 + end2, sz_buf2 - end2);
if (temp > 0)
end2 += temp;
}
}
bool ret = !(end1 == 0 && end2 == 0);
if (end1 == -1)
end1 = 0;
if (end2 == -1)
end2 = 0;
return ret;
}
bool child_reader::getline(string & result)
{
// some stl lacks string::clear()
result.erase(result.begin(), result.end());
bool ok = true;
bool can_stop = false;
do {
int temp = end2;
if (pos1 >= end1) {
pos1 = 0;
block_read();
}
// for efficiency try to copy as much as we can of data
ssize_t temp_pos = pos1;
while (temp_pos < end1 && ok) {
char ch = buf1[temp_pos++];
if (ch == '\n')
ok = false;
}
// !ok ==> endl has been read so do not copy it.
result.append(&buf1[pos1], (temp_pos - pos1) - !ok);
if (!ok || !end1)
can_stop = true;
// reading zero byte from stdout don't mean than we exhausted
// all stdout output, we must continue to try until reading
// stdout and stderr return zero byte.
if (ok && temp != end2)
can_stop = false;
pos1 = temp_pos;
} while (!can_stop);
// Is this correct ?
return end1 != 0 || result.length() != 0;
}
bool child_reader::get_data(ostream & out, ostream & err)
{
bool ret = true;
while (ret) {
ret = block_read();
out.write(buf1, end1);
err.write(buf2, end2);
end1 = end2 = 0;
}
return first_error == 0;
}
int child_reader::terminate_process()
{
// can be called explicitely or by dtor,
// we must protect against multiple call
if (!is_terminated) {
int ret;
waitpid(pid, &ret, 0);
is_terminated = true;
if (WIFEXITED(ret)) {
first_error = WEXITSTATUS(ret) | WIFSIGNALED(ret);
} else if (WIFSIGNALED(ret)) {
terminate_on_exception = true;
first_error = WTERMSIG(ret);
} else {
// FIXME: this seems impossible, waitpid *must* wait
// and either the process terminate normally or through
// a signal.
first_error = -1;
}
}
if (fd1 != -1) {
close(fd1);
fd1 = -1;
}
if (fd2 != -1) {
close(fd2);
fd2 = -1;
}
return first_error;
}
string child_reader::error_str() const
{
ostringstream err;
if (!forked) {
err << string("unable to fork, error: ")
<< strerror(first_error);
} else if (is_terminated) {
if (first_error) {
if (terminate_on_exception) {
err << process_name << " terminated by signal "
<< first_error;
} else {
err << process_name << " return "
<< first_error;
}
}
}
return err.str();
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks