Qt 4.8
Classes | Public Functions | Static Public Functions | Public Variables | List of all members
QProcessPrivate Class Reference

#include <qprocess_p.h>

Inheritance diagram for QProcessPrivate:
QIODevicePrivate QObjectPrivate QObjectData

Classes

struct  Channel
 

Public Functions

bool _q_canReadStandardError ()
 
bool _q_canReadStandardOutput ()
 
bool _q_canWrite ()
 
void _q_notified ()
 
bool _q_processDied ()
 
bool _q_startupNotification ()
 
qint64 bytesAvailableFromStderr () const
 
qint64 bytesAvailableFromStdout () const
 
void cleanup ()
 
void closeWriteChannel ()
 
bool createChannel (Channel &channel)
 
void destroyPipe (Q_PIPE pipe[2])
 
void execChild (const char *workingDirectory, char **path, char **argv, char **envp)
 
void findExitCode ()
 
void flushPipeWriter ()
 
void killProcess ()
 
qint64 pipeWriterBytesToWrite () const
 
bool processStarted ()
 
 QProcessPrivate ()
 
qint64 readFromStderr (char *data, qint64 maxlen)
 
qint64 readFromStdout (char *data, qint64 maxlen)
 
void startProcess ()
 
void terminateProcess ()
 
bool waitForBytesWritten (int msecs=30000)
 
bool waitForDeadChild ()
 
bool waitForFinished (int msecs=30000)
 
bool waitForReadyRead (int msecs=30000)
 
bool waitForStarted (int msecs=30000)
 
bool waitForWrite (int msecs=30000)
 
qint64 writeToStdin (const char *data, qint64 maxlen)
 
virtual ~QProcessPrivate ()
 
- Public Functions inherited from QIODevicePrivate
bool isSequential () const
 
virtual qint64 peek (char *data, qint64 maxSize)
 
virtual QByteArray peek (qint64 maxSize)
 
virtual bool putCharHelper (char c)
 
 QIODevicePrivate ()
 
virtual ~QIODevicePrivate ()
 
- Public Functions inherited from QObjectPrivate
void _q_reregisterTimers (void *pointer)
 
void addConnection (int signal, Connection *c)
 
void cleanConnectionLists ()
 
void connectNotify (const char *signal)
 
void deleteChildren ()
 
void disconnectNotify (const char *signal)
 
bool isSender (const QObject *receiver, const char *signal) const
 
bool isSignalConnected (uint signalIdx) const
 Returns true if the signal with index signal_index from object sender is connected. More...
 
void moveToThread_helper ()
 
 QObjectPrivate (int version=QObjectPrivateVersion)
 
QObjectList receiverList (const char *signal) const
 
QObjectList senderList () const
 
void setParent_helper (QObject *)
 
void setThreadData_helper (QThreadData *currentData, QThreadData *targetData)
 
int signalIndex (const char *signalName) const
 Returns the signal index used in the internal connectionLists vector. More...
 
virtual ~QObjectPrivate ()
 
- Public Functions inherited from QObjectData
virtual ~QObjectData ()=0
 

Static Public Functions

static void initializeProcessManager ()
 
static bool startDetached (const QString &program, const QStringList &arguments, const QString &workingDirectory=QString(), qint64 *pid=0)
 
- Static Public Functions inherited from QObjectPrivate
static void clearGuards (QObject *)
 
static QObjectPrivateget (QObject *o)
 
static void resetCurrentSender (QObject *receiver, Sender *currentSender, Sender *previousSender)
 
static SendersetCurrentSender (QObject *receiver, Sender *sender)
 
static void signalSignature (const QMetaMethod &signal, QVarLengthArray< char > *result)
 

Public Variables

QStringList arguments
 
Q_PIPE childStartedPipe [2]
 
bool crashed
 
QSocketNotifierdeathNotifier
 
Q_PIPE deathPipe [2]
 
bool dying
 
bool emittedBytesWritten
 
bool emittedReadyRead
 
QProcessEnvironment environment
 
QRingBuffer errorReadBuffer
 
int exitCode
 
QProcess::ExitStatus exitStatus
 
QString nativeArguments
 
QTimernotifier
 
QRingBuffer outputReadBuffer
 
Q_PID pid
 
QWindowsPipeWriterpipeWriter
 
QProcess::ProcessChannel processChannel
 
QProcess::ProcessChannelMode processChannelMode
 
QProcess::ProcessError processError
 
QWinEventNotifierprocessFinishedNotifier
 
QProcess::ProcessState processState
 
QString program
 
int sequenceNumber
 
int serial
 
QSocketNotifierstartupSocketNotifier
 
Channel stderrChannel
 
Channel stdinChannel
 
Channel stdoutChannel
 
QString workingDirectory
 
QRingBuffer writeBuffer
 
- Public Variables inherited from QIODevicePrivate
AccessMode accessMode
 
bool baseReadLineDataCalled
 
QIODevicePrivateLinearBuffer buffer
 
qint64 devicePos
 
QString errorString
 
bool firstRead
 
QIODevice::OpenMode openMode
 
qint64pDevicePos
 
qint64 pos
 
qint64pPos
 
qint64 seqDumpPos
 
- Public Variables inherited from QObjectPrivate
union {
   QObject *   currentChildBeingDeleted
 
   QAbstractDeclarativeData *   declarativeData
 
}; 
 
quint32 connectedSignals [2]
 
QObjectConnectionListVectorconnectionLists
 
SendercurrentSender
 
QList< QPointer< QObject > > eventFilters
 
ExtraDataextraData
 
QString objectName
 
Connectionsenders
 
QAtomicPointer< QtSharedPointer::ExternalRefCountData > sharedRefcount
 
QThreadDatathreadData
 
void * unused
 
- Public Variables inherited from QObjectData
uint blockSig: 1
 
QObjectList children
 
uint hasGuards: 1
 
uint inEventHandler: 1
 
uint inThreadChangeEvent: 1
 
uint isWidget: 1
 
QMetaObjectmetaObject
 
uint ownObjectName: 1
 
QObjectparent
 
uint pendTimer: 1
 
int postedEvents
 
QObjectq_ptr
 
uint receiveChildEvents: 1
 
uint sendChildEvents: 1
 
uint unused: 22
 
uint wasDeleted: 1
 

Additional Inherited Members

- Public Types inherited from QIODevicePrivate
enum  AccessMode { Unset, Sequential, RandomAccess }
 
- Public Types inherited from QObjectPrivate
typedef void(* StaticMetaCallFunction) (QObject *, QMetaObject::Call, int, void **)
 

Detailed Description

Definition at line 240 of file qprocess_p.h.

Constructors and Destructors

◆ QProcessPrivate()

QProcessPrivate::QProcessPrivate ( )
Warning
This function is not part of the public interface.

Definition at line 820 of file qprocess.cpp.

821 {
826  pid = 0;
827  sequenceNumber = 0;
828  exitCode = 0;
831  deathNotifier = 0;
832  notifier = 0;
833  pipeWriter = 0;
838  exitCode = 0;
839  crashed = false;
840  dying = false;
841  emittedReadyRead = false;
842  emittedBytesWritten = false;
843 #ifdef Q_WS_WIN
844  pipeWriter = 0;
846 #endif // Q_WS_WIN
847 #ifdef Q_OS_UNIX
848  serial = 0;
849 #endif
850 #ifdef Q_OS_SYMBIAN
851  symbianProcess = NULL;
852  processLaunched = false;
853 #endif
854 }
bool emittedBytesWritten
Definition: qprocess_p.h:315
QProcess::ProcessState processState
Definition: qprocess_p.h:308
#define INVALID_Q_PIPE
Definition: qprocess_p.h:69
QProcess::ProcessChannel processChannel
Definition: qprocess_p.h:305
QWindowsPipeWriter * pipeWriter
Definition: qprocess_p.h:343
QProcess::ProcessError processError
Definition: qprocess_p.h:307
bool emittedReadyRead
Definition: qprocess_p.h:314
QProcess::ProcessChannelMode processChannelMode
Definition: qprocess_p.h:306
QProcess::ExitStatus exitStatus
Definition: qprocess_p.h:368
Q_PIPE childStartedPipe[2]
Definition: qprocess_p.h:334
QSocketNotifier * startupSocketNotifier
Definition: qprocess_p.h:338
QWinEventNotifier * processFinishedNotifier
Definition: qprocess_p.h:344
QSocketNotifier * deathNotifier
Definition: qprocess_p.h:339
Q_PIPE deathPipe[2]
Definition: qprocess_p.h:335
QTimer * notifier
Definition: qprocess_p.h:342

◆ ~QProcessPrivate()

QProcessPrivate::~QProcessPrivate ( )
virtual
Warning
This function is not part of the public interface.

Definition at line 858 of file qprocess.cpp.

Functions

◆ _q_canReadStandardError()

bool QProcessPrivate::_q_canReadStandardError ( )
Warning
This function is not part of the public interface.

Definition at line 992 of file qprocess.cpp.

Referenced by pipeWriterBytesToWrite(), and qt_terminateApp().

