-
Notifications
You must be signed in to change notification settings - Fork 7
/
cmd.cpp
79 lines (71 loc) · 2.5 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
76
77
78
79
#include "cmd.h"
#include <QApplication>
#include <QDebug>
#include <QEventLoop>
#include <QFile>
#include <unistd.h>
Cmd::Cmd(QObject *parent)
: QProcess(parent),
asRoot {QFile::exists("/usr/bin/pkexec") ? "/usr/bin/pkexec" : "/usr/bin/gksu"},
helper {"/usr/lib/" + QApplication::applicationName() + "/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::getCmdOut(const QString &cmd, bool quiet, bool elevate)
{
QString output;
run(cmd, &output, nullptr, quiet, elevate);
return output;
}
bool Cmd::proc(const QString &cmd, const QStringList &args, QString *output, const QByteArray *input, bool quiet,
bool elevate)
{
out_buffer.clear();
connect(this, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &Cmd::done);
if (this->state() != QProcess::NotRunning) {
qDebug() << "Process already running:" << this->program() << this->arguments();
return false;
}
if (!quiet) {
qDebug() << cmd << args;
}
QEventLoop loop;
connect(this, &Cmd::done, &loop, &QEventLoop::quit);
if (elevate && getuid() != 0) {
QStringList cmdAndArgs = QStringList() << helper << cmd << args;
start(asRoot, {cmdAndArgs});
} else {
start(cmd, args);
}
if (input) {
write(*input);
}
closeWriteChannel();
loop.exec();
if (output) {
*output = out_buffer.trimmed();
}
return (exitStatus() == QProcess::NormalExit && exitCode() == 0);
}
bool Cmd::procAsRoot(const QString &cmd, const QStringList &args, QString *output, const QByteArray *input, bool quiet)
{
return proc(cmd, args, output, input, quiet, true);
}
bool Cmd::run(const QString &cmd, QString *output, const QByteArray *input, bool quiet, bool elevate)
{
if (!quiet) {
qDebug().noquote() << cmd;
}
if (elevate && getuid() != 0) {
return proc(asRoot, {helper, cmd}, output, input, true);
} else {
return proc("/bin/bash", {"-c", cmd}, output, input, true);
}
}
bool Cmd::runAsRoot(const QString &cmd, QString *output, const QByteArray *input, bool quiet)
{
return run(cmd, output, input, quiet, true);
}