2021-07-07 11:36:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "launchersockethandler.h"
|
|
|
|
|
|
|
|
|
|
#include "launcherlogging.h"
|
2021-09-06 14:48:08 +02:00
|
|
|
#include "processreaper.h"
|
2021-08-09 14:01:14 +02:00
|
|
|
#include "processutils.h"
|
2021-07-07 11:36:03 +02:00
|
|
|
|
2021-08-19 16:39:21 +02:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QLocalSocket>
|
|
|
|
|
#include <QProcess>
|
2021-07-07 11:36:03 +02:00
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2021-08-09 14:01:14 +02:00
|
|
|
class Process : public ProcessHelper
|
2021-07-07 11:36:03 +02:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
Process(quintptr token, QObject *parent = nullptr) :
|
2021-09-06 14:48:08 +02:00
|
|
|
ProcessHelper(parent), m_token(token) { }
|
2021-07-07 11:36:03 +02:00
|
|
|
|
|
|
|
|
quintptr token() const { return m_token; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const quintptr m_token;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LauncherSocketHandler::LauncherSocketHandler(QString serverPath, QObject *parent)
|
|
|
|
|
: QObject(parent),
|
|
|
|
|
m_serverPath(std::move(serverPath)),
|
|
|
|
|
m_socket(new QLocalSocket(this))
|
|
|
|
|
{
|
|
|
|
|
m_packetParser.setDevice(m_socket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LauncherSocketHandler::~LauncherSocketHandler()
|
|
|
|
|
{
|
2022-03-07 17:22:53 +01:00
|
|
|
for (auto it = m_processes.cbegin(); it != m_processes.cend(); ++it) {
|
|
|
|
|
Process *p = it.value();
|
|
|
|
|
if (p->state() != QProcess::NotRunning)
|
|
|
|
|
logWarn(QStringLiteral("Shutting down while process %1 is running").arg(p->program()));
|
|
|
|
|
ProcessReaper::reap(p);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-07 11:36:03 +02:00
|
|
|
m_socket->disconnect();
|
2022-03-07 17:22:53 +01:00
|
|
|
m_socket->disconnectFromServer();
|
|
|
|
|
if (m_socket->state() != QLocalSocket::UnconnectedState
|
|
|
|
|
&& !m_socket->waitForDisconnected()) {
|
|
|
|
|
logWarn("Could not disconnect from server");
|
2021-07-07 11:36:03 +02:00
|
|
|
m_socket->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::start()
|
|
|
|
|
{
|
|
|
|
|
connect(m_socket, &QLocalSocket::disconnected,
|
|
|
|
|
this, &LauncherSocketHandler::handleSocketClosed);
|
|
|
|
|
connect(m_socket, &QLocalSocket::readyRead, this, &LauncherSocketHandler::handleSocketData);
|
|
|
|
|
connect(m_socket,
|
|
|
|
|
&QLocalSocket::errorOccurred,
|
|
|
|
|
this, &LauncherSocketHandler::handleSocketError);
|
|
|
|
|
m_socket->connectToServer(m_serverPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::handleSocketData()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
if (!m_packetParser.parse())
|
|
|
|
|
return;
|
|
|
|
|
} catch (const PacketParser::InvalidPacketSizeException &e) {
|
2022-03-07 17:22:53 +01:00
|
|
|
logWarn(QStringLiteral("Internal protocol error: Invalid packet size %1")
|
2021-07-07 11:36:03 +02:00
|
|
|
.arg(e.size));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
switch (m_packetParser.type()) {
|
|
|
|
|
case LauncherPacketType::StartProcess:
|
|
|
|
|
handleStartPacket();
|
|
|
|
|
break;
|
2021-08-06 14:43:03 +02:00
|
|
|
case LauncherPacketType::WriteIntoProcess:
|
|
|
|
|
handleWritePacket();
|
|
|
|
|
break;
|
2021-07-07 11:36:03 +02:00
|
|
|
case LauncherPacketType::StopProcess:
|
|
|
|
|
handleStopPacket();
|
|
|
|
|
break;
|
|
|
|
|
case LauncherPacketType::Shutdown:
|
|
|
|
|
handleShutdownPacket();
|
|
|
|
|
return;
|
|
|
|
|
default:
|
2022-03-07 17:22:53 +01:00
|
|
|
logWarn(QStringLiteral("Internal protocol error: Invalid packet type %1")
|
2021-07-07 11:36:03 +02:00
|
|
|
.arg(static_cast<int>(m_packetParser.type())));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
handleSocketData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::handleSocketError()
|
|
|
|
|
{
|
|
|
|
|
if (m_socket->error() != QLocalSocket::PeerClosedError) {
|
2022-03-07 17:22:53 +01:00
|
|
|
logError(QStringLiteral("Socket error: %1").arg(m_socket->errorString()));
|
2021-07-07 11:36:03 +02:00
|
|
|
m_socket->disconnect();
|
|
|
|
|
qApp->quit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::handleSocketClosed()
|
|
|
|
|
{
|
2022-03-07 17:22:53 +01:00
|
|
|
logWarn("The connection has closed unexpectedly, shutting down");
|
2021-07-07 11:36:03 +02:00
|
|
|
m_socket->disconnect();
|
|
|
|
|
qApp->quit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::handleProcessError()
|
|
|
|
|
{
|
|
|
|
|
Process * proc = senderProcess();
|
2022-03-15 14:41:28 +01:00
|
|
|
|
|
|
|
|
// In case of FailedToStart we won't receive finished signal, so we send the error
|
|
|
|
|
// packet and remove the process here and now. For all other errors we should expect
|
|
|
|
|
// corresponding finished signal to appear, so we will send the error data together with
|
|
|
|
|
// the finished packet later on.
|
|
|
|
|
if (proc->error() != QProcess::FailedToStart)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-07-07 11:36:03 +02:00
|
|
|
ProcessErrorPacket packet(proc->token());
|
|
|
|
|
packet.error = proc->error();
|
|
|
|
|
packet.errorString = proc->errorString();
|
|
|
|
|
sendPacket(packet);
|
2022-03-15 14:41:28 +01:00
|
|
|
removeProcess(proc->token());
|
2021-07-07 11:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 15:49:20 +02:00
|
|
|
void LauncherSocketHandler::handleProcessStarted()
|
|
|
|
|
{
|
|
|
|
|
Process *proc = senderProcess();
|
|
|
|
|
ProcessStartedPacket packet(proc->token());
|
|
|
|
|
packet.processId = proc->processId();
|
2021-12-16 10:43:58 +01:00
|
|
|
proc->processStartHandler()->handleProcessStarted();
|
2021-07-12 15:49:20 +02:00
|
|
|
sendPacket(packet);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 13:26:33 +02:00
|
|
|
void LauncherSocketHandler::handleReadyReadStandardOutput()
|
|
|
|
|
{
|
|
|
|
|
Process * proc = senderProcess();
|
|
|
|
|
ReadyReadStandardOutputPacket packet(proc->token());
|
|
|
|
|
packet.standardChannel = proc->readAllStandardOutput();
|
|
|
|
|
sendPacket(packet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::handleReadyReadStandardError()
|
|
|
|
|
{
|
|
|
|
|
Process * proc = senderProcess();
|
|
|
|
|
ReadyReadStandardErrorPacket packet(proc->token());
|
|
|
|
|
packet.standardChannel = proc->readAllStandardError();
|
|
|
|
|
sendPacket(packet);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-07 11:36:03 +02:00
|
|
|
void LauncherSocketHandler::handleProcessFinished()
|
|
|
|
|
{
|
|
|
|
|
Process * proc = senderProcess();
|
|
|
|
|
ProcessFinishedPacket packet(proc->token());
|
|
|
|
|
packet.error = proc->error();
|
|
|
|
|
packet.errorString = proc->errorString();
|
|
|
|
|
packet.exitCode = proc->exitCode();
|
|
|
|
|
packet.exitStatus = proc->exitStatus();
|
|
|
|
|
packet.stdErr = proc->readAllStandardError();
|
|
|
|
|
packet.stdOut = proc->readAllStandardOutput();
|
|
|
|
|
sendPacket(packet);
|
2021-08-03 10:30:55 +02:00
|
|
|
removeProcess(proc->token());
|
2021-07-07 11:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::handleStartPacket()
|
|
|
|
|
{
|
|
|
|
|
Process *& process = m_processes[m_packetParser.token()];
|
|
|
|
|
if (!process)
|
|
|
|
|
process = setupProcess(m_packetParser.token());
|
|
|
|
|
if (process->state() != QProcess::NotRunning) {
|
2022-03-07 17:22:53 +01:00
|
|
|
logWarn("Got start request while process was running");
|
2021-07-07 11:36:03 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const auto packet = LauncherPacket::extractPacket<StartProcessPacket>(
|
|
|
|
|
m_packetParser.token(),
|
|
|
|
|
m_packetParser.packetData());
|
|
|
|
|
process->setEnvironment(packet.env);
|
|
|
|
|
process->setWorkingDirectory(packet.workingDir);
|
2021-09-02 12:58:33 +02:00
|
|
|
// Forwarding is handled by the LauncherInterface
|
2021-07-12 15:11:30 +02:00
|
|
|
process->setStandardInputFile(packet.standardInputFile);
|
2021-08-06 16:20:47 +02:00
|
|
|
ProcessStartHandler *handler = process->processStartHandler();
|
|
|
|
|
handler->setProcessMode(packet.processMode);
|
|
|
|
|
handler->setWriteData(packet.writeData);
|
2021-08-09 12:17:13 +02:00
|
|
|
if (packet.belowNormalPriority)
|
2021-12-16 10:43:58 +01:00
|
|
|
handler->setBelowNormalPriority();
|
|
|
|
|
handler->setNativeArguments(packet.nativeArguments);
|
2021-08-09 14:01:14 +02:00
|
|
|
if (packet.lowPriority)
|
|
|
|
|
process->setLowPriority();
|
|
|
|
|
if (packet.unixTerminalDisabled)
|
|
|
|
|
process->setUnixTerminalDisabled();
|
2022-03-14 17:02:11 +01:00
|
|
|
process->setUseCtrlCStub(packet.useCtrlCStub);
|
2021-08-06 16:20:47 +02:00
|
|
|
process->start(packet.command, packet.arguments, handler->openMode());
|
2021-12-16 10:43:58 +01:00
|
|
|
handler->handleProcessStart();
|
2021-08-06 14:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::handleWritePacket()
|
|
|
|
|
{
|
|
|
|
|
Process * const process = m_processes.value(m_packetParser.token());
|
|
|
|
|
if (!process) {
|
2022-03-07 17:22:53 +01:00
|
|
|
logWarn("Got write request for unknown process");
|
2021-08-06 14:43:03 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (process->state() != QProcess::Running) {
|
2022-03-07 17:22:53 +01:00
|
|
|
logDebug("Can't write into not running process");
|
2021-08-06 14:43:03 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const auto packet = LauncherPacket::extractPacket<WritePacket>(
|
|
|
|
|
m_packetParser.token(),
|
|
|
|
|
m_packetParser.packetData());
|
|
|
|
|
process->write(packet.inputData);
|
2021-07-07 11:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::handleStopPacket()
|
|
|
|
|
{
|
|
|
|
|
Process * const process = m_processes.value(m_packetParser.token());
|
|
|
|
|
if (!process) {
|
2021-09-06 14:48:08 +02:00
|
|
|
// This can happen when the process finishes on its own at about the same time the client
|
|
|
|
|
// sends the request. In this case the process was already deleted.
|
2022-03-07 17:22:53 +01:00
|
|
|
logDebug("Got stop request for unknown process");
|
2021-07-07 11:36:03 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (process->state() == QProcess::NotRunning) {
|
2021-09-06 14:48:08 +02:00
|
|
|
// This shouldn't happen, since as soon as process finishes or error occurrs
|
|
|
|
|
// the process is being removed.
|
2022-03-07 17:22:53 +01:00
|
|
|
logWarn("Got stop request when process was not running");
|
2021-08-04 09:58:20 +02:00
|
|
|
} else {
|
|
|
|
|
// We got the client request to stop the starting / running process.
|
|
|
|
|
// We report process exit to the client.
|
|
|
|
|
ProcessFinishedPacket packet(process->token());
|
|
|
|
|
packet.error = QProcess::Crashed;
|
|
|
|
|
packet.exitCode = -1;
|
|
|
|
|
packet.exitStatus = QProcess::CrashExit;
|
|
|
|
|
packet.stdErr = process->readAllStandardError();
|
|
|
|
|
packet.stdOut = process->readAllStandardOutput();
|
|
|
|
|
sendPacket(packet);
|
2021-07-07 11:36:03 +02:00
|
|
|
}
|
2021-08-03 10:30:55 +02:00
|
|
|
removeProcess(process->token());
|
2021-07-07 11:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::handleShutdownPacket()
|
|
|
|
|
{
|
2022-03-07 17:22:53 +01:00
|
|
|
logDebug("Got shutdown request, closing down");
|
2021-07-07 11:36:03 +02:00
|
|
|
m_socket->disconnect();
|
|
|
|
|
qApp->quit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LauncherSocketHandler::sendPacket(const LauncherPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
m_socket->write(packet.serialize());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Process *LauncherSocketHandler::setupProcess(quintptr token)
|
|
|
|
|
{
|
|
|
|
|
const auto p = new Process(token, this);
|
|
|
|
|
connect(p, &QProcess::errorOccurred, this, &LauncherSocketHandler::handleProcessError);
|
2021-07-12 15:49:20 +02:00
|
|
|
connect(p, &QProcess::started, this, &LauncherSocketHandler::handleProcessStarted);
|
2021-08-02 13:26:33 +02:00
|
|
|
connect(p, &QProcess::readyReadStandardOutput,
|
|
|
|
|
this, &LauncherSocketHandler::handleReadyReadStandardOutput);
|
|
|
|
|
connect(p, &QProcess::readyReadStandardError,
|
|
|
|
|
this, &LauncherSocketHandler::handleReadyReadStandardError);
|
2021-07-07 11:36:03 +02:00
|
|
|
connect(p, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
|
|
|
|
|
this, &LauncherSocketHandler::handleProcessFinished);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 10:30:55 +02:00
|
|
|
void LauncherSocketHandler::removeProcess(quintptr token)
|
|
|
|
|
{
|
2021-12-16 18:15:47 +01:00
|
|
|
const auto it = m_processes.constFind(token);
|
|
|
|
|
if (it == m_processes.constEnd())
|
2021-08-03 10:30:55 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Process *process = it.value();
|
|
|
|
|
m_processes.erase(it);
|
2021-09-06 14:48:08 +02:00
|
|
|
ProcessReaper::reap(process);
|
2021-08-03 10:30:55 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-07 11:36:03 +02:00
|
|
|
Process *LauncherSocketHandler::senderProcess() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<Process *>(sender());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Utils
|
|
|
|
|
|
|
|
|
|
#include <launchersockethandler.moc>
|