993 {
994  Q_Q(QProcess);
995  qint64 available = bytesAvailableFromStderr();
996  if (available == 0) {
1000  return false;
1001  }
1002 
1003  char *ptr = errorReadBuffer.reserve(available);
1004  qint64 readBytes = readFromStderr(ptr, available);
1005  if (readBytes == -1) {
1007  q->setErrorString(QProcess::tr("Error reading from process"));
1008  emit q->error(processError);
1009  return false;
1010  }
1011  if (stderrChannel.closed) {
1012  errorReadBuffer.chop(readBytes);
1013  return false;
1014  }
1015 
1016  errorReadBuffer.chop(available - readBytes);
1017 
1018  bool didRead = false;
1019  if (readBytes == 0) {
1020  if (stderrChannel.notifier)
1022  } else if (processChannel == QProcess::StandardError) {
1023  didRead = true;
1024  if (!emittedReadyRead) {
1025  emittedReadyRead = true;
1026  emit q->readyRead();
1027  emittedReadyRead = false;
1028  }
1029  }
1030  emit q->readyReadStandardError();
1031  return didRead;
1032 }
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
qint64 bytesAvailableFromStderr() const
QSocketNotifier * notifier
Definition: qprocess_p.h:286
#define Q_Q(Class)
Definition: qglobal.h:2483
QProcess::ProcessChannel processChannel
Definition: qprocess_p.h:305
char * reserve(int bytes)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
#define emit
Definition: qobjectdefs.h:76
bool emittedReadyRead
Definition: qprocess_p.h:314
const T * ptr(const T &t)
__int64 qint64
Definition: qglobal.h:942
qint64 readFromStderr(char *data, qint64 maxlen)
void chop(int bytes)
Channel stderrChannel
Definition: qprocess_p.h:319
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
QRingBuffer errorReadBuffer
Definition: qprocess_p.h:331
void destroyPipe(Q_PIPE pipe[2])
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

◆ _q_canReadStandardOutput()

bool QProcessPrivate::_q_canReadStandardOutput ( )
Warning
This function is not part of the public interface.

Definition at line 937 of file qprocess.cpp.

Referenced by pipeWriterBytesToWrite(), and qt_terminateApp().

938 {
939  Q_Q(QProcess);
940  qint64 available = bytesAvailableFromStdout();
941  if (available == 0) {
945 #if defined QPROCESS_DEBUG
946  qDebug("QProcessPrivate::canReadStandardOutput(), 0 bytes available");
947 #endif
948  return false;
949  }
950 
951  char *ptr = outputReadBuffer.reserve(available);
952  qint64 readBytes = readFromStdout(ptr, available);
953  if (readBytes == -1) {
955  q->setErrorString(QProcess::tr("Error reading from process"));
956  emit q->error(processError);
957 #if defined QPROCESS_DEBUG
958  qDebug("QProcessPrivate::canReadStandardOutput(), failed to read from the process");
959 #endif
960  return false;
961  }
962 #if defined QPROCESS_DEBUG
963  qDebug("QProcessPrivate::canReadStandardOutput(), read %d bytes from the process' output",
964  int(readBytes));
965 #endif
966 
967  if (stdoutChannel.closed) {
968  outputReadBuffer.chop(readBytes);
969  return false;
970  }
971 
972  outputReadBuffer.chop(available - readBytes);
973 
974  bool didRead = false;
975  if (readBytes == 0) {
979  didRead = true;
980  if (!emittedReadyRead) {
981  emittedReadyRead = true;
982  emit q->readyRead();
983  emittedReadyRead = false;
984  }
985  }
986  emit q->readyReadStandardOutput();
987  return didRead;
988 }
qint64 bytesAvailableFromStdout() const
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QSocketNotifier * notifier
Definition: qprocess_p.h:286
#define Q_Q(Class)
Definition: qglobal.h:2483
QProcess::ProcessChannel processChannel
Definition: qprocess_p.h:305
Q_CORE_EXPORT void qDebug(const char *,...)
char * reserve(int bytes)
qint64 readFromStdout(char *data, qint64 maxlen)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
Channel stdoutChannel
Definition: qprocess_p.h:318
#define emit
Definition: qobjectdefs.h:76
bool emittedReadyRead
Definition: qprocess_p.h:314
const T * ptr(const T &t)
__int64 qint64
Definition: qglobal.h:942
void chop(int bytes)
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
void destroyPipe(Q_PIPE pipe[2])
QRingBuffer outputReadBuffer
Definition: qprocess_p.h:330
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

◆ _q_canWrite()

bool QProcessPrivate::_q_canWrite ( )
Warning
This function is not part of the public interface.

Definition at line 1036 of file qprocess.cpp.

Referenced by pipeWriterBytesToWrite(), and qt_terminateApp().

1037 {
1038  Q_Q(QProcess);
1039  if (stdinChannel.notifier)
1041 
1042  if (writeBuffer.isEmpty()) {
1043 #if defined QPROCESS_DEBUG
1044  qDebug("QProcessPrivate::canWrite(), not writing anything (empty write buffer).");
1045 #endif
1046  return false;
1047  }
1048 
1051  if (written < 0) {
1054  q->setErrorString(QProcess::tr("Error writing to process"));
1055  emit q->error(processError);
1056  return false;
1057  }
1058 
1059 #if defined QPROCESS_DEBUG
1060  qDebug("QProcessPrivate::canWrite(), wrote %d bytes to the process input", int(written));
1061 #endif
1062 
1063  if (written != 0) {
1064  writeBuffer.free(written);
1065  if (!emittedBytesWritten) {
1066  emittedBytesWritten = true;
1067  emit q->bytesWritten(written);
1068  emittedBytesWritten = false;
1069  }
1070  }
1075  return true;
1076 }
const char * readPointer() const
Definition: qringbuffer_p.h:73
bool emittedBytesWritten
Definition: qprocess_p.h:315
int nextDataBlockSize() const
Definition: qringbuffer_p.h:69
Channel stdinChannel
Definition: qprocess_p.h:317
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
bool isEmpty() const
qint64 writeToStdin(const char *data, qint64 maxlen)
QSocketNotifier * notifier
Definition: qprocess_p.h:286
#define Q_Q(Class)
Definition: qglobal.h:2483
Q_CORE_EXPORT void qDebug(const char *,...)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
void closeWriteChannel()
Definition: qprocess.cpp:1178
#define emit
Definition: qobjectdefs.h:76
__int64 qint64
Definition: qglobal.h:942
void free(int bytes)
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
void destroyPipe(Q_PIPE pipe[2])
QRingBuffer writeBuffer
Definition: qprocess_p.h:332
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

◆ _q_notified()

void QProcessPrivate::_q_notified ( )

Definition at line 1325 of file qprocess_unix.cpp.

Referenced by pipeWriterBytesToWrite(), and qt_create_commandline().

1326 {
1327 }

◆ _q_processDied()

bool QProcessPrivate::_q_processDied ( )
Warning
This function is not part of the public interface.

Definition at line 1080 of file qprocess.cpp.

Referenced by qt_create_commandline(), and qt_terminateApp().

1081 {
1082  Q_Q(QProcess);
1083 #if defined QPROCESS_DEBUG
1084  qDebug("QProcessPrivate::_q_processDied()");
1085 #endif
1086 #ifdef Q_OS_UNIX
1087  if (!waitForDeadChild())
1088  return false;
1089 #endif
1090 #ifdef Q_OS_WIN
1093 #endif
1094 
1095  // the process may have died before it got a chance to report that it was
1096  // either running or stopped, so we will call _q_startupNotification() and
1097  // give it a chance to emit started() or error(FailedToStart).
1099  if (!_q_startupNotification())
1100  return true;
1101  }
1102 
1103  if (dying) {
1104  // at this point we know the process is dead. prevent
1105  // reentering this slot recursively by calling waitForFinished()
1106  // or opening a dialog inside slots connected to the readyRead
1107  // signals emitted below.
1108  return true;
1109  }
1110  dying = true;
1111 
1112  // in case there is data in the pipe line and this slot by chance
1113  // got called before the read notifications, call these two slots
1114  // so the data is made available before the process dies.
1117 
1118  findExitCode();
1119 
1120  if (crashed) {
1123  q->setErrorString(QProcess::tr("Process crashed"));
1124  emit q->error(processError);
1125  }
1126 
1127  bool wasRunning = (processState == QProcess::Running);
1128 
1129  cleanup();
1130 
1131  if (wasRunning) {
1132  // we received EOF now:
1133  emit q->readChannelFinished();
1134  // in the future:
1135  //emit q->standardOutputClosed();
1136  //emit q->standardErrorClosed();
1137 
1138  emit q->finished(exitCode);
1139  emit q->finished(exitCode, exitStatus);
1140  }
1141 #if defined QPROCESS_DEBUG
1142  qDebug("QProcessPrivate::_q_processDied() process is dead");
1143 #endif
1144  return true;
1145 }
bool _q_canReadStandardOutput()
Definition: qprocess.cpp:937
QProcess::ProcessState processState
Definition: qprocess_p.h:308
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
#define Q_Q(Class)
Definition: qglobal.h:2483
Q_CORE_EXPORT void qDebug(const char *,...)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
#define emit
Definition: qobjectdefs.h:76
bool _q_startupNotification()
Definition: qprocess.cpp:1149
QProcess::ExitStatus exitStatus
Definition: qprocess_p.h:368
bool _q_canReadStandardError()
Definition: qprocess.cpp:992
QWinEventNotifier * processFinishedNotifier
Definition: qprocess_p.h:344
void setEnabled(bool enable)
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

◆ _q_startupNotification()

bool QProcessPrivate::_q_startupNotification ( )
Warning
This function is not part of the public interface.

Definition at line 1149 of file qprocess.cpp.

Referenced by qt_create_commandline().

1150 {
1151  Q_Q(QProcess);
1152 #if defined QPROCESS_DEBUG
1153  qDebug("QProcessPrivate::startupNotification()");
1154 #endif
1155 
1158  if (processStarted()) {
1159  q->setProcessState(QProcess::Running);
1160  emit q->started();
1161  return true;
1162  }
1163 
1164  q->setProcessState(QProcess::NotRunning);
1166  emit q->error(processError);
1167 #ifdef Q_OS_UNIX
1168  // make sure the process manager removes this entry
1169  waitForDeadChild();
1170  findExitCode();
1171 #endif
1172  cleanup();
1173  return false;
1174 }
#define Q_Q(Class)
Definition: qglobal.h:2483
Q_CORE_EXPORT void qDebug(const char *,...)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
#define emit
Definition: qobjectdefs.h:76
QSocketNotifier * startupSocketNotifier
Definition: qprocess_p.h:338
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

◆ bytesAvailableFromStderr()

qint64 QProcessPrivate::bytesAvailableFromStderr ( ) const

Definition at line 963 of file qprocess_unix.cpp.

Referenced by pipeWriterBytesToWrite(), qt_create_commandline(), and qt_terminateApp().

964 {
965  int nbytes = 0;
966  qint64 available = 0;
967  if (::ioctl(stderrChannel.pipe[0], FIONREAD, (char *) &nbytes) >= 0)
968  available = (qint64) nbytes;
969 #if defined (QPROCESS_DEBUG)
970  qDebug("QProcessPrivate::bytesAvailableFromStderr() == %lld", available);
971 #endif
972  return available;
973 }
Q_CORE_EXPORT void qDebug(const char *,...)
__int64 qint64
Definition: qglobal.h:942
Channel stderrChannel
Definition: qprocess_p.h:319

◆ bytesAvailableFromStdout()

qint64 QProcessPrivate::bytesAvailableFromStdout ( ) const

Definition at line 951 of file qprocess_unix.cpp.

Referenced by pipeWriterBytesToWrite(), qt_create_commandline(), and qt_terminateApp().

952 {
953  int nbytes = 0;
954  qint64 available = 0;
955  if (::ioctl(stdoutChannel.pipe[0], FIONREAD, (char *) &nbytes) >= 0)
956  available = (qint64) nbytes;
957 #if defined (QPROCESS_DEBUG)
958  qDebug("QProcessPrivate::bytesAvailableFromStdout() == %lld", available);
959 #endif
960  return available;
961 }
Q_CORE_EXPORT void qDebug(const char *,...)
Channel stdoutChannel
Definition: qprocess_p.h:318
__int64 qint64
Definition: qglobal.h:942

◆ cleanup()

void QProcessPrivate::cleanup ( )
Warning
This function is not part of the public interface.

Definition at line 868 of file qprocess.cpp.

Referenced by qt_create_commandline(), and qt_create_pipe().

869 {
870  q_func()->setProcessState(QProcess::NotRunning);
871 #ifdef Q_OS_WIN
872  if (pid) {
873  CloseHandle(pid->hThread);
874  CloseHandle(pid->hProcess);
875  delete pid;
876  pid = 0;
877  }
882  }
883 
884 #endif
885  pid = 0;
886  sequenceNumber = 0;
887  dying = false;
888 
889  if (stdoutChannel.notifier) {
893  }
894  if (stderrChannel.notifier) {
898  }
899  if (stdinChannel.notifier) {
903  }
904  if (startupSocketNotifier) {
908  }
909  if (deathNotifier) {
910  deathNotifier->setEnabled(false);
912  deathNotifier = 0;
913  }
914  if (notifier) {
916  notifier = 0;
917  }
923 #ifdef Q_OS_UNIX
924  serial = 0;
925 #endif
926 #ifdef Q_OS_SYMBIAN
927  if (symbianProcess) {
928  symbianProcess->Close();
929  delete symbianProcess;
930  symbianProcess = NULL;
931  }
932 #endif
933 }
Channel stdinChannel
Definition: qprocess_p.h:317
QSocketNotifier * notifier
Definition: qprocess_p.h:286
Channel stdoutChannel
Definition: qprocess_p.h:318
void qDeleteInEventHandler(QObject *o)
Definition: qobject.cpp:4348
Q_PIPE childStartedPipe[2]
Definition: qprocess_p.h:334
Channel stderrChannel
Definition: qprocess_p.h:319
QSocketNotifier * startupSocketNotifier
Definition: qprocess_p.h:338
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
QWinEventNotifier * processFinishedNotifier
Definition: qprocess_p.h:344
QSocketNotifier * deathNotifier
Definition: qprocess_p.h:339
Q_PIPE deathPipe[2]
Definition: qprocess_p.h:335
void destroyPipe(Q_PIPE pipe[2])
void setEnabled(bool enable)
QTimer * notifier
Definition: qprocess_p.h:342

◆ closeWriteChannel()

void QProcessPrivate::closeWriteChannel ( )
Warning
This function is not part of the public interface.

Definition at line 1178 of file qprocess.cpp.

1179 {
1180 #if defined QPROCESS_DEBUG
1181  qDebug("QProcessPrivate::closeWriteChannel()");
1182 #endif
1183  if (stdinChannel.notifier) {
1184  extern void qDeleteInEventHandler(QObject *o);
1186  if (stdinChannel.notifier) {
1188  stdinChannel.notifier = 0;
1189  }
1190  }
1191 #ifdef Q_OS_WIN
1192  // ### Find a better fix, feeding the process little by little
1193  // instead.
1194  flushPipeWriter();
1195 #endif
1197 }
Channel stdinChannel
Definition: qprocess_p.h:317
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QSocketNotifier * notifier
Definition: qprocess_p.h:286
Q_CORE_EXPORT void qDebug(const char *,...)
void qDeleteInEventHandler(QObject *o)
Definition: qobject.cpp:4348
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
void destroyPipe(Q_PIPE pipe[2])

◆ createChannel()

bool QProcessPrivate::createChannel ( Channel channel)

Definition at line 383 of file qprocess_unix.cpp.

Referenced by qt_create_commandline(), and qt_create_pipe().

384 {
385  Q_Q(QProcess);
386 
388  channel.pipe[0] = -1;
389  channel.pipe[1] = -1;
390  return true;
391  }
392 
393  if (channel.type == Channel::Normal) {
394  // we're piping this channel to our own process
395  if (qt_create_pipe(channel.pipe) != 0)
396  return false;
397 
398  // create the socket notifiers
400  if (&channel == &stdinChannel) {
401  channel.notifier = new QSocketNotifier(channel.pipe[1],
403  channel.notifier->setEnabled(false);
404  QObject::connect(channel.notifier, SIGNAL(activated(int)),
405  q, SLOT(_q_canWrite()));
406  } else {
407  channel.notifier = new QSocketNotifier(channel.pipe[0],
409  const char *receiver;
410  if (&channel == &stdoutChannel)
411  receiver = SLOT(_q_canReadStandardOutput());
412  else
413  receiver = SLOT(_q_canReadStandardError());
414  QObject::connect(channel.notifier, SIGNAL(activated(int)),
415  q, receiver);
416  }
417  }
418 
419  return true;
420  } else if (channel.type == Channel::Redirect) {
421  // we're redirecting the channel to/from a file
422  QByteArray fname = QFile::encodeName(channel.file);
423 
424  if (&channel == &stdinChannel) {
425  // try to open in read-only mode
426  channel.pipe[1] = -1;
427  if ( (channel.pipe[0] = qt_safe_open(fname, O_RDONLY)) != -1)
428  return true; // success
429 
430  q->setErrorString(QProcess::tr("Could not open input redirection for reading"));
431  } else {
432  int mode = O_WRONLY | O_CREAT;
433  if (channel.append)
434  mode |= O_APPEND;
435  else
436  mode |= O_TRUNC;
437 
438  channel.pipe[0] = -1;
439  if ( (channel.pipe[1] = qt_safe_open(fname, mode, 0666)) != -1)
440  return true; // success
441 
442  q->setErrorString(QProcess::tr("Could not open output redirection for writing"));
443  }
444 
445  // could not open file
447  emit q->error(processError);
448  cleanup();
449  return false;
450  } else {
451  Q_ASSERT_X(channel.process, "QProcess::start", "Internal error");
452 
453  Channel *source;
454  Channel *sink;
455 
456  if (channel.type == Channel::PipeSource) {
457  // we are the source
458  source = &channel;
459  sink = &channel.process->stdinChannel;
460 
461  Q_ASSERT(source == &stdoutChannel);
462  Q_ASSERT(sink->process == this && sink->type == Channel::PipeSink);
463  } else {
464  // we are the sink;
465  source = &channel.process->stdoutChannel;
466  sink = &channel;
467 
468  Q_ASSERT(sink == &stdinChannel);
469  Q_ASSERT(source->process == this && source->type == Channel::PipeSource);
470  }
471 
472  if (source->pipe[1] != INVALID_Q_PIPE || sink->pipe[0] != INVALID_Q_PIPE) {
473  // already created, do nothing
474  return true;
475  } else {
476  Q_ASSERT(source->pipe[0] == INVALID_Q_PIPE && source->pipe[1] == INVALID_Q_PIPE);
477  Q_ASSERT(sink->pipe[0] == INVALID_Q_PIPE && sink->pipe[1] == INVALID_Q_PIPE);
478 
479  Q_PIPE pipe[2] = { -1, -1 };
480  if (qt_create_pipe(pipe) != 0)
481  return false;
482  sink->pipe[0] = pipe[0];
483  source->pipe[1] = pipe[1];
484 
485  return true;
486  }
487  }
488 }
bool _q_canReadStandardOutput()
Definition: qprocess.cpp:937
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
#define O_RDONLY
#define O_TRUNC
Channel stdinChannel
Definition: qprocess_p.h:317
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define INVALID_Q_PIPE
Definition: qprocess_p.h:69
#define O_CREAT
QThreadData * threadData
Definition: qobject_p.h:195
#define O_APPEND
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
#define Q_Q(Class)
Definition: qglobal.h:2483
bool _q_canWrite()
Definition: qprocess.cpp:1036
#define SIGNAL(a)
Definition: qobjectdefs.h:227
static int qt_safe_open(const char *pathname, int flags, mode_t mode=0777)
Definition: qcore_unix_p.h:171
QProcess::ProcessError processError
Definition: qprocess_p.h:307
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
Channel stdoutChannel
Definition: qprocess_p.h:318
#define emit
Definition: qobjectdefs.h:76
QProcess::ProcessChannelMode processChannelMode
Definition: qprocess_p.h:306
static int qt_create_pipe(int *pipe)
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
Channel stderrChannel
Definition: qprocess_p.h:319
HANDLE Q_PIPE
Definition: qprocess_p.h:68
bool _q_canReadStandardError()
Definition: qprocess.cpp:992
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
QAbstractEventDispatcher * eventDispatcher
Definition: qthread_p.h:264
#define O_WRONLY
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

◆ destroyPipe()

void QProcessPrivate::destroyPipe ( Q_PIPE  pipe[2])

Definition at line 366 of file qprocess_unix.cpp.

Referenced by qt_create_pipe().

367 {
368  if (pipe[1] != -1) {
369  qt_safe_close(pipe[1]);
370  pipe[1] = -1;
371  }
372  if (pipe[0] != -1) {
373  qt_safe_close(pipe[0]);
374  pipe[0] = -1;
375  }
376 }
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297

◆ execChild()

void QProcessPrivate::execChild ( const char *  workingDirectory,
char **  path,
char **  argv,
char **  envp 
)

Definition at line 770 of file qprocess_unix.cpp.

771 {
772  ::signal(SIGPIPE, SIG_DFL); // reset the signal that we ignored
773 
774  Q_Q(QProcess);
775 
776  // copy the stdin socket (without closing on exec)
777  qt_safe_dup2(stdinChannel.pipe[0], STDIN_FILENO, 0);
778 
779  // copy the stdout and stderr if asked to
781  qt_safe_dup2(stdoutChannel.pipe[1], STDOUT_FILENO, 0);
782 
783  // merge stdout and stderr if asked to
785  qt_safe_dup2(STDOUT_FILENO, STDERR_FILENO, 0);
786  } else {
787  qt_safe_dup2(stderrChannel.pipe[1], STDERR_FILENO, 0);
788  }
789  }
790 
791  // make sure this fd is closed if execvp() succeeds
793 
794  // enter the working directory
795  if (workingDir)
796  QT_CHDIR(workingDir);
797 
798  // this is a virtual call, and it base behavior is to do nothing.
799  q->setupChildProcess();
800 
801  // execute the process
802  if (!envp) {
803  qt_safe_execvp(argv[0], argv);
804  } else {
805  if (path) {
806  char **arg = path;
807  while (*arg) {
808  argv[0] = *arg;
809 #if defined (QPROCESS_DEBUG)
810  fprintf(stderr, "QProcessPrivate::execChild() searching / starting %s\n", argv[0]);
811 #endif
812  qt_safe_execve(argv[0], argv, envp);
813  ++arg;
814  }
815  } else {
816 #if defined (QPROCESS_DEBUG)
817  fprintf(stderr, "QProcessPrivate::execChild() starting %s\n", argv[0]);
818 #endif
819  qt_safe_execve(argv[0], argv, envp);
820  }
821  }
822 
823  // notify failure
825 #if defined (QPROCESS_DEBUG)
826  fprintf(stderr, "QProcessPrivate::execChild() failed (%s), notifying parent process\n", qPrintable(error));
827 #endif
828  qt_safe_write(childStartedPipe[1], error.data(), error.length() * sizeof(QChar));
830  childStartedPipe[1] = -1;
831 }
QString qt_error_string(int errorCode)
Definition: qglobal.cpp:2600
#define error(msg)
static int qt_safe_execvp(const char *file, char *const argv[])
Definition: qcore_unix_p.h:324
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
Channel stdinChannel
Definition: qprocess_p.h:317
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
#define Q_Q(Class)
Definition: qglobal.h:2483
static int qt_safe_execve(const char *filename, char *const argv[], char *const envp[])
Definition: qcore_unix_p.h:309
Channel stdoutChannel
Definition: qprocess_p.h:318
static int qt_safe_dup2(int oldfd, int newfd, int flags=FD_CLOEXEC)
Definition: qcore_unix_p.h:251
QProcess::ProcessChannelMode processChannelMode
Definition: qprocess_p.h:306
Q_PIPE childStartedPipe[2]
Definition: qprocess_p.h:334
Channel stderrChannel
Definition: qprocess_p.h:319
#define qPrintable(string)
Definition: qglobal.h:1750
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102
static qint64 qt_safe_write(int fd, const void *data, qint64 len)
Definition: qcore_unix_p.h:282
int errno

◆ findExitCode()

void QProcessPrivate::findExitCode ( )

Definition at line 1293 of file qprocess_unix.cpp.

Referenced by qt_terminateApp().

1294 {
1295  Q_Q(QProcess);
1296  processManager()->remove(q);
1297 }
#define Q_Q(Class)
Definition: qglobal.h:2483
static QProcessManager * processManager()
void remove(QProcess *process)
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

◆ flushPipeWriter()

void QProcessPrivate::flushPipeWriter ( )

Definition at line 774 of file qprocess_win.cpp.

775 {
776  if (pipeWriter && pipeWriter->bytesToWrite() > 0) {
777  pipeWriter->waitForWrite(ULONG_MAX);
778  }
779 }
QWindowsPipeWriter * pipeWriter
Definition: qprocess_p.h:343
qint64 bytesToWrite() const
bool waitForWrite(int msecs)

◆ initializeProcessManager()

void QProcessPrivate::initializeProcessManager ( )
static

Definition at line 1485 of file qprocess_unix.cpp.

Referenced by QCoreApplication::init().

1486 {
1487  (void) processManager();
1488 }
static QProcessManager * processManager()

◆ killProcess()

void QProcessPrivate::killProcess ( )

Definition at line 1021 of file qprocess_unix.cpp.

Referenced by qt_terminateApp().

1022 {
1023 #if defined (QPROCESS_DEBUG)
1024  qDebug("QProcessPrivate::killProcess()");
1025 #endif
1026  if (pid)
1027  ::kill(pid_t(pid), SIGKILL);
1028 }
Q_CORE_EXPORT void qDebug(const char *,...)

◆ pipeWriterBytesToWrite()

qint64 QProcessPrivate::pipeWriterBytesToWrite ( ) const

Definition at line 781 of file qprocess_win.cpp.

782 {
783  return pipeWriter ? pipeWriter->bytesToWrite() : qint64(0);
784 }
QWindowsPipeWriter * pipeWriter
Definition: qprocess_p.h:343
qint64 bytesToWrite() const
__int64 qint64
Definition: qglobal.h:942

◆ processStarted()

bool QProcessPrivate::processStarted ( )

Definition at line 835 of file qprocess_unix.cpp.

Referenced by qt_create_commandline(), and qt_terminateApp().

836 {
837  ushort buf[errorBufferMax];
838  int i = qt_safe_read(childStartedPipe[0], &buf, sizeof buf);
839  if (startupSocketNotifier) {
843  }
845  childStartedPipe[0] = -1;
846 
847 #if defined (QPROCESS_DEBUG)
848  qDebug("QProcessPrivate::processStarted() == %s", i <= 0 ? "true" : "false");
849 #endif
850 
851  // did we read an error message?
852  if (i > 0)
853  q_func()->setErrorString(QString((const QChar *)buf, i / sizeof(QChar)));
854 
855  return i <= 0;
856 }
static const int errorBufferMax
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
Q_CORE_EXPORT void qDebug(const char *,...)
Q_PIPE childStartedPipe[2]
Definition: qprocess_p.h:334
unsigned short ushort
Definition: qglobal.h:995
QSocketNotifier * startupSocketNotifier
Definition: qprocess_p.h:338
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
void deleteLater()
Schedules this object for deletion.
Definition: qobject.cpp:2145
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
Definition: qcore_unix_p.h:273

◆ readFromStderr()

qint64 QProcessPrivate::readFromStderr ( char *  data,
qint64  maxlen 
)

Definition at line 985 of file qprocess_unix.cpp.

Referenced by qt_create_commandline().

986 {
987  qint64 bytesRead = qt_safe_read(stderrChannel.pipe[0], data, maxlen);
988 #if defined QPROCESS_DEBUG
989  qDebug("QProcessPrivate::readFromStderr(%p \"%s\", %lld) == %lld",
990  data, qt_prettyDebug(data, bytesRead, 16).constData(), maxlen, bytesRead);
991 #endif
992  return bytesRead;
993 }
Q_CORE_EXPORT void qDebug(const char *,...)
static const char * data(const QByteArray &arr)
__int64 qint64
Definition: qglobal.h:942
Channel stderrChannel
Definition: qprocess_p.h:319
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
Definition: qcore_unix_p.h:273

◆ readFromStdout()

qint64 QProcessPrivate::readFromStdout ( char *  data,
qint64  maxlen 
)

Definition at line 975 of file qprocess_unix.cpp.

Referenced by qt_create_commandline().

976 {
977  qint64 bytesRead = qt_safe_read(stdoutChannel.pipe[0], data, maxlen);
978 #if defined QPROCESS_DEBUG
979  qDebug("QProcessPrivate::readFromStdout(%p \"%s\", %lld) == %lld",
980  data, qt_prettyDebug(data, bytesRead, 16).constData(), maxlen, bytesRead);
981 #endif
982  return bytesRead;
983 }
Q_CORE_EXPORT void qDebug(const char *,...)
Channel stdoutChannel
Definition: qprocess_p.h:318
static const char * data(const QByteArray &arr)
__int64 qint64
Definition: qglobal.h:942
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
Definition: qcore_unix_p.h:273

