void tst_QProcessEnvironment::clearAndIsEmpty() { QProcessEnvironment e; e.insert("FOO", "bar"); QVERIFY(!e.isEmpty()); e.clear(); QVERIFY(e.isEmpty()); }
void tst_QProcessEnvironment::keys() { QProcessEnvironment e; QVERIFY(e.isEmpty()); QVERIFY(e.keys().isEmpty()); e.insert("FOO", "bar"); QStringList result = e.keys(); QCOMPARE(result.length(), 1); QCOMPARE(result.at(0), QString("FOO")); e.clear(); e.insert("BAZ", ""); result = e.keys(); QCOMPARE(result.at(0), QString("BAZ")); e.insert("FOO", "bar"); e.insert("A", "bc"); e.insert("HELLO", "World"); result = e.keys(); QCOMPARE(result.length(), 4); // order is not specified, so use contains() QVERIFY(result.contains("FOO")); QVERIFY(result.contains("BAZ")); QVERIFY(result.contains("A")); QVERIFY(result.contains("HELLO")); }
void tst_QProcessEnvironment::systemEnvironment() { static const char envname[] = "THIS_ENVIRONMENT_VARIABLE_HOPEFULLY_DOESNT_EXIST"; QByteArray path = qgetenv("PATH"); QByteArray nonexistant = qgetenv(envname); QProcessEnvironment system = QProcessEnvironment::systemEnvironment(); QVERIFY(nonexistant.isNull()); #ifdef Q_OS_WINCE // Windows CE has no environment QVERIFY(path.isEmpty()); QVERIFY(!system.contains("PATH")); QVERIFY(system.isEmpty()); #else // all other system have environments if (path.isEmpty()) QFAIL("Could not find the PATH environment variable -- please correct the test environment"); QVERIFY(system.contains("PATH")); QCOMPARE(system.value("PATH"), QString::fromLocal8Bit(path)); QVERIFY(!system.contains(envname)); # ifdef Q_OS_WIN // check case-insensitive too QVERIFY(system.contains("path")); QCOMPARE(system.value("path"), QString::fromLocal8Bit(path)); QVERIFY(!system.contains(QString(envname).toLower())); # endif #endif }
bool DebuggerInjector::startDebugger(const QStringList &args, const QProcessEnvironment &env) { m_process.reset(new QProcess); if (!env.isEmpty()) m_process->setProcessEnvironment(env); connect(m_process.data(), &QProcess::readyReadStandardError, this, &DebuggerInjector::readyReadStandardError); connect(m_process.data(), &QProcess::readyReadStandardOutput, this, &DebuggerInjector::readyReadStandardOutput); connect(m_process.data(), &QProcess::started, this, &AbstractInjector::started); connect(m_process.data(), static_cast<void(QProcess::*)(int)>(&QProcess::finished), this, &DebuggerInjector::processFinished); m_process->setProcessChannelMode(QProcess::SeparateChannels); m_process->start(filePath(), args); bool status = m_process->waitForStarted(-1); if (!status) { mExitCode = m_process->exitCode(); mExitStatus = m_process->exitStatus(); if (!mManualError) { mProcessError = m_process->error(); mErrorString = m_process->errorString(); } } else { emit started(); } return status; }
QProcessEnvironment Process::getProcessEnvironment() // ---------------------------------------------------------------------------- // Return the process environment that will be used to start the process // ---------------------------------------------------------------------------- { QProcessEnvironment env = processEnvironment(); if (env.isEmpty()) env = QProcessEnvironment::systemEnvironment(); return env; }
/* * Initialize and launch process object */ bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args) { static AssignProcessToJobObjectFun AssignProcessToJobObjectPtr = NULL; QMutexLocker lock(m_mutex_startProcess); emit messageLogged(commandline2string(program, args) + "\n"); QProcessEnvironment env = process.processEnvironment(); if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment(); env.insert("TEMP", QDir::toNativeSeparators(lamexp_temp_folder2())); env.insert("TMP", QDir::toNativeSeparators(lamexp_temp_folder2())); process.setProcessEnvironment(env); if(!AssignProcessToJobObjectPtr) { QLibrary Kernel32Lib("kernel32.dll"); AssignProcessToJobObjectPtr = (AssignProcessToJobObjectFun) Kernel32Lib.resolve("AssignProcessToJobObject"); } process.setProcessChannelMode(QProcess::MergedChannels); process.setReadChannel(QProcess::StandardOutput); process.start(program, args); if(process.waitForStarted()) { if(AssignProcessToJobObjectPtr) { AssignProcessToJobObjectPtr(m_handle_jobObject, process.pid()->hProcess); } if(!SetPriorityClass(process.pid()->hProcess, BELOW_NORMAL_PRIORITY_CLASS)) { SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS); } lock.unlock(); if(m_firstLaunch) { emit statusUpdated(0); m_firstLaunch = false; } return true; } emit messageLogged("Process creation has failed :-("); QString errorMsg= process.errorString().trimmed(); if(!errorMsg.isEmpty()) emit messageLogged(errorMsg); process.kill(); process.waitForFinished(-1); return false; }
void tst_QProcessEnvironment::insert() { QProcessEnvironment e; e.insert("FOO", "bar"); QVERIFY(!e.isEmpty()); QVERIFY(e.contains("FOO")); QCOMPARE(e.value("FOO"), QString("bar")); e.remove("FOO"); QVERIFY(!e.contains("FOO")); QVERIFY(e.value("FOO").isNull()); e.clear(); QVERIFY(!e.contains("FOO")); }
/* * Setup QPorcess object */ void lamexp_init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir) { //Environment variable names static const char *const s_envvar_names_temp[] = { "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL }; static const char *const s_envvar_names_remove[] = { "WGETRC", "SYSTEM_WGETRC", "HTTP_PROXY", "FTP_PROXY", "NO_PROXY", "GNUPGHOME", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LANG", NULL }; //Initialize environment QProcessEnvironment env = process.processEnvironment(); if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment(); //Clean a number of enviroment variables that might affect our tools for(size_t i = 0; s_envvar_names_remove[i]; i++) { env.remove(QString::fromLatin1(s_envvar_names_remove[i])); env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower()); } const QString tempDir = QDir::toNativeSeparators(lamexp_temp_folder2()); //Replace TEMP directory in environment if(bReplaceTempDir) { for(size_t i = 0; s_envvar_names_temp[i]; i++) { env.insert(s_envvar_names_temp[i], tempDir); } } //Setup PATH variable const QString path = env.value("PATH", QString()).trimmed(); env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path)); //Setup QPorcess object process.setWorkingDirectory(wokringDir); process.setProcessChannelMode(QProcess::MergedChannels); process.setReadChannel(QProcess::StandardOutput); process.setProcessEnvironment(env); }
bool StyleInjector::launch(const QStringList &programAndArgs, const QString &probeDll, const QString &probeFunc, const QProcessEnvironment &e) { auto env = e.isEmpty()? QProcessEnvironment::systemEnvironment() : e; env.insert("GAMMARAY_STYLEINJECTOR_PROBEDLL", probeDll); env.insert("GAMMARAY_STYLEINJECTOR_PROBEFUNC", probeFunc); QString qtPluginPath = env.value("QT_PLUGIN_PATH"); if (!qtPluginPath.isEmpty()) { qtPluginPath.append(":"); } qtPluginPath.append(Paths::currentProbePath()); env.insert("QT_PLUGIN_PATH", qtPluginPath); QStringList args = programAndArgs; args << QLatin1String("-style") << QLatin1String("gammaray-injector"); return launchProcess(args, env); }
static bool runProcess(const QString &binary, const QStringList &arguments, QString *errorMessage, const QString &workingDir = QString(), const QProcessEnvironment &env = QProcessEnvironment(), int timeOut = 5000, QByteArray *stdOut = Q_NULLPTR, QByteArray *stdErr = Q_NULLPTR) { QProcess process; if (!env.isEmpty()) process.setProcessEnvironment(env); if (!workingDir.isEmpty()) process.setWorkingDirectory(workingDir); qDebug().noquote().nospace() << "Running: " << QDir::toNativeSeparators(binary) << ' ' << arguments.join(QLatin1Char(' ')); process.start(binary, arguments, QIODevice::ReadOnly); if (!process.waitForStarted()) { *errorMessage = msgProcessError(process, "Failed to start"); return false; } if (!process.waitForFinished(timeOut)) { *errorMessage = msgProcessError(process, "Timed out"); process.terminate(); if (!process.waitForFinished(300)) process.kill(); return false; } if (stdOut) *stdOut = process.readAllStandardOutput(); if (stdErr) *stdErr= process.readAllStandardError(); if (process.exitStatus() != QProcess::NormalExit) { *errorMessage = msgProcessError(process, "Crashed"); return false; } if (process.exitCode() != QProcess::NormalExit) { *errorMessage = msgProcessError(process, "Exit code " + QString::number(process.exitCode())); return false; } return true; }
ProcessControl::ProcessControl(QObject *parent) : QObject(parent) , mPolicy(RestartOnCrash) , mFailedToStart(false) , mCrashCount(0) , mRestartOnceOnExit(false) , mShutdownTimeout(1000) { connect(&mProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(slotError(QProcess::ProcessError))); connect(&mProcess, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotFinished(int,QProcess::ExitStatus))); mProcess.setProcessChannelMode(QProcess::ForwardedChannels); if (Akonadi::Instance::hasIdentifier()) { QProcessEnvironment env = mProcess.processEnvironment(); if (env.isEmpty()) { env = QProcessEnvironment::systemEnvironment(); } env.insert(QStringLiteral("AKONADI_INSTANCE"), Akonadi::Instance::identifier()); mProcess.setProcessEnvironment(env); } }