Skip to content

Commit 196ad45

Browse files
committed
Start process with interactive process environment
Change-Id: I4fb38bd85099f1f9c3816f28abde04914c7e3abf Reviewed-by: Rainer Keller <[email protected]>
1 parent 6f1f70f commit 196ad45

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

process.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ void Process::finished(int exitCode, QProcess::ExitStatus exitStatus)
178178

179179
void Process::startup(QStringList args)
180180
{
181+
#ifdef Q_OS_ANDROID
182+
QProcessEnvironment pe = interactiveProcessEnvironment();
183+
#else
181184
QProcessEnvironment pe = QProcessEnvironment::systemEnvironment();
185+
#endif
182186

183187
foreach (const QString &key, mConfig.env.keys()) {
184188
qDebug() << key << mConfig.env.value(key);
@@ -239,3 +243,70 @@ void Process::setConfig(const Config &config)
239243
{
240244
mConfig = config;
241245
}
246+
247+
QProcessEnvironment Process::interactiveProcessEnvironment() const
248+
{
249+
QProcessEnvironment env;
250+
251+
QProcess process;
252+
process.start("sh");
253+
if (!process.waitForStarted(3000)) {
254+
printf("Could not start shell.\n");
255+
return env;
256+
}
257+
258+
process.write("source /system/etc/mkshrc\n");
259+
process.write("export -p\n");
260+
process.closeWriteChannel();
261+
262+
printf("waiting for process to finish\n");
263+
if (!process.waitForFinished(1000)) {
264+
printf("did not finish: terminate\n");
265+
process.terminate();
266+
if (!process.waitForFinished(1000)) {
267+
printf("did not terminate: kill\n");
268+
process.kill();
269+
if (!process.waitForFinished(1000)) {
270+
printf("Could not stop process.\n");
271+
}
272+
}
273+
}
274+
275+
QList<QByteArray> list = process.readAllStandardOutput().split('\n');
276+
if (list.isEmpty())
277+
printf("Failed to read environment output\n");
278+
279+
foreach (QByteArray entry, list) {
280+
if (entry.startsWith("export ")) {
281+
entry = entry.mid(7);
282+
} else if (entry.startsWith("declare -x ")) {
283+
entry = entry.mid(11);
284+
} else {
285+
continue;
286+
}
287+
288+
QByteArray key;
289+
QByteArray value;
290+
int index = entry.indexOf('=');
291+
292+
if (index > 0) {
293+
key = entry.left(index);
294+
value = entry.mid(index + 1);
295+
} else {
296+
key = entry;
297+
// value is empty
298+
}
299+
300+
// Remove simple escaping.
301+
// This is not complete.
302+
if (value.startsWith('\'') and value.endsWith('\''))
303+
value = value.mid(1, value.size()-2);
304+
else if (value.startsWith('"') and value.endsWith('"'))
305+
value = value.mid(1, value.size()-2);
306+
307+
env.insert(key, value);
308+
}
309+
310+
return env;
311+
}
312+

process.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ private slots:
6363
void incomingConnection(int);
6464
private:
6565
void startup(QStringList);
66+
QProcessEnvironment interactiveProcessEnvironment() const;
6667
QProcess *mProcess;
6768
int mDebuggee;
6869
bool mDebug;

0 commit comments

Comments
 (0)