File: qprocess_unix.cpp

package info (click to toggle)
qt-x11 3%3A2.3.1-22
  • links: PTS
  • area: main
  • in suites: woody
  • size: 48,524 kB
  • ctags: 46,337
  • sloc: cpp: 260,077; ansic: 32,457; makefile: 31,131; yacc: 2,444; sh: 1,513; lex: 480; perl: 422; xml: 68; lisp: 15
file content (422 lines) | stat: -rw-r--r-- 10,664 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qt Designer.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "qapplication.h"
#include "qlist.h"
#include "qprocess.h"


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>

//#define QPROCESS_DEBUG


#if defined(Q_C_CALLBACKS)
extern "C" {
#endif

#if defined(_OS_IRIX_) && defined(_CC_GNU_)
    static void qt_C_sigchldHnd();
#else
    static void qt_C_sigchldHnd( int );
#endif

#if defined(Q_C_CALLBACKS)
}
#endif

#if defined(_OS_IRIX_) && defined(_CC_GNU_)
void qt_C_sigchldHnd()
#else
void qt_C_sigchldHnd( int )
#endif
{
    QProcessPrivate::sigchldHnd();
}

struct sigaction *QProcessPrivate::oldact;
QList<QProcess> *QProcessPrivate::proclist = 0;

QProcessPrivate::QProcessPrivate( QProcess *proc ) : d( proc )
{
#if defined(QPROCESS_DEBUG)
    qDebug( "QProcessPrivate: Constructor" );
#endif
    stdinBufRead = 0;

    notifierStdin = 0;
    notifierStdout = 0;
    notifierStderr = 0;

    socketStdin[0] = 0;
    socketStdin[1] = 0;
    socketStdout[0] = 0;
    socketStdout[1] = 0;
    socketStderr[0] = 0;
    socketStderr[1] = 0;

    exitValuesCalculated = FALSE;
    exitStat = 0;
    exitNormal = FALSE;

    // install a SIGCHLD handler
    if ( proclist == 0 ) {
	proclist = new QList<QProcess>;

	struct sigaction act;
	act.sa_handler = qt_C_sigchldHnd;
	sigemptyset( &(act.sa_mask) );
	sigaddset( &(act.sa_mask), SIGCHLD );
	act.sa_flags = SA_NOCLDSTOP;
#if defined(SA_RESTART)
	act.sa_flags |= SA_RESTART;
#endif
	if ( sigaction( SIGCHLD, &act, oldact ) != 0 )
	    qWarning( "Error installing SIGCHLD handler" );
    }
    proclist->append( d );
}

QProcessPrivate::~QProcessPrivate()
{
#if defined(QPROCESS_DEBUG)
    qDebug( "QProcessPrivate: Destructor" );
#endif
    // restore SIGCHLD handler
    if ( proclist != 0 ) {
	proclist->remove( d );
	if ( proclist->count() == 0 ) {
	    delete proclist;
	    proclist = 0;
	    if ( sigaction( SIGCHLD, oldact, 0 ) != 0 )
		qWarning( "Error restoring SIGCHLD handler" );
	}
    }

    while ( !stdinBuf.isEmpty() ) {
	delete stdinBuf.dequeue();
    }
    if ( notifierStdin ) {
	notifierStdin->setEnabled( FALSE );
	delete notifierStdin;
    }
    if ( notifierStdout ) {
	notifierStdout->setEnabled( FALSE );
	delete notifierStdout;
    }
    if ( notifierStderr ) {
	notifierStderr->setEnabled( FALSE );
	delete notifierStderr;
    }
    if( socketStdin[1] != 0 )
	::close( socketStdin[1] );
    if( socketStdout[0] != 0 )
	::close( socketStdout[0] );
    if( socketStderr[0] != 0 )
	::close( socketStderr[0] );
}

void QProcessPrivate::sigchldHnd()
{
    if ( !proclist )
	return;
    QProcess *proc;
    for ( proc=proclist->first(); proc!=0; proc=proclist->next() ) {
	if ( !proc->d->exitValuesCalculated && !proc->isRunning() ) {
#if defined(QPROCESS_DEBUG)
	    qDebug( "QProcessPrivate::sigchldHnd(): process exited" );
#endif
	    // read pending data
	    proc->socketRead( proc->d->socketStdout[0] );
	    proc->socketRead( proc->d->socketStderr[0] );

	    emit proc->processExited();
	    // the slot might have deleted the last process...
	    if ( !proclist )
		return;
	}
    }
}


