?? qprocess_unix.cpp
字號:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************///#define QPROCESS_DEBUG#include "qdebug.h"#ifndef QT_NO_PROCESS#if defined QPROCESS_DEBUG#include "qstring.h"#include <ctype.h>/* Returns a human readable representation of the first \a len characters in \a data.*/static QByteArray qt_prettyDebug(const char *data, int len, int maxSize){ if (!data) return "(null)"; QByteArray out; for (int i = 0; i < len; ++i) { char c = data[i]; if (isprint(c)) { out += c; } else switch (c) { case '\n': out += "\\n"; break; case '\r': out += "\\r"; break; case '\t': out += "\\t"; break; default: QString tmp; tmp.sprintf("\\%o", c); out += tmp.toLatin1(); } } if (len < maxSize) out += "..."; return out;}#endif#include "qplatformdefs.h"#include "qprocess.h"#ifdef Q_OS_MAC#include <private/qcore_mac_p.h>#endif#include <private/qcoreapplication_p.h>#include <private/qthread_p.h>#include <qdatetime.h>#include <qfile.h>#include <qfileinfo.h>#include <qlist.h>#include <qmap.h>#include <qmutex.h>#include <qsemaphore.h>#include <qsocketnotifier.h>#include <qthread.h>#include <errno.h>#include <stdlib.h>static qint64 qt_native_read(int fd, char *data, qint64 maxlen){ qint64 ret = 0; do { ret = ::read(fd, data, maxlen); } while (ret == -1 && errno == EINTR); return ret;}static qint64 qt_native_write(int fd, const char *data, qint64 len){ qint64 ret = 0; do { ret = ::write(fd, data, len); } while (ret == -1 && errno == EINTR); return ret;}static void qt_native_close(int fd){ int ret; do { ret = ::close(fd); } while (ret == -1 && errno == EINTR);}static void qt_native_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact){ int ret; do { ret = ::sigaction(signum, act, oldact); } while (ret == -1 && errno == EINTR);}static void qt_native_dup2(int oldfd, int newfd){ int ret; do { ret = ::dup2(oldfd, newfd); } while (ret == -1 && errno == EINTR);}static void qt_native_chdir(const char *path){ int ret; do { ret = ::chdir(path); } while (ret == -1 && errno == EINTR);}static void qt_native_execve(const char *filename, char *const argv[], char *const envp[]){ int ret; do { ret = ::execve(filename, argv, envp); } while (ret == -1 && errno == EINTR);}static void qt_native_execv(const char *path, char *const argv[]){ int ret; do { ret = ::execv(path, argv); } while (ret == -1 && errno == EINTR);}static void qt_native_execvp(const char *file, char *const argv[]){ int ret; do { ret = ::execvp(file, argv); } while (ret == -1 && errno == EINTR);}static int qt_qprocess_deadChild_pipe[2];static void (*qt_sa_old_sigchld_handler)(int) = 0;static void qt_sa_sigchld_handler(int signum){ qt_native_write(qt_qprocess_deadChild_pipe[1], "", 1);#if defined (QPROCESS_DEBUG) fprintf(stderr, "*** SIGCHLD\n");#endif if (qt_sa_old_sigchld_handler && qt_sa_old_sigchld_handler != SIG_IGN) qt_sa_old_sigchld_handler(signum);}struct QProcessInfo { QProcess *process; int deathPipe; int exitResult; pid_t pid; int serialNumber;};class QProcessManager : public QThread{ Q_OBJECTpublic: QProcessManager(); ~QProcessManager(); void run(); void catchDeadChildren(); void add(pid_t pid, QProcess *process); void remove(QProcess *process); void lock(); void unlock();private: QMutex mutex; QMap<int, QProcessInfo *> children;};Q_GLOBAL_STATIC(QProcessManager, processManager)QProcessManager::QProcessManager(){#if defined (QPROCESS_DEBUG) qDebug() << "QProcessManager::QProcessManager()";#endif // initialize the dead child pipe and make it non-blocking. in the // extremely unlikely event that the pipe fills up, we do not under any // circumstances want to block. ::pipe(qt_qprocess_deadChild_pipe); ::fcntl(qt_qprocess_deadChild_pipe[0], F_SETFD, FD_CLOEXEC); ::fcntl(qt_qprocess_deadChild_pipe[1], F_SETFD, FD_CLOEXEC); ::fcntl(qt_qprocess_deadChild_pipe[0], F_SETFL, ::fcntl(qt_qprocess_deadChild_pipe[0], F_GETFL) | O_NONBLOCK); ::fcntl(qt_qprocess_deadChild_pipe[1], F_SETFL, ::fcntl(qt_qprocess_deadChild_pipe[1], F_GETFL) | O_NONBLOCK); // set up the SIGCHLD handler, which writes a single byte to the dead // child pipe every time a child dies. struct sigaction oldAction; struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_handler = qt_sa_sigchld_handler; action.sa_flags = SA_NOCLDSTOP; qt_native_sigaction(SIGCHLD, &action, &oldAction); if (oldAction.sa_handler != qt_sa_sigchld_handler) qt_sa_old_sigchld_handler = oldAction.sa_handler;}QProcessManager::~QProcessManager(){ // notify the thread that we're shutting down. qt_native_write(qt_qprocess_deadChild_pipe[1], "@", 1); qt_native_close(qt_qprocess_deadChild_pipe[1]); wait(); // on certain unixes, closing the reading end of the pipe will cause // select in run() to block forever, rather than return with EBADF. qt_native_close(qt_qprocess_deadChild_pipe[0]); qt_qprocess_deadChild_pipe[0] = -1; qt_qprocess_deadChild_pipe[1] = -1; qDeleteAll(children.values()); children.clear();}void QProcessManager::run(){ forever { fd_set readset; FD_ZERO(&readset); FD_SET(qt_qprocess_deadChild_pipe[0], &readset);#if defined (QPROCESS_DEBUG) qDebug() << "QProcessManager::run() waiting for children to die";#endif // block forever, or until activity is detected on the dead child // pipe. the only other peers are the SIGCHLD signal handler, and the // QProcessManager destructor. int nselect = select(qt_qprocess_deadChild_pipe[0] + 1, &readset, 0, 0, 0); if (nselect < 0) { if (errno == EINTR) continue; break; } // empty only one byte from the pipe, even though several SIGCHLD // signals may have been delivered in the meantime, to avoid race // conditions. char c; if (qt_native_read(qt_qprocess_deadChild_pipe[0], &c, 1) < 0 || c == '@') break; // catch any and all children that we can. catchDeadChildren(); }}void QProcessManager::catchDeadChildren(){ QMutexLocker locker(&mutex); // try to catch all children whose pid we have registered, and whose // deathPipe is still valid (i.e, we have not already notified it). QMap<int, QProcessInfo *>::Iterator it = children.begin(); while (it != children.end()) { // notify all children that they may have died. they need to run // waitpid() in their own thread. QProcessInfo *info = it.value(); qt_native_write(info->deathPipe, "", 1);#if defined (QPROCESS_DEBUG) qDebug() << "QProcessManager::run() sending death notice to" << info->process;#endif ++it; }}static QBasicAtomic idCounter = Q_ATOMIC_INIT(1);static int qt_qprocess_nextId(){ register int id; for (;;) { id = idCounter; if (idCounter.testAndSet(id, id + 1)) break; } return id;}void QProcessManager::add(pid_t pid, QProcess *process){#if defined (QPROCESS_DEBUG) qDebug() << "QProcessManager::add() adding pid" << pid << "process" << process;#endif // insert a new info structure for this process QProcessInfo *info = new QProcessInfo; info->process = process; info->deathPipe = process->d_func()->deathPipe[1]; info->exitResult = 0; info->pid = pid; int serial = qt_qprocess_nextId(); process->d_func()->serial = serial; children.insert(serial, info);}void QProcessManager::remove(QProcess *process){ QMutexLocker locker(&mutex); int serial = process->d_func()->serial; QProcessInfo *info = children.value(serial); if (!info) return;#if defined (QPROCESS_DEBUG) qDebug() << "QProcessManager::remove() removing pid" << info->pid << "process" << info->process;#endif children.remove(serial); delete info;}void QProcessManager::lock(){ mutex.lock();}void QProcessManager::unlock(){ mutex.unlock();}static void qt_create_pipe(int *pipe){ if (pipe[0] != -1) qt_native_close(pipe[0]); if (pipe[1] != -1) qt_native_close(pipe[1]);#ifdef Q_OS_IRIX if (::socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == -1) { qWarning("QProcessPrivate::createPipe: Cannot create pipe %p: %s", pipe, qPrintable(qt_error_string(errno))); }#else if (::pipe(pipe) != 0) { qWarning("QProcessPrivate::createPipe: Cannot create pipe %p: %s", pipe, qPrintable(qt_error_string(errno))); }#endif ::fcntl(pipe[0], F_SETFD, FD_CLOEXEC); ::fcntl(pipe[1], F_SETFD, FD_CLOEXEC);}void QProcessPrivate::destroyPipe(int *pipe){ if (pipe[1] != -1) { qt_native_close(pipe[1]); pipe[1] = -1; } if (pipe[0] != -1) { qt_native_close(pipe[0]); pipe[0] = -1; }}/* Create the pipes to a QProcessPrivate::Channel. This function must be called in order: stdin, stdout, stderr*/bool QProcessPrivate::createChannel(Channel &channel){ Q_Q(QProcess); if (&channel == &stderrChannel && processChannelMode == QProcess::MergedChannels) { channel.pipe[0] = -1; channel.pipe[1] = -1; return true; } if (channel.type == Channel::Normal) { // we're piping this channel to our own process qt_create_pipe(channel.pipe); // create the socket notifiers if (threadData->eventDispatcher) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -