Files
qt-creator/src/libs/utils/launcherpackets.cpp

192 lines
4.8 KiB
C++
Raw Normal View History

// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "launcherpackets.h"
#include <QByteArray>
namespace Utils {
namespace Internal {
LauncherPacket::~LauncherPacket() = default;
QByteArray LauncherPacket::serialize() const
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << static_cast<int>(0) << static_cast<quint8>(type) << token;
doSerialize(stream);
stream.device()->reset();
stream << static_cast<int>(data.size() - sizeof(int));
return data;
}
void LauncherPacket::deserialize(const QByteArray &data)
{
QDataStream stream(data);
doDeserialize(stream);
}
StartProcessPacket::StartProcessPacket(quintptr token)
: LauncherPacket(LauncherPacketType::StartProcess, token)
{
}
void StartProcessPacket::doSerialize(QDataStream &stream) const
{
stream << command
<< arguments
<< workingDir
<< env
<< int(processMode)
<< writeData
<< int(processChannelMode)
<< standardInputFile
<< belowNormalPriority
<< nativeArguments
<< lowPriority
<< unixTerminalDisabled
<< useCtrlCStub
<< reaperTimeout;
}
void StartProcessPacket::doDeserialize(QDataStream &stream)
{
int processModeInt;
int processChannelModeInt;
stream >> command
>> arguments
>> workingDir
>> env
>> processModeInt
>> writeData
>> processChannelModeInt
>> standardInputFile
>> belowNormalPriority
>> nativeArguments
>> lowPriority
>> unixTerminalDisabled
>> useCtrlCStub
>> reaperTimeout;
processMode = Utils::ProcessMode(processModeInt);
processChannelMode = QProcess::ProcessChannelMode(processChannelModeInt);
}
ProcessStartedPacket::ProcessStartedPacket(quintptr token)
: LauncherPacket(LauncherPacketType::ProcessStarted, token)
{
}
void ProcessStartedPacket::doSerialize(QDataStream &stream) const
{
stream << processId;
}
void ProcessStartedPacket::doDeserialize(QDataStream &stream)
{
stream >> processId;
}
ControlProcessPacket::ControlProcessPacket(quintptr token)
: LauncherPacket(LauncherPacketType::ControlProcess, token)
{
}
void ControlProcessPacket::doSerialize(QDataStream &stream) const
{
QtcProcess: Fix terminate() for process launcher ProcessLauncherImpl::terminate() was behaving the same as ProcessLauncherImpl::kill() in case of process launcher implementation - in both cases the launcher immediately returned confirmation about receiving the close request and was putting the corresponding process into its reaper. However, the issue with this approach is that we can't receive anymore any potential ready read signal from the process being terminated after a call to terminate(). The fix is to diverge the behavior of terminate() and kill() of ProcessLauncherImpl. The behavior of kill() remains the same, while terminate() just instructs the process launcher to start termination without putting the process into the reaper, yet. Add a test that checks for any possible zombies. We run the RecursiveBlockingProcess recursively. The most nested process blocks. After a short wait we terminate the outermost process. With this test we are trying to see if terminating the middle process terminates also its children and doesn't leave zombies. Before we execute the test (and also by the end of this test) we call Singleton::deleteAll() in order to ensure that reaping of previously running processes has finished. We ensure the number of running processtestapps is zero. Later, we ensure that the leaf process was already started and the number of running processtestapps equals the depth of recursion. Fix apparent bug in getLocalProcessesUsingProc(). Change-Id: I7e2bc46ad5ca22f26620da86fbaf0fa00a7db3c3 Reviewed-by: hjk <hjk@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
2022-04-01 15:14:05 +02:00
stream << int(signalType);
}
void ControlProcessPacket::doDeserialize(QDataStream &stream)
{
int signalTypeInt;
stream >> signalTypeInt;
signalType = SignalType(signalTypeInt);
}
void WritePacket::doSerialize(QDataStream &stream) const
{
stream << inputData;
}
void WritePacket::doDeserialize(QDataStream &stream)
{
stream >> inputData;
}
void ReadyReadPacket::doSerialize(QDataStream &stream) const
{
stream << standardChannel;
}
void ReadyReadPacket::doDeserialize(QDataStream &stream)
{
stream >> standardChannel;
}
ProcessDonePacket::ProcessDonePacket(quintptr token)
: LauncherPacket(LauncherPacketType::ProcessDone, token)
{
}
void ProcessDonePacket::doSerialize(QDataStream &stream) const
{
stream << exitCode
<< int(exitStatus)
<< int(error)
<< errorString
<< stdOut
<< stdErr;
}
void ProcessDonePacket::doDeserialize(QDataStream &stream)
{
int exitStatusInt, errorInt;
stream >> exitCode
>> exitStatusInt
>> errorInt
>> errorString
>> stdOut
>> stdErr;
exitStatus = QProcess::ExitStatus(exitStatusInt);
error = QProcess::ProcessError(errorInt);
}
ShutdownPacket::ShutdownPacket() : LauncherPacket(LauncherPacketType::Shutdown, 0) { }
void ShutdownPacket::doSerialize(QDataStream &stream) const { Q_UNUSED(stream); }
void ShutdownPacket::doDeserialize(QDataStream &stream) { Q_UNUSED(stream); }
void PacketParser::setDevice(QIODevice *device)
{
m_stream.setDevice(device);
m_sizeOfNextPacket = -1;
}
bool PacketParser::parse()
{
static const int commonPayloadSize = static_cast<int>(1 + sizeof(quintptr));
if (m_sizeOfNextPacket == -1) {
if (m_stream.device()->bytesAvailable() < static_cast<int>(sizeof m_sizeOfNextPacket))
return false;
m_stream >> m_sizeOfNextPacket;
if (m_sizeOfNextPacket < commonPayloadSize)
throw InvalidPacketSizeException(m_sizeOfNextPacket);
}
if (m_stream.device()->bytesAvailable() < m_sizeOfNextPacket)
return false;
quint8 type;
m_stream >> type;
m_type = static_cast<LauncherPacketType>(type);
m_stream >> m_token;
m_packetData = m_stream.device()->read(m_sizeOfNextPacket - commonPayloadSize);
m_sizeOfNextPacket = -1;
return true;
}
} // namespace Internal
} // namespace Utils