◆ startDetached()

bool QProcessPrivate::startDetached ( const QString program,
const QStringList arguments,
const QString workingDirectory = QString(),
qint64 pid = 0 
)
static
Warning
This function is not part of the public interface.

Definition at line 1365 of file qprocess_unix.cpp.

Referenced by _q_notified(), pipeWriterBytesToWrite(), and QProcess::startDetached().

1366 {
1367  processManager()->start();
1368 
1369  QByteArray encodedWorkingDirectory = QFile::encodeName(workingDirectory);
1370 
1371  // To catch the startup of the child
1372  int startedPipe[2];
1373  if (qt_safe_pipe(startedPipe) != 0)
1374  return false;
1375  // To communicate the pid of the child
1376  int pidPipe[2];
1377  if (qt_safe_pipe(pidPipe) != 0) {
1378  qt_safe_close(startedPipe[0]);
1379  qt_safe_close(startedPipe[1]);
1380  return false;
1381  }
1382 
1383  pid_t childPid = fork();
1384  if (childPid == 0) {
1385  struct sigaction noaction;
1386  memset(&noaction, 0, sizeof(noaction));
1387  noaction.sa_handler = SIG_IGN;
1388  ::sigaction(SIGPIPE, &noaction, 0);
1389 
1390  ::setsid();
1391 
1392  qt_safe_close(startedPipe[0]);
1393  qt_safe_close(pidPipe[0]);
1394 
1395  pid_t doubleForkPid = fork();
1396  if (doubleForkPid == 0) {
1397  qt_safe_close(pidPipe[1]);
1398 
1399  if (!encodedWorkingDirectory.isEmpty())
1400  QT_CHDIR(encodedWorkingDirectory.constData());
1401 
1402  char **argv = new char *[arguments.size() + 2];
1403  for (int i = 0; i < arguments.size(); ++i) {
1404 #ifdef Q_OS_MAC
1405  argv[i + 1] = ::strdup(arguments.at(i).toUtf8().constData());
1406 #else
1407  argv[i + 1] = ::strdup(arguments.at(i).toLocal8Bit().constData());
1408 #endif
1409  }
1410  argv[arguments.size() + 1] = 0;
1411 
1412  if (!program.contains(QLatin1Char('/'))) {
1413  const QString path = QString::fromLocal8Bit(::getenv("PATH"));
1414  if (!path.isEmpty()) {
1415  QStringList pathEntries = path.split(QLatin1Char(':'));
1416  for (int k = 0; k < pathEntries.size(); ++k) {
1417  QByteArray tmp = QFile::encodeName(pathEntries.at(k));
1418  if (!tmp.endsWith('/')) tmp += '/';
1419  tmp += QFile::encodeName(program);
1420  argv[0] = tmp.data();
1421  qt_safe_execv(argv[0], argv);
1422  }
1423  }
1424  } else {
1425  QByteArray tmp = QFile::encodeName(program);
1426  argv[0] = tmp.data();
1427  qt_safe_execv(argv[0], argv);
1428  }
1429 
1430  struct sigaction noaction;
1431  memset(&noaction, 0, sizeof(noaction));
1432  noaction.sa_handler = SIG_IGN;
1433  ::sigaction(SIGPIPE, &noaction, 0);
1434 
1435  // '\1' means execv failed
1436  char c = '\1';
1437  qt_safe_write(startedPipe[1], &c, 1);
1438  qt_safe_close(startedPipe[1]);
1439  ::_exit(1);
1440  } else if (doubleForkPid == -1) {
1441  struct sigaction noaction;
1442  memset(&noaction, 0, sizeof(noaction));
1443  noaction.sa_handler = SIG_IGN;
1444  ::sigaction(SIGPIPE, &noaction, 0);
1445 
1446  // '\2' means internal error
1447  char c = '\2';
1448  qt_safe_write(startedPipe[1], &c, 1);
1449  }
1450 
1451  qt_safe_close(startedPipe[1]);
1452  qt_safe_write(pidPipe[1], (const char *)&doubleForkPid, sizeof(pid_t));
1453  QT_CHDIR("/");
1454  ::_exit(1);
1455  }
1456 
1457  qt_safe_close(startedPipe[1]);
1458  qt_safe_close(pidPipe[1]);
1459 
1460  if (childPid == -1) {
1461  qt_safe_close(startedPipe[0]);
1462  qt_safe_close(pidPipe[0]);
1463  return false;
1464  }
1465 
1466  char reply = '\0';
1467  int startResult = qt_safe_read(startedPipe[0], &reply, 1);
1468  int result;
1469  qt_safe_close(startedPipe[0]);
1470  qt_safe_waitpid(childPid, &result, 0);
1471  bool success = (startResult != -1 && reply == '\0');
1472  if (success && pid) {
1473  pid_t actualPid = 0;
1474  if (qt_safe_read(pidPipe[0], (char *)&actualPid, sizeof(pid_t)) == sizeof(pid_t)) {
1475  *pid = actualPid;
1476  } else {
1477  *pid = 0;
1478  }
1479  }
1480  qt_safe_close(pidPipe[0]);
1481  return success;
1482 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
unsigned char c[8]
Definition: qnumeric_p.h:62
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static int qt_safe_pipe(int pipefd[2], int flags=0)
Definition: qcore_unix_p.h:191
static int qt_safe_execv(const char *path, char *const argv[])
Definition: qcore_unix_p.h:317
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void start(Priority=InheritPriority)
Begins execution of the thread by calling run().
int sigaction(int, const struct sigaction *, struct sigaction *)
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
static QProcessManager * processManager()
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
Definition: qcore_unix_p.h:333
static qint64 qt_safe_write(int fd, const void *data, qint64 len)
Definition: qcore_unix_p.h:282
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
Definition: qcore_unix_p.h:273
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...

◆ startProcess()

void QProcessPrivate::startProcess ( )

Definition at line 562 of file qprocess_unix.cpp.

Referenced by qt_create_commandline().

563 {
564  Q_Q(QProcess);
565 
566 #if defined (QPROCESS_DEBUG)
567  qDebug("QProcessPrivate::startProcess()");
568 #endif
569 
570  processManager()->start();
571 
572  // Initialize pipes
573  if (!createChannel(stdinChannel) ||
577  qt_create_pipe(deathPipe) != 0) {
579  q->setErrorString(qt_error_string(errno));
580  emit q->error(processError);
581  cleanup();
582  return;
583  }
584 
592  QObject::connect(deathNotifier, SIGNAL(activated(int)),
593  q, SLOT(_q_processDied()));
594  }
595 
596  // Start the process (platform dependent)
597  q->setProcessState(QProcess::Starting);
598 
599  // Create argument list with right number of elements, and set the final
600  // one to 0.
601  char **argv = new char *[arguments.count() + 2];
602  argv[arguments.count() + 1] = 0;
603 
604  // Encode the program name.
605  QByteArray encodedProgramName = QFile::encodeName(program);
606 #ifdef Q_OS_MAC
607  // allow invoking of .app bundles on the Mac.
608  QFileInfo fileInfo(QString::fromUtf8(encodedProgramName.constData()));
609  if (encodedProgramName.endsWith(".app") && fileInfo.isDir()) {
610  QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0,
611  QCFString(fileInfo.absoluteFilePath()),
612  kCFURLPOSIXPathStyle, true);
613  {
614  // CFBundle is not reentrant, since CFBundleCreate might return a reference
615  // to a cached bundle object. Protect the bundle calls with a mutex lock.
616  QMutexLocker lock(cfbundleMutex());
617  QCFType<CFBundleRef> bundle = CFBundleCreate(0, url);
618  url = CFBundleCopyExecutableURL(bundle);
619  }
620  if (url) {
621  QCFString str = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
622  encodedProgramName += "/Contents/MacOS/" + static_cast<QString>(str).toUtf8();
623  }
624  }
625 #endif
626 
627  // Add the program name to the argument list.
628  char *dupProgramName = ::strdup(encodedProgramName.constData());
629  argv[0] = dupProgramName;
630 
631  // Add every argument to the list
632  for (int i = 0; i < arguments.count(); ++i) {
633  QString arg = arguments.at(i);
634 #ifdef Q_OS_MAC
635  // Mac OS X uses UTF8 for exec, regardless of the system locale.
636  argv[i + 1] = ::strdup(arg.toUtf8().constData());
637 #else
638  argv[i + 1] = ::strdup(arg.toLocal8Bit().constData());
639 #endif
640  }
641 
642  // Duplicate the environment.
643  int envc = 0;
644  char **envp = 0;
645  if (environment.d.constData()) {
647  envp = _q_dupEnvironment(environment.d.constData()->hash, &envc);
648  }
649 
650  // Encode the working directory if it's non-empty, otherwise just pass 0.
651  const char *workingDirPtr = 0;
652  QByteArray encodedWorkingDirectory;
653  if (!workingDirectory.isEmpty()) {
654  encodedWorkingDirectory = QFile::encodeName(workingDirectory);
655  workingDirPtr = encodedWorkingDirectory.constData();
656  }
657 
658  // If the program does not specify a path, generate a list of possible
659  // locations for the binary using the PATH environment variable.
660  char **path = 0;
661  int pathc = 0;
662  if (!program.contains(QLatin1Char('/'))) {
663  const QString pathEnv = QString::fromLocal8Bit(::getenv("PATH"));
664  if (!pathEnv.isEmpty()) {
665  QStringList pathEntries = pathEnv.split(QLatin1Char(':'), QString::SkipEmptyParts);
666  if (!pathEntries.isEmpty()) {
667  pathc = pathEntries.size();
668  path = new char *[pathc + 1];
669  path[pathc] = 0;
670 
671  for (int k = 0; k < pathEntries.size(); ++k) {
672  QByteArray tmp = QFile::encodeName(pathEntries.at(k));
673  if (!tmp.endsWith('/')) tmp += '/';
674  tmp += encodedProgramName;
675  path[k] = ::strdup(tmp.constData());
676  }
677  }
678  }
679  }
680 
681  // Start the process manager, and fork off the child process.
682  processManager()->lock();
683 #if defined(Q_OS_QNX)
684  pid_t childPid = spawnChild(workingDirPtr, argv, envp);
685 #else
686  pid_t childPid = fork();
687  int lastForkErrno = errno;
688 #endif
689  if (childPid != 0) {
690  // Clean up duplicated memory.
691  free(dupProgramName);
692  for (int i = 1; i <= arguments.count(); ++i)
693  free(argv[i]);
694  for (int i = 0; i < envc; ++i)
695  free(envp[i]);
696  for (int i = 0; i < pathc; ++i)
697  free(path[i]);
698  delete [] argv;
699  delete [] envp;
700  delete [] path;
701  }
702 
703  // This is not a valid check under QNX, because the semantics are
704  // different. While under other platforms where fork() may succeed and exec() can still fail,
705  // causing the childPid to hold a valid value (and thus evaluating the
706  // following if to false), and then signaling the error via
707  // childStartedPipe, under QNX on the other hand, spawn() return value will be assigned
708  // to childPid (which will be -1 in case of failure). This will force
709  // QProcess to cleanup, instead of signaling the error via
710  // childStartedPipe. Since it will invalidade the pipes, functions like
711  // QProcess::waitForStarted() will fail, for childStartedPipe will be
712  // '-1' and mess with the select() calls.
713 #if !defined(Q_OS_QNX)
714  if (childPid < 0) {
715  // Cleanup, report error and return
716 #if defined (QPROCESS_DEBUG)
717  qDebug("fork() failed: %s", qPrintable(qt_error_string(lastForkErrno)));
718 #endif
719  processManager()->unlock();
720  q->setProcessState(QProcess::NotRunning);
722  q->setErrorString(QProcess::tr("Resource error (fork failure): %1").arg(qt_error_string(lastForkErrno)));
723  emit q->error(processError);
724  cleanup();
725  return;
726  }
727 
728  // Start the child.
729  if (childPid == 0) {
730  execChild(workingDirPtr, path, argv, envp);
731  ::_exit(-1);
732  }
733 #endif
734 
735  // Register the child. In the mean time, we can get a SIGCHLD, so we need
736  // to keep the lock held to avoid a race to catch the child.
737  processManager()->add(childPid, q);
738  pid = Q_PID(childPid);
739  processManager()->unlock();
740 
741  // parent
742  // close the ends we don't use and make all pipes non-blocking
743  ::fcntl(deathPipe[0], F_SETFL, ::fcntl(deathPipe[0], F_GETFL) | O_NONBLOCK);
745  childStartedPipe[1] = -1;
746 
747  if (stdinChannel.pipe[0] != -1) {
749  stdinChannel.pipe[0] = -1;
750  }
751  if (stdinChannel.pipe[1] != -1)
752  ::fcntl(stdinChannel.pipe[1], F_SETFL, ::fcntl(stdinChannel.pipe[1], F_GETFL) | O_NONBLOCK);
753 
754  if (stdoutChannel.pipe[1] != -1) {
756  stdoutChannel.pipe[1] = -1;
757  }
758  if (stdoutChannel.pipe[0] != -1)
759  ::fcntl(stdoutChannel.pipe[0], F_SETFL, ::fcntl(stdoutChannel.pipe[0], F_GETFL) | O_NONBLOCK);
760 
761  if (stderrChannel.pipe[1] != -1) {
763  stderrChannel.pipe[1] = -1;
764  }
765  if (stderrChannel.pipe[0] != -1)
766  ::fcntl(stderrChannel.pipe[0], F_SETFL, ::fcntl(stderrChannel.pipe[0], F_GETFL) | O_NONBLOCK);
767 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
QString qt_error_string(int errorCode)
Definition: qglobal.cpp:2600
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
Channel stdinChannel
Definition: qprocess_p.h:317
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QSharedDataPointer< QProcessEnvironmentPrivate > d
Definition: qprocess.h:99
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297
The QString class provides a Unicode character string.
Definition: qstring.h:83
QProcessEnvironment environment
Definition: qprocess_p.h:328
QThreadData * threadData
Definition: qobject_p.h:195
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
#define Q_Q(Class)
Definition: qglobal.h:2483
QStringList arguments
Definition: qprocess_p.h:324
Q_CORE_EXPORT void qDebug(const char *,...)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
static char ** _q_dupEnvironment(const QProcessEnvironmentPrivate::Hash &environment, int *envc)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
Channel stdoutChannel
Definition: qprocess_p.h:318
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
const T * constData() const
Returns a const pointer to the shared data object.
Definition: qshareddata.h:84
bool _q_startupNotification()
Definition: qprocess.cpp:1149
void add(pid_t pid, QProcess *process)
static int qt_create_pipe(int *pipe)
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
qint64 Q_PID
Definition: qprocess.h:58
int fcntl(int, int,...)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
Q_PIPE childStartedPipe[2]
Definition: qprocess_p.h:334
Channel stderrChannel
Definition: qprocess_p.h:319
void start(Priority=InheritPriority)
Begins execution of the thread by calling run().
QString workingDirectory
Definition: qprocess_p.h:309
QSocketNotifier * startupSocketNotifier
Definition: qprocess_p.h:338
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static QReadWriteLock lock
Definition: proxyconf.cpp:399
bool createChannel(Channel &channel)
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
bool _q_processDied()
Definition: qprocess.cpp:1080
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
static QProcessManager * processManager()
QSocketNotifier * deathNotifier
Definition: qprocess_p.h:339
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
Q_PIPE deathPipe[2]
Definition: qprocess_p.h:335
#define qPrintable(string)
Definition: qglobal.h:1750
QString program
Definition: qprocess_p.h:323
QAbstractEventDispatcher * eventDispatcher
Definition: qthread_p.h:264
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void execChild(const char *workingDirectory, char **path, char **argv, char **envp)
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102
int errno
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...

