summaryrefslogtreecommitdiff
path: root/library/process_unix.cpp
blob: e0fbf8c3708340cb8aa8c11805f7ae0beed7f225 (plain)
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** 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.
**
**********************************************************************/

#ifndef QT_H
# include <qfeatures.h>
#endif // QT_H

#ifndef QT_NO_PROCESS

//#include "qplatformdefs.h"
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>

#include "process.h"
#include "qapplication.h"
//#include "qptrqueue.h"
//#include "qptrlist.h"
#include "qsocketnotifier.h"
#include "qtimer.h"
//#include "qcleanuphandler.h"
#include "qregexp.h"

#include <stdlib.h>
#include <errno.h>

#define QPtrList QList

//#define QT_QPROCESS_DEBUG


class Proc;
class ProcessManager;
class ProcessPrivate
{
public:
    ProcessPrivate();
    ~ProcessPrivate();

    void closeOpenSocketsForChild();
    void newProc( pid_t pid, Process *process );

    QByteArray bufStdout;
    QByteArray bufStderr;

    QSocketNotifier *notifierStdin;
    QSocketNotifier *notifierStdout;
    QSocketNotifier *notifierStderr;

    ssize_t stdinBufRead;
    Proc *proc;

    bool exitValuesCalculated;
    bool socketReadCalled;

    static ProcessManager *procManager;
};


/***********************************************************************
 *
 * Proc
 *
 **********************************************************************/
/*
  The class Process does not necessarily map exactly to the running
  child processes: if the process is finished, the Process class may still be
  there; furthermore a user can use Process to start more than one process.

  The helper-class Proc has the semantics that one instance of this class maps
  directly to a running child process.
*/
class Proc
{
public:
    Proc( pid_t p, Process *proc=0 ) : pid(p), process(proc)
    {
#if defined(QT_QPROCESS_DEBUG)
	qDebug( "Proc: Constructor for pid %d and Process %p", pid, process );
#endif
	socketStdin = 0;
	socketStdout = 0;
	socketStderr = 0;
    }
    ~Proc()
    {
#if defined(QT_QPROCESS_DEBUG)
	qDebug( "Proc: Destructor for pid %d and Process %p", pid, process );
#endif
	if ( process != 0 ) {
	    if ( process->d->notifierStdin )
		process->d->notifierStdin->setEnabled( FALSE );
	    if ( process->d->notifierStdout )
		process->d->notifierStdout->setEnabled( FALSE );
	    if ( process->d->notifierStderr )
		process->d->notifierStderr->setEnabled( FALSE );
	    process->d->proc = 0;
	}
	if( socketStdin != 0 )
	    ::close( socketStdin );
	// ### close these sockets even on parent exit or is it better only on
	// sigchld (but what do I have to do with them on exit then)?
	if( socketStdout != 0 )
	    ::close( socketStdout );
	if( socketStderr != 0 )
	    ::close( socketStderr );
    }

    pid_t pid;
    int socketStdin;
    int socketStdout;
    int socketStderr;
    Process *process;
};

/***********************************************************************
 *
 * ProcessManager
 *
 **********************************************************************/
class ProcessManager : public QObject
{
    Q_OBJECT

public:
    ProcessManager();
    ~ProcessManager();

    void append( Proc *p );
    void remove( Proc *p );

public slots:
    void removeMe();

public:
    struct sigaction oldactChld;
    struct sigaction oldactPipe;
    QPtrList<Proc> *procList;
    int sigchldFd[2];
};


ProcessManager::ProcessManager()
{
    procList = new QPtrList<Proc>;
    procList->setAutoDelete( TRUE );
}

ProcessManager::~ProcessManager()
{
    delete procList;
}

void ProcessManager::append( Proc *p )
{
    procList->append( p );
#if defined(QT_QPROCESS_DEBUG)
    qDebug( "ProcessManager: append process (procList.count(): %d)", procList->count() );
#endif
}

void ProcessManager::remove( Proc *p )
{
    procList->remove( p );
#if defined(QT_QPROCESS_DEBUG)
    qDebug( "ProcessManager: remove process (procList.count(): %d)", procList->count() );
#endif
    if ( procList->count() == 0 ) {
	QTimer::singleShot( 0, this, SLOT(removeMe()) );
    }
}

void ProcessManager::removeMe()
{
    ProcessPrivate::procManager = 0;
    delete this;
}

#include "process_unix.moc"


/***********************************************************************
 *
 * ProcessPrivate
 *
 **********************************************************************/
