File: RunFile.cpp

package info (click to toggle)
cvsnt 2.5.03.2382-3.3%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 32,288 kB
  • ctags: 24,567
  • sloc: ansic: 136,648; cpp: 102,037; sh: 42,846; asm: 3,495; makefile: 1,763; ada: 1,681; perl: 1,352; pascal: 1,089; cs: 1,008; yacc: 805; python: 453; xml: 263; sql: 210; lisp: 7
file content (350 lines) | stat: -rw-r--r-- 7,220 bytes parent folder | download | duplicates (2)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
	CVSNT Generic API
    Copyright (C) 2004 Tony Hoyle and March-Hare Software Ltd

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License version 2.1 as published by the Free Software Foundation.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/* Unix specific */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <system.h>

#include <sys/types.h>
#include <sys/wait.h>

#include <signal.h>

#include "../RunFile.h"
#include "../ServerIO.h"

int (*const CRunFile::StandardInput)(char *,size_t, void *) = (int (*const)(char *,size_t, void *))-1;
int (*const CRunFile::StandardOutput)(const char *,size_t, void *) = (int (*const)(const char *,size_t, void *))-1;
int (*const CRunFile::StandardError)(const char *,size_t, void *) = (int (*const)(const char *,size_t, void *))-1;

enum enPipes
{
	PIPE_READ = 0,
	PIPE_WRITE = 1 
};

CRunFile::CRunFile() 
{
	m_args = new CTokenLine; 
	m_inputFn=NULL;
	m_outputFn=NULL;
	m_errorFn=NULL;
	m_debugFn=NULL;
	m_child = 0;
}

CRunFile::~CRunFile()
{
	delete m_args;
}

bool CRunFile::resetArgs()
{
	return m_args->resetArgs();
}

bool CRunFile::setArgs(const char *args)
{
	return m_args->setArgs(args);
}


bool CRunFile::setArgs(int argc, const char *const*argv)
{
	return m_args->setArgs(argc, argv);
}

bool CRunFile::addArg(const char *arg)
{
	return m_args->addArg(arg);
}

bool CRunFile::addArgs(const char *args)
{
	return m_args->addArgs(args);
}

const char *CRunFile::toString()
{
	return m_args->toString(0);
}

bool CRunFile::setInput(int (*inputFn)(char *,size_t,void*), void *userData)
{
	m_inputFn = inputFn;
	m_inputData = userData;
	return true;
}

bool CRunFile::setOutput(int (*outputFn)(const char *,size_t,void*), void *userData)
{
	m_outputFn = outputFn;
	m_inputData = userData;
	return true;
}

bool CRunFile::setError(int (*errorFn)(const char *,size_t,void*), void *userData)
{
	m_errorFn = errorFn;
	m_errorData = userData;
	return true;
}

bool CRunFile::setDebug(int (*debugFn)(int type, const char *,size_t, void *), void *userData)
{
	m_debugFn = debugFn;
	m_debugData = userData;
	return true;
}

/* On Unix, the bShow parameter does nothing */
bool CRunFile::run(const char *path, bool bShow /* = false */)
{
  	int pid;
  	int fd1[2],fd2[2],fd3[2];

	if(m_inputFn && m_inputFn != StandardInput)
  	{
		pipe(fd1);
		m_inFd=fd1[PIPE_WRITE];
    }
    else
      m_inFd=-1;

	if(m_outputFn && m_outputFn != StandardOutput)
    {
        pipe(fd2);
        m_outFd=fd2[PIPE_READ];
    }
    else
        m_outFd=-1;

	if(!m_errorFn)
		m_errorFn = m_outputFn;

	if(m_errorFn && m_errorFn != StandardError)
    {
        pipe(fd3);
        m_errFd=fd3[PIPE_READ];
    }
	else
		m_errFd=-1;

	if(path)
	  m_args->insertArg(0,path);

	pid = fork();
	if(pid<0)
		return false;
	
	signal(SIGPIPE,SIG_IGN);
	if(!pid) /* Child */
	{	
		int nullfd = open("/dev/null",O_RDWR);

		if(m_inFd>=0)
		{
		  close(fd1[PIPE_WRITE]);
		  dup2(fd1[PIPE_READ],0);
		}
		else if(!m_inputFn)
			dup2(nullfd,0);
		if(m_outFd>=0)
		{
		  close(fd2[PIPE_READ]);
		  dup2(fd2[PIPE_WRITE],1);
		}
		else if(!m_outputFn)
			dup2(nullfd,1);
		if(m_errFd>=0)
		{
		  close(fd2[PIPE_READ]);
		  dup2(fd2[PIPE_WRITE],2);
		}
		else if(!m_errorFn)
			dup2(nullfd,2);

		close(nullfd);

		char **myargv = (char**)m_args->toArgv();
		execvp (myargv[0], myargv);
		perror("Exec failed");
		exit(-1);
	}

	if(m_inFd>=0)
		close(fd1[PIPE_READ]);
	if(m_outFd>=0)
		close(fd2[PIPE_WRITE]);
	if(m_errFd>=0)
		close(fd3[PIPE_WRITE]);

	m_child = pid;

	return true;
}