bool QProcess::start()
{
#if defined(QPROCESS_DEBUG)
    qDebug( "QProcess::start()" );
#endif
    d->exitValuesCalculated = FALSE;
    d->exitStat = 0;
    d->exitNormal = FALSE;

    // cleanup the notifiers
    if ( d->notifierStdin ) {
	d->notifierStdin->setEnabled( FALSE );
	delete d->notifierStdin;
	d->notifierStdin = 0;
    }
    if ( d->notifierStdout ) {
	d->notifierStdout->setEnabled( FALSE );
	delete d->notifierStdout;
	d->notifierStdout = 0;
    }
    if ( d->notifierStderr ) {
	d->notifierStderr->setEnabled( FALSE );
	delete d->notifierStderr;
	d->notifierStderr = 0;
    }

#if defined(_OS_QNX_) // socketpair() is not yet implemented in QNX/RTP
    return FALSE;
#else
    // open sockets for piping
    if ( ::socketpair( AF_UNIX, SOCK_STREAM, 0, d->socketStdin ) ) {
	return FALSE;
    }
    if ( ::socketpair( AF_UNIX, SOCK_STREAM, 0, d->socketStdout ) ) {
	return FALSE;
    }
    if ( ::socketpair( AF_UNIX, SOCK_STREAM, 0, d->socketStderr ) ) {
	return FALSE;
    }
#endif

    // construct the arguments for exec
    const char** arglist = new const char*[ d->arguments.count() + 2 ];
    arglist[0] = d->command.latin1();
    int i = 1;
    for ( QStringList::Iterator it = d->arguments.begin(); it != d->arguments.end(); ++it ) {
	arglist[ i++ ] = (*it).latin1();
    }
    arglist[i] = 0;

    // fork and exec
    QApplication::flushX();
    d->pid = fork();
    if ( d->pid == 0 ) {
	// child
	::close( d->socketStdin[1] );
	::close( d->socketStdout[0] );
	::close( d->socketStderr[0] );
	::dup2( d->socketStdin[0], STDIN_FILENO );
	::dup2( d->socketStdout[1], STDOUT_FILENO );
	::dup2( d->socketStderr[1], STDERR_FILENO );
	::chdir( d->workingDir.absPath().latin1() );
	::execvp( d->command.latin1(), (char*const*)arglist ); // ### a hack
	::exit( -1 );
    } else if ( d->pid == -1 ) {
	// error forking
	::close( d->socketStdin[1] );
	::close( d->socketStdout[0] );
	::close( d->socketStderr[0] );
	::close( d->socketStdin[0] );
	::close( d->socketStdout[1] );
	::close( d->socketStderr[1] );
	delete[] arglist;
	return FALSE;
    }
    ::close( d->socketStdin[0] );
    ::close( d->socketStdout[1] );
    ::close( d->socketStderr[1] );
    // TODO? test if exec was successful

    // setup notifiers for the sockets
    d->notifierStdin = new QSocketNotifier( d->socketStdin[1],
	    QSocketNotifier::Write, this );
    d->notifierStdout = new QSocketNotifier( d->socketStdout[0],
	    QSocketNotifier::Read, this );
    d->notifierStderr = new QSocketNotifier( d->socketStderr[0],
	    QSocketNotifier::Read, this );
    connect( d->notifierStdin, SIGNAL(activated(int)),
	    this, SLOT(socketWrite(int)) );
    connect( d->notifierStdout, SIGNAL(activated(int)),
	    this, SLOT(socketRead(int)) );
    connect( d->notifierStderr, SIGNAL(activated(int)),
	    this, SLOT(socketRead(int)) );
    d->notifierStdin->setEnabled( TRUE );
    d->notifierStdout->setEnabled( TRUE );
    d->notifierStderr->setEnabled( TRUE );

    // cleanup and return
    delete[] arglist;
    return TRUE;
}


bool QProcess::hangUp()
{
    return ::kill( d->pid, SIGHUP ) == 0;
}

bool QProcess::kill()
{
    return ::kill( d->pid, SIGKILL ) == 0;
}

