Parent: [ae8ff5] (diff)

Child: [7ded97] (diff)

Download this file

execmd.cpp    270 lines (249 with data), 6.7 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
#ifndef lint
static char rcsid[] = "@(#$Id: execmd.cpp,v 1.12 2006-01-23 13:32:28 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
/*
* 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.
*/
#ifndef TEST_EXECMD
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <fcntl.h>
#include <errno.h>
#ifdef PUTENV_ARG_NOT_CONST
#include <string.h>
#endif
#include <list>
#include <string>
#include <sstream>
#include <iostream>
#include "execmd.h"
#include "pathut.h"
#include "debuglog.h"
#ifndef NO_NAMESPACES
using namespace std;
#endif /* NO_NAMESPACES */
#define MAX(A,B) (A>B?A:B)
void ExecCmd::putenv(const string &ea)
{
env.push_back(ea);
}
int ExecCmd::doexec(const string &cmd, const list<string>& args,
const string *input, string *output)
{
{ // Debug and logging
string command = cmd + " ";
for (list<string>::const_iterator it = args.begin();it != args.end();
it++) {
command += "{" + *it + "} ";
}
LOGDEB(("ExecCmd::doexec: %s\n", command.c_str()));
}
int pipein[2]; // subproc input
int pipeout[2]; // subproc output
pipein[0] = pipein[1] = pipeout[0] = pipeout[1] = -1;
if (input && pipe(pipein) < 0) {
LOGERR(("ExecCmd::doexec: pipe(2) failed. errno %d\n", errno));
return -1;
}
if (output && pipe(pipeout) < 0) {
LOGERR(("ExecCmd::doexec: pipe(2) failed. errno %d\n", errno));
close(pipein[0]);
close(pipein[1]);
return -1;
}
pid_t pid = fork();
if (pid < 0) {
LOGERR(("ExecCmd::doexec: fork(2) failed. errno %d\n", errno));
return -1;
}
if (pid) {
if (input) {
close(pipein[0]);
pipein[0] = -1;
}
if (output) {
close(pipeout[1]);
pipeout[1] = -1;
}
fd_set readfds, writefds;
if (input || output) {
if (input)
fcntl(pipein[1], F_SETFL, O_NONBLOCK);
if (output)
fcntl(pipeout[0], F_SETFL, O_NONBLOCK);
int nwritten = 0;
int nfds = MAX(pipein[1], pipeout[0]) + 1;
for(;nfds > 0;) {
FD_ZERO(&writefds);
FD_ZERO(&readfds);
if (pipein[1] >= 0)
FD_SET(pipein[1], &writefds);
if (pipeout[0] >= 0)
FD_SET(pipeout[0], &readfds);
nfds = MAX(pipein[1], pipeout[0]) + 1;
//struct timeval to; to.tv_sec = 1;to.tv_usec=0;
//cerr << "pipein[1] "<< pipein[1] << " pipeout[0] " <<
//pipeout[0] << " nfds " << nfds << endl;
if (select(nfds, &readfds, &writefds, 0, 0) <= 0) {
LOGERR(("ExecCmd::doexec: select(2) failed. errno %d\n",
errno));
break;
}
if (pipein[1] >= 0 && FD_ISSET(pipein[1], &writefds)) {
int n = write(pipein[1], input->c_str()+nwritten,
input->length() - nwritten);
if (n < 0) {
LOGERR(("ExecCmd::doexec: write(2) failed. errno %d\n",
errno));
goto out;
}
nwritten += n;
if (nwritten == (int)input->length()) {
// cerr << "Closing output" << endl;
close(pipein[1]);
pipein[1] = -1;
}
}
if (pipeout[0] > 0 && FD_ISSET(pipeout[0], &readfds)) {
char buf[1024];
int n = read(pipeout[0], buf, 1024);
if (n == 0) {
goto out;
} else if (n < 0) {
LOGERR(("ExecCmd::doexec: read(2) failed. errno %d\n",
errno));
goto out;
} else if (n > 0) {
// cerr << "READ: " << n << endl;
output->append(buf, n);
}
}
}
}
out:
int status;
pid = waitpid(pid, &status, 0);
if (pipein[0] >= 0)
close(pipein[0]);
if (pipein[1] >= 0)
close(pipein[1]);
if (pipeout[0] >= 0)
close(pipeout[0]);
if (pipeout[1] >= 0)
close(pipeout[1]);
LOGDEB1(("ExecCmd::doexec: father got status 0x%x\n", status));
return status;
} else {
if (input) {
close(pipein[1]);
pipein[1] = -1;
if (pipein[0] != 0) {
dup2(pipein[0], 0);
close(pipein[0]);
pipein[0] = -1;
}
}
if (output) {
close(pipeout[0]);
pipeout[0] = -1;
if (pipeout[1] != 1) {
if (dup2(pipeout[1], 1) < 0) {
LOGERR(("ExecCmd::doexec: dup2(2) failed. errno %d\n",
errno));
}
if (close(pipeout[1]) < 0) {
LOGERR(("ExecCmd::doexec: close(2) failed. errno %d\n",
errno));
}
pipeout[1] = -1;
}
}
// Count args
list<string>::const_iterator it;
int i = 0;
for (it = args.begin(); it != args.end(); it++) i++;
// Allocate arg vector (2 more for arg0 + final 0)
typedef const char *Ccharp;
Ccharp *argv;
argv = (Ccharp *)malloc((i+2) * sizeof(char *));
if (argv == 0) {
LOGERR(("ExecCmd::doexec: malloc() failed. errno %d\n",
errno));
exit(1);
}
// Fill up argv
argv[0] = path_getsimple(cmd).c_str();
i = 1;
for (it = args.begin(); it != args.end(); it++) {
argv[i++] = it->c_str();
}
argv[i] = 0;
#if 0
{int i = 0;cerr << "cmd: " << cmd << endl << "ARGS: " << endl;
while (argv[i]) cerr << argv[i++] << endl;}
#endif
for (it = env.begin(); it != env.end(); it++) {
#ifdef PUTENV_ARG_NOT_CONST
::putenv(strdup(it->c_str()));
#else
::putenv(it->c_str());
#endif
}
execvp(cmd.c_str(), (char *const*)argv);
// Hu ho
LOGERR(("ExecCmd::doexec: execvp(%s) failed. errno %d\n", cmd.c_str(),
errno));
_exit(128);
}
}
#else // TEST
#include <stdio.h>
#include <string>
#include <iostream>
#include <list>
#include "debuglog.h"
using namespace std;
#include "execmd.h"
const char *data = "Une ligne de donnees\n";
int main(int argc, const char **argv)
{
DebugLog::getdbl()->setloglevel(DEBDEB1);
DebugLog::setfilename("stderr");
if (argc < 2) {
cerr << "Usage: execmd cmd arg1 arg2 ..." << endl;
exit(1);
}
const string cmd = argv[1];
list<string> l;
for (int i = 2; i < argc; i++) {
l.push_back(argv[i]);
}
ExecCmd mexec;
string input, output;
input = data;
string *ip = 0;
//ip = &input;
mexec.putenv("TESTVARIABLE1=TESTVALUE1");
mexec.putenv("TESTVARIABLE2=TESTVALUE2");
mexec.putenv("TESTVARIABLE3=TESTVALUE3");
int status = mexec.doexec(cmd, l, ip, &output);
fprintf(stderr, "Status: 0x%x\n", status);
cout << "Output:" << output << endl;
exit (status >> 8);
}
#endif // TEST