2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** No Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "consoleprocess.h"
|
|
|
|
|
|
2010-10-19 11:14:03 +02:00
|
|
|
#include "environment.h"
|
|
|
|
|
#include "qtcprocess.h"
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
#include <QtCore/QCoreApplication>
|
2009-04-09 20:09:10 +02:00
|
|
|
#include <QtCore/QDir>
|
|
|
|
|
#include <QtCore/QSettings>
|
2009-02-20 12:33:16 +01:00
|
|
|
#include <QtCore/QTemporaryFile>
|
|
|
|
|
|
|
|
|
|
#include <QtNetwork/QLocalSocket>
|
2010-09-02 13:39:19 +02:00
|
|
|
#include <QtNetwork/QLocalServer>
|
2009-02-20 12:33:16 +01:00
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
namespace Utils {
|
|
|
|
|
struct ConsoleProcessPrivate {
|
|
|
|
|
ConsoleProcessPrivate();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
ConsoleProcess::Mode m_mode;
|
|
|
|
|
qint64 m_appPid;
|
|
|
|
|
qint64 m_appMainThreadId;
|
|
|
|
|
int m_appCode;
|
|
|
|
|
QString m_executable;
|
|
|
|
|
QProcess::ExitStatus m_appStatus;
|
|
|
|
|
QLocalServer m_stubServer;
|
|
|
|
|
QLocalSocket *m_stubSocket;
|
|
|
|
|
QTemporaryFile *m_tempFile;
|
|
|
|
|
|
|
|
|
|
QProcess m_process;
|
|
|
|
|
QByteArray m_stubServerDir;
|
|
|
|
|
QSettings *m_settings;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ConsoleProcessPrivate::ConsoleProcessPrivate() :
|
|
|
|
|
m_mode(ConsoleProcess::Run),
|
2009-05-06 14:26:20 +02:00
|
|
|
m_appPid(0),
|
|
|
|
|
m_stubSocket(0),
|
2010-10-19 11:14:03 +02:00
|
|
|
m_tempFile(0),
|
2009-05-06 14:26:20 +02:00
|
|
|
m_settings(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-09-02 13:39:19 +02:00
|
|
|
}
|
|
|
|
|
ConsoleProcess::ConsoleProcess(QObject *parent) :
|
|
|
|
|
QObject(parent), d(new ConsoleProcessPrivate)
|
|
|
|
|
{
|
|
|
|
|
connect(&d->m_stubServer, SIGNAL(newConnection()), SLOT(stubConnectionAvailable()));
|
2009-02-20 12:33:16 +01:00
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_process.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
|
|
|
connect(&d->m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
|
2009-02-20 12:33:16 +01:00
|
|
|
SLOT(stubExited()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
ConsoleProcess::~ConsoleProcess()
|
2009-01-15 19:44:59 +01:00
|
|
|
{
|
2009-02-20 12:33:16 +01:00
|
|
|
stop();
|
2009-01-15 19:44:59 +01:00
|
|
|
}
|
|
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
void ConsoleProcess::setMode(Mode m)
|
|
|
|
|
{
|
|
|
|
|
d->m_mode = m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConsoleProcess::Mode ConsoleProcess::mode() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 ConsoleProcess::applicationPID() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_appPid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ConsoleProcess::exitCode() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_appCode;
|
|
|
|
|
} // This will be the signal number if exitStatus == CrashExit
|
|
|
|
|
|
|
|
|
|
QProcess::ExitStatus ConsoleProcess::exitStatus() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_appStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConsoleProcess::setSettings(QSettings *settings)
|
|
|
|
|
{
|
|
|
|
|
d->m_settings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-19 11:14:03 +02:00
|
|
|
bool ConsoleProcess::start(const QString &program, const QString &args)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-20 12:33:16 +01:00
|
|
|
if (isRunning())
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
|
|
|
|
|
2010-10-19 11:14:03 +02:00
|
|
|
QtcProcess::SplitError perr;
|
|
|
|
|
QStringList pargs = QtcProcess::prepareArgs(args, &perr, &m_environment, &m_workingDir);
|
|
|
|
|
QString pcmd;
|
|
|
|
|
if (perr == QtcProcess::SplitOk) {
|
|
|
|
|
pcmd = program;
|
|
|
|
|
} else {
|
|
|
|
|
if (perr != QtcProcess::FoundMeta) {
|
|
|
|
|
emit processMessage(tr("Quoting error in command."), true);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (d->m_mode == Debug) {
|
|
|
|
|
// FIXME: QTCREATORBUG-2809
|
|
|
|
|
emit processMessage(tr("Debugging complex shell commands in a terminal"
|
|
|
|
|
" is currently not supported."), true);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
pcmd = QLatin1String("/bin/sh");
|
|
|
|
|
pargs << QLatin1String("-c") << (QtcProcess::quoteArg(program) + QLatin1Char(' ') + args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QtcProcess::SplitError qerr;
|
|
|
|
|
QStringList xtermArgs = QtcProcess::prepareArgs(terminalEmulator(d->m_settings), &qerr,
|
|
|
|
|
&m_environment, &m_workingDir);
|
|
|
|
|
if (qerr != QtcProcess::SplitOk) {
|
|
|
|
|
emit processMessage(qerr == QtcProcess::BadQuoting
|
|
|
|
|
? tr("Quoting error in terminal command.")
|
|
|
|
|
: tr("Terminal command may not be a shell command."), true);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-06 14:26:20 +02:00
|
|
|
const QString err = stubServerListen();
|
2009-02-20 12:33:16 +01:00
|
|
|
if (!err.isEmpty()) {
|
2010-04-19 14:21:33 +02:00
|
|
|
emit processMessage(msgCommChannelFailed(err), true);
|
2009-02-20 12:33:16 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-10-19 11:14:03 +02:00
|
|
|
QStringList env = m_environment.toStringList();
|
|
|
|
|
if (!env.isEmpty()) {
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_tempFile = new QTemporaryFile();
|
|
|
|
|
if (!d->m_tempFile->open()) {
|
2009-04-09 19:13:40 +02:00
|
|
|
stubServerShutdown();
|
2010-09-02 13:39:19 +02:00
|
|
|
emit processMessage(msgCannotCreateTempFile(d->m_tempFile->errorString()), true);
|
|
|
|
|
delete d->m_tempFile;
|
|
|
|
|
d->m_tempFile = 0;
|
2009-04-09 19:13:40 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-10-19 11:14:03 +02:00
|
|
|
foreach (const QString &var, env) {
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_tempFile->write(var.toLocal8Bit());
|
|
|
|
|
d->m_tempFile->write("", 1);
|
2009-04-09 19:13:40 +02:00
|
|
|
}
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_tempFile->flush();
|
2009-04-09 19:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
2009-04-09 20:09:10 +02:00
|
|
|
xtermArgs
|
2009-03-11 16:07:49 +01:00
|
|
|
#ifdef Q_OS_MAC
|
2009-05-06 14:26:20 +02:00
|
|
|
<< (QCoreApplication::applicationDirPath() + QLatin1String("/../Resources/qtcreator_process_stub"))
|
2009-03-11 16:07:49 +01:00
|
|
|
#else
|
2009-05-06 14:26:20 +02:00
|
|
|
<< (QCoreApplication::applicationDirPath() + QLatin1String("/qtcreator_process_stub"))
|
2009-03-11 16:07:49 +01:00
|
|
|
#endif
|
2010-09-02 13:39:19 +02:00
|
|
|
<< modeOption(d->m_mode)
|
|
|
|
|
<< d->m_stubServer.fullServerName()
|
2009-05-06 14:26:20 +02:00
|
|
|
<< msgPromptToClose()
|
2009-04-09 19:13:40 +02:00
|
|
|
<< workingDirectory()
|
2010-09-02 13:39:19 +02:00
|
|
|
<< (d->m_tempFile ? d->m_tempFile->fileName() : QString())
|
2010-10-19 11:14:03 +02:00
|
|
|
<< pcmd << pargs;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-04-09 20:09:10 +02:00
|
|
|
QString xterm = xtermArgs.takeFirst();
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_process.start(xterm, xtermArgs);
|
|
|
|
|
if (!d->m_process.waitForStarted()) {
|
2009-02-20 12:33:16 +01:00
|
|
|
stubServerShutdown();
|
2010-04-19 14:21:33 +02:00
|
|
|
emit processMessage(tr("Cannot start the terminal emulator '%1'.").arg(xterm), true);
|
2010-09-02 13:39:19 +02:00
|
|
|
delete d->m_tempFile;
|
|
|
|
|
d->m_tempFile = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_executable = program;
|
2009-02-20 12:33:16 +01:00
|
|
|
emit wrapperStarted();
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
void ConsoleProcess::stop()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-20 12:33:16 +01:00
|
|
|
if (!isRunning())
|
|
|
|
|
return;
|
|
|
|
|
stubServerShutdown();
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_appPid = 0;
|
|
|
|
|
d->m_process.terminate();
|
|
|
|
|
if (!d->m_process.waitForFinished(1000))
|
|
|
|
|
d->m_process.kill();
|
|
|
|
|
d->m_process.waitForFinished();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConsoleProcess::isRunning() const
|
|
|
|
|
{
|
2010-09-02 13:39:19 +02:00
|
|
|
return d->m_process.state() != QProcess::NotRunning;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
QString ConsoleProcess::stubServerListen()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-20 12:33:16 +01:00
|
|
|
// We need to put the socket in a private directory, as some systems simply do not
|
|
|
|
|
// check the file permissions of sockets.
|
|
|
|
|
QString stubFifoDir;
|
|
|
|
|
forever {
|
|
|
|
|
{
|
|
|
|
|
QTemporaryFile tf;
|
|
|
|
|
if (!tf.open())
|
2009-05-06 14:26:20 +02:00
|
|
|
return msgCannotCreateTempFile(tf.errorString());
|
2009-02-20 12:33:16 +01:00
|
|
|
stubFifoDir = QFile::encodeName(tf.fileName());
|
|
|
|
|
}
|
|
|
|
|
// By now the temp file was deleted again
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_stubServerDir = QFile::encodeName(stubFifoDir);
|
|
|
|
|
if (!::mkdir(d->m_stubServerDir.constData(), 0700))
|
2009-02-20 12:33:16 +01:00
|
|
|
break;
|
|
|
|
|
if (errno != EEXIST)
|
2009-05-06 14:26:20 +02:00
|
|
|
return msgCannotCreateTempDir(stubFifoDir, QString::fromLocal8Bit(strerror(errno)));
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
2009-05-06 14:26:20 +02:00
|
|
|
const QString stubServer = stubFifoDir + "/stub-socket";
|
2010-09-02 13:39:19 +02:00
|
|
|
if (!d->m_stubServer.listen(stubServer)) {
|
|
|
|
|
::rmdir(d->m_stubServerDir.constData());
|
|
|
|
|
return tr("Cannot create socket '%1': %2").arg(stubServer, d->m_stubServer.errorString());
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConsoleProcess::stubServerShutdown()
|
|
|
|
|
{
|
2010-09-02 13:39:19 +02:00
|
|
|
delete d->m_stubSocket;
|
|
|
|
|
d->m_stubSocket = 0;
|
|
|
|
|
if (d->m_stubServer.isListening()) {
|
|
|
|
|
d->m_stubServer.close();
|
|
|
|
|
::rmdir(d->m_stubServerDir.constData());
|
2009-02-20 12:33:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConsoleProcess::stubConnectionAvailable()
|
|
|
|
|
{
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_stubSocket = d->m_stubServer.nextPendingConnection();
|
|
|
|
|
connect(d->m_stubSocket, SIGNAL(readyRead()), SLOT(readStubOutput()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
static QString errorMsg(int code)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-20 12:33:16 +01:00
|
|
|
return QString::fromLocal8Bit(strerror(code));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
void ConsoleProcess::readStubOutput()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-09-02 13:39:19 +02:00
|
|
|
while (d->m_stubSocket->canReadLine()) {
|
|
|
|
|
QByteArray out = d->m_stubSocket->readLine();
|
2009-02-20 12:33:16 +01:00
|
|
|
out.chop(1); // \n
|
|
|
|
|
if (out.startsWith("err:chdir ")) {
|
2010-04-19 14:21:33 +02:00
|
|
|
emit processMessage(msgCannotChangeToWorkDir(workingDirectory(), errorMsg(out.mid(10).toInt())), true);
|
2009-02-20 12:33:16 +01:00
|
|
|
} else if (out.startsWith("err:exec ")) {
|
2010-09-02 13:39:19 +02:00
|
|
|
emit processMessage(msgCannotExecute(d->m_executable, errorMsg(out.mid(9).toInt())), true);
|
2009-02-20 12:33:16 +01:00
|
|
|
} else if (out.startsWith("pid ")) {
|
2009-04-09 19:13:40 +02:00
|
|
|
// Will not need it any more
|
2010-09-02 13:39:19 +02:00
|
|
|
delete d->m_tempFile;
|
|
|
|
|
d->m_tempFile = 0;
|
2009-04-09 19:13:40 +02:00
|
|
|
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_appPid = out.mid(4).toInt();
|
2009-02-20 12:33:16 +01:00
|
|
|
emit processStarted();
|
|
|
|
|
} else if (out.startsWith("exit ")) {
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_appStatus = QProcess::NormalExit;
|
|
|
|
|
d->m_appCode = out.mid(5).toInt();
|
|
|
|
|
d->m_appPid = 0;
|
2009-02-20 12:33:16 +01:00
|
|
|
emit processStopped();
|
|
|
|
|
} else if (out.startsWith("crash ")) {
|
2010-09-02 13:39:19 +02:00
|
|
|
d->m_appStatus = QProcess::CrashExit;
|
|
|
|
|
d->m_appCode = out.mid(6).toInt();
|
|
|
|
|
d->m_appPid = 0;
|
2009-02-20 12:33:16 +01:00
|
|
|
emit processStopped();
|
|
|
|
|
} else {
|
2010-09-02 13:39:19 +02:00
|
|
|
emit processMessage(msgUnexpectedOutput(out), true);
|
|
|
|
|
d->m_process.terminate();
|
2009-02-20 12:33:16 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:33:16 +01:00
|
|
|
void ConsoleProcess::stubExited()
|
|
|
|
|
{
|
|
|
|
|
// The stub exit might get noticed before we read the error status.
|
2010-09-02 13:39:19 +02:00
|
|
|
if (d->m_stubSocket && d->m_stubSocket->state() == QLocalSocket::ConnectedState)
|
|
|
|
|
d->m_stubSocket->waitForDisconnected();
|
2009-02-20 12:33:16 +01:00
|
|
|
stubServerShutdown();
|
2010-09-02 13:39:19 +02:00
|
|
|
delete d->m_tempFile;
|
|
|
|
|
d->m_tempFile = 0;
|
|
|
|
|
if (d->m_appPid) {
|
|
|
|
|
d->m_appStatus = QProcess::CrashExit;
|
|
|
|
|
d->m_appCode = -1;
|
|
|
|
|
d->m_appPid = 0;
|
2009-02-20 12:33:16 +01:00
|
|
|
emit processStopped(); // Maybe it actually did not, but keep state consistent
|
|
|
|
|
}
|
|
|
|
|
emit wrapperStopped();
|
|
|
|
|
}
|
2009-04-09 20:09:10 +02:00
|
|
|
|
|
|
|
|
QString ConsoleProcess::defaultTerminalEmulator()
|
|
|
|
|
{
|
|
|
|
|
// FIXME: enable this once runInTerminal works nicely
|
|
|
|
|
#if 0 //def Q_OS_MAC
|
|
|
|
|
return QDir::cleanPath(QCoreApplication::applicationDirPath()
|
|
|
|
|
+ QLatin1String("/../Resources/runInTerminal.command"));
|
|
|
|
|
#else
|
|
|
|
|
return QLatin1String("xterm");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ConsoleProcess::terminalEmulator(const QSettings *settings)
|
|
|
|
|
{
|
2009-05-06 14:26:20 +02:00
|
|
|
const QString dflt = defaultTerminalEmulator() + QLatin1String(" -e");
|
2009-04-09 20:09:10 +02:00
|
|
|
if (!settings)
|
|
|
|
|
return dflt;
|
|
|
|
|
return settings->value(QLatin1String("General/TerminalEmulator"), dflt).toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConsoleProcess::setTerminalEmulator(QSettings *settings, const QString &term)
|
|
|
|
|
{
|
|
|
|
|
return settings->setValue(QLatin1String("General/TerminalEmulator"), term);
|
|
|
|
|
}
|
2010-09-02 13:39:19 +02:00
|
|
|
} // namespace Utils
|