2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2013-09-30 13:20:02 +02:00
|
|
|
|
|
|
|
|
#include "androidconfigurations.h"
|
|
|
|
|
#include "androidsignaloperation.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
2021-08-20 09:05:03 +02:00
|
|
|
#include <utils/qtcprocess.h>
|
2013-09-30 13:20:02 +02:00
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
using namespace Utils;
|
2013-09-30 13:20:02 +02:00
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
namespace Android {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
AndroidSignalOperation::AndroidSignalOperation()
|
|
|
|
|
: m_adbPath(AndroidConfigurations::currentConfig().adbToolPath())
|
2013-09-30 13:20:02 +02:00
|
|
|
, m_timeout(new QTimer(this))
|
|
|
|
|
{
|
|
|
|
|
m_timeout->setInterval(5000);
|
2016-06-26 22:52:59 +03:00
|
|
|
connect(m_timeout, &QTimer::timeout, this, &AndroidSignalOperation::handleTimeout);
|
2013-09-30 13:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-31 09:18:23 +02:00
|
|
|
AndroidSignalOperation::~AndroidSignalOperation() = default;
|
|
|
|
|
|
2022-04-05 17:57:04 +02:00
|
|
|
bool AndroidSignalOperation::handleCrashMessage()
|
|
|
|
|
{
|
|
|
|
|
if (m_adbProcess->exitStatus() == QProcess::NormalExit)
|
|
|
|
|
return false;
|
|
|
|
|
m_errorMessage = QLatin1String(" adb process exit code: ") + QString::number(m_adbProcess->exitCode());
|
|
|
|
|
const QString adbError = m_adbProcess->errorString();
|
|
|
|
|
if (!adbError.isEmpty())
|
|
|
|
|
m_errorMessage += QLatin1String(" adb process error: ") + adbError;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
void AndroidSignalOperation::adbFindRunAsFinished()
|
2013-09-30 13:20:02 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(m_state == RunAs, return);
|
|
|
|
|
m_timeout->stop();
|
|
|
|
|
|
2022-04-05 17:57:04 +02:00
|
|
|
handleCrashMessage();
|
|
|
|
|
const QString runAs = QString::fromLatin1(m_adbProcess->readAllStandardOutput());
|
2022-03-22 17:41:52 +01:00
|
|
|
m_adbProcess.release()->deleteLater();
|
2013-09-30 13:20:02 +02:00
|
|
|
if (runAs.isEmpty() || !m_errorMessage.isEmpty()) {
|
2022-04-05 17:57:04 +02:00
|
|
|
m_errorMessage.prepend(QLatin1String("Cannot find User for process: ")
|
|
|
|
|
+ QString::number(m_pid));
|
2013-09-30 13:20:02 +02:00
|
|
|
m_state = Idle;
|
|
|
|
|
emit finished(m_errorMessage);
|
|
|
|
|
} else {
|
2022-03-22 17:41:52 +01:00
|
|
|
startAdbProcess(Kill, {m_adbPath, {"shell", "run-as", runAs, "kill",
|
|
|
|
|
QString("-%1").arg(m_signal), QString::number(m_pid)}},
|
|
|
|
|
[this] { adbKillFinished(); });
|
2013-09-30 13:20:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
void AndroidSignalOperation::adbKillFinished()
|
2013-09-30 13:20:02 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(m_state == Kill, return);
|
|
|
|
|
m_timeout->stop();
|
|
|
|
|
|
2022-04-05 17:57:04 +02:00
|
|
|
if (!handleCrashMessage())
|
2013-09-30 13:20:02 +02:00
|
|
|
m_errorMessage = QString::fromLatin1(m_adbProcess->readAllStandardError());
|
2022-03-22 17:41:52 +01:00
|
|
|
m_adbProcess.release()->deleteLater();
|
2022-04-05 17:57:04 +02:00
|
|
|
if (!m_errorMessage.isEmpty())
|
|
|
|
|
m_errorMessage.prepend(QLatin1String("Cannot kill process: ") + QString::number(m_pid));
|
2013-09-30 13:20:02 +02:00
|
|
|
m_state = Idle;
|
|
|
|
|
emit finished(m_errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
void AndroidSignalOperation::handleTimeout()
|
2013-09-30 13:20:02 +02:00
|
|
|
{
|
2022-03-22 17:41:52 +01:00
|
|
|
m_adbProcess.reset();
|
2013-09-30 13:20:02 +02:00
|
|
|
m_timeout->stop();
|
|
|
|
|
m_state = Idle;
|
|
|
|
|
m_errorMessage = QLatin1String("adb process timed out");
|
|
|
|
|
emit finished(m_errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
void AndroidSignalOperation::signalOperationViaADB(qint64 pid, int signal)
|
2013-09-30 13:20:02 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(m_state == Idle, return);
|
|
|
|
|
m_pid = pid;
|
|
|
|
|
m_signal = signal;
|
2022-03-22 17:41:52 +01:00
|
|
|
startAdbProcess(RunAs, {m_adbPath, {"shell", "cat", QString("/proc/%1/cmdline").arg(m_pid)}},
|
|
|
|
|
[this] { adbFindRunAsFinished(); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AndroidSignalOperation::startAdbProcess(State state, const Utils::CommandLine &commandLine,
|
|
|
|
|
FinishHandler handler)
|
|
|
|
|
{
|
|
|
|
|
m_state = state;
|
2013-09-30 13:20:02 +02:00
|
|
|
m_timeout->start();
|
2022-03-22 17:41:52 +01:00
|
|
|
m_adbProcess.reset(new QtcProcess);
|
2022-04-05 17:57:04 +02:00
|
|
|
connect(m_adbProcess.get(), &QtcProcess::done, this, handler);
|
2022-03-22 17:41:52 +01:00
|
|
|
m_adbProcess->setCommand(commandLine);
|
2021-08-20 09:05:03 +02:00
|
|
|
m_adbProcess->start();
|
2013-09-30 13:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
void AndroidSignalOperation::killProcess(qint64 pid)
|
2013-09-30 13:20:02 +02:00
|
|
|
{
|
|
|
|
|
signalOperationViaADB(pid, 9);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
void AndroidSignalOperation::killProcess(const QString &filePath)
|
2013-09-30 13:20:02 +02:00
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(filePath)
|
2013-09-30 13:20:02 +02:00
|
|
|
m_errorMessage = QLatin1String("The android signal operation does "
|
|
|
|
|
"not support killing by filepath.");
|
|
|
|
|
emit finished(m_errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
void AndroidSignalOperation::interruptProcess(qint64 pid)
|
2013-09-30 13:20:02 +02:00
|
|
|
{
|
|
|
|
|
signalOperationViaADB(pid, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:05:03 +02:00
|
|
|
void AndroidSignalOperation::interruptProcess(const QString &filePath)
|
2013-09-30 13:20:02 +02:00
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(filePath)
|
2013-09-30 13:20:02 +02:00
|
|
|
m_errorMessage = QLatin1String("The android signal operation does "
|
|
|
|
|
"not support interrupting by filepath.");
|
|
|
|
|
emit finished(m_errorMessage);
|
|
|
|
|
}
|
2021-08-20 09:05:03 +02:00
|
|
|
|
|
|
|
|
} // Internal
|
|
|
|
|
} // Android
|