/* Unix gets no I/O until wait() is called - if we had
   reliable threads this wouldn't be necessary... fork()
   isolates the address spaces so is not suitable for this
   task */
bool CRunFile::wait(int& result, int timeout)
{	
	char buf[BUFSIZ],inbuf[BUFSIZ];
	int status,size,wsize,w,l;

	CServerIo::trace(3,"wait() called, m_child=%d",m_child);
	
	if(!m_child)
	  return -1;
		
	if(m_outFd>=0)
		fcntl(m_outFd,F_SETFL, O_NONBLOCK);
	if(m_errFd>=0)
    		fcntl(m_errFd,F_SETFL, O_NONBLOCK);
	if(m_inFd>=0)
    		fcntl(m_errFd,F_SETFL, O_NONBLOCK);

	size = 0;
	if(m_inFd>=0)
	{
		size=l=m_inputFn(inbuf,sizeof(inbuf),m_inputData);
		if(size<=0)
		{
			close(m_inFd);
			m_inFd=-1;
		}
	}
	w = waitpid(m_child,&status,WNOHANG);
	while((timeout==-1 || timeout>0) && ((m_inFd>=0 && size>0) || m_outFd>=0 || m_errFd>=0) && !w)
	{
		if(m_inFd>=0 && size>0)
		{
		  int s = write(m_inFd, inbuf+l-size,size);
		  if(m_debugFn) m_debugFn(0,inbuf+l-size,size,m_debugData);
		  if(s<0)
		  {
		    close(m_inFd);
		    m_inFd=-1;
		  }
		  if(s)
		  {
		    size-=s;
		    if(!size)
		      size=l=m_inputFn(inbuf,sizeof(inbuf),m_inputData);
		    if(size<=0)
		    {
			close(m_inFd);
			m_inFd=-1;
		    }
		  }
		}
		if(!w)
			w = waitpid(m_child,&status,WNOHANG);
		wsize=0;
      	while(!w && m_errFd>=0 && (wsize=read(m_errFd,buf,sizeof(buf)))>0)
		{
       		(m_errorFn?m_errorFn:m_outputFn)(buf,wsize,m_errorFn?m_errorData:m_outputData);
			if(m_debugFn) m_debugFn(2,buf,wsize,m_debugData);
		}
		if(wsize<0 && errno!=EAGAIN)
		{
			close(m_errFd);
			m_errFd=-1;
		}
		if(!w)
			w = waitpid(m_child,&status,WNOHANG);
		wsize=0;
   		while(!w && m_outFd>=0 && (wsize=read(m_outFd,buf,sizeof(buf)))>0)
		{
			m_outputFn(buf,wsize,m_outputData);
			if(m_debugFn) m_debugFn(1,buf,wsize,m_debugData);
		}
		if(wsize<0 && errno!=EAGAIN)
		{
			close(m_outFd);
			m_outFd=-1;
		}
		if(!w)
		{
		  usleep(100);
		  w = waitpid(m_child,&status,WNOHANG);
		}
		if(timeout!=-1)
		{
			timeout-=100;
			if(timeout==-1) timeout--;
		}
	}
	if(!w && timeout!=-1 && timeout<=0) /* timed out */
	   return false;
	if(m_inFd)
	{
		close(m_inFd);
		m_inFd=-1;
	}

	/* If we've fallen through all of the above with the app still running, wait for it */
	if(!w)
	{
		if(timeout==-1)
			waitpid(m_child,&status,0);
		else
		{
			while(!w && timeout>0)
			{
			   w=waitpid(m_child,&status,WNOHANG);
		 	   usleep(100);
			   timeout-=100;
			}
			if(!w)
			   return false;
		}
	}     	
	else /* App died.. soak up stdio */
	{
		CServerIo::trace(3,"Process finished");
      	while(m_errFd>=0 && (wsize=read(m_errFd,buf,sizeof(buf)))>0)
		{
       		(m_errorFn?m_errorFn:m_outputFn)(buf,wsize,m_errorFn?m_errorData:m_outputData);
			if(m_debugFn) m_debugFn(2,buf,wsize,m_debugData);
		}
      	while(m_outFd>=0 && (wsize=read(m_outFd,buf,sizeof(buf)))>0)
		{
       		m_outputFn(buf,wsize,m_outputData);
			if(m_debugFn) m_debugFn(1,buf,wsize,m_debugData);
		}
		if(m_outFd>=0)
		{
			close(m_outFd);	
			m_outFd=-1;
		}
		if(m_errFd>=0)
		{
			close(m_errFd);
			m_errFd=-1;
		}
	}

	m_exitCode=result=WEXITSTATUS(status);
	CServerIo::trace(3,"Exit status is %d",m_exitCode);
	return true;
}

// vi:ts=4:sw=4