Utils: Introduce QtcProcess::controlEnvironment()

... and drop QtcProcess::removeEnvironment()

This shifts the meaning of the unqualified QtcProcess::environment()
to always refer to (possibly remote) "main" process this QtcProcess
wraps. The controlEnvironment is now referring to secondary helper
processes (if any...) typically(?) running on the host system.

This helps to avoid local/remote distinctions in some places.

Change-Id: Ib7cd15dd226617484b5358acd7deaed29e751883
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2022-05-09 15:02:23 +02:00
parent 9bf0efcbb9
commit e657bc8781
9 changed files with 36 additions and 48 deletions

View File

@@ -106,8 +106,9 @@ QStringList SshConnectionParameters::connectionOptions(const FilePath &binary) c
bool SshConnectionParameters::setupSshEnvironment(QtcProcess *process)
{
Environment env = process->hasEnvironment() ? process->environment()
: Environment::systemEnvironment();
Environment env = process->controlEnvironment();
if (env.size() == 0)
env = Environment::systemEnvironment();
const bool hasDisplay = env.hasKey("DISPLAY") && (env.value("DISPLAY") != QString(":0"));
if (SshSettings::askpassFilePath().exists()) {
env.set("SSH_ASKPASS", SshSettings::askpassFilePath().toUserOutput());

View File

@@ -47,7 +47,7 @@ public:
CommandLine m_commandLine;
FilePath m_workingDirectory;
Environment m_environment;
Environment m_remoteEnvironment;
Environment m_controlEnvironment;
QByteArray m_writeData;
QProcess::ProcessChannelMode m_processChannelMode = QProcess::SeparateChannels;
QVariantHash m_extraData;
@@ -56,7 +56,6 @@ public:
bool m_abortOnMetaChars = true;
bool m_runAsRoot = false;
bool m_haveEnv = false;
bool m_lowPriority = false;
bool m_unixTerminalDisabled = false;
bool m_useCtrlCStub = false; // release only

View File

@@ -580,7 +580,9 @@ public:
explicit QtcProcessPrivate(QtcProcess *parent)
: QObject(parent)
, q(parent)
{}
{
m_setup.m_controlEnvironment = Environment::systemEnvironment();
}
ProcessInterface *createProcessInterface()
{
@@ -615,22 +617,17 @@ public:
Environment fullEnvironment() const
{
Environment env;
if (m_setup.m_haveEnv) {
if (m_setup.m_environment.size()) {
env = m_setup.m_environment;
} else {
qWarning("QtcProcess::start: Empty environment set when running '%s'.",
qPrintable(m_setup.m_commandLine.executable().toString()));
env = Environment::systemEnvironment();
Environment env = m_setup.m_environment;
if (env.size() == 0) {
// FIXME: Either switch to using EnvironmentChange instead of full Environments, or
// feed the full environment into the QtcProcess instead of fixing it up here.
// qWarning("QtcProcess::start: Empty environment set when running '%s'.",
// qPrintable(m_setup.m_commandLine.executable().toString()));
env = m_setup.m_commandLine.executable().deviceEnvironment();
}
} else {
env = Environment::systemEnvironment();
}
// TODO: needs SshSettings
// if (m_runAsRoot)
// RunControl::provideAskPassEntry(env);
// TODO: needs SshSettings
// if (m_runAsRoot)
// RunControl::provideAskPassEntry(env);
return env;
}
@@ -1017,13 +1014,6 @@ void QtcProcess::setProcessMode(ProcessMode processMode)
void QtcProcess::setEnvironment(const Environment &env)
{
d->m_setup.m_environment = env;
d->m_setup.m_haveEnv = true;
}
void QtcProcess::unsetEnvironment()
{
d->m_setup.m_environment = Environment();
d->m_setup.m_haveEnv = false;
}
const Environment &QtcProcess::environment() const
@@ -1031,19 +1021,14 @@ const Environment &QtcProcess::environment() const
return d->m_setup.m_environment;
}
bool QtcProcess::hasEnvironment() const
void QtcProcess::setControlEnvironment(const Environment &environment)
{
return d->m_setup.m_haveEnv;
d->m_setup.m_controlEnvironment = environment;
}
void QtcProcess::setRemoteEnvironment(const Environment &environment)
const Environment &QtcProcess::controlEnvironment() const
{
d->m_setup.m_remoteEnvironment = environment;
}
Environment QtcProcess::remoteEnvironment() const
{
return d->m_setup.m_remoteEnvironment;
return d->m_setup.m_controlEnvironment;
}
void QtcProcess::setCommand(const CommandLine &cmdLine)

View File

@@ -99,13 +99,11 @@ public:
void setProcessMode(ProcessMode processMode);
ProcessMode processMode() const;
void setEnvironment(const Environment &env);
void unsetEnvironment();
void setEnvironment(const Environment &env); // Main process
const Environment &environment() const;
bool hasEnvironment() const;
void setRemoteEnvironment(const Environment &env);
Environment remoteEnvironment() const;
void setControlEnvironment(const Environment &env); // Possible helper process (ssh on host etc)
const Environment &controlEnvironment() const;
void setCommand(const CommandLine &cmdLine);
const CommandLine &commandLine() const;

View File

@@ -239,7 +239,7 @@ CommandLine DockerProcessImpl::fullLocalCommandLine(bool interactive)
args.append({"echo", s_pidMarker, "&&"});
const Environment &env = m_setup.m_remoteEnvironment;
const Environment &env = m_setup.m_environment;
for (auto it = env.constBegin(); it != env.constEnd(); ++it)
args.append(env.key(it) + "='" + env.expandedValueForKey(env.key(it)) + '\'');

View File

@@ -343,7 +343,7 @@ void ApplicationLauncherPrivate::start()
cmd.setExecutable(m_runnable.device->mapToGlobalPath(cmd.executable()));
m_process.setCommand(cmd);
m_process.setWorkingDirectory(m_runnable.workingDirectory);
m_process.setRemoteEnvironment(m_runnable.environment);
m_process.setEnvironment(m_runnable.environment);
m_process.setExtraData(m_runnable.extraData);
}

View File

@@ -83,7 +83,7 @@ QString QnxProcessImpl::fullCommandLine(const CommandLine &commandLine) const
fullCommandLine += QString::fromLatin1("cd %1 ; ").arg(
ProcessArgs::quoteArg(m_setup.m_workingDirectory.toString()));
const Environment env = m_setup.m_remoteEnvironment;
const Environment env = m_setup.m_environment;
for (auto it = env.constBegin(); it != env.constEnd(); ++it) {
fullCommandLine += QString::fromLatin1("%1='%2' ")
.arg(env.key(it)).arg(env.expandedValueForKey(env.key(it)));

View File

@@ -595,7 +595,7 @@ QString LinuxProcessInterface::fullCommandLine(const CommandLine &commandLine) c
if (m_setup.m_terminalMode == TerminalMode::Off)
cmd.addArgs(QString("echo ") + s_pidMarker + "$$" + s_pidMarker + " && ", CommandLine::Raw);
const Environment &env = m_setup.m_remoteEnvironment;
const Environment &env = m_setup.m_environment;
for (auto it = env.constBegin(); it != env.constEnd(); ++it)
cmd.addArgs(env.key(it) + "='" + env.expandedValueForKey(env.key(it)) + '\'', CommandLine::Raw);
@@ -760,11 +760,11 @@ void SshProcessInterfacePrivate::doStart()
// TODO: what about other fields from m_setup?
SshConnectionParameters::setupSshEnvironment(&m_process);
if (!m_sshParameters.x11DisplayName.isEmpty()) {
Environment env = m_process.environment();
Environment env = m_process.controlEnvironment();
// Note: it seems this is no-op when shared connection is used.
// In this case the display is taken from master process.
env.set("DISPLAY", m_sshParameters.x11DisplayName);
m_process.setEnvironment(env);
m_process.setControlEnvironment(env);
}
m_process.setCommand(fullLocalCommandLine());
m_process.start();
@@ -1030,7 +1030,6 @@ LinuxDevice::LinuxDevice()
proc->setCommand({ mapToGlobalPath({}), {}});
proc->setTerminalMode(TerminalMode::On);
proc->setEnvironment(env);
proc->setRemoteEnvironment(env);
proc->setWorkingDirectory(workingDir);
proc->start();
});
@@ -1133,6 +1132,11 @@ ProcessInterface *LinuxDevice::createProcessInterface() const
return new LinuxProcessInterface(this);
}
Environment LinuxDevice::systemEnvironment() const
{
return {}; // FIXME. See e.g. Docker implementation.
}
LinuxDevicePrivate::LinuxDevicePrivate(LinuxDevice *parent)
: q(parent)
{

View File

@@ -81,6 +81,7 @@ public:
bool writeFileContents(const Utils::FilePath &filePath, const QByteArray &data) const override;
QDateTime lastModified(const Utils::FilePath &filePath) const override;
Utils::ProcessInterface *createProcessInterface() const override;
Utils::Environment systemEnvironment() const override;
qint64 fileSize(const Utils::FilePath &filePath) const override;
qint64 bytesAvailable(const Utils::FilePath &filePath) const override;
QFileDevice::Permissions permissions(const Utils::FilePath &filePath) const override;