ProcessManager *ProcessPrivate::procManager = 0;

ProcessPrivate::ProcessPrivate()
{
#if defined(QT_QPROCESS_DEBUG)
    qDebug( "ProcessPrivate: Constructor" );
#endif
    stdinBufRead = 0;

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

    exitValuesCalculated = FALSE;
    socketReadCalled = FALSE;

    proc = 0;
}

ProcessPrivate::~ProcessPrivate()
{
#if defined(QT_QPROCESS_DEBUG)
    qDebug( "ProcessPrivate: Destructor" );
#endif

    if ( proc != 0 ) {
	if ( proc->socketStdin != 0 ) {
	    ::close( proc->socketStdin );
	    proc->socketStdin = 0;
	}
	proc->process = 0;
    }

    delete notifierStdin;
    delete notifierStdout;
    delete notifierStderr;
}

/*
  Closes all open sockets in the child process that are not needed by the child
  process. Otherwise one child may have an open socket on standard input, etc.
  of another child.
*/
void ProcessPrivate::closeOpenSocketsForChild()
{
    if ( procManager != 0 ) {
	if ( procManager->sigchldFd[0] != 0 )
	    ::close( procManager->sigchldFd[0] );
	if ( procManager->sigchldFd[1] != 0 )
	    ::close( procManager->sigchldFd[1] );

	// close also the sockets from other Process instances
	Proc *proc;
	for ( proc=procManager->procList->first(); proc!=0; proc=procManager->procList->next() ) {
	    ::close( proc->socketStdin );
	    ::close( proc->socketStdout );
	    ::close( proc->socketStderr );
	}
    }
}

void ProcessPrivate::newProc( pid_t pid, Process *process )
{
    proc = new Proc( pid, process );
    if ( procManager == 0 ) {
	procManager = new ProcessManager;
    }
    // the ProcessManager takes care of deleting the Proc instances
    procManager->append( proc );
}


/***********************************************************************
 *
 * Process
 *
 **********************************************************************/
/*!
  This private class does basic initialization.
*/
void Process::init()
{
    d = new ProcessPrivate();
    exitStat = 0;
    exitNormal = FALSE;
}

/*!
  Destroys the class.

  If the process is running, it is NOT terminated! Standard input, standard
  output and standard error of the process are closed.

  \sa hangUp() kill()
*/
Process::~Process()
{
    delete d;
}

bool Process::exec( const QByteArray& in, QByteArray& out, QStringList *env )
{
#if defined(QT_QPROCESS_DEBUG)
    qDebug( "Process::exec()" );
#endif

    int sStdin[2];
    int sStdout[2];
    int sStderr[2];

    // open sockets for piping
    if ( ::socketpair( AF_UNIX, SOCK_STREAM, 0, sStdin ) ) {
	return FALSE;
    }
    if ( ::socketpair( AF_UNIX, SOCK_STREAM, 0, sStderr ) ) {
	return FALSE;
    }
    if ( ::socketpair( AF_UNIX, SOCK_STREAM, 0, sStdout ) ) {
	return FALSE;
    }

    // the following pipe is only used to determine if the process could be
    // started
    int fd[2];
    if ( pipe( fd ) < 0 ) {
	// non critical error, go on
	fd[0] = 0;
	fd[1] = 0;
    }

    // construct the arguments for exec
    QCString *arglistQ = new QCString[ _arguments.count() + 1 ];
    const char** arglist = new const char*[ _arguments.count() + 1 ];
    int i = 0;
    for ( QStringList::Iterator it = _arguments.begin(); it != _arguments.end(); ++it ) {
	arglistQ[i] = (*it).local8Bit();
	arglist[i] = arglistQ[i];
#if defined(QT_QPROCESS_DEBUG)
	qDebug( "Process::start(): arg %d = %s", i, arglist[i] );
#endif
	i++;
    }
    arglist[i] = 0;

    // fork and exec
    QApplication::flushX();
    pid_t pid = fork();
    if ( pid == 0 ) {
	// child
	d->closeOpenSocketsForChild();
	::close( sStdin[1] );
	::close( sStdout[0] );
	::close( sStderr[0] );
	::dup2( sStdin[0], STDIN_FILENO );
	::dup2( sStdout[1], STDOUT_FILENO );
	::dup2( sStderr[1], STDERR_FILENO );
	if ( fd[0] )
	    ::close( fd[0] );
	if ( fd[1] )
	    ::fcntl( fd[1], F_SETFD, FD_CLOEXEC ); // close on exec shows sucess

	if ( env == 0 ) { // inherit environment and start process
	    ::execvp( arglist[0], (char*const*)arglist ); // ### cast not nice
	} else { // start process with environment settins as specified in env
	    // construct the environment for exec
	    int numEntries = env->count();
	    bool setLibraryPath =
		env->grep( QRegExp( "^LD_LIBRARY_PATH=" ) ).isEmpty() &&
		getenv( "LD_LIBRARY_PATH" ) != 0;
	    if ( setLibraryPath )
		numEntries++;
	    QCString *envlistQ = new QCString[ numEntries + 1 ];
	    const char** envlist = new const char*[ numEntries + 1 ];
	    int i = 0;
	    if ( setLibraryPath ) {
		envlistQ[i] = QString( "LD_LIBRARY_PATH=%1" ).arg( getenv( "LD_LIBRARY_PATH" ) ).local8Bit();
		envlist[i] = envlistQ[i];
		i++;
	    }
	    for ( QStringList::Iterator it = env->begin(); it != env->end(); ++it ) {
		envlistQ[i] = (*it).local8Bit();
		envlist[i] = envlistQ[i];
		i++;
	    }
	    envlist[i] = 0;

	    // look for the executable in the search path
	    if ( _arguments.count()>0 && getenv("PATH")!=0 ) {
		QString command = _arguments[0];
		if ( !command.contains( '/' ) ) {
		    QStringList pathList = QStringList::split( ':', getenv( "PATH" ) );
		    for (QStringList::Iterator it = pathList.begin(); it != pathList.end(); ++it ) {
			QFileInfo fileInfo( *it, command );
			if ( fileInfo.isExecutable() ) {
			    arglistQ[0] = fileInfo.filePath().local8Bit();
			    arglist[0] = arglistQ[0];
			    break;
			}
		    }
		}
	    }
	    ::execve( arglist[0], (char*const*)arglist, (char*const*)envlist ); // ### casts not nice
	}
	if ( fd[1] ) {
	    char buf = 0;
	    ::write( fd[1], &buf, 1 );
	    ::close( fd[1] );
	}
	::exit( -1 );
    } else if ( pid == -1 ) {
	// error forking
	goto error;
    }
    // test if exec was successful
    if ( fd[1] )
	close( fd[1] );
    if ( fd[0] ) {
	char buf;
	for ( ;; ) {
	    int n = ::read( fd[0], &buf, 1 );
	    if ( n==1 ) {
		// socket was not closed => error
		goto error;
	    } else if ( n==-1 ) {
		if ( errno==EAGAIN || errno==EINTR )
		    // try it again
		    continue;
	    }
	    break;
	}
    }


    ::close( sStdin[0] );
    ::close( sStdout[1] );
    ::close( sStderr[1] );

    // DIFFERENT

    {
	int written=0;
	int readden=0; // sic.
	while (1) {
	    const int bufsize=4096;
	    struct timeval *timeout = 0; // #### could have this
	    fd_set r; FD_ZERO(&r);
	    fd_set w; FD_ZERO(&w);
	    FD_SET( sStdout[0], &r );
	    out.resize( readden+bufsize );
	    if ( int(in.size()) > written )
		FD_SET( sStdin[1], &w );
	    int highest = QMAX(sStdout[0],sStdin[1])+1;
	    select(highest, &r, &w, 0, timeout);
	    if ( FD_ISSET( sStdout[0], &r ) ) {
		int n = read( sStdout[0], out.data()+readden, bufsize );
		if ( n > 0 )
		    readden += n;
		else
		    break;
	    }
	    if ( FD_ISSET( sStdin[1], &w ) ) {
		int n = write( sStdin[1], in.data()+written, in.size()-written );
		if ( n > 0 )
		    written += n;
	    }
	}
	out.resize(readden);
    }

    // cleanup and return
    delete[] arglistQ;
    delete[] arglist;
    ::close( sStdin[1] );
    ::close( sStdout[0] );
    ::close( sStderr[0] );
    return TRUE;

error:
#if defined(QT_QPROCESS_DEBUG)
    qDebug( "Process::start(): error starting process" );
#endif
    ::close( sStdin[1] );
    ::close( sStdout[0] );
    ::close( sStderr[0] );
    ::close( sStdin[0] );
    ::close( sStdout[1] );
    ::close( sStderr[1] );
    ::close( fd[0] );
    ::close( fd[1] );
    delete[] arglistQ;
    delete[] arglist;
    return FALSE;
}


#endif // QT_NO_PROCESS