◆ terminateProcess()

void QProcessPrivate::terminateProcess ( )

Definition at line 1012 of file qprocess_unix.cpp.

Referenced by qt_terminateApp().

1013 {
1014 #if defined (QPROCESS_DEBUG)
1015  qDebug("QProcessPrivate::killProcess()");
1016 #endif
1017  if (pid)
1018  ::kill(pid_t(pid), SIGTERM);
1019 }
Q_CORE_EXPORT void qDebug(const char *,...)

◆ waitForBytesWritten()

bool QProcessPrivate::waitForBytesWritten ( int  msecs = 30000)

Definition at line 1154 of file qprocess_unix.cpp.

Referenced by qt_terminateApp().

1155 {
1156  Q_Q(QProcess);
1157 #if defined (QPROCESS_DEBUG)
1158  qDebug("QProcessPrivate::waitForBytesWritten(%d)", msecs);
1159 #endif
1160 
1161  QElapsedTimer stopWatch;
1162  stopWatch.start();
1163 
1164  while (!writeBuffer.isEmpty()) {
1165  fd_set fdread;
1166  fd_set fdwrite;
1167 
1168  FD_ZERO(&fdread);
1169  FD_ZERO(&fdwrite);
1170 
1171  int nfds = deathPipe[0];
1172  FD_SET(deathPipe[0], &fdread);
1173 
1175  add_fd(nfds, childStartedPipe[0], &fdread);
1176 
1177  if (stdoutChannel.pipe[0] != -1)
1178  add_fd(nfds, stdoutChannel.pipe[0], &fdread);
1179  if (stderrChannel.pipe[0] != -1)
1180  add_fd(nfds, stderrChannel.pipe[0], &fdread);
1181 
1182 
1183  if (!writeBuffer.isEmpty() && stdinChannel.pipe[1] != -1)
1184  add_fd(nfds, stdinChannel.pipe[1], &fdwrite);
1185 
1186  int timeout = qt_timeout_value(msecs, stopWatch.elapsed());
1187  int ret = select_msecs(nfds + 1, &fdread, &fdwrite, timeout);
1188  if (ret < 0) {
1189  break;
1190  }
1191 
1192  if (ret == 0) {
1194  q->setErrorString(QProcess::tr("Process operation timed out"));
1195  return false;
1196  }
1197 
1198  if (childStartedPipe[0] != -1 && FD_ISSET(childStartedPipe[0], &fdread)) {
1199  if (!_q_startupNotification())
1200  return false;
1201  }
1202 
1203  if (stdinChannel.pipe[1] != -1 && FD_ISSET(stdinChannel.pipe[1], &fdwrite))
1204  return _q_canWrite();
1205 
1206  if (stdoutChannel.pipe[0] != -1 && FD_ISSET(stdoutChannel.pipe[0], &fdread))
1208 
1209  if (stderrChannel.pipe[0] != -1 && FD_ISSET(stderrChannel.pipe[0], &fdread))
1211 
1212  if (deathPipe[0] == -1 || FD_ISSET(deathPipe[0], &fdread)) {
1213  if (_q_processDied())
1214  return false;
1215  }
1216  }
1217 
1218  return false;
1219 }
bool _q_canReadStandardOutput()
Definition: qprocess.cpp:937
QProcess::ProcessState processState
Definition: qprocess_p.h:308
Channel stdinChannel
Definition: qprocess_p.h:317
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
bool isEmpty() const
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
static int qt_timeout_value(int msecs, int elapsed)
#define Q_Q(Class)
Definition: qglobal.h:2483
bool _q_canWrite()
Definition: qprocess.cpp:1036
Q_CORE_EXPORT void qDebug(const char *,...)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
Channel stdoutChannel
Definition: qprocess_p.h:318
bool _q_startupNotification()
Definition: qprocess.cpp:1149
Q_PIPE childStartedPipe[2]
Definition: qprocess_p.h:334
static int select_msecs(int nfds, fd_set *fdread, fd_set *fdwrite, int timeout)
Channel stderrChannel
Definition: qprocess_p.h:319
bool _q_canReadStandardError()
Definition: qprocess.cpp:992
bool _q_processDied()
Definition: qprocess.cpp:1080
Q_PIPE deathPipe[2]
Definition: qprocess_p.h:335
QRingBuffer writeBuffer
Definition: qprocess_p.h:332
void start()
Starts this timer.
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102
static void add_fd(int &nfds, int fd, fd_set *fdset)