bool QProcess::isRunning()
{
    if ( d->exitValuesCalculated ) {
#if defined(QPROCESS_DEBUG)
	qDebug( "QProcess::isRunning(): FALSE (already computed)" );
#endif
	return FALSE;
    } else {
	int status;
	if ( ::waitpid( d->pid, &status, WNOHANG ) == d->pid )
	{
	    // compute the exit values
	    d->exitNormal = WIFEXITED( status ) != 0;
	    if ( d->exitNormal ) {
		d->exitStat = WEXITSTATUS( status );
	    }
	    d->exitValuesCalculated = TRUE;
#if defined(QPROCESS_DEBUG)
	    qDebug( "QProcess::isRunning(): FALSE" );
#endif
	    return FALSE;
	} else {
#if defined(QPROCESS_DEBUG)
	    qDebug( "QProcess::isRunning(): TRUE" );
#endif
	    return TRUE;
	}
    }
}

void QProcess::dataStdin( const QByteArray& buf )
{
#if defined(QPROCESS_DEBUG)
//    qDebug( "QProcess::dataStdin(): write to stdin (%d)", d->socketStdin[1] );
#endif
    d->stdinBuf.enqueue( new QByteArray(buf) );
    if ( d->notifierStdin != 0 )
        d->notifierStdin->setEnabled( TRUE );
    socketWrite( d->socketStdin[1] );
}


void QProcess::closeStdin()
{
    if ( d->socketStdin[1] !=0 ) {
	// ### what is with pending data?
	if ( d->notifierStdin ) {
	    d->notifierStdin->setEnabled( FALSE );
	    delete d->notifierStdin;
	    d->notifierStdin = 0;
	}
	if ( ::close( d->socketStdin[1] ) != 0 ) {
	    qWarning( "Could not close stdin of child process" );
	}
#if defined(QPROCESS_DEBUG)
	qDebug( "QProcess::closeStdin(): stdin (%d) closed", d->socketStdin[1] );
#endif
	d->socketStdin[1] =0 ;
    }
}


void QProcess::socketRead( int fd )
{
#if defined(QPROCESS_DEBUG)
    qDebug( "QProcess::socketRead(): %d", fd );
#endif
    if ( fd == 0 )
	return;
    const size_t bufsize = 4096;
    char *buffer = new char[bufsize];
    int n = ::read( fd, buffer, bufsize-1 );

    if ( n == 0 || n == -1 ) {
	if ( fd == d->socketStdout[0] ) {
#if defined(QPROCESS_DEBUG)
	    qDebug( "QProcess::socketRead(): stdout (%d) closed", fd );
#endif
	    d->notifierStdout->setEnabled( FALSE );
	    delete d->notifierStdout;
	    d->notifierStdout = 0;
	    ::close( d->socketStdout[0] );
	    d->socketStdout[0] = 0;
	    delete buffer;
	    return;
	} else {
#if defined(QPROCESS_DEBUG)
	    qDebug( "QProcess::socketRead(): stderr (%d) closed", fd );
#endif
	    d->notifierStderr->setEnabled( FALSE );
	    delete d->notifierStderr;
	    d->notifierStderr = 0;
	    ::close( d->socketStderr[0] );
	    d->socketStderr[0] = 0;
	    delete buffer;
	    return;
	}
    }

    buffer[n] = 0;
    QString str( QString::fromLocal8Bit( buffer ) );
    QByteArray buf;
    buf.assign( buffer, n );

    if ( fd == d->socketStdout[0] ) {
#if defined(QPROCESS_DEBUG)
	qDebug( "QProcess::socketRead(): read from stdout (%d)", fd );
#endif
	emit dataStdout( buf );
    } else {
#if defined(QPROCESS_DEBUG)
	qDebug( "QProcess::socketRead(): read from stderr (%d)", fd );
#endif
	emit dataStderr( buf );
    }
    if ( fd == d->socketStdout[0] ) {
	emit dataStdout( str );
    } else {
	emit dataStderr( str );
    }
}


void QProcess::socketWrite( int fd )
{
    if ( fd != d->socketStdin[1] || d->socketStdin[1] == 0 )
	return;
    if ( d->stdinBuf.isEmpty() ) {
	d->notifierStdin->setEnabled( FALSE );
	return;
    }
#if defined(QPROCESS_DEBUG)
    qDebug( "QProcess::socketWrite(): write to stdin (%d)", fd );
#endif
    d->stdinBufRead += write( fd,
	    d->stdinBuf.head()->data() + d->stdinBufRead,
	    d->stdinBuf.head()->size() - d->stdinBufRead );
    if ( d->stdinBufRead == (ssize_t)d->stdinBuf.head()->size() ) {
	d->stdinBufRead = 0;
	delete d->stdinBuf.dequeue();
	socketWrite( fd );
    }
}

// only used under Windows
void QProcess::timeout()
{
}