forked from AdrianTM/mx-packageinstaller
-
Notifications
You must be signed in to change notification settings - Fork 11
/
cmd.cpp
75 lines (66 loc) · 2.02 KB
/
cmd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "cmd.h"
#include <QCoreApplication>
#include <QDebug>
#include <QEventLoop>
#include <QFileInfo>
#include <unistd.h>
Cmd::Cmd(QObject *parent)
: QProcess(parent),
elevate {QFile::exists("/usr/bin/pkexec") ? "/usr/bin/pkexec" : "/usr/bin/gksu"},
helper {"/usr/lib/mx-packageinstaller/helper"}
{
connect(this, &Cmd::readyReadStandardOutput, [this] { emit outputAvailable(readAllStandardOutput()); });
connect(this, &Cmd::readyReadStandardError, [this] { emit errorAvailable(readAllStandardError()); });
connect(this, &Cmd::outputAvailable, [this](const QString &out) { out_buffer += out; });
connect(this, &Cmd::errorAvailable, [this](const QString &out) { out_buffer += out; });
}
QString Cmd::getOut(const QString &cmd, bool quiet, bool asRoot)
{
out_buffer.clear();
run(cmd, quiet, asRoot);
return out_buffer.trimmed();
}
QString Cmd::getOutAsRoot(const QString &cmd, bool quiet)
{
return getOut(cmd, quiet, true);
}
bool Cmd::run(const QString &cmd, bool quiet, bool asRoot)
{
out_buffer.clear();
if (state() != QProcess::NotRunning) {
qDebug() << "Process already running:" << program() << arguments();
return false;
}
if (!quiet) {
qDebug().noquote() << cmd;
}
QEventLoop loop;
connect(this, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &loop, &QEventLoop::quit);
if (asRoot && getuid() != 0) {
start(elevate, {helper, cmd});
} else {
start("/bin/bash", {"-c", cmd});
}
loop.exec();
emit done();
return (exitStatus() == QProcess::NormalExit && exitCode() == 0);
}
bool Cmd::runAsRoot(const QString &cmd, bool quiet)
{
return run(cmd, quiet, true);
}
// Return true when process is killed or not running
bool Cmd::terminateAndKill()
{
if (state() != QProcess::NotRunning) {
terminate();
if (!waitForFinished(2000)) {
kill();
}
}
return state() == QProcess::NotRunning;
}
QString Cmd::readAllOutput()
{
return out_buffer.trimmed();
}