◆ waitForDeadChild()

bool QProcessPrivate::waitForDeadChild ( )

Definition at line 1299 of file qprocess_unix.cpp.

1300 {
1301  Q_Q(QProcess);
1302 
1303  // read a byte from the death pipe
1304  char c;
1305  qt_safe_read(deathPipe[0], &c, 1);
1306 
1307  // check if our process is dead
1308  int exitStatus;
1309  if (qt_safe_waitpid(pid_t(pid), &exitStatus, WNOHANG) > 0) {
1310  processManager()->remove(q);
1311  crashed = !WIFEXITED(exitStatus);
1312  exitCode = WEXITSTATUS(exitStatus);
1313 #if defined QPROCESS_DEBUG
1314  qDebug() << "QProcessPrivate::waitForDeadChild() dead with exitCode"
1315  << exitCode << ", crashed?" << crashed;
1316 #endif
1317  return true;
1318  }
1319 #if defined QPROCESS_DEBUG
1320  qDebug() << "QProcessPrivate::waitForDeadChild() not dead!";
1321 #endif
1322  return false;
1323 }
unsigned char c[8]
Definition: qnumeric_p.h:62
#define Q_Q(Class)
Definition: qglobal.h:2483
Q_CORE_EXPORT void qDebug(const char *,...)
QProcess::ExitStatus exitStatus
Definition: qprocess_p.h:368
static QProcessManager * processManager()
Q_PIPE deathPipe[2]
Definition: qprocess_p.h:335
void remove(QProcess *process)
static pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
Definition: qcore_unix_p.h:333
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
Definition: qcore_unix_p.h:273

◆ waitForFinished()

bool QProcessPrivate::waitForFinished ( int  msecs = 30000)

Definition at line 1221 of file qprocess_unix.cpp.

Referenced by qt_terminateApp().

1222 {
1223  Q_Q(QProcess);
1224 #if defined (QPROCESS_DEBUG)
1225  qDebug("QProcessPrivate::waitForFinished(%d)", msecs);
1226 #endif
1227 
1228  QElapsedTimer stopWatch;
1229  stopWatch.start();
1230 
1231  forever {
1232  fd_set fdread;
1233  fd_set fdwrite;
1234  int nfds = -1;
1235 
1236  FD_ZERO(&fdread);
1237  FD_ZERO(&fdwrite);
1238 
1240  add_fd(nfds, childStartedPipe[0], &fdread);
1241 
1242  if (stdoutChannel.pipe[0] != -1)
1243  add_fd(nfds, stdoutChannel.pipe[0], &fdread);
1244  if (stderrChannel.pipe[0] != -1)
1245  add_fd(nfds, stderrChannel.pipe[0], &fdread);
1246 
1248  add_fd(nfds, deathPipe[0], &fdread);
1249 
1250  if (!writeBuffer.isEmpty() && stdinChannel.pipe[1] != -1)
1251  add_fd(nfds, stdinChannel.pipe[1], &fdwrite);
1252 
1253  int timeout = qt_timeout_value(msecs, stopWatch.elapsed());
1254  int ret = select_msecs(nfds + 1, &fdread, &fdwrite, timeout);
1255  if (ret < 0) {
1256  break;
1257  }
1258  if (ret == 0) {
1260  q->setErrorString(QProcess::tr("Process operation timed out"));
1261  return false;
1262  }
1263 
1264  if (childStartedPipe[0] != -1 && FD_ISSET(childStartedPipe[0], &fdread)) {
1265  if (!_q_startupNotification())
1266  return false;
1267  }
1268  if (stdinChannel.pipe[1] != -1 && FD_ISSET(stdinChannel.pipe[1], &fdwrite))
1269  _q_canWrite();
1270 
1271  if (stdoutChannel.pipe[0] != -1 && FD_ISSET(stdoutChannel.pipe[0], &fdread))
1273 
1274  if (stderrChannel.pipe[0] != -1 && FD_ISSET(stderrChannel.pipe[0], &fdread))
1276 
1277  if (deathPipe[0] == -1 || FD_ISSET(deathPipe[0], &fdread)) {
1278  if (_q_processDied())
1279  return true;
1280  }
1281  }
1282  return false;
1283 }
bool _q_canReadStandardOutput()
Definition: qprocess.cpp:937
QProcess::ProcessState processState
Definition: qprocess_p.h:308
Channel stdinChannel
Definition: qprocess_p.h:317
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
bool isEmpty() const
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
static int qt_timeout_value(int msecs, int elapsed)
#define Q_Q(Class)
Definition: qglobal.h:2483
bool _q_canWrite()
Definition: qprocess.cpp:1036
Q_CORE_EXPORT void qDebug(const char *,...)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
Channel stdoutChannel
Definition: qprocess_p.h:318
bool _q_startupNotification()
Definition: qprocess.cpp:1149
Q_PIPE childStartedPipe[2]
Definition: qprocess_p.h:334
static int select_msecs(int nfds, fd_set *fdread, fd_set *fdwrite, int timeout)
Channel stderrChannel
Definition: qprocess_p.h:319
bool _q_canReadStandardError()
Definition: qprocess.cpp:992
bool _q_processDied()
Definition: qprocess.cpp:1080
Q_PIPE deathPipe[2]
Definition: qprocess_p.h:335
QRingBuffer writeBuffer
Definition: qprocess_p.h:332
void start()
Starts this timer.
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102
static void add_fd(int &nfds, int fd, fd_set *fdset)
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452

◆ waitForReadyRead()

bool QProcessPrivate::waitForReadyRead ( int  msecs = 30000)

Definition at line 1082 of file qprocess_unix.cpp.

Referenced by qt_terminateApp().

1083 {
1084  Q_Q(QProcess);
1085 #if defined (QPROCESS_DEBUG)
1086  qDebug("QProcessPrivate::waitForReadyRead(%d)", msecs);
1087 #endif
1088 
1089  QElapsedTimer stopWatch;
1090  stopWatch.start();
1091 
1092  forever {
1093  fd_set fdread;
1094  fd_set fdwrite;
1095 
1096  FD_ZERO(&fdread);
1097  FD_ZERO(&fdwrite);
1098 
1099  int nfds = deathPipe[0];
1100  FD_SET(deathPipe[0], &fdread);
1101 
1103  add_fd(nfds, childStartedPipe[0], &fdread);
1104 
1105  if (stdoutChannel.pipe[0] != -1)
1106  add_fd(nfds, stdoutChannel.pipe[0], &fdread);
1107  if (stderrChannel.pipe[0] != -1)
1108  add_fd(nfds, stderrChannel.pipe[0], &fdread);
1109 
1110  if (!writeBuffer.isEmpty() && stdinChannel.pipe[1] != -1)
1111  add_fd(nfds, stdinChannel.pipe[1], &fdwrite);
1112 
1113  int timeout = qt_timeout_value(msecs, stopWatch.elapsed());
1114  int ret = select_msecs(nfds + 1, &fdread, &fdwrite, timeout);
1115  if (ret < 0) {
1116  break;
1117  }
1118  if (ret == 0) {
1120  q->setErrorString(QProcess::tr("Process operation timed out"));
1121  return false;
1122  }
1123 
1124  if (childStartedPipe[0] != -1 && FD_ISSET(childStartedPipe[0], &fdread)) {
1125  if (!_q_startupNotification())
1126  return false;
1127  }
1128 
1129  bool readyReadEmitted = false;
1130  if (stdoutChannel.pipe[0] != -1 && FD_ISSET(stdoutChannel.pipe[0], &fdread)) {
1131  bool canRead = _q_canReadStandardOutput();
1132  if (processChannel == QProcess::StandardOutput && canRead)
1133  readyReadEmitted = true;
1134  }
1135  if (stderrChannel.pipe[0] != -1 && FD_ISSET(stderrChannel.pipe[0], &fdread)) {
1136  bool canRead = _q_canReadStandardError();
1137  if (processChannel == QProcess::StandardError && canRead)
1138  readyReadEmitted = true;
1139  }
1140  if (readyReadEmitted)
1141  return true;
1142 
1143  if (stdinChannel.pipe[1] != -1 && FD_ISSET(stdinChannel.pipe[1], &fdwrite))
1144  _q_canWrite();
1145 
1146  if (deathPipe[0] == -1 || FD_ISSET(deathPipe[0], &fdread)) {
1147  if (_q_processDied())
1148  return false;
1149  }
1150  }
1151  return false;
1152 }
bool _q_canReadStandardOutput()
Definition: qprocess.cpp:937
QProcess::ProcessState processState
Definition: qprocess_p.h:308
Channel stdinChannel
Definition: qprocess_p.h:317
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
bool isEmpty() const
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
static int qt_timeout_value(int msecs, int elapsed)
#define Q_Q(Class)
Definition: qglobal.h:2483
QProcess::ProcessChannel processChannel
Definition: qprocess_p.h:305
bool _q_canWrite()
Definition: qprocess.cpp:1036
Q_CORE_EXPORT void qDebug(const char *,...)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
Channel stdoutChannel
Definition: qprocess_p.h:318
bool _q_startupNotification()
Definition: qprocess.cpp:1149
Q_PIPE childStartedPipe[2]
Definition: qprocess_p.h:334
static int select_msecs(int nfds, fd_set *fdread, fd_set *fdwrite, int timeout)
Channel stderrChannel
Definition: qprocess_p.h:319
bool _q_canReadStandardError()
Definition: qprocess.cpp:992
bool _q_processDied()
Definition: qprocess.cpp:1080
Q_PIPE deathPipe[2]
Definition: qprocess_p.h:335
QRingBuffer writeBuffer
Definition: qprocess_p.h:332
void start()
Starts this timer.
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102
static void add_fd(int &nfds, int fd, fd_set *fdset)
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452

