1
/* ----------------------------------------------------------------------
2
* Definition of class VKProcess vk_process.h
3
* ---------------------------------------------------------------------
4
* This file is part of Valkyrie, a front-end for Valgrind
5
* Copyright (C) 2005-2008, OpenWorks LLP <info@open-works.co.uk>
6
* This program is released under the terms of the GNU GPL v.2
7
* See the file COPYING for the full license details.
8
* ---------------------------------------------------------------------
9
* This file is a re-implementation of QProcess:
10
* ** $Id: qt/qprocess.h 3.3.4 edited May 27 2003 $
11
* ** Created : 20000905
13
* ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
15
* ** This file may be distributed and/or modified under the terms of the
16
* ** GNU General Public License version 2 as published by the Free Software
17
* ** Foundation and appearing in the file COPYING included in the
18
* ** packaging of this file.
25
#include <qstringlist.h>
27
#include <qptrqueue.h>
28
#include <qsocketnotifier.h>
31
#include <stdlib.h> // ssize_t
40
void append( QByteArray *ba );
43
bool consumeBytes( Q_ULONG nbytes, char *sink );
45
bool scanNewline( QByteArray *store );
46
bool canReadLine() const;
48
int ungetch( int ch );
50
QIODevice::Offset size() const;
53
QPtrList<QByteArray> *buf;
54
QIODevice::Offset _size;
55
QIODevice::Offset _index;
58
inline void VKMembuf::append( QByteArray *ba )
59
{ buf->append( ba ); _size += ba->size(); }
61
inline void VKMembuf::clear()
62
{ buf->clear(); _size=0; _index=0; }
64
inline QByteArray VKMembuf::readAll()
65
{ QByteArray ba(_size); consumeBytes(_size,ba.data()); return ba; }
67
inline bool VKMembuf::canReadLine() const
68
{ return ((VKMembuf*)this)->scanNewline( 0 ); }
70
inline QIODevice::Offset VKMembuf::size() const
76
/* class VKProc --------------------------------------------------------------
77
The class VKProcess does not necessarily map exactly to the running
78
child processes: if the process is finished, the VKProcess class
79
may still be there; furthermore a user can use VKProcess to start
80
more than one process.
81
The helper-class VKProc has the semantics that one instance of this
82
class maps directly to a running child process. */
88
VKProc( pid_t p, VKProcess *proc=0 );
102
/* class VKProcessManager ---------------------------------------------------- */
103
class VKProcessManager : public QObject
110
void append( VKProc *p );
111
void remove( VKProc *p );
117
void sigchldHnd( int );
120
struct sigaction oldactChld;
121
struct sigaction oldactPipe;
122
QPtrList<VKProc> *procList;
132
/* class VKProcess ----------------------------------------------------------- */
133
class VKProcessPrivate
139
void closeOpenSocketsForChild();
140
void newProc( pid_t pid, VKProcess *process );
146
QPtrQueue<QByteArray> fdinBuf;
147
QPtrQueue<QByteArray> stdinBuf;
149
QSocketNotifier *notifierFDin;
150
QSocketNotifier *notifierFDout;
151
QSocketNotifier *notifierStdin;
152
QSocketNotifier *notifierStdout;
153
QSocketNotifier *notifierStderr;
156
ssize_t stdinBufRead;
159
bool exitValuesCalculated;
160
bool socketReadCalled;
162
static VKProcessManager *procManager;
166
class VKProcess : public QObject
170
VKProcess( QObject *parent=0, const char *name=0 );
171
VKProcess( const QString& arg0, QObject *parent=0, const char *name=0 );
172
VKProcess( const QStringList& args, QObject *parent=0, const char *name=0 );
175
// set and get the arguments and working directory
176
QStringList arguments() const;
177
void clearArguments();
178
virtual void setArguments( const QStringList& args );
179
virtual void addArgument( const QString& arg );
181
QDir workingDirectory() const;
182
virtual void setWorkingDirectory( const QDir& dir );
185
// set and get the comms wanted
186
enum Communication { Stdin=0x01, Stdout=0x02, Stderr=0x04, DupStderr=0x08, FDin=0x10, FDout=0x20 };
187
int setCommunication( int c );
188
int communication() const;
189
void setFDin( int fd );
190
void setFDout( int fd );
194
// start the execution
195
virtual bool start( QStringList *env=0 );
196
virtual bool launch( const QString& buf, QStringList *env=0 );
197
virtual bool launch( const QByteArray& buf, QStringList *env=0 );
199
// inquire the status
200
bool isRunning() const;
201
bool normalExit() const;
202
int exitStatus() const;
205
virtual QByteArray readFDout();
206
virtual QByteArray readStdout();
207
virtual QByteArray readStderr();
209
bool canReadLineFDout() const;
210
bool canReadLineStdout() const;
211
bool canReadLineStderr() const;
213
virtual QString readLineFDout();
214
virtual QString readLineStdout();
215
virtual QString readLineStderr();
217
// get platform dependent process information
218
#if defined(Q_OS_WIN32)
223
PID processIdentifier();
229
void readyReadFDout();
230
void readyReadStdout();
231
void readyReadStderr();
232
void processExited();
235
void launchFinished();
239
void stop( int msec_kill_timeout=2000 );
240
void tryTerminate() const;
244
virtual void writeToFDin( const QByteArray& buf );
245
virtual void writeToFDin( const QString& buf );
246
virtual void closeFDin();
247
virtual void writeToStdin( const QByteArray& buf );
248
virtual void writeToStdin( const QString& buf );
249
virtual void closeStdin();
251
protected: // ### or private?
252
void connectNotify( const char * signal );
253
void disconnectNotify( const char * signal );
256
void reprioritiseComms();
258
void setIoRedirection( bool value );
259
void setNotifyOnExit( bool value );
260
void setWroteFDinConnected( bool value );
261
void setWroteStdinConnected( bool value );
265
#if defined(Q_OS_WIN32)
266
uint readStddev( HANDLE dev, char *buf, uint bytes );
268
VKMembuf* membufFDout();
269
VKMembuf* membufStdout();
270
VKMembuf* membufStderr();
273
void socketRead( int fd );
274
void socketWrite( int fd );
276
void closeStdinLaunch();
283
QStringList _arguments;
285
int exitStat; // exit status
286
bool exitNormal; // normal exit?
287
bool ioRedirection; // automatically set be (dis)connectNotify
288
bool notifyOnExit; // automatically set be (dis)connectNotify
289
bool wroteToFDinConnected; // automatically set be (dis)connectNotify
290
bool wroteToStdinConnected; // automatically set be (dis)connectNotify
292
bool readFDoutCalled;
293
bool readStdoutCalled;
294
bool readStderrCalled;
296
int filedesc_in, filedesc_out;
298
friend class VKProcessPrivate;
299
#if defined(Q_OS_UNIX)
300
friend class VKProcessManager;
304
#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator=
305
VKProcess( const VKProcess & );
306
VKProcess &operator=( const VKProcess & );
309
/* Keep track of FDout/in disabling stdout/err/in */
316
#endif // VK_PROCESS_H