2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-10-14 18:05:43 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-10-14 18:05:43 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-10-14 18:05:43 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-10-14 18:05:43 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 17:14:20 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-10-14 18:05:43 +02:00
|
|
|
|
|
|
|
|
#include "qtcprocess.h"
|
|
|
|
|
|
2021-05-05 18:21:22 +02:00
|
|
|
#include "stringutils.h"
|
|
|
|
|
#include "executeondestruction.h"
|
|
|
|
|
#include "hostosinfo.h"
|
2021-05-11 14:34:56 +02:00
|
|
|
#include "commandline.h"
|
2021-05-05 18:21:22 +02:00
|
|
|
#include "qtcassert.h"
|
2013-05-03 13:52:52 +02:00
|
|
|
|
2021-04-30 12:31:36 +02:00
|
|
|
#include <QCoreApplication>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
2021-05-05 18:21:22 +02:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
#include <QTextCodec>
|
2021-04-30 12:31:36 +02:00
|
|
|
#include <QThread>
|
2021-05-05 18:21:22 +02:00
|
|
|
#include <QTimer>
|
2021-04-30 12:31:36 +02:00
|
|
|
|
|
|
|
|
#ifdef QT_GUI_LIB
|
|
|
|
|
// qmlpuppet does not use that.
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#endif
|
2011-02-28 10:14:52 +01:00
|
|
|
|
2021-05-05 18:21:22 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
2011-08-03 12:04:46 +02:00
|
|
|
#ifdef Q_OS_WIN
|
2021-05-05 10:28:07 +02:00
|
|
|
#ifdef QTCREATOR_PCH_H
|
2021-05-01 21:04:48 +02:00
|
|
|
#define CALLBACK WINAPI
|
2021-05-05 10:28:07 +02:00
|
|
|
#endif
|
2011-08-03 12:04:46 +02:00
|
|
|
#include <qt_windows.h>
|
2019-09-10 23:39:29 +03:00
|
|
|
#else
|
|
|
|
|
#include <errno.h>
|
2020-02-18 15:33:15 -08:00
|
|
|
#include <stdio.h>
|
2019-09-10 23:39:29 +03:00
|
|
|
#include <unistd.h>
|
2011-08-03 12:04:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
2014-02-05 10:43:21 +01:00
|
|
|
|
2021-05-05 16:05:53 +02:00
|
|
|
using namespace Utils::Internal;
|
|
|
|
|
|
2014-02-05 10:43:21 +01:00
|
|
|
namespace Utils {
|
2021-05-06 14:02:50 +02:00
|
|
|
namespace Internal {
|
2010-10-14 18:05:43 +02:00
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
enum { debug = 0 };
|
|
|
|
|
enum { syncDebug = 0 };
|
|
|
|
|
|
|
|
|
|
enum { defaultMaxHangTimerCount = 10 };
|
|
|
|
|
|
|
|
|
|
static Q_LOGGING_CATEGORY(processLog, "qtc.utils.synchronousprocess", QtWarningMsg);
|
|
|
|
|
|
2021-05-26 17:40:57 +02:00
|
|
|
static DeviceProcessHooks s_deviceHooks;
|
2021-04-27 15:18:58 +02:00
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
// Data for one channel buffer (stderr/stdout)
|
2021-05-17 10:09:17 +02:00
|
|
|
class ChannelBuffer
|
2021-05-05 16:05:53 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2021-05-06 11:15:28 +02:00
|
|
|
void clearForRun();
|
|
|
|
|
|
|
|
|
|
QString linesRead();
|
2021-05-14 15:21:54 +02:00
|
|
|
void append(const QByteArray &text);
|
2021-05-06 11:15:28 +02:00
|
|
|
|
|
|
|
|
QByteArray rawData;
|
|
|
|
|
QString incompleteLineBuffer; // lines not yet signaled
|
|
|
|
|
QTextCodec *codec = nullptr; // Not owner
|
|
|
|
|
std::unique_ptr<QTextCodec::ConverterState> codecState;
|
|
|
|
|
int rawDataPos = 0;
|
|
|
|
|
std::function<void(const QString &lines)> outputCallback;
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
class ProcessHelper : public QProcess
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ProcessHelper()
|
|
|
|
|
{
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) && defined(Q_OS_UNIX)
|
|
|
|
|
setChildProcessModifier([this] { d->setupChildProcess_impl(); });
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
|
void setupChildProcess() override { setupChildProcess_impl(); }
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void setupChildProcess_impl()
|
|
|
|
|
{
|
|
|
|
|
#if defined Q_OS_UNIX
|
|
|
|
|
// nice value range is -20 to +19 where -20 is highest, 0 default and +19 is lowest
|
|
|
|
|
if (m_lowPriority) {
|
|
|
|
|
errno = 0;
|
|
|
|
|
if (::nice(5) == -1 && errno != 0)
|
|
|
|
|
perror("Failed to set nice value");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Disable terminal by becoming a session leader.
|
|
|
|
|
if (m_disableUnixTerminal)
|
|
|
|
|
setsid();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using QProcess::setErrorString;
|
|
|
|
|
|
|
|
|
|
bool m_lowPriority = false;
|
|
|
|
|
bool m_disableUnixTerminal = false;
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
class QtcProcessPrivate : public QObject
|
|
|
|
|
{
|
|
|
|
|
public:
|
2021-05-14 15:21:54 +02:00
|
|
|
explicit QtcProcessPrivate(QtcProcess *parent)
|
|
|
|
|
: q(parent), m_process(new ProcessHelper)
|
|
|
|
|
{
|
|
|
|
|
connect(m_process, &QProcess::started,
|
|
|
|
|
q, &QtcProcess::started);
|
|
|
|
|
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
|
|
|
|
q, &QtcProcess::finished);
|
|
|
|
|
connect(m_process, &QProcess::errorOccurred,
|
|
|
|
|
q, &QtcProcess::errorOccurred);
|
|
|
|
|
connect(m_process, &QProcess::stateChanged,
|
|
|
|
|
q, &QtcProcess::stateChanged);
|
|
|
|
|
connect(m_process, &QProcess::readyReadStandardOutput,
|
|
|
|
|
this, &QtcProcessPrivate::handleReadyReadStandardOutput);
|
|
|
|
|
connect(m_process, &QProcess::readyReadStandardError,
|
|
|
|
|
this, &QtcProcessPrivate::handleReadyReadStandardError);
|
|
|
|
|
}
|
2021-05-06 11:15:28 +02:00
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
~QtcProcessPrivate()
|
|
|
|
|
{
|
|
|
|
|
delete m_process;
|
|
|
|
|
}
|
2021-05-05 16:05:53 +02:00
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
void handleReadyReadStandardOutput()
|
|
|
|
|
{
|
|
|
|
|
m_stdOut.append(m_process->readAllStandardOutput());
|
|
|
|
|
m_hangTimerCount = 0;
|
|
|
|
|
emit q->readyReadStandardOutput();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void handleReadyReadStandardError()
|
|
|
|
|
{
|
|
|
|
|
m_stdErr.append(m_process->readAllStandardError());
|
|
|
|
|
m_hangTimerCount = 0;
|
|
|
|
|
emit q->readyReadStandardOutput();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QtcProcess *q;
|
|
|
|
|
ProcessHelper *m_process;
|
2021-05-05 16:05:53 +02:00
|
|
|
CommandLine m_commandLine;
|
|
|
|
|
Environment m_environment;
|
2021-05-06 17:54:28 +02:00
|
|
|
QByteArray m_writeData;
|
2021-05-05 16:05:53 +02:00
|
|
|
bool m_haveEnv = false;
|
|
|
|
|
bool m_useCtrlCStub = false;
|
|
|
|
|
|
|
|
|
|
QProcess::OpenMode m_openMode = QProcess::ReadWrite;
|
2021-05-06 11:15:28 +02:00
|
|
|
|
|
|
|
|
// SynchronousProcess left overs:
|
|
|
|
|
void slotTimeout();
|
|
|
|
|
void slotFinished(int exitCode, QProcess::ExitStatus e);
|
|
|
|
|
void slotError(QProcess::ProcessError);
|
|
|
|
|
void clearForRun();
|
|
|
|
|
|
2021-05-12 14:25:50 +02:00
|
|
|
QtcProcess::Result interpretExitCode(int exitCode);
|
2021-05-06 15:54:02 +02:00
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
QTextCodec *m_codec = QTextCodec::codecForLocale();
|
|
|
|
|
QTimer m_timer;
|
|
|
|
|
QEventLoop m_eventLoop;
|
2021-05-17 11:17:53 +02:00
|
|
|
QtcProcess::Result m_result = QtcProcess::StartFailed;
|
|
|
|
|
int m_exitCode = -1;
|
2021-05-06 11:15:28 +02:00
|
|
|
FilePath m_binary;
|
|
|
|
|
ChannelBuffer m_stdOut;
|
|
|
|
|
ChannelBuffer m_stdErr;
|
2021-05-06 15:54:02 +02:00
|
|
|
ExitCodeInterpreter m_exitCodeInterpreter;
|
2021-05-06 11:15:28 +02:00
|
|
|
|
|
|
|
|
int m_hangTimerCount = 0;
|
|
|
|
|
int m_maxHangTimerCount = defaultMaxHangTimerCount;
|
|
|
|
|
bool m_startFailure = false;
|
|
|
|
|
bool m_timeOutMessageBoxEnabled = false;
|
|
|
|
|
bool m_waitingForUser = false;
|
2021-05-17 12:02:42 +02:00
|
|
|
bool m_isSynchronousProcess = false;
|
2021-05-20 13:14:15 +02:00
|
|
|
bool m_processUserEvents = false;
|
2021-05-05 16:05:53 +02:00
|
|
|
};
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcessPrivate::clearForRun()
|
|
|
|
|
{
|
|
|
|
|
m_hangTimerCount = 0;
|
|
|
|
|
m_stdOut.clearForRun();
|
|
|
|
|
m_stdOut.codec = m_codec;
|
|
|
|
|
m_stdErr.clearForRun();
|
|
|
|
|
m_stdErr.codec = m_codec;
|
2021-05-17 11:17:53 +02:00
|
|
|
m_result = QtcProcess::StartFailed;
|
|
|
|
|
m_exitCode = -1;
|
2021-05-06 11:15:28 +02:00
|
|
|
m_startFailure = false;
|
|
|
|
|
m_binary = {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:25:50 +02:00
|
|
|
QtcProcess::Result QtcProcessPrivate::interpretExitCode(int exitCode)
|
2021-05-06 15:54:02 +02:00
|
|
|
{
|
|
|
|
|
if (m_exitCodeInterpreter)
|
|
|
|
|
return m_exitCodeInterpreter(exitCode);
|
|
|
|
|
|
|
|
|
|
// default:
|
2021-05-12 14:25:50 +02:00
|
|
|
return exitCode ? QtcProcess::FinishedError : QtcProcess::Finished;
|
2021-05-06 15:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-05 16:05:53 +02:00
|
|
|
} // Internal
|
|
|
|
|
|
2021-05-06 14:02:50 +02:00
|
|
|
/*!
|
|
|
|
|
\class Utils::QtcProcess
|
|
|
|
|
|
|
|
|
|
\brief The QtcProcess class provides functionality for with processes.
|
|
|
|
|
|
|
|
|
|
\sa Utils::ProcessArgs
|
|
|
|
|
*/
|
|
|
|
|
|
2016-06-08 13:30:30 +02:00
|
|
|
QtcProcess::QtcProcess(QObject *parent)
|
2021-05-14 15:21:54 +02:00
|
|
|
: QObject(parent), d(new QtcProcessPrivate(this))
|
2016-06-08 13:30:30 +02:00
|
|
|
{
|
|
|
|
|
static int qProcessExitStatusMeta = qRegisterMetaType<QProcess::ExitStatus>();
|
|
|
|
|
static int qProcessProcessErrorMeta = qRegisterMetaType<QProcess::ProcessError>();
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(qProcessExitStatusMeta)
|
|
|
|
|
Q_UNUSED(qProcessProcessErrorMeta)
|
2016-06-08 13:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-05 16:05:53 +02:00
|
|
|
QtcProcess::~QtcProcess()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::setEnvironment(const Environment &env)
|
|
|
|
|
{
|
|
|
|
|
d->m_environment = env;
|
|
|
|
|
d->m_haveEnv = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Environment &QtcProcess::environment() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_environment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::setCommand(const CommandLine &cmdLine)
|
|
|
|
|
{
|
|
|
|
|
d->m_commandLine = cmdLine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CommandLine &QtcProcess::commandLine() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_commandLine;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
QString QtcProcess::workingDirectory() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->workingDirectory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::setWorkingDirectory(const QString &dir)
|
|
|
|
|
{
|
|
|
|
|
d->m_process->setWorkingDirectory(dir);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-16 15:57:02 +02:00
|
|
|
void QtcProcess::setUseCtrlCStub(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
// Do not use the stub in debug mode. Activating the stub will shut down
|
|
|
|
|
// Qt Creator otherwise, because they share the same Windows console.
|
|
|
|
|
// See QTCREATORBUG-11995 for details.
|
|
|
|
|
#ifndef QT_DEBUG
|
2021-05-05 16:05:53 +02:00
|
|
|
d->m_useCtrlCStub = enabled;
|
2014-05-20 15:17:32 +02:00
|
|
|
#else
|
|
|
|
|
Q_UNUSED(enabled)
|
2014-05-16 15:57:02 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 06:35:00 +02:00
|
|
|
void QtcProcess::start(const QString &cmd, const QStringList &args)
|
|
|
|
|
{
|
|
|
|
|
setCommand({cmd, args});
|
|
|
|
|
start();
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-14 18:05:43 +02:00
|
|
|
void QtcProcess::start()
|
|
|
|
|
{
|
2021-05-06 17:54:28 +02:00
|
|
|
QTC_CHECK(d->m_writeData.isEmpty()); // FIXME: Use it.
|
|
|
|
|
|
2021-05-05 16:05:53 +02:00
|
|
|
if (d->m_commandLine.executable().needsDevice()) {
|
2021-05-26 17:40:57 +02:00
|
|
|
QTC_ASSERT(s_deviceHooks.startProcessHook, return);
|
|
|
|
|
s_deviceHooks.startProcessHook(*this);
|
2021-04-27 15:18:58 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-14 18:05:43 +02:00
|
|
|
Environment env;
|
2014-02-05 10:43:21 +01:00
|
|
|
const OsType osType = HostOsInfo::hostOs();
|
2021-05-05 16:05:53 +02:00
|
|
|
if (d->m_haveEnv) {
|
|
|
|
|
if (d->m_environment.size() == 0)
|
2019-05-28 18:59:45 +02:00
|
|
|
qWarning("QtcProcess::start: Empty environment set when running '%s'.",
|
2021-05-05 16:05:53 +02:00
|
|
|
qPrintable(d->m_commandLine.executable().toString()));
|
|
|
|
|
env = d->m_environment;
|
2011-11-23 15:21:07 +01:00
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_process->setProcessEnvironment(env.toProcessEnvironment());
|
2010-10-14 18:05:43 +02:00
|
|
|
} else {
|
|
|
|
|
env = Environment::systemEnvironment();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString &workDir = workingDirectory();
|
|
|
|
|
QString command;
|
2021-05-06 13:07:36 +02:00
|
|
|
ProcessArgs arguments;
|
|
|
|
|
bool success = ProcessArgs::prepareCommand(d->m_commandLine.executable().toString(),
|
|
|
|
|
d->m_commandLine.arguments(),
|
|
|
|
|
&command, &arguments, osType, &env, &workDir);
|
2014-02-05 10:43:21 +01:00
|
|
|
if (osType == OsTypeWindows) {
|
2014-02-19 14:30:07 +01:00
|
|
|
QString args;
|
2021-05-05 16:05:53 +02:00
|
|
|
if (d->m_useCtrlCStub) {
|
2021-05-14 15:21:54 +02:00
|
|
|
if (d->m_process->m_lowPriority)
|
2021-05-06 13:07:36 +02:00
|
|
|
ProcessArgs::addArg(&args, "-nice");
|
|
|
|
|
ProcessArgs::addArg(&args, QDir::toNativeSeparators(command));
|
2014-02-19 14:30:07 +01:00
|
|
|
command = QCoreApplication::applicationDirPath()
|
|
|
|
|
+ QLatin1String("/qtcreator_ctrlc_stub.exe");
|
2021-05-14 15:21:54 +02:00
|
|
|
} else if (d->m_process->m_lowPriority) {
|
2019-09-10 23:39:29 +03:00
|
|
|
#ifdef Q_OS_WIN
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_process->setCreateProcessArgumentsModifier(
|
|
|
|
|
[](QProcess::CreateProcessArguments *args) {
|
|
|
|
|
args->flags |= BELOW_NORMAL_PRIORITY_CLASS;
|
2019-09-10 23:39:29 +03:00
|
|
|
});
|
|
|
|
|
#endif
|
2014-02-19 14:30:07 +01:00
|
|
|
}
|
2021-05-06 13:07:36 +02:00
|
|
|
ProcessArgs::addArgs(&args, arguments.toWindowsArgs());
|
2010-10-14 18:05:43 +02:00
|
|
|
#ifdef Q_OS_WIN
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_process->setNativeArguments(args);
|
2010-10-14 18:05:43 +02:00
|
|
|
#endif
|
2014-02-19 14:30:07 +01:00
|
|
|
// Note: Arguments set with setNativeArgs will be appended to the ones
|
|
|
|
|
// passed with start() below.
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_process->start(command, QStringList(), d->m_openMode);
|
2014-02-05 10:43:21 +01:00
|
|
|
} else {
|
|
|
|
|
if (!success) {
|
|
|
|
|
setErrorString(tr("Error in command line."));
|
|
|
|
|
// Should be FailedToStart, but we cannot set the process error from the outside,
|
|
|
|
|
// so it would be inconsistent.
|
2016-08-03 19:43:54 +03:00
|
|
|
emit errorOccurred(QProcess::UnknownError);
|
2014-02-05 10:43:21 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_process->start(command, arguments.toUnixArgs(), d->m_openMode);
|
2014-02-05 10:43:21 +01:00
|
|
|
}
|
2010-10-14 18:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
2011-08-03 12:04:46 +02:00
|
|
|
#ifdef Q_OS_WIN
|
2013-05-03 13:52:52 +02:00
|
|
|
static BOOL sendMessage(UINT message, HWND hwnd, LPARAM lParam)
|
2011-08-03 12:04:46 +02:00
|
|
|
{
|
|
|
|
|
DWORD dwProcessID;
|
|
|
|
|
GetWindowThreadProcessId(hwnd, &dwProcessID);
|
2011-08-09 13:43:56 +02:00
|
|
|
if ((DWORD)lParam == dwProcessID) {
|
2013-05-03 13:52:52 +02:00
|
|
|
SendNotifyMessage(hwnd, message, 0, 0);
|
2011-08-03 12:04:46 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2013-05-03 13:52:52 +02:00
|
|
|
|
|
|
|
|
BOOL CALLBACK sendShutDownMessageToAllWindowsOfProcess_enumWnd(HWND hwnd, LPARAM lParam)
|
|
|
|
|
{
|
|
|
|
|
static UINT uiShutDownMessage = RegisterWindowMessage(L"qtcctrlcstub_shutdown");
|
|
|
|
|
return sendMessage(uiShutDownMessage, hwnd, lParam);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOL CALLBACK sendInterruptMessageToAllWindowsOfProcess_enumWnd(HWND hwnd, LPARAM lParam)
|
|
|
|
|
{
|
|
|
|
|
static UINT uiInterruptMessage = RegisterWindowMessage(L"qtcctrlcstub_interrupt");
|
|
|
|
|
return sendMessage(uiInterruptMessage, hwnd, lParam);
|
|
|
|
|
}
|
2011-08-03 12:04:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void QtcProcess::terminate()
|
|
|
|
|
{
|
|
|
|
|
#ifdef Q_OS_WIN
|
2021-05-05 16:05:53 +02:00
|
|
|
if (d->m_useCtrlCStub)
|
2020-11-02 11:19:12 +01:00
|
|
|
EnumWindows(sendShutDownMessageToAllWindowsOfProcess_enumWnd, processId());
|
2011-08-03 12:04:46 +02:00
|
|
|
else
|
|
|
|
|
#endif
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_process->terminate();
|
2011-08-03 12:04:46 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-03 13:52:52 +02:00
|
|
|
void QtcProcess::interrupt()
|
|
|
|
|
{
|
2013-09-12 16:34:34 +02:00
|
|
|
#ifdef Q_OS_WIN
|
2021-05-05 16:05:53 +02:00
|
|
|
QTC_ASSERT(d->m_useCtrlCStub, return);
|
2020-11-02 11:19:12 +01:00
|
|
|
EnumWindows(sendInterruptMessageToAllWindowsOfProcess_enumWnd, processId());
|
2013-09-12 16:34:34 +02:00
|
|
|
#endif
|
2013-05-03 13:52:52 +02:00
|
|
|
}
|
2010-10-14 18:05:43 +02:00
|
|
|
|
2021-05-05 16:05:53 +02:00
|
|
|
void QtcProcess::setLowPriority()
|
|
|
|
|
{
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_process->m_lowPriority = true;
|
2021-05-05 16:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::setDisableUnixTerminal()
|
|
|
|
|
{
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_process->m_disableUnixTerminal = true;
|
2021-05-05 16:05:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-26 17:40:57 +02:00
|
|
|
void QtcProcess::setRemoteProcessHooks(const DeviceProcessHooks &hooks)
|
2021-04-27 15:18:58 +02:00
|
|
|
{
|
2021-05-26 17:40:57 +02:00
|
|
|
s_deviceHooks = hooks;
|
2021-04-27 15:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
void QtcProcess::setOpenMode(QIODevice::OpenMode mode)
|
2021-04-29 17:15:48 +02:00
|
|
|
{
|
2021-05-05 16:05:53 +02:00
|
|
|
d->m_openMode = mode;
|
2021-04-29 17:15:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QtcProcess::stopProcess()
|
|
|
|
|
{
|
|
|
|
|
if (state() == QProcess::NotRunning)
|
|
|
|
|
return true;
|
|
|
|
|
terminate();
|
|
|
|
|
if (waitForFinished(300))
|
|
|
|
|
return true;
|
|
|
|
|
kill();
|
|
|
|
|
return waitForFinished(300);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 12:31:36 +02:00
|
|
|
static bool askToKill(const QString &command)
|
|
|
|
|
{
|
|
|
|
|
#ifdef QT_GUI_LIB
|
|
|
|
|
if (QThread::currentThread() != QCoreApplication::instance()->thread())
|
|
|
|
|
return true;
|
|
|
|
|
const QString title = QtcProcess::tr("Process not Responding");
|
|
|
|
|
QString msg = command.isEmpty() ?
|
|
|
|
|
QtcProcess::tr("The process is not responding.") :
|
|
|
|
|
QtcProcess::tr("The process \"%1\" is not responding.").arg(command);
|
|
|
|
|
msg += ' ';
|
|
|
|
|
msg += QtcProcess::tr("Would you like to terminate it?");
|
|
|
|
|
// Restore the cursor that is set to wait while running.
|
|
|
|
|
const bool hasOverrideCursor = QApplication::overrideCursor() != nullptr;
|
|
|
|
|
if (hasOverrideCursor)
|
|
|
|
|
QApplication::restoreOverrideCursor();
|
|
|
|
|
QMessageBox::StandardButton answer = QMessageBox::question(nullptr, title, msg, QMessageBox::Yes|QMessageBox::No);
|
|
|
|
|
if (hasOverrideCursor)
|
|
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
|
|
|
return answer == QMessageBox::Yes;
|
|
|
|
|
#else
|
|
|
|
|
Q_UNUSED(command)
|
|
|
|
|
return true;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 07:49:33 +02:00
|
|
|
// Helper for running a process synchronously in the foreground with timeout
|
|
|
|
|
// detection similar SynchronousProcess' handling (taking effect after no more output
|
|
|
|
|
// occurs on stderr/stdout as opposed to waitForFinished()). Returns false if a timeout
|
|
|
|
|
// occurs. Checking of the process' exit state/code still has to be done.
|
|
|
|
|
|
2021-04-30 12:31:36 +02:00
|
|
|
bool QtcProcess::readDataFromProcess(int timeoutS,
|
|
|
|
|
QByteArray *stdOut,
|
|
|
|
|
QByteArray *stdErr,
|
|
|
|
|
bool showTimeOutMessageBox)
|
|
|
|
|
{
|
|
|
|
|
enum { syncDebug = 0 };
|
|
|
|
|
if (syncDebug)
|
|
|
|
|
qDebug() << ">readDataFromProcess" << timeoutS;
|
|
|
|
|
if (state() != QProcess::Running) {
|
|
|
|
|
qWarning("readDataFromProcess: Process in non-running state passed in.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
QTC_ASSERT(d->m_process->readChannel() == QProcess::StandardOutput, return false);
|
2021-04-30 12:31:36 +02:00
|
|
|
|
|
|
|
|
// Keep the process running until it has no longer has data
|
|
|
|
|
bool finished = false;
|
|
|
|
|
bool hasData = false;
|
|
|
|
|
do {
|
|
|
|
|
finished = waitForFinished(timeoutS > 0 ? timeoutS * 1000 : -1)
|
|
|
|
|
|| state() == QProcess::NotRunning;
|
|
|
|
|
// First check 'stdout'
|
2021-05-14 15:21:54 +02:00
|
|
|
if (d->m_process->bytesAvailable()) { // applies to readChannel() only
|
2021-04-30 12:31:36 +02:00
|
|
|
hasData = true;
|
2021-05-14 15:21:54 +02:00
|
|
|
const QByteArray newStdOut = d->m_process->readAllStandardOutput();
|
2021-04-30 12:31:36 +02:00
|
|
|
if (stdOut)
|
|
|
|
|
stdOut->append(newStdOut);
|
|
|
|
|
}
|
|
|
|
|
// Check 'stderr' separately. This is a special handling
|
|
|
|
|
// for 'git pull' and the like which prints its progress on stderr.
|
2021-05-14 15:21:54 +02:00
|
|
|
const QByteArray newStdErr = d->m_process->readAllStandardError();
|
2021-04-30 12:31:36 +02:00
|
|
|
if (!newStdErr.isEmpty()) {
|
|
|
|
|
hasData = true;
|
|
|
|
|
if (stdErr)
|
|
|
|
|
stdErr->append(newStdErr);
|
|
|
|
|
}
|
|
|
|
|
// Prompt user, pretend we have data if says 'No'.
|
|
|
|
|
const bool hang = !hasData && !finished;
|
2021-05-14 15:21:54 +02:00
|
|
|
hasData = hang && showTimeOutMessageBox && !askToKill(d->m_process->program());
|
2021-04-30 12:31:36 +02:00
|
|
|
} while (hasData && !finished);
|
|
|
|
|
if (syncDebug)
|
|
|
|
|
qDebug() << "<readDataFromProcess" << finished;
|
|
|
|
|
return finished;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 17:21:43 +02:00
|
|
|
QString QtcProcess::normalizeNewlines(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
QString res = text;
|
|
|
|
|
const auto newEnd = std::unique(res.begin(), res.end(), [](const QChar &c1, const QChar &c2) {
|
|
|
|
|
return c1 == '\r' && c2 == '\r'; // QTCREATORBUG-24556
|
|
|
|
|
});
|
|
|
|
|
res.chop(std::distance(newEnd, res.end()));
|
|
|
|
|
res.replace("\r\n", "\n");
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:25:50 +02:00
|
|
|
QtcProcess::Result QtcProcess::result() const
|
|
|
|
|
{
|
2021-05-17 11:17:53 +02:00
|
|
|
return d->m_result;
|
2021-05-12 14:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::setResult(Result result)
|
|
|
|
|
{
|
2021-05-17 11:17:53 +02:00
|
|
|
d->m_result = result;
|
2021-05-12 14:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int QtcProcess::exitCode() const
|
|
|
|
|
{
|
2021-05-14 15:21:54 +02:00
|
|
|
return d->m_isSynchronousProcess ? d->m_exitCode : d->m_process->exitCode(); // FIXME: Unify.
|
2021-05-12 14:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-05 10:43:21 +01:00
|
|
|
|
2021-05-05 15:04:30 +02:00
|
|
|
// Path utilities
|
|
|
|
|
|
|
|
|
|
// Locate a binary in a directory, applying all kinds of
|
|
|
|
|
// extensions the operating system supports.
|
|
|
|
|
static QString checkBinary(const QDir &dir, const QString &binary)
|
|
|
|
|
{
|
|
|
|
|
// naive UNIX approach
|
|
|
|
|
const QFileInfo info(dir.filePath(binary));
|
|
|
|
|
if (info.isFile() && info.isExecutable())
|
|
|
|
|
return info.absoluteFilePath();
|
|
|
|
|
|
|
|
|
|
// Does the OS have some weird extension concept or does the
|
|
|
|
|
// binary have a 3 letter extension?
|
|
|
|
|
if (HostOsInfo::isAnyUnixHost() && !HostOsInfo::isMacHost())
|
|
|
|
|
return QString();
|
|
|
|
|
const int dotIndex = binary.lastIndexOf(QLatin1Char('.'));
|
|
|
|
|
if (dotIndex != -1 && dotIndex == binary.size() - 4)
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
switch (HostOsInfo::hostOs()) {
|
|
|
|
|
case OsTypeLinux:
|
|
|
|
|
case OsTypeOtherUnix:
|
|
|
|
|
case OsTypeOther:
|
|
|
|
|
break;
|
|
|
|
|
case OsTypeWindows: {
|
|
|
|
|
static const char *windowsExtensions[] = {".cmd", ".bat", ".exe", ".com"};
|
|
|
|
|
// Check the Windows extensions using the order
|
|
|
|
|
const int windowsExtensionCount = sizeof(windowsExtensions)/sizeof(const char*);
|
|
|
|
|
for (int e = 0; e < windowsExtensionCount; e ++) {
|
|
|
|
|
const QFileInfo windowsBinary(dir.filePath(binary + QLatin1String(windowsExtensions[e])));
|
|
|
|
|
if (windowsBinary.isFile() && windowsBinary.isExecutable())
|
|
|
|
|
return windowsBinary.absoluteFilePath();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case OsTypeMac: {
|
|
|
|
|
// Check for Mac app folders
|
|
|
|
|
const QFileInfo appFolder(dir.filePath(binary + QLatin1String(".app")));
|
|
|
|
|
if (appFolder.isDir()) {
|
|
|
|
|
QString macBinaryPath = appFolder.absoluteFilePath();
|
|
|
|
|
macBinaryPath += QLatin1String("/Contents/MacOS/");
|
|
|
|
|
macBinaryPath += binary;
|
|
|
|
|
const QFileInfo macBinary(macBinaryPath);
|
|
|
|
|
if (macBinary.isFile() && macBinary.isExecutable())
|
|
|
|
|
return macBinary.absoluteFilePath();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString QtcProcess::locateBinary(const QString &path, const QString &binary)
|
|
|
|
|
{
|
|
|
|
|
// Absolute file?
|
|
|
|
|
const QFileInfo absInfo(binary);
|
|
|
|
|
if (absInfo.isAbsolute())
|
|
|
|
|
return checkBinary(absInfo.dir(), absInfo.fileName());
|
|
|
|
|
|
|
|
|
|
// Windows finds binaries in the current directory
|
|
|
|
|
if (HostOsInfo::isWindowsHost()) {
|
|
|
|
|
const QString currentDirBinary = checkBinary(QDir::current(), binary);
|
|
|
|
|
if (!currentDirBinary.isEmpty())
|
|
|
|
|
return currentDirBinary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QStringList paths = path.split(HostOsInfo::pathListSeparator());
|
|
|
|
|
if (paths.empty())
|
|
|
|
|
return QString();
|
|
|
|
|
const QStringList::const_iterator cend = paths.constEnd();
|
|
|
|
|
for (QStringList::const_iterator it = paths.constBegin(); it != cend; ++it) {
|
|
|
|
|
const QDir dir(*it);
|
|
|
|
|
const QString rc = checkBinary(dir, binary);
|
|
|
|
|
if (!rc.isEmpty())
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 17:40:57 +02:00
|
|
|
Environment QtcProcess::systemEnvironmentForBinary(const FilePath &filePath)
|
|
|
|
|
{
|
|
|
|
|
if (filePath.needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.systemEnvironmentForBinary, return {});
|
|
|
|
|
return s_deviceHooks.systemEnvironmentForBinary(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Environment::systemEnvironment();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
void QtcProcess::setProcessChannelMode(QProcess::ProcessChannelMode mode)
|
|
|
|
|
{
|
|
|
|
|
d->m_process->setProcessChannelMode(mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QProcess::ProcessError QtcProcess::error() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->error();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QProcess::ProcessState QtcProcess::state() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->state();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString QtcProcess::errorString() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->errorString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::setErrorString(const QString &str)
|
|
|
|
|
{
|
|
|
|
|
d->m_process->setErrorString(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 QtcProcess::processId() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->processId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QtcProcess::waitForStarted(int msecs)
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->waitForStarted(msecs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QtcProcess::waitForReadyRead(int msecs)
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->waitForReadyRead(msecs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QtcProcess::waitForFinished(int msecs)
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->waitForFinished(msecs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray QtcProcess::readAllStandardOutput()
|
|
|
|
|
{
|
|
|
|
|
QByteArray buf = d->m_stdOut.rawData;
|
|
|
|
|
d->m_stdOut.rawData.clear();
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray QtcProcess::readAllStandardError()
|
|
|
|
|
{
|
|
|
|
|
QByteArray buf = d->m_stdErr.rawData;
|
|
|
|
|
d->m_stdErr.rawData.clear();
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray QtcProcess::readAll()
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->readAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray QtcProcess::readLine()
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->readLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QProcess::ExitStatus QtcProcess::exitStatus() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->exitStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::kill()
|
|
|
|
|
{
|
|
|
|
|
d->m_process->kill();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 QtcProcess::write(const QByteArray &input)
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->write(input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::closeWriteChannel()
|
|
|
|
|
{
|
|
|
|
|
d->m_process->closeWriteChannel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::close()
|
|
|
|
|
{
|
|
|
|
|
d->m_process->close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtcProcess::setReadChannel(QProcess::ProcessChannel channel)
|
|
|
|
|
{
|
|
|
|
|
d->m_process->setReadChannel(channel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QtcProcess::canReadLine() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->canReadLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QtcProcess::atEnd() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_process->atEnd();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 15:04:30 +02:00
|
|
|
QString QtcProcess::locateBinary(const QString &binary)
|
|
|
|
|
{
|
|
|
|
|
const QByteArray path = qgetenv("PATH");
|
|
|
|
|
return locateBinary(QString::fromLocal8Bit(path), binary);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 18:21:22 +02:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\class Utils::SynchronousProcess
|
|
|
|
|
|
|
|
|
|
\brief The SynchronousProcess class runs a synchronous process in its own
|
|
|
|
|
event loop that blocks only user input events. Thus, it allows for the GUI to
|
|
|
|
|
repaint and append output to log windows.
|
|
|
|
|
|
2021-05-06 08:56:42 +02:00
|
|
|
The callbacks set with setStdOutCallBack(), setStdErrCallback() are called
|
|
|
|
|
with complete lines based on the '\\n' marker.
|
2021-05-05 18:21:22 +02:00
|
|
|
They would typically be used for log windows.
|
|
|
|
|
|
|
|
|
|
There is a timeout handling that takes effect after the last data have been
|
|
|
|
|
read from stdout/stdin (as opposed to waitForFinished(), which measures time
|
|
|
|
|
since it was invoked). It is thus also suitable for slow processes that
|
|
|
|
|
continuously output data (like version system operations).
|
|
|
|
|
|
|
|
|
|
The property timeOutMessageBoxEnabled influences whether a message box is
|
|
|
|
|
shown asking the user if they want to kill the process on timeout (default: false).
|
|
|
|
|
|
|
|
|
|
There are also static utility functions for dealing with fully synchronous
|
|
|
|
|
processes, like reading the output with correct timeout handling.
|
|
|
|
|
|
|
|
|
|
Caution: This class should NOT be used if there is a chance that the process
|
|
|
|
|
triggers opening dialog boxes (for example, by file watchers triggering),
|
|
|
|
|
as this will cause event loop problems.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-05-14 13:12:46 +02:00
|
|
|
QString QtcProcess::exitMessage()
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-14 13:12:46 +02:00
|
|
|
const QString fullCmd = commandLine().toUserOutput();
|
2021-05-12 14:25:50 +02:00
|
|
|
switch (result()) {
|
2021-05-05 18:21:22 +02:00
|
|
|
case Finished:
|
2021-05-14 13:12:46 +02:00
|
|
|
return QtcProcess::tr("The command \"%1\" finished successfully.").arg(fullCmd);
|
2021-05-05 18:21:22 +02:00
|
|
|
case FinishedError:
|
2021-05-14 13:12:46 +02:00
|
|
|
return QtcProcess::tr("The command \"%1\" terminated with exit code %2.")
|
|
|
|
|
.arg(fullCmd).arg(exitCode());
|
2021-05-05 18:21:22 +02:00
|
|
|
case TerminatedAbnormally:
|
2021-05-14 13:12:46 +02:00
|
|
|
return QtcProcess::tr("The command \"%1\" terminated abnormally.").arg(fullCmd);
|
2021-05-05 18:21:22 +02:00
|
|
|
case StartFailed:
|
2021-05-14 13:12:46 +02:00
|
|
|
return QtcProcess::tr("The command \"%1\" could not be started.").arg(fullCmd);
|
2021-05-05 18:21:22 +02:00
|
|
|
case Hang:
|
2021-05-06 11:15:28 +02:00
|
|
|
return QtcProcess::tr("The command \"%1\" did not respond within the timeout limit (%2 s).")
|
2021-05-14 13:12:46 +02:00
|
|
|
.arg(fullCmd).arg(d->m_hangTimerCount);
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
QIODevice *QtcProcess::ioDevice()
|
|
|
|
|
{
|
|
|
|
|
return d->m_process;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:25:50 +02:00
|
|
|
QByteArray QtcProcess::allRawOutput() const
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-17 10:07:16 +02:00
|
|
|
if (!d->m_stdOut.rawData.isEmpty() && !d->m_stdErr.rawData.isEmpty()) {
|
|
|
|
|
QByteArray result = d->m_stdOut.rawData;
|
2021-05-05 18:21:22 +02:00
|
|
|
if (!result.endsWith('\n'))
|
|
|
|
|
result += '\n';
|
2021-05-17 10:07:16 +02:00
|
|
|
result += d->m_stdErr.rawData;
|
2021-05-05 18:21:22 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
2021-05-17 10:07:16 +02:00
|
|
|
return !d->m_stdOut.rawData.isEmpty() ? d->m_stdOut.rawData : d->m_stdErr.rawData;
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:25:50 +02:00
|
|
|
QString QtcProcess::allOutput() const
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
|
|
|
|
const QString out = stdOut();
|
|
|
|
|
const QString err = stdErr();
|
|
|
|
|
|
|
|
|
|
if (!out.isEmpty() && !err.isEmpty()) {
|
|
|
|
|
QString result = out;
|
|
|
|
|
if (!result.endsWith('\n'))
|
|
|
|
|
result += '\n';
|
|
|
|
|
result += err;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return !out.isEmpty() ? out : err;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:25:50 +02:00
|
|
|
QString QtcProcess::stdOut() const
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-17 10:07:16 +02:00
|
|
|
return normalizeNewlines(d->m_codec->toUnicode(d->m_stdOut.rawData));
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:25:50 +02:00
|
|
|
QString QtcProcess::stdErr() const
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-17 10:07:16 +02:00
|
|
|
return normalizeNewlines(d->m_codec->toUnicode(d->m_stdErr.rawData));
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:25:50 +02:00
|
|
|
QByteArray QtcProcess::rawStdOut() const
|
|
|
|
|
{
|
2021-05-17 10:07:16 +02:00
|
|
|
return d->m_stdOut.rawData;
|
2021-05-12 14:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray QtcProcess::rawStdErr() const
|
|
|
|
|
{
|
2021-05-17 10:07:16 +02:00
|
|
|
return d->m_stdErr.rawData;
|
2021-05-12 14:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug str, const QtcProcess &r)
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
|
|
|
|
QDebug nsp = str.nospace();
|
2021-05-12 14:25:50 +02:00
|
|
|
nsp << "QtcProcess: result="
|
2021-05-17 11:17:53 +02:00
|
|
|
<< r.d->m_result << " ex=" << r.exitCode() << '\n'
|
2021-05-17 10:07:16 +02:00
|
|
|
<< r.d->m_stdOut.rawData.size() << " bytes stdout, stderr=" << r.d->m_stdErr.rawData << '\n';
|
2021-05-05 18:21:22 +02:00
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChannelBuffer::clearForRun()
|
|
|
|
|
{
|
|
|
|
|
rawDataPos = 0;
|
|
|
|
|
rawData.clear();
|
|
|
|
|
codecState.reset(new QTextCodec::ConverterState);
|
|
|
|
|
incompleteLineBuffer.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check for complete lines read from the device and return them, moving the
|
|
|
|
|
* buffer position. */
|
|
|
|
|
QString ChannelBuffer::linesRead()
|
|
|
|
|
{
|
|
|
|
|
// Convert and append the new input to the buffer of incomplete lines
|
|
|
|
|
const char *start = rawData.constData() + rawDataPos;
|
|
|
|
|
const int len = rawData.size() - rawDataPos;
|
|
|
|
|
incompleteLineBuffer.append(codec->toUnicode(start, len, codecState.get()));
|
|
|
|
|
rawDataPos = rawData.size();
|
|
|
|
|
|
|
|
|
|
// Any completed lines in the incompleteLineBuffer?
|
|
|
|
|
const int lastLineIndex = qMax(incompleteLineBuffer.lastIndexOf('\n'),
|
|
|
|
|
incompleteLineBuffer.lastIndexOf('\r'));
|
|
|
|
|
if (lastLineIndex == -1)
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
// Get completed lines and remove them from the incompleteLinesBuffer:
|
|
|
|
|
const QString lines = QtcProcess::normalizeNewlines(incompleteLineBuffer.left(lastLineIndex + 1));
|
|
|
|
|
incompleteLineBuffer = incompleteLineBuffer.mid(lastLineIndex + 1);
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 15:21:54 +02:00
|
|
|
void ChannelBuffer::append(const QByteArray &text)
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
|
|
|
|
if (text.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
rawData += text;
|
|
|
|
|
|
|
|
|
|
// Buffered. Emit complete lines?
|
2021-05-06 08:56:42 +02:00
|
|
|
if (outputCallback) {
|
2021-05-05 18:21:22 +02:00
|
|
|
const QString lines = linesRead();
|
2021-05-06 08:56:42 +02:00
|
|
|
if (!lines.isEmpty())
|
|
|
|
|
outputCallback(lines);
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ----------- SynchronousProcess
|
2021-05-06 11:15:28 +02:00
|
|
|
SynchronousProcess::SynchronousProcess()
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-17 12:02:42 +02:00
|
|
|
d->m_isSynchronousProcess = true; // Only for QTC_ASSERTs above.
|
|
|
|
|
|
2021-05-05 18:21:22 +02:00
|
|
|
d->m_timer.setInterval(1000);
|
2021-05-06 11:15:28 +02:00
|
|
|
connect(&d->m_timer, &QTimer::timeout, d, &QtcProcessPrivate::slotTimeout);
|
2021-05-14 15:21:54 +02:00
|
|
|
connect(d->m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
2021-05-06 11:15:28 +02:00
|
|
|
d, &QtcProcessPrivate::slotFinished);
|
2021-05-14 15:21:54 +02:00
|
|
|
connect(d->m_process, &QProcess::errorOccurred, d, &QtcProcessPrivate::slotError);
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SynchronousProcess::~SynchronousProcess()
|
|
|
|
|
{
|
|
|
|
|
disconnect(&d->m_timer, nullptr, this, nullptr);
|
2021-05-06 10:06:45 +02:00
|
|
|
disconnect(this, nullptr, this, nullptr);
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-20 13:14:15 +02:00
|
|
|
void SynchronousProcess::setProcessUserEventWhileRunning()
|
|
|
|
|
{
|
|
|
|
|
d->m_processUserEvents = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcess::setTimeoutS(int timeoutS)
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-17 12:02:42 +02:00
|
|
|
QTC_CHECK(d->m_isSynchronousProcess);
|
2021-05-05 18:21:22 +02:00
|
|
|
if (timeoutS > 0)
|
|
|
|
|
d->m_maxHangTimerCount = qMax(2, timeoutS);
|
|
|
|
|
else
|
|
|
|
|
d->m_maxHangTimerCount = INT_MAX / 1000;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcess::setCodec(QTextCodec *c)
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(c, return);
|
|
|
|
|
d->m_codec = c;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcess::setTimeOutMessageBoxEnabled(bool v)
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-17 12:02:42 +02:00
|
|
|
QTC_CHECK(d->m_isSynchronousProcess);
|
2021-05-05 18:21:22 +02:00
|
|
|
d->m_timeOutMessageBoxEnabled = v;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcess::setExitCodeInterpreter(const ExitCodeInterpreter &interpreter)
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
|
|
|
|
d->m_exitCodeInterpreter = interpreter;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 17:54:28 +02:00
|
|
|
void QtcProcess::setWriteData(const QByteArray &writeData)
|
|
|
|
|
{
|
2021-05-17 12:02:42 +02:00
|
|
|
QTC_CHECK(d->m_isSynchronousProcess);
|
2021-05-06 17:54:28 +02:00
|
|
|
d->m_writeData = writeData;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 18:21:22 +02:00
|
|
|
#ifdef QT_GUI_LIB
|
|
|
|
|
static bool isGuiThread()
|
|
|
|
|
{
|
|
|
|
|
return QThread::currentThread() == QCoreApplication::instance()->thread();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-05-20 13:14:15 +02:00
|
|
|
void SynchronousProcess::runBlocking()
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-17 12:02:42 +02:00
|
|
|
QTC_CHECK(d->m_isSynchronousProcess);
|
2021-05-05 18:21:22 +02:00
|
|
|
// FIXME: Implement properly
|
2021-05-17 12:02:42 +02:00
|
|
|
if (d->m_commandLine.executable().needsDevice()) {
|
2021-05-05 18:21:22 +02:00
|
|
|
|
|
|
|
|
// writeData ?
|
2021-05-20 13:14:15 +02:00
|
|
|
QtcProcess::start();
|
2021-05-05 18:21:22 +02:00
|
|
|
|
2021-05-06 17:51:09 +02:00
|
|
|
waitForFinished();
|
2021-05-05 18:21:22 +02:00
|
|
|
|
2021-05-17 11:17:53 +02:00
|
|
|
d->m_result = QtcProcess::Finished;
|
|
|
|
|
d->m_exitCode = exitCode();
|
2021-05-12 14:25:50 +02:00
|
|
|
return;
|
2021-05-05 18:21:22 +02:00
|
|
|
};
|
|
|
|
|
|
2021-05-20 13:14:15 +02:00
|
|
|
qCDebug(processLog).noquote() << "Starting blocking:" << d->m_commandLine.toUserOutput()
|
|
|
|
|
<< " process user events: " << d->m_processUserEvents;
|
2021-05-12 14:25:50 +02:00
|
|
|
ExecuteOnDestruction logResult([this] { qCDebug(processLog) << *this; });
|
2021-05-05 18:21:22 +02:00
|
|
|
|
|
|
|
|
d->clearForRun();
|
|
|
|
|
|
2021-05-17 12:02:42 +02:00
|
|
|
d->m_binary = d->m_commandLine.executable();
|
2021-05-20 13:14:15 +02:00
|
|
|
|
|
|
|
|
if (d->m_processUserEvents) {
|
|
|
|
|
if (!d->m_writeData.isEmpty()) {
|
2021-05-14 15:21:54 +02:00
|
|
|
connect(d->m_process, &QProcess::started, this, [this] {
|
2021-05-20 13:14:15 +02:00
|
|
|
write(d->m_writeData);
|
|
|
|
|
closeWriteChannel();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setOpenMode(d->m_writeData.isEmpty() ? QIODevice::ReadOnly : QIODevice::ReadWrite);
|
|
|
|
|
QtcProcess::start();
|
|
|
|
|
|
|
|
|
|
// On Windows, start failure is triggered immediately if the
|
|
|
|
|
// executable cannot be found in the path. Do not start the
|
|
|
|
|
// event loop in that case.
|
|
|
|
|
if (!d->m_startFailure) {
|
|
|
|
|
d->m_timer.start();
|
2021-05-05 18:21:22 +02:00
|
|
|
#ifdef QT_GUI_LIB
|
2021-05-20 13:14:15 +02:00
|
|
|
if (isGuiThread())
|
|
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
2021-05-05 18:21:22 +02:00
|
|
|
#endif
|
2021-05-20 13:14:15 +02:00
|
|
|
d->m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_stdOut.append(d->m_process->readAllStandardOutput());
|
|
|
|
|
d->m_stdErr.append(d->m_process->readAllStandardError());
|
2021-05-05 18:21:22 +02:00
|
|
|
|
2021-05-20 13:14:15 +02:00
|
|
|
d->m_timer.stop();
|
2021-05-05 18:21:22 +02:00
|
|
|
#ifdef QT_GUI_LIB
|
2021-05-20 13:14:15 +02:00
|
|
|
if (isGuiThread())
|
|
|
|
|
QApplication::restoreOverrideCursor();
|
2021-05-05 18:21:22 +02:00
|
|
|
#endif
|
2021-05-20 13:14:15 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
setOpenMode(QIODevice::ReadOnly);
|
|
|
|
|
QtcProcess::start();
|
|
|
|
|
if (!waitForStarted(d->m_maxHangTimerCount * 1000)) {
|
|
|
|
|
d->m_result = QtcProcess::StartFailed;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
closeWriteChannel();
|
|
|
|
|
if (!waitForFinished(d->m_maxHangTimerCount * 1000)) {
|
|
|
|
|
d->m_result = QtcProcess::Hang;
|
|
|
|
|
terminate();
|
|
|
|
|
if (!waitForFinished(1000)) {
|
|
|
|
|
kill();
|
|
|
|
|
waitForFinished(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-05 18:21:22 +02:00
|
|
|
|
2021-05-20 13:14:15 +02:00
|
|
|
if (state() != QProcess::NotRunning)
|
|
|
|
|
return;
|
2021-05-05 18:21:22 +02:00
|
|
|
|
2021-05-17 11:17:53 +02:00
|
|
|
d->m_exitCode = exitCode();
|
2021-05-20 13:14:15 +02:00
|
|
|
if (d->m_result == QtcProcess::StartFailed) {
|
|
|
|
|
if (exitStatus() != QProcess::NormalExit)
|
|
|
|
|
d->m_result = QtcProcess::TerminatedAbnormally;
|
|
|
|
|
else
|
|
|
|
|
d->m_result = d->interpretExitCode(d->m_exitCode);
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
2021-05-14 15:21:54 +02:00
|
|
|
d->m_stdOut.append(d->m_process->readAllStandardOutput());
|
|
|
|
|
d->m_stdErr.append(d->m_process->readAllStandardError());
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcess::setStdOutCallback(const std::function<void (const QString &)> &callback)
|
2021-05-06 08:56:42 +02:00
|
|
|
{
|
2021-05-17 12:02:42 +02:00
|
|
|
QTC_CHECK(d->m_isSynchronousProcess);
|
2021-05-06 08:56:42 +02:00
|
|
|
d->m_stdOut.outputCallback = callback;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcess::setStdErrCallback(const std::function<void (const QString &)> &callback)
|
2021-05-06 08:56:42 +02:00
|
|
|
{
|
2021-05-17 12:02:42 +02:00
|
|
|
QTC_CHECK(d->m_isSynchronousProcess);
|
2021-05-06 08:56:42 +02:00
|
|
|
d->m_stdErr.outputCallback = callback;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcessPrivate::slotTimeout()
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-06 11:15:28 +02:00
|
|
|
if (!m_waitingForUser && (++m_hangTimerCount > m_maxHangTimerCount)) {
|
2021-05-05 18:21:22 +02:00
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << "HANG detected, killing";
|
2021-05-06 11:15:28 +02:00
|
|
|
m_waitingForUser = true;
|
|
|
|
|
const bool terminate = !m_timeOutMessageBoxEnabled || askToKill(m_binary.toString());
|
|
|
|
|
m_waitingForUser = false;
|
2021-05-05 18:21:22 +02:00
|
|
|
if (terminate) {
|
2021-05-06 11:15:28 +02:00
|
|
|
q->stopProcess();
|
2021-05-17 11:17:53 +02:00
|
|
|
m_result = QtcProcess::Hang;
|
2021-05-05 18:21:22 +02:00
|
|
|
} else {
|
2021-05-06 11:15:28 +02:00
|
|
|
m_hangTimerCount = 0;
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (debug)
|
2021-05-06 11:15:28 +02:00
|
|
|
qDebug() << Q_FUNC_INFO << m_hangTimerCount;
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcessPrivate::slotFinished(int exitCode, QProcess::ExitStatus e)
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << exitCode << e;
|
2021-05-06 11:15:28 +02:00
|
|
|
m_hangTimerCount = 0;
|
2021-05-05 18:21:22 +02:00
|
|
|
|
|
|
|
|
switch (e) {
|
|
|
|
|
case QProcess::NormalExit:
|
2021-05-17 11:17:53 +02:00
|
|
|
m_result = interpretExitCode(exitCode);
|
|
|
|
|
m_exitCode = exitCode;
|
2021-05-05 18:21:22 +02:00
|
|
|
break;
|
|
|
|
|
case QProcess::CrashExit:
|
|
|
|
|
// Was hang detected before and killed?
|
2021-05-17 11:17:53 +02:00
|
|
|
if (m_result != QtcProcess::Hang)
|
|
|
|
|
m_result = QtcProcess::TerminatedAbnormally;
|
|
|
|
|
m_exitCode = -1;
|
2021-05-05 18:21:22 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2021-05-06 11:15:28 +02:00
|
|
|
m_eventLoop.quit();
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
void QtcProcessPrivate::slotError(QProcess::ProcessError e)
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-06 11:15:28 +02:00
|
|
|
m_hangTimerCount = 0;
|
2021-05-05 18:21:22 +02:00
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << e;
|
|
|
|
|
// Was hang detected before and killed?
|
2021-05-17 11:17:53 +02:00
|
|
|
if (m_result != QtcProcess::Hang)
|
|
|
|
|
m_result = QtcProcess::StartFailed;
|
2021-05-06 11:15:28 +02:00
|
|
|
m_startFailure = true;
|
|
|
|
|
m_eventLoop.quit();
|
2021-05-05 18:21:22 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-12 09:00:12 +02:00
|
|
|
} // namespace Utils
|