◆ waitForStarted()

bool QProcessPrivate::waitForStarted ( int  msecs = 30000)

Definition at line 1054 of file qprocess_unix.cpp.

Referenced by qt_terminateApp().

1055 {
1056  Q_Q(QProcess);
1057 
1058 #if defined (QPROCESS_DEBUG)
1059  qDebug("QProcessPrivate::waitForStarted(%d) waiting for child to start (fd = %d)", msecs,
1060  childStartedPipe[0]);
1061 #endif
1062 
1063  fd_set fds;
1064  FD_ZERO(&fds);
1065  FD_SET(childStartedPipe[0], &fds);
1066  if (select_msecs(childStartedPipe[0] + 1, &fds, 0, msecs) == 0) {
1068  q->setErrorString(QProcess::tr("Process operation timed out"));
1069 #if defined (QPROCESS_DEBUG)
1070  qDebug("QProcessPrivate::waitForStarted(%d) == false (timed out)", msecs);
1071 #endif
1072  return false;
1073  }
1074 
1075  bool startedEmitted = _q_startupNotification();
1076 #if defined (QPROCESS_DEBUG)
1077  qDebug("QProcessPrivate::waitForStarted() == %s", startedEmitted ? "true" : "false");
1078 #endif
1079  return startedEmitted;
1080 }
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
#define Q_Q(Class)
Definition: qglobal.h:2483
Q_CORE_EXPORT void qDebug(const char *,...)
QProcess::ProcessError processError
Definition: qprocess_p.h:307
bool _q_startupNotification()
Definition: qprocess.cpp:1149
Q_PIPE childStartedPipe[2]
Definition: qprocess_p.h:334
static int select_msecs(int nfds, fd_set *fdread, fd_set *fdwrite, int timeout)
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

◆ waitForWrite()

bool QProcessPrivate::waitForWrite ( int  msecs = 30000)

Definition at line 1285 of file qprocess_unix.cpp.

Referenced by pipeWriterBytesToWrite().

1286 {
1287  fd_set fdwrite;
1288  FD_ZERO(&fdwrite);
1289  FD_SET(stdinChannel.pipe[1], &fdwrite);
1290  return select_msecs(stdinChannel.pipe[1] + 1, 0, &fdwrite, msecs < 0 ? 0 : msecs) == 1;
1291 }
Channel stdinChannel
Definition: qprocess_p.h:317
static int select_msecs(int nfds, fd_set *fdread, fd_set *fdwrite, int timeout)

◆ writeToStdin()

qint64 QProcessPrivate::writeToStdin ( const char *  data,
qint64  maxlen 
)

Definition at line 995 of file qprocess_unix.cpp.

Referenced by pipeWriterBytesToWrite().

996 {
997  qint64 written = qt_safe_write_nosignal(stdinChannel.pipe[1], data, maxlen);
998 #if defined QPROCESS_DEBUG
999  qDebug("QProcessPrivate::writeToStdin(%p \"%s\", %lld) == %lld",
1000  data, qt_prettyDebug(data, maxlen, 16).constData(), maxlen, written);
1001  if (written == -1)
1002  qDebug("QProcessPrivate::writeToStdin(), failed to write (%s)", qPrintable(qt_error_string(errno)));
1003 #endif
1004  // If the O_NONBLOCK flag is set and If some data can be written without blocking
1005  // the process, write() will transfer what it can and return the number of bytes written.
1006  // Otherwise, it will return -1 and set errno to EAGAIN
1007  if (written == -1 && errno == EAGAIN)
1008  written = 0;
1009  return written;
1010 }
QString qt_error_string(int errorCode)
Definition: qglobal.cpp:2600
Channel stdinChannel
Definition: qprocess_p.h:317
Q_CORE_EXPORT void qDebug(const char *,...)
static const char * data(const QByteArray &arr)
__int64 qint64
Definition: qglobal.h:942
static qint64 qt_safe_write_nosignal(int fd, const void *data, qint64 len)
Definition: qcore_unix_p.h:291
#define qPrintable(string)
Definition: qglobal.h:1750
int errno

Properties

◆ arguments

QStringList QProcessPrivate::arguments

Definition at line 324 of file qprocess_p.h.

Referenced by pipeWriterBytesToWrite(), and qt_create_commandline().

◆ childStartedPipe

Q_PIPE QProcessPrivate::childStartedPipe[2]

Definition at line 334 of file qprocess_p.h.

◆ crashed

bool QProcessPrivate::crashed

Definition at line 369 of file qprocess_p.h.

Referenced by qt_terminateApp().

◆ deathNotifier

QSocketNotifier* QProcessPrivate::deathNotifier

Definition at line 339 of file qprocess_p.h.

◆ deathPipe

Q_PIPE QProcessPrivate::deathPipe[2]

Definition at line 335 of file qprocess_p.h.

◆ dying

bool QProcessPrivate::dying

Definition at line 313 of file qprocess_p.h.

◆ emittedBytesWritten

bool QProcessPrivate::emittedBytesWritten

Definition at line 315 of file qprocess_p.h.

◆ emittedReadyRead

bool QProcessPrivate::emittedReadyRead

Definition at line 314 of file qprocess_p.h.

◆ environment

QProcessEnvironment QProcessPrivate::environment

Definition at line 328 of file qprocess_p.h.

Referenced by qt_create_commandline().

◆ errorReadBuffer

QRingBuffer QProcessPrivate::errorReadBuffer

Definition at line 331 of file qprocess_p.h.

◆ exitCode

int QProcessPrivate::exitCode

Definition at line 367 of file qprocess_p.h.

Referenced by qt_terminateApp().

◆ exitStatus

QProcess::ExitStatus QProcessPrivate::exitStatus

Definition at line 368 of file qprocess_p.h.

◆ nativeArguments

QString QProcessPrivate::nativeArguments

Definition at line 326 of file qprocess_p.h.

Referenced by qt_create_commandline().

◆ notifier

QTimer* QProcessPrivate::notifier

Definition at line 342 of file qprocess_p.h.

Referenced by pipeWriterBytesToWrite(), and qt_create_commandline().

◆ outputReadBuffer

QRingBuffer QProcessPrivate::outputReadBuffer

Definition at line 330 of file qprocess_p.h.

◆ pid

Q_PID QProcessPrivate::pid

Definition at line 310 of file qprocess_p.h.

Referenced by pipeWriterBytesToWrite(), qt_create_commandline(), and qt_terminateApp().

◆ pipeWriter

QWindowsPipeWriter* QProcessPrivate::pipeWriter

◆ processChannel

QProcess::ProcessChannel QProcessPrivate::processChannel

Definition at line 305 of file qprocess_p.h.

◆ processChannelMode

QProcess::ProcessChannelMode QProcessPrivate::processChannelMode

Definition at line 306 of file qprocess_p.h.

Referenced by qt_create_commandline(), and qt_create_pipe().

◆ processError

QProcess::ProcessError QProcessPrivate::processError

◆ processFinishedNotifier

QWinEventNotifier* QProcessPrivate::processFinishedNotifier

Definition at line 344 of file qprocess_p.h.

Referenced by qt_create_commandline().

◆ processState

QProcess::ProcessState QProcessPrivate::processState

Definition at line 308 of file qprocess_p.h.

Referenced by pipeWriterBytesToWrite(), and qt_create_commandline().

◆ program

QString QProcessPrivate::program

Definition at line 323 of file qprocess_p.h.

Referenced by pipeWriterBytesToWrite(), and qt_create_commandline().

◆ sequenceNumber

int QProcessPrivate::sequenceNumber

Definition at line 311 of file qprocess_p.h.

◆ serial

int QProcessPrivate::serial

Definition at line 371 of file qprocess_p.h.

◆ startupSocketNotifier

QSocketNotifier* QProcessPrivate::startupSocketNotifier

Definition at line 338 of file qprocess_p.h.

◆ stderrChannel

Channel QProcessPrivate::stderrChannel

Definition at line 319 of file qprocess_p.h.

Referenced by qt_create_commandline(), and qt_create_pipe().

◆ stdinChannel

Channel QProcessPrivate::stdinChannel

◆ stdoutChannel

Channel QProcessPrivate::stdoutChannel

◆ workingDirectory

QString QProcessPrivate::workingDirectory

Definition at line 309 of file qprocess_p.h.

Referenced by qt_create_commandline().

◆ writeBuffer

QRingBuffer QProcessPrivate::writeBuffer

Definition at line 332 of file qprocess_p.h.

Referenced by pipeWriterBytesToWrite(), and qt_terminateApp().


The documentation for this class was